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.List;
022
023import javax.persistence.EntityManager;
024import javax.persistence.Query;
025
026import org.apache.oozie.CoordinatorActionBean;
027import org.apache.oozie.ErrorCode;
028import org.apache.oozie.StringBlob;
029import org.apache.oozie.client.CoordinatorAction;
030import org.apache.oozie.util.DateUtils;
031import org.apache.oozie.util.ParamChecker;
032
033/**
034 * JPAExecutor to get attributes of CoordinatorActionBean required by CoordActionCheckCommand
035 */
036public class CoordActionGetForCheckJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
037
038    private String coordActionId = null;
039
040    public CoordActionGetForCheckJPAExecutor(String coordActionId) {
041        ParamChecker.notNull(coordActionId, "coordActionId");
042        this.coordActionId = coordActionId;
043    }
044
045    /* (non-Javadoc)
046     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
047     */
048    @Override
049    public String getName() {
050        return "CoordActionGetForCheckJPAExecutor";
051    }
052
053    /* (non-Javadoc)
054     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
055     */
056    @Override
057    public CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
058        try {
059            Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_CHECK");
060            q.setParameter("id", coordActionId);
061            Object[] obj = (Object[]) q.getSingleResult();
062            CoordinatorActionBean caBean = getBeanForRunningCoordAction(obj);
063            return caBean;
064        }
065        catch (Exception e) {
066            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
067        }
068
069    }
070
071    private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
072        CoordinatorActionBean bean = new CoordinatorActionBean();
073        if (arr[0] != null) {
074            bean.setId((String) arr[0]);
075        }
076        if (arr[1] != null) {
077            bean.setJobId((String) arr[1]);
078        }
079        if (arr[2] != null) {
080            bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
081        }
082        if (arr[3] != null) {
083            bean.setPending((Integer) arr[3]);
084        }
085        if (arr[4] != null) {
086            bean.setExternalId((String) arr[4]);
087        }
088        if (arr[5] != null) {
089            bean.setLastModifiedTime(DateUtils.toDate((Timestamp) arr[5]));
090        }
091        if (arr[6] != null){
092            bean.setSlaXmlBlob((StringBlob) arr[6]);
093        }
094        if (arr[7] != null){
095            bean.setNominalTime(DateUtils.toDate((Timestamp) arr[7]));
096        }
097        if (arr[8] != null){
098            bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[8]));
099        }
100        return bean;
101    }
102}