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.client.CoordinatorAction;
029import org.apache.oozie.util.DateUtils;
030import org.apache.oozie.util.ParamChecker;
031
032/**
033 * Load coordinator action by externalId.
034 */
035public class CoordActionGetForExternalIdJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
036
037    private String externalId = null;
038
039    public CoordActionGetForExternalIdJPAExecutor(String externalId) {
040        ParamChecker.notNull(externalId, "externalId");
041        this.externalId = externalId;
042    }
043
044    /* (non-Javadoc)
045     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
046     */
047    @Override
048    public String getName() {
049        return "CoordActionGetForExternalIdJPAExecutor";
050    }
051
052    /* (non-Javadoc)
053     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
054     */
055    @Override
056    @SuppressWarnings("unchecked")
057    public CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
058        try {
059            CoordinatorActionBean caBean = null;
060            Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_EXTERNALID");
061            q.setParameter("externalId", externalId);
062            List<Object[]> actionArr = q.getResultList();
063            // Actionarr may be empty if the CoordAction table is not yet updated by CoordActionStart thread
064            if (actionArr.size() > 0) {
065                caBean = getBeanForRunningCoordAction(actionArr.get(0));
066            }
067            return caBean;
068        }
069        catch (Exception e) {
070            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
071        }
072    }
073
074    private CoordinatorActionBean getBeanForRunningCoordAction(Object arr[]) {
075        CoordinatorActionBean bean = new CoordinatorActionBean();
076        if (arr[0] != null) {
077            bean.setId((String) arr[0]);
078        }
079        if (arr[1] != null) {
080            bean.setJobId((String) arr[1]);
081        }
082        if (arr[2] != null) {
083            bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
084        }
085        if (arr[3] != null) {
086            bean.setPending((Integer) arr[3]);
087        }
088        if (arr[4] != null) {
089            bean.setExternalId((String) arr[4]);
090        }
091        if (arr[5] != null) {
092            bean.setLastModifiedTime(DateUtils.toDate((Timestamp) arr[5]));
093        }
094        if (arr[6] != null){
095            bean.setSlaXml((String) arr[6]);
096        }
097        if (arr[7] != null){
098            bean.setNominalTime(DateUtils.toDate((Timestamp) arr[7]));
099        }
100        if (arr[8] != null){
101            bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[8]));
102        }
103        return bean;
104    }
105
106
107}