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