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