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