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