001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.store;
019    
020    import java.sql.SQLException;
021    import java.util.ArrayList;
022    import java.util.List;
023    import java.util.concurrent.Callable;
024    
025    import javax.persistence.EntityManager;
026    import javax.persistence.Query;
027    
028    import org.apache.oozie.ErrorCode;
029    import org.apache.oozie.SLAEventBean;
030    import org.apache.oozie.command.CommandException;
031    import org.apache.oozie.executor.jpa.JPAExecutorException;
032    import org.apache.oozie.executor.jpa.SLAEventsGetForSeqIdJPAExecutor;
033    import org.apache.oozie.service.InstrumentationService;
034    import org.apache.oozie.service.JPAService;
035    import org.apache.oozie.service.Services;
036    import org.apache.oozie.util.Instrumentation;
037    import org.apache.oozie.util.ParamChecker;
038    
039    public class SLAStore extends Store {
040        private EntityManager entityManager;
041        private static final String INSTR_GROUP = "db";
042    
043        public SLAStore() throws StoreException {
044            super();
045            entityManager = getEntityManager();
046    
047        }
048    
049        public SLAStore(Store store) throws StoreException {
050            super(store);
051            entityManager = getEntityManager();
052        }
053    
054        /**
055         * Create a CoordJobBean. It also creates the process instance for the job.
056         *
057         * @param workflow workflow bean
058         * @throws StoreException
059         */
060    
061        public void insertSLAEvent(final SLAEventBean slaEvent) throws StoreException {
062            ParamChecker.notNull(slaEvent, "sLaEvent");
063    
064            doOperation("insertSLAEvent", new Callable<Void>() {
065                public Void call() throws StoreException {
066                    entityManager.persist(slaEvent);
067                    return null;
068                }
069            });
070        }
071    
072        /**
073         * Get a list of SLA Events newer than a specific sequence with limit
074         * clause.
075         *
076         * @param seqId sequence id
077         * @return List of SLA Events
078         * @throws StoreException
079         */
080        public List<SLAEventBean> getSLAEventListNewerSeqLimited(final long seqId, final int limitLen, long[] lastSeqId)
081                throws StoreException {
082            ParamChecker.notNull(seqId, "SLAEventListNewerSeqLimited");
083            ParamChecker.checkGTZero(limitLen, "SLAEventListNewerSeqLimited");
084    
085            lastSeqId[0] = seqId;
086    
087            List<SLAEventBean> seBeans = (List<SLAEventBean>) doOperation("getSLAEventListNewerSeqLimited",
088                    new Callable<List<SLAEventBean>>() {
089    
090                        public List<SLAEventBean> call() throws StoreException, JPAExecutorException {
091    
092                            List<SLAEventBean> seBeans;
093                            try {
094    
095                                JPAService jpaService = Services.get().get(JPAService.class);
096                                List<SLAEventBean> slaEventList = null;
097                                long lastSeqId[] = new long[1];
098                                if (jpaService != null) {
099                                    seBeans = jpaService.execute(new SLAEventsGetForSeqIdJPAExecutor(seqId, limitLen,
100                                            lastSeqId));
101                                }
102                                else {
103                                    throw new StoreException(ErrorCode.E0610);
104                                }
105    
106                            }
107                            catch (IllegalStateException e) {
108                                throw new StoreException(ErrorCode.E0601, e.getMessage(), e);
109                            }
110                            catch (JPAExecutorException e) {
111                                throw new JPAExecutorException(ErrorCode.E0610, e.getMessage(), e);
112                            }
113                            return seBeans;
114                        }
115                    });
116            List<SLAEventBean> eventList = new ArrayList<SLAEventBean>();
117            for (SLAEventBean j : seBeans) {
118                lastSeqId[0] = Math.max(lastSeqId[0], j.getEvent_id());
119                eventList.add(j);
120            }
121            return eventList;
122        }
123    
124        private SLAEventBean copyEventBean(SLAEventBean e) {
125            SLAEventBean event = new SLAEventBean();
126            event.setAlertContact(e.getAlertContact());
127            event.setAlertFrequency(e.getAlertFrequency());
128            event.setAlertPercentage(e.getAlertPercentage());
129            event.setAppName(e.getAppName());
130            event.setAppType(e.getAppType());
131            event.setAppTypeStr(e.getAppTypeStr());
132            event.setDevContact(e.getDevContact());
133            event.setEvent_id(e.getEvent_id());
134            event.setEventType(e.getEventType());
135            event.setExpectedEnd(e.getExpectedEnd());
136            event.setExpectedStart(e.getExpectedStart());
137            event.setGroupName(e.getGroupName());
138            event.setJobData(e.getJobData());
139            event.setJobStatus(e.getJobStatus());
140            event.setJobStatusStr(e.getJobStatusStr());
141            event.setNotificationMsg(e.getNotificationMsg());
142            event.setParentClientId(e.getParentClientId());
143            event.setParentSlaId(e.getParentSlaId());
144            event.setQaContact(e.getQaContact());
145            event.setSeContact(e.getSeContact());
146            event.setSlaId(e.getSlaId());
147            event.setStatusTimestamp(e.getStatusTimestamp());
148            event.setUpstreamApps(e.getUpstreamApps());
149            event.setUser(e.getUser());
150            return event;
151        }
152    
153        private <V> V doOperation(String name, Callable<V> command) throws StoreException {
154            try {
155                Instrumentation.Cron cron = new Instrumentation.Cron();
156                cron.start();
157                V retVal;
158                try {
159                    retVal = command.call();
160                }
161                finally {
162                    cron.stop();
163                }
164                Services.get().get(InstrumentationService.class).get().addCron(INSTR_GROUP, name, cron);
165                return retVal;
166            }
167            catch (StoreException ex) {
168                throw ex;
169            }
170            catch (SQLException ex) {
171                throw new StoreException(ErrorCode.E0611, name, ex.getMessage(), ex);
172            }
173            catch (Exception e) {
174                throw new StoreException(ErrorCode.E0607, name, e.getMessage(), e);
175            }
176        }
177    
178    }