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