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