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.List;
021    
022    import javax.persistence.EntityManager;
023    import javax.persistence.Query;
024    
025    import org.apache.oozie.CoordinatorActionBean;
026    import org.apache.oozie.ErrorCode;
027    import org.apache.oozie.util.ParamChecker;
028    
029    /**
030     * Load coordinator actions in READY state for a coordinator job.
031     */
032    public class CoordJobGetReadyActionsJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
033    
034        private String coordJobId = null;
035        private int numResults;
036        private String executionOrder = null;
037    
038        public CoordJobGetReadyActionsJPAExecutor(String coordJobId, int numResults, String executionOrder) {
039            ParamChecker.notNull(coordJobId, "coordJobId");
040            this.coordJobId = coordJobId;
041            this.numResults = numResults;
042            this.executionOrder = executionOrder;
043        }
044    
045        @Override
046        public String getName() {
047            return "CoordJobGetReadyActionsJPAExecutor";
048        }
049    
050        @Override
051        @SuppressWarnings("unchecked")
052        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
053            List<CoordinatorActionBean> actionBeans = null;
054            try {
055                Query q;
056                // check if executionOrder is FIFO, LIFO, or LAST_ONLY
057                if (executionOrder.equalsIgnoreCase("FIFO")) {
058                    q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_FIFO");
059                }
060                else {
061                    q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_LIFO");
062                }
063                q.setParameter("jobId", coordJobId);
064                
065                // if executionOrder is LAST_ONLY, only retrieve first record in LIFO,
066                // otherwise, use numResults if it is positive.
067                if (executionOrder.equalsIgnoreCase("LAST_ONLY")) {
068                    q.setMaxResults(1);
069                }
070                else {
071                    if (numResults > 0) {
072                        q.setMaxResults(numResults);
073                    }
074                }
075                actionBeans = q.getResultList();
076                return actionBeans;
077            }
078            catch (Exception e) {
079                throw new JPAExecutorException(ErrorCode.E0603, e);
080            }        
081        }
082    
083    }