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.executor.jpa; 019 020import java.util.List; 021 022import javax.persistence.EntityManager; 023import javax.persistence.Query; 024 025import org.apache.oozie.ErrorCode; 026import org.apache.oozie.service.JPAService; 027import org.apache.oozie.service.Services; 028import org.apache.oozie.sla.SLARegistrationBean; 029 030/** 031 * Query Executor for SLA Event 032 * 033 */ 034public class SLARegistrationQueryExecutor extends QueryExecutor<SLARegistrationBean, SLARegistrationQueryExecutor.SLARegQuery> { 035 036 public enum SLARegQuery { 037 UPDATE_SLA_REG_ALL, 038 GET_SLA_REG_ALL, 039 GET_SLA_REG_ON_RESTART 040 }; 041 042 private static SLARegistrationQueryExecutor instance = new SLARegistrationQueryExecutor(); 043 044 private SLARegistrationQueryExecutor() { 045 } 046 047 public static QueryExecutor<SLARegistrationBean, SLARegistrationQueryExecutor.SLARegQuery> getInstance() { 048 return SLARegistrationQueryExecutor.instance; 049 } 050 051 @Override 052 public Query getUpdateQuery(SLARegQuery namedQuery, SLARegistrationBean bean, EntityManager em) 053 throws JPAExecutorException { 054 055 Query query = em.createNamedQuery(namedQuery.name()); 056 switch (namedQuery) { 057 case UPDATE_SLA_REG_ALL: 058 query.setParameter("jobId", bean.getId()); 059 query.setParameter("nominalTime", bean.getNominalTimestamp()); 060 query.setParameter("expectedStartTime", bean.getExpectedStartTimestamp()); 061 query.setParameter("expectedEndTime", bean.getExpectedEndTimestamp()); 062 query.setParameter("expectedDuration", bean.getExpectedDuration()); 063 query.setParameter("slaConfig", bean.getSlaConfig()); 064 query.setParameter("notificationMsg", bean.getNotificationMsg()); 065 query.setParameter("upstreamApps", bean.getUpstreamApps()); 066 query.setParameter("appType", bean.getAppType().toString()); 067 query.setParameter("appName", bean.getAppName()); 068 query.setParameter("user", bean.getUser()); 069 query.setParameter("parentId", bean.getParentId()); 070 query.setParameter("jobData", bean.getJobData()); 071 break; 072 default: 073 throw new JPAExecutorException(ErrorCode.E0603, "QueryExecutor cannot set parameters for " 074 + namedQuery.name()); 075 } 076 return query; 077 } 078 079 @Override 080 public Query getSelectQuery(SLARegQuery namedQuery, EntityManager em, Object... parameters) 081 throws JPAExecutorException { 082 Query query = em.createNamedQuery(namedQuery.name()); 083 switch (namedQuery) { 084 case GET_SLA_REG_ALL: 085 case GET_SLA_REG_ON_RESTART: 086 query.setParameter("id", parameters[0]); 087 break; 088 default: 089 throw new JPAExecutorException(ErrorCode.E0603, "QueryExecutor cannot set parameters for " 090 + namedQuery.name()); 091 } 092 return query; 093 } 094 095 @Override 096 public int executeUpdate(SLARegQuery namedQuery, SLARegistrationBean jobBean) throws JPAExecutorException { 097 JPAService jpaService = Services.get().get(JPAService.class); 098 EntityManager em = jpaService.getEntityManager(); 099 Query query = getUpdateQuery(namedQuery, jobBean, em); 100 int ret = jpaService.executeUpdate(namedQuery.name(), query, em); 101 return ret; 102 } 103 104 @Override 105 public SLARegistrationBean get(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 106 JPAService jpaService = Services.get().get(JPAService.class); 107 EntityManager em = jpaService.getEntityManager(); 108 Query query = getSelectQuery(namedQuery, em, parameters); 109 Object ret = jpaService.executeGet(namedQuery.name(), query, em); 110 if (ret == null && !namedQuery.equals(SLARegQuery.GET_SLA_REG_ALL)) { 111 throw new JPAExecutorException(ErrorCode.E0604, query.toString()); 112 } 113 SLARegistrationBean bean = constructBean(namedQuery, ret, parameters); 114 return bean; 115 } 116 117 @Override 118 public List<SLARegistrationBean> getList(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 119 JPAService jpaService = Services.get().get(JPAService.class); 120 EntityManager em = jpaService.getEntityManager(); 121 Query query = getSelectQuery(namedQuery, em, parameters); 122 @SuppressWarnings("unchecked") 123 List<SLARegistrationBean> beanList = (List<SLARegistrationBean>) jpaService.executeGetList(namedQuery.name(), 124 query, em); 125 return beanList; 126 } 127 128 private SLARegistrationBean constructBean(SLARegQuery namedQuery, Object ret, Object... parameters) 129 throws JPAExecutorException { 130 SLARegistrationBean bean; 131 Object[] arr; 132 switch (namedQuery) { 133 case GET_SLA_REG_ALL: 134 bean = (SLARegistrationBean) ret; 135 if(bean != null) { 136 bean.setSlaConfig(bean.getSlaConfig()); 137 } 138 break; 139 case GET_SLA_REG_ON_RESTART: 140 bean = new SLARegistrationBean(); 141 arr = (Object[]) ret; 142 bean.setNotificationMsg((String) arr[0]); 143 bean.setUpstreamApps((String) arr[1]); 144 bean.setSlaConfig((String) arr[2]); 145 bean.setJobData((String) arr[3]); 146 break; 147 default: 148 throw new JPAExecutorException(ErrorCode.E0603, "QueryExecutor cannot construct job bean for " 149 + namedQuery.name()); 150 } 151 return bean; 152 } 153 154 @Override 155 public Object getSingleValue(SLARegQuery namedQuery, Object... parameters) throws JPAExecutorException { 156 throw new UnsupportedOperationException(); 157 } 158}