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