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.sql.Timestamp; 022 import java.util.ArrayList; 023 import java.util.Date; 024 import java.util.List; 025 import java.util.concurrent.Callable; 026 027 import javax.persistence.EntityManager; 028 import javax.persistence.Query; 029 030 import org.apache.oozie.ErrorCode; 031 import org.apache.oozie.SLAEventBean; 032 import org.apache.oozie.XException; 033 import org.apache.oozie.service.InstrumentationService; 034 import org.apache.oozie.service.Services; 035 import org.apache.oozie.util.Instrumentation; 036 import org.apache.oozie.util.ParamChecker; 037 038 public 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 a CoordJobBean. It also creates the process instance for the job. 055 * 056 * @param workflow workflow 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 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 { 089 090 List<SLAEventBean> seBeans; 091 try { 092 Query q = entityManager.createNamedQuery("GET_SLA_EVENT_NEWER_SEQ_LIMITED"); 093 q.setParameter("id", seqId); 094 // q.setFirstResult(0); 095 q.setMaxResults(limitLen); 096 seBeans = q.getResultList(); 097 } 098 catch (IllegalStateException e) { 099 throw new StoreException(ErrorCode.E0601, e.getMessage(), e); 100 } 101 return seBeans; 102 } 103 }); 104 List<SLAEventBean> eventList = new ArrayList<SLAEventBean>(); 105 for (SLAEventBean j : seBeans) { 106 lastSeqId[0] = Math.max(lastSeqId[0], j.getEvent_id()); 107 eventList.add(j); 108 } 109 return eventList; 110 } 111 112 private SLAEventBean copyEventBean(SLAEventBean e) { 113 SLAEventBean event = new SLAEventBean(); 114 event.setAlertContact(e.getAlertContact()); 115 event.setAlertFrequency(e.getAlertFrequency()); 116 event.setAlertPercentage(e.getAlertPercentage()); 117 event.setAppName(e.getAppName()); 118 event.setAppType(e.getAppType()); 119 event.setAppTypeStr(e.getAppTypeStr()); 120 event.setDevContact(e.getDevContact()); 121 event.setEvent_id(e.getEvent_id()); 122 event.setEventType(e.getEventType()); 123 event.setExpectedEnd(e.getExpectedEnd()); 124 event.setExpectedStart(e.getExpectedStart()); 125 event.setGroupName(e.getGroupName()); 126 event.setJobData(e.getJobData()); 127 event.setJobStatus(e.getJobStatus()); 128 event.setJobStatusStr(e.getJobStatusStr()); 129 event.setNotificationMsg(e.getNotificationMsg()); 130 event.setParentClientId(e.getParentClientId()); 131 event.setParentSlaId(e.getParentSlaId()); 132 event.setQaContact(e.getQaContact()); 133 event.setSeContact(e.getSeContact()); 134 event.setSlaId(e.getSlaId()); 135 event.setStatusTimestamp(e.getStatusTimestamp()); 136 event.setUpstreamApps(e.getUpstreamApps()); 137 event.setUser(e.getUser()); 138 return event; 139 } 140 141 private <V> V doOperation(String name, Callable<V> command) throws StoreException { 142 try { 143 Instrumentation.Cron cron = new Instrumentation.Cron(); 144 cron.start(); 145 V retVal; 146 try { 147 retVal = command.call(); 148 } 149 finally { 150 cron.stop(); 151 } 152 Services.get().get(InstrumentationService.class).get().addCron(INSTR_GROUP, name, cron); 153 return retVal; 154 } 155 catch (StoreException ex) { 156 throw ex; 157 } 158 catch (SQLException ex) { 159 throw new StoreException(ErrorCode.E0603, name, ex.getMessage(), ex); 160 } 161 catch (Exception e) { 162 throw new StoreException(ErrorCode.E0607, name, e.getMessage(), e); 163 } 164 } 165 166 }