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 the list of CoordinatorAction for a CoordJob and return the list.
031     */
032    public class CoordActionsGetForJobJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
033    
034        private String coordJobId = null;
035        private int numResults = 50;
036        private String executionOrder = "FIFO";
037    
038        public CoordActionsGetForJobJPAExecutor(String coordJobId) {
039            ParamChecker.notNull(coordJobId, "coordJobId");
040            this.coordJobId = coordJobId;
041        }
042    
043        public CoordActionsGetForJobJPAExecutor(String coordJobId, int numResults, String executionOrder) {
044            this(coordJobId);
045            ParamChecker.notNull(executionOrder, "executionOrder");
046            this.numResults = numResults;
047            this.executionOrder = executionOrder;
048        }
049    
050        /* (non-Javadoc)
051         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
052         */
053        @Override
054        public String getName() {
055            return "CoordActionsGetForJobJPAExecutor";
056        }
057    
058        /* (non-Javadoc)
059         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
060         */
061        @Override
062        @SuppressWarnings("unchecked")
063        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
064            List<CoordinatorActionBean> caBeans;
065            try {
066                Query q;
067                // check if executionOrder is FIFO, LIFO, or LAST_ONLY
068                if (executionOrder.equalsIgnoreCase("FIFO")) {
069                    q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_FIFO");
070                }
071                else {
072                    q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_LIFO");
073                }
074                q.setParameter("jobId", coordJobId);
075                if (executionOrder.equalsIgnoreCase("LAST_ONLY")) {
076                    q.setMaxResults(1);
077                }
078                else {
079                    if (numResults > 0) {
080                        q.setMaxResults(numResults);
081                    }
082                }
083                caBeans = q.getResultList();
084            }
085            catch (Exception e) {
086                throw new JPAExecutorException(ErrorCode.E0603, e);
087            }
088            return caBeans;
089        }
090    }