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.service.InstrumentationService;
031    import org.apache.oozie.service.Services;
032    import org.apache.oozie.util.Instrumentation;
033    import org.apache.oozie.util.ParamChecker;
034    
035    public class SLAStore extends Store {
036        private EntityManager entityManager;
037        private static final String INSTR_GROUP = "db";
038    
039        public SLAStore() throws StoreException {
040            super();
041            entityManager = getEntityManager();
042    
043        }
044    
045        public SLAStore(Store store) throws StoreException {
046            super(store);
047            entityManager = getEntityManager();
048        }
049    
050        /**
051         * Create a CoordJobBean. It also creates the process instance for the job.
052         *
053         * @param workflow workflow bean
054         * @throws StoreException
055         */
056    
057        public void insertSLAEvent(final SLAEventBean slaEvent) throws StoreException {
058            ParamChecker.notNull(slaEvent, "sLaEvent");
059    
060            doOperation("insertSLAEvent", new Callable<Void>() {
061                public Void call() throws StoreException {
062                    entityManager.persist(slaEvent);
063                    return null;
064                }
065            });
066        }
067    
068        /**
069         * Get a list of SLA Events newer than a specific sequence with limit clause.
070         *
071         * @param seqId sequence id
072         * @return List of SLA Events
073         * @throws StoreException
074         */
075        public List<SLAEventBean> getSLAEventListNewerSeqLimited(final long seqId, final int limitLen, long[] lastSeqId)
076                throws StoreException {
077            ParamChecker.notNull(seqId, "SLAEventListNewerSeqLimited");
078            ParamChecker.checkGTZero(limitLen, "SLAEventListNewerSeqLimited");
079    
080            lastSeqId[0] = seqId;
081    
082            List<SLAEventBean> seBeans = (List<SLAEventBean>) doOperation("getSLAEventListNewerSeqLimited",
083                                                                          new Callable<List<SLAEventBean>>() {
084    
085                                                                              public List<SLAEventBean> call() throws StoreException {
086    
087                                                                                  List<SLAEventBean> seBeans;
088                                                                                  try {
089                                                                                      Query q = entityManager.createNamedQuery("GET_SLA_EVENT_NEWER_SEQ_LIMITED");
090                                                                                      q.setParameter("id", seqId);
091                                                                                      // q.setFirstResult(0);
092                                                                                      q.setMaxResults(limitLen);
093                                                                                      seBeans = q.getResultList();
094                                                                                  }
095                                                                                  catch (IllegalStateException e) {
096                                                                                      throw new StoreException(ErrorCode.E0601, e.getMessage(), e);
097                                                                                  }
098                                                                                  return seBeans;
099                                                                              }
100                                                                          });
101            List<SLAEventBean> eventList = new ArrayList<SLAEventBean>();
102            for (SLAEventBean j : seBeans) {
103                lastSeqId[0] = Math.max(lastSeqId[0], j.getEvent_id());
104                eventList.add(j);
105            }
106            return eventList;
107        }
108    
109        private SLAEventBean copyEventBean(SLAEventBean e) {
110            SLAEventBean event = new SLAEventBean();
111            event.setAlertContact(e.getAlertContact());
112            event.setAlertFrequency(e.getAlertFrequency());
113            event.setAlertPercentage(e.getAlertPercentage());
114            event.setAppName(e.getAppName());
115            event.setAppType(e.getAppType());
116            event.setAppTypeStr(e.getAppTypeStr());
117            event.setDevContact(e.getDevContact());
118            event.setEvent_id(e.getEvent_id());
119            event.setEventType(e.getEventType());
120            event.setExpectedEnd(e.getExpectedEnd());
121            event.setExpectedStart(e.getExpectedStart());
122            event.setGroupName(e.getGroupName());
123            event.setJobData(e.getJobData());
124            event.setJobStatus(e.getJobStatus());
125            event.setJobStatusStr(e.getJobStatusStr());
126            event.setNotificationMsg(e.getNotificationMsg());
127            event.setParentClientId(e.getParentClientId());
128            event.setParentSlaId(e.getParentSlaId());
129            event.setQaContact(e.getQaContact());
130            event.setSeContact(e.getSeContact());
131            event.setSlaId(e.getSlaId());
132            event.setStatusTimestamp(e.getStatusTimestamp());
133            event.setUpstreamApps(e.getUpstreamApps());
134            event.setUser(e.getUser());
135            return event;
136        }
137    
138        private <V> V doOperation(String name, Callable<V> command) throws StoreException {
139            try {
140                Instrumentation.Cron cron = new Instrumentation.Cron();
141                cron.start();
142                V retVal;
143                try {
144                    retVal = command.call();
145                }
146                finally {
147                    cron.stop();
148                }
149                Services.get().get(InstrumentationService.class).get().addCron(INSTR_GROUP, name, cron);
150                return retVal;
151            }
152            catch (StoreException ex) {
153                throw ex;
154            }
155            catch (SQLException ex) {
156                throw new StoreException(ErrorCode.E0603, name, ex.getMessage(), ex);
157            }
158            catch (Exception e) {
159                throw new StoreException(ErrorCode.E0607, name, e.getMessage(), e);
160            }
161        }
162    
163    }