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.executor.jpa; 019 020 import java.util.ArrayList; 021 import java.util.List; 022 023 import javax.persistence.EntityManager; 024 import javax.persistence.Query; 025 026 import org.apache.oozie.CoordinatorActionBean; 027 import org.apache.oozie.CoordinatorJobBean; 028 import org.apache.oozie.ErrorCode; 029 import org.apache.oozie.client.CoordinatorAction; 030 import org.apache.oozie.client.Job.Status; 031 import org.apache.oozie.util.ParamChecker; 032 033 /** 034 * Load the running Coordinator actions for a given Coordinator job 035 */ 036 public class CoordJobGetActionsRunningJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> { 037 038 private String coordJobId = null; 039 040 public CoordJobGetActionsRunningJPAExecutor(String coordJobId) { 041 ParamChecker.notNull(coordJobId, "coordJobId"); 042 this.coordJobId = coordJobId; 043 } 044 045 @Override 046 public String getName() { 047 return "CoordJobGetActionsRunningJPAExecutor"; 048 } 049 050 @Override 051 @SuppressWarnings("unchecked") 052 public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException { 053 try { 054 List<CoordinatorActionBean> actionBeansList = new ArrayList<CoordinatorActionBean>(); 055 Query q = em.createNamedQuery("GET_COORD_ACTIONS_RUNNING"); 056 q.setParameter("jobId", coordJobId); 057 List<Object[]> objectArrList = q.getResultList(); 058 for (Object[] arr : objectArrList) { 059 CoordinatorActionBean caa = getBeanForCoordinatorActionFromArray(arr); 060 actionBeansList.add(caa); 061 } 062 return actionBeansList; 063 } 064 catch (Exception e) { 065 throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e); 066 } 067 } 068 069 /* 070 * A Coordinator action bean is constructed from an array of objects. 071 * ActionId, status, pending and externalId of the Coordinator action are 072 * updated. If values of id, status and externalId fetched from db are 073 * null, they will be automatically initialized to default values of null as 074 * they are Strings. If value of pending fetched from db is null, it will be 075 * initialized with default value of 0 as it is a int. 076 */ 077 private CoordinatorActionBean getBeanForCoordinatorActionFromArray(Object[] arr) { 078 CoordinatorActionBean bean = new CoordinatorActionBean(); 079 if (arr[0] != null) { 080 bean.setId((String) arr[0]); 081 } 082 if (arr[1] != null) { 083 bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[1])); 084 } 085 if (arr[2] != null) { 086 bean.setPending((Integer) arr[2]); 087 } 088 if (arr[3] != null) { 089 bean.setExternalId((String) arr[3]); 090 } 091 return bean; 092 } 093 094 }