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, or LAST_ONLY
059                if (executionOrder.equalsIgnoreCase("FIFO")) {
060                    q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_FIFO");
061                }
062                else {
063                    q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_JOB_LIFO");
064                }
065                q.setParameter("jobId", coordJobId);
066    
067                // if executionOrder is LAST_ONLY, only retrieve first record in LIFO,
068                // otherwise, use numResults if it is positive.
069                if (executionOrder.equalsIgnoreCase("LAST_ONLY")) {
070                    q.setMaxResults(1);
071                }
072                else {
073                    if (numResults > 0) {
074                        q.setMaxResults(numResults);
075                    }
076                }
077                List<Object[]> objectArrList = q.getResultList();
078                actionBeans = new ArrayList<CoordinatorActionBean>();
079                for (Object[] arr : objectArrList) {
080                    CoordinatorActionBean caa = getBeanForCoordinatorActionFromArray(arr);
081                    actionBeans.add(caa);
082                }
083                return actionBeans;
084            }
085            catch (Exception e) {
086                throw new JPAExecutorException(ErrorCode.E0603, e);
087            }
088        }
089    
090        private CoordinatorActionBean getBeanForCoordinatorActionFromArray(Object arr[]) {
091            CoordinatorActionBean bean = new CoordinatorActionBean();
092            if (arr[0] != null) {
093                bean.setId((String) arr[0]);
094            }
095            if (arr[1] != null) {
096                bean.setJobId((String) arr[1]);
097            }
098            if (arr[2] != null) {
099                bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
100            }
101            if (arr[3] != null) {
102                bean.setPending((Integer) arr[3]);
103            }
104            return bean;
105        }
106    
107    }