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 the Coordinator actions which are not completed for a given Coordinator job
033     */
034    public class CoordJobGetActionsNotCompletedJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
035    
036        private String coordJobId = null;
037    
038        public CoordJobGetActionsNotCompletedJPAExecutor(String coordJobId) {
039            ParamChecker.notNull(coordJobId, "coordJobId");
040            this.coordJobId = coordJobId;
041        }
042    
043        @Override
044        public String getName() {
045            return "CoordJobGetActionsNotCompletedJPAExecutor";
046        }
047    
048        @Override
049        @SuppressWarnings("unchecked")
050        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
051            try {
052                List<CoordinatorActionBean> actionBeansList = new ArrayList<CoordinatorActionBean>();
053                Query q = em.createNamedQuery("GET_COORD_ACTIONS_NOT_COMPLETED");
054                q.setParameter("jobId", coordJobId);
055                List<Object[]> objectArrList = q.getResultList();
056    
057                for (Object[] arr : objectArrList) {
058                    CoordinatorActionBean caa = getBeanForCoordinatorActionFromArray(arr);
059                    actionBeansList.add(caa);
060                }
061                return actionBeansList;
062            }
063            catch (Exception e) {
064                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
065            }
066        }
067    
068        /*
069         * A Coordinator action bean is constructed from an array of objects.
070         * ActionId, status, pending and externalId of the Coordinator action are
071         * updated. If values of id, status and externalId fetched from db are
072         * null, they will be automatically initialized to default values of null as
073         * they are Strings. If value of pending fetched from db is null, it will be
074         * initialized with default value of 0 as it is a int.
075         */
076        private CoordinatorActionBean getBeanForCoordinatorActionFromArray(Object[] arr) {
077            CoordinatorActionBean bean = new CoordinatorActionBean();
078            if (arr[0] != null) {
079                bean.setId((String) arr[0]);
080            }
081            if (arr[1] != null) {
082                bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[1]));
083            }
084            if (arr[2] != null) {
085                bean.setPending((Integer) arr[2]);
086            }
087            else{
088                bean.setPending(0);
089            }
090            if (arr[3] != null) {
091                bean.setExternalId((String) arr[3]);
092            }
093            return bean;
094        }
095    
096    }