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