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