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.ErrorCode;
030import org.apache.oozie.client.CoordinatorAction;
031import org.apache.oozie.util.DateUtils;
032import org.apache.oozie.util.ParamChecker;
033
034/**
035 * Load the suspended Coordinator actions of a given Coordinator job
036 */
037public class CoordJobGetActionsSuspendedJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
038
039    private String coordJobId = null;
040
041    public CoordJobGetActionsSuspendedJPAExecutor(String coordJobId) {
042        ParamChecker.notNull(coordJobId, "coordJobId");
043        this.coordJobId = coordJobId;
044    }
045
046    @Override
047    public String getName() {
048        return "CoordJobGetActionsSuspendedJPAExecutor";
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_SUSPENDED");
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        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}