001 /** 002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved. 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. See accompanying LICENSE file. 014 */ 015 package org.apache.oozie.executor.jpa; 016 017 import java.util.List; 018 019 import javax.persistence.EntityManager; 020 import javax.persistence.Query; 021 022 import org.apache.oozie.CoordinatorActionBean; 023 import org.apache.oozie.ErrorCode; 024 import org.apache.oozie.util.ParamChecker; 025 026 /** 027 * Load coordinator actions in READY state for a coordinator job. 028 */ 029 public class CoordJobGetReadyActionsJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> { 030 031 private String coordJobId = null; 032 private int numResults; 033 private String executionOrder = null; 034 035 public CoordJobGetReadyActionsJPAExecutor(String coordJobId, int numResults, String executionOrder) { 036 ParamChecker.notNull(coordJobId, "coordJobId"); 037 this.coordJobId = coordJobId; 038 this.numResults = numResults; 039 this.executionOrder = executionOrder; 040 } 041 042 @Override 043 public String getName() { 044 return "CoordJobGetReadyActionsJPAExecutor"; 045 } 046 047 @Override 048 @SuppressWarnings("unchecked") 049 public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException { 050 List<CoordinatorActionBean> actionBeans = null; 051 try { 052 Query q; 053 // check if executionOrder is FIFO, LIFO, or LAST_ONLY 054 if (executionOrder.equalsIgnoreCase("FIFO")) { 055 q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_FIFO"); 056 } 057 else { 058 q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_LIFO"); 059 } 060 q.setParameter("jobId", coordJobId); 061 062 // if executionOrder is LAST_ONLY, only retrieve first record in LIFO, 063 // otherwise, use numResults if it is positive. 064 if (executionOrder.equalsIgnoreCase("LAST_ONLY")) { 065 q.setMaxResults(1); 066 } 067 else { 068 if (numResults > 0) { 069 q.setMaxResults(numResults); 070 } 071 } 072 actionBeans = q.getResultList(); 073 return actionBeans; 074 } 075 catch (Exception e) { 076 throw new JPAExecutorException(ErrorCode.E0603, e); 077 } 078 } 079 080 }