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