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.CoordinatorJobBean;
029    import org.apache.oozie.ErrorCode;
030    import org.apache.oozie.client.CoordinatorAction;
031    import org.apache.oozie.client.Job.Status;
032    import org.apache.oozie.util.DateUtils;
033    import org.apache.oozie.util.ParamChecker;
034    
035    /**
036     * Load the running Coordinator actions for a given Coordinator job
037     */
038    public class CoordJobGetActionsRunningJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
039    
040        private String coordJobId = null;
041    
042        public CoordJobGetActionsRunningJPAExecutor(String coordJobId) {
043            ParamChecker.notNull(coordJobId, "coordJobId");
044            this.coordJobId = coordJobId;
045        }
046    
047        @Override
048        public String getName() {
049            return "CoordJobGetActionsRunningJPAExecutor";
050        }
051    
052        @Override
053        @SuppressWarnings("unchecked")
054        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
055          try {
056                List<CoordinatorActionBean> actionBeansList = new ArrayList<CoordinatorActionBean>();
057                Query q = em.createNamedQuery("GET_COORD_ACTIONS_RUNNING");
058                q.setParameter("jobId", coordJobId);
059                List<Object[]> objectArrList = q.getResultList();
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            if (arr[3] != null) {
091                bean.setExternalId((String) arr[3]);
092            }
093            if (arr[4] != null){
094                bean.setNominalTime(DateUtils.toDate((Timestamp) arr[4]));
095            }
096            if (arr[5] != null){
097                bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[5]));
098            }
099            return bean;
100        }
101    
102    }