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.CoordinatorActionBean; 029import org.apache.oozie.ErrorCode; 030import org.apache.oozie.client.CoordinatorAction; 031import org.apache.oozie.util.DateUtils; 032import org.apache.oozie.util.ParamChecker; 033 034/** 035 * Load coordinator actions in READY state for a coordinator job. 036 */ 037public class CoordJobGetReadyActionsJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> { 038 039 private String coordJobId = null; 040 private String executionOrder = null; 041 042 public CoordJobGetReadyActionsJPAExecutor(String coordJobId, String executionOrder) { 043 ParamChecker.notNull(coordJobId, "coordJobId"); 044 this.coordJobId = coordJobId; 045 this.executionOrder = executionOrder; 046 } 047 048 @Override 049 public String getName() { 050 return "CoordJobGetReadyActionsJPAExecutor"; 051 } 052 053 @Override 054 @SuppressWarnings("unchecked") 055 public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException { 056 List<CoordinatorActionBean> actionBeans = null; 057 try { 058 Query q; 059 // check if executionOrder is FIFO, LIFO, LAST_ONLY, or NONE 060 if (executionOrder.equalsIgnoreCase("FIFO")) { 061 q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_FIFO"); 062 } 063 else { // LIFO, LAST_ONLY, or NONE 064 q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_LIFO"); 065 } 066 q.setParameter("jobId", coordJobId); 067 068 List<Object[]> objectArrList = q.getResultList(); 069 actionBeans = new ArrayList<CoordinatorActionBean>(); 070 for (Object[] arr : objectArrList) { 071 CoordinatorActionBean caa = getBeanForCoordinatorActionFromArray(arr); 072 actionBeans.add(caa); 073 } 074 return actionBeans; 075 } 076 catch (Exception e) { 077 throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e); 078 } 079 } 080 081 private CoordinatorActionBean getBeanForCoordinatorActionFromArray(Object arr[]) { 082 CoordinatorActionBean bean = new CoordinatorActionBean(); 083 if (arr[0] != null) { 084 bean.setId((String) arr[0]); 085 } 086 if (arr[1] != null) { 087 bean.setActionNumber((Integer) arr[1]); 088 } 089 if (arr[2] != null) { 090 bean.setJobId((String) arr[2]); 091 } 092 if (arr[3] != null) { 093 bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[3])); 094 } 095 if (arr[4] != null) { 096 bean.setPending((Integer) arr[4]); 097 } 098 if (arr[5] != null) { 099 bean.setNominalTime(DateUtils.toDate((Timestamp) arr[5])); 100 } 101 if (arr[6] != null) { 102 bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[6])); 103 } 104 return bean; 105 } 106 107}