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.executor.jpa; 020 021import java.sql.Timestamp; 022import java.util.ArrayList; 023import java.util.List; 024 025import javax.persistence.EntityManager; 026import javax.persistence.Query; 027 028import org.apache.oozie.ErrorCode; 029import org.apache.oozie.service.JPAService; 030import org.apache.oozie.service.Services; 031import org.apache.oozie.sla.SLARegistrationBean; 032 033/** 034 * Query Executor for SLA Event 035 * 036 */ 037public class SLARegistrationQueryExecutor extends QueryExecutor<SLARegistrationBean, SLARegistrationQueryExecutor.SLARegQuery> { 038 039 public enum SLARegQuery { 040 UPDATE_SLA_REG_ALL, 041 UPDATE_SLA_CONFIG, 042 UPDATE_SLA_EXPECTED_VALUE, 043 GET_SLA_REG_ALL, 044 GET_SLA_EXPECTED_VALUE_CONFIG, 045 GET_SLA_REG_FOR_PARENT_ID, 046 GET_SLA_REG_ON_RESTART, 047 GET_SLA_CONFIGS 048 }; 049 050 private static SLARegistrationQueryExecutor instance = new SLARegistrationQueryExecutor(); 051 052 private SLARegistrationQueryExecutor() { 053 } 054 055 public static QueryExecutor<SLARegistrationBean, SLARegistrationQueryExecutor.SLARegQuery> getInstance() { 056 return SLARegistrationQueryExecutor.instance; 057 } 058 059 @Override 060 public Query getUpdateQuery(SLARegQuery namedQuery, SLARegistrationBean bean, EntityManager em) 061 throws JPAExecutorException { 062 063 Query query = em.createNamedQuery(namedQuery.name()); 064 switch (namedQuery) { 065 case UPDATE_SLA_REG_ALL: 066 query.setParameter("jobId", bean.getId()); 067 query.setParameter("nominalTime", bean.getNominalTimestamp()); 068 query.setParameter("expectedStartTime", bean.getExpectedStartTimestamp()); 069 query.setParameter("expectedEndTime", bean.getExpectedEndTimestamp()); 070 query.setParameter("expectedDuration", bean.getExpectedDuration()); 071 query.setParameter("slaConfig", bean.getSlaConfig()); 072 query.setParameter("notificationMsg", bean.getNotificationMsg()); 073 query.setParameter("upstreamApps", bean.getUpstreamApps()); 074 query.setParameter("appType", bean.getAppType().toString()); 075 query.setParameter("appName", bean.getAppName()); 076 query.setParameter("user", bean.getUser()); 077 query.setParameter("parentId", bean.getParentId()); 078 query.setParameter("jobData", bean.getJobData()); 079 break; 080 case UPDATE_SLA_EXPECTED_VALUE: 081 query.setParameter("jobId", bean.getId()); 082 query.setParameter("expectedStartTime", bean.getExpectedStartTimestamp()); 083 query.setParameter("expectedEndTime", bean.getExpectedEndTimestamp()); 084 query.setParameter("expectedDuration", bean.getExpectedDuration()); 085 break; 086 case UPDATE_SLA_CONFIG: 087 query.setParameter("jobId", bean.getId()); 088 query.setParameter("slaConfig", bean.getSlaConfig()); 089 break; 090 091 default: 092 throw new JPAExecutorException(ErrorCode.E0603, "QueryExecutor cannot set parameters for " 093 + namedQuery.name()); 094 } 095 return query; 096 } 097 098 @Override 099 public Query getSelectQuery(SLARegQuery namedQuery, EntityManager em, Object... parameters) 100 throws JPAExecutorException { 101 Query query = em.createNamedQuery(namedQuery.name()); 102 switch (namedQuery) { 103 case GET_SLA_REG_ALL: 104 case GET_SLA_REG_ON_RESTART: 105 query.setParameter("id", parameters[0]); 106 break; 107 case GET_SLA_CONFIGS: 108 query.setParameter("ids", parameters[0]); 109 break; 110 case GET_SLA_EXPECTED_VALUE_CONFIG: 111 query.setParameter("id", parameters[0]); 112 break; 113 case GET_SLA_REG_FOR_PARENT_ID: 114 query.setParameter("parentId", parameters[0]); 115 break; 116 117 default: 118 throw new JPAExecutorException(ErrorCode.E0603, "QueryExecutor cannot set parameters for " 119 + namedQuery.name()); 120 } 121 return query; 122 } 123 124 @Override 125 public int executeUpdate(SLARegQuery namedQuery, SLARegistrationBean jobBean) throws JPAExecutorException { 126 JPAService jpaService = Services.get().get(JPAService.class); 127 EntityManager em = jpaService.getEntityManager(); 128 Query query = getUpdateQuery(namedQuery, jobBean, em); 129 int ret = jpaService.executeUpdate(namedQuery.name(), query, em); 130 return ret; 131 } 132 133 @Override 134 public SLARegistrationBean get(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 135 JPAService jpaService = Services.get().get(JPAService.class); 136 EntityManager em = jpaService.getEntityManager(); 137 Query query = getSelectQuery(namedQuery, em, parameters); 138 Object ret = jpaService.executeGet(namedQuery.name(), query, em); 139 if (ret == null && !namedQuery.equals(SLARegQuery.GET_SLA_REG_ALL)) { 140 throw new JPAExecutorException(ErrorCode.E0604, query.toString()); 141 } 142 SLARegistrationBean bean = constructBean(namedQuery, ret, parameters); 143 return bean; 144 } 145 146 @Override 147 public SLARegistrationBean getIfExist(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 148 JPAService jpaService = Services.get().get(JPAService.class); 149 EntityManager em = jpaService.getEntityManager(); 150 Query query = getSelectQuery(namedQuery, em, parameters); 151 Object ret = jpaService.executeGet(namedQuery.name(), query, em); 152 if (ret == null && !namedQuery.equals(SLARegQuery.GET_SLA_REG_ALL)) { 153 return null; 154 } 155 SLARegistrationBean bean = constructBean(namedQuery, ret, parameters); 156 return bean; 157 } 158 159 @Override 160 public List<SLARegistrationBean> getList(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 161 JPAService jpaService = Services.get().get(JPAService.class); 162 EntityManager em = jpaService.getEntityManager(); 163 Query query = getSelectQuery(namedQuery, em, parameters); 164 List<?> retList = (List<?>) jpaService.executeGetList(namedQuery.name(), query, em); 165 List<SLARegistrationBean> beanList = new ArrayList<SLARegistrationBean>(); 166 if (retList != null) { 167 for (Object ret : retList) { 168 beanList.add(constructBean(namedQuery, ret)); 169 } 170 } 171 return beanList; 172 } 173 174 private SLARegistrationBean constructBean(SLARegQuery namedQuery, Object ret, Object... parameters) 175 throws JPAExecutorException { 176 SLARegistrationBean bean; 177 Object[] arr; 178 switch (namedQuery) { 179 case GET_SLA_REG_ALL: 180 bean = (SLARegistrationBean) ret; 181 if(bean != null) { 182 bean.setSlaConfig(bean.getSlaConfig()); 183 } 184 break; 185 case GET_SLA_REG_ON_RESTART: 186 bean = new SLARegistrationBean(); 187 arr = (Object[]) ret; 188 bean.setNotificationMsg((String) arr[0]); 189 bean.setUpstreamApps((String) arr[1]); 190 bean.setSlaConfig((String) arr[2]); 191 bean.setJobData((String) arr[3]); 192 break; 193 case GET_SLA_CONFIGS: 194 bean = new SLARegistrationBean(); 195 arr = (Object[]) ret; 196 bean.setId((String) arr[0]); 197 bean.setSlaConfig((String) arr[1]); 198 break; 199 case GET_SLA_EXPECTED_VALUE_CONFIG: 200 bean = new SLARegistrationBean(); 201 arr = (Object[]) ret; 202 bean.setId((String) arr[0]); 203 bean.setSlaConfig((String) arr[1]); 204 bean.setExpectedStart((Timestamp)arr[2]); 205 bean.setExpectedEnd((Timestamp)arr[3]); 206 bean.setExpectedDuration((Long)arr[4]); 207 bean.setNominalTime((Timestamp)arr[5]); 208 break; 209 case GET_SLA_REG_FOR_PARENT_ID: 210 bean = new SLARegistrationBean(); 211 arr = (Object[]) ret; 212 bean.setId((String) arr[0]); 213 bean.setSlaConfig((String) arr[1]); 214 break; 215 default: 216 throw new JPAExecutorException(ErrorCode.E0603, "QueryExecutor cannot construct job bean for " 217 + namedQuery.name()); 218 } 219 return bean; 220 } 221 222 @Override 223 public Object getSingleValue(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 224 throw new UnsupportedOperationException(); 225 } 226}