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