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