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.sql.Timestamp;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.persistence.EntityManager;
025    import javax.persistence.Query;
026    
027    import org.apache.oozie.CoordinatorActionBean;
028    import org.apache.oozie.ErrorCode;
029    import org.apache.oozie.StringBlob;
030    import org.apache.oozie.client.CoordinatorAction;
031    import org.apache.oozie.util.DateUtils;
032    import org.apache.oozie.util.ParamChecker;
033    
034    /**
035     * Load the Coordinator actions which are not completed for a given Coordinator job
036     */
037    public class CoordJobGetActionsNotCompletedJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
038    
039        private String coordJobId = null;
040    
041        public CoordJobGetActionsNotCompletedJPAExecutor(String coordJobId) {
042            ParamChecker.notNull(coordJobId, "coordJobId");
043            this.coordJobId = coordJobId;
044        }
045    
046        @Override
047        public String getName() {
048            return "CoordJobGetActionsNotCompletedJPAExecutor";
049        }
050    
051        @Override
052        @SuppressWarnings("unchecked")
053        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
054            try {
055                List<CoordinatorActionBean> actionBeansList = new ArrayList<CoordinatorActionBean>();
056                Query q = em.createNamedQuery("GET_COORD_ACTIONS_NOT_COMPLETED");
057                q.setParameter("jobId", coordJobId);
058                List<Object[]> objectArrList = q.getResultList();
059    
060                for (Object[] arr : objectArrList) {
061                    CoordinatorActionBean caa = getBeanForCoordinatorActionFromArray(arr);
062                    actionBeansList.add(caa);
063                }
064                return actionBeansList;
065            }
066            catch (Exception e) {
067                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
068            }
069        }
070    
071        /*
072         * A Coordinator action bean is constructed from an array of objects.
073         * ActionId, status, pending and externalId of the Coordinator action are
074         * updated. If values of id, status and externalId fetched from db are
075         * null, they will be automatically initialized to default values of null as
076         * they are Strings. If value of pending fetched from db is null, it will be
077         * initialized with default value of 0 as it is a int.
078         */
079        private CoordinatorActionBean getBeanForCoordinatorActionFromArray(Object[] arr) {
080            CoordinatorActionBean bean = new CoordinatorActionBean();
081            if (arr[0] != null) {
082                bean.setId((String) arr[0]);
083            }
084            if (arr[1] != null) {
085                bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[1]));
086            }
087            if (arr[2] != null) {
088                bean.setPending((Integer) arr[2]);
089            }
090            else{
091                bean.setPending(0);
092            }
093            if (arr[3] != null) {
094                bean.setExternalId((String) arr[3]);
095            }
096            if (arr[4] != null) {
097                bean.setPushMissingDependenciesBlob((StringBlob) arr[4]);
098            }
099            if (arr[5] != null){
100                bean.setNominalTime(DateUtils.toDate((Timestamp) arr[5]));
101            }
102            if (arr[6] != null){
103                bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[6]));
104            }
105            if (arr[7] != null) {
106                bean.setJobId((String) arr[7]);
107            }
108            return bean;
109        }
110    
111    }