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.service.Services;
030import org.apache.oozie.util.DateUtils;
031import org.apache.oozie.util.ParamChecker;
032
033/**
034 * Load the CoordinatorAction into a Bean and return it.
035 */
036public class CoordActionGetForInfoJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
037
038    private String coordActionId = null;
039    public static final String COORD_GET_ALL_COLS_FOR_ACTION = "oozie.coord.action.get.all.attributes";
040
041    public CoordActionGetForInfoJPAExecutor(String coordActionId) {
042        ParamChecker.notNull(coordActionId, "coordActionId");
043        this.coordActionId = coordActionId;
044    }
045
046    /* (non-Javadoc)
047     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
048     */
049    @Override
050    public String getName() {
051        return "CoordActionGetForInfoJPAExecutor";
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        // Maintain backward compatibility for action info cmd
061        if (!(Services.get().getConf().getBoolean(COORD_GET_ALL_COLS_FOR_ACTION, false))) {
062            List<Object[]> actionObjects;
063            try {
064                Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_INFO");
065                q.setParameter("id", coordActionId);
066                actionObjects = q.getResultList();
067            }
068            catch (Exception e) {
069                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
070            }
071
072            if (actionObjects != null && actionObjects.size() > 0) {
073                CoordinatorActionBean bean = getBeanForRunningCoordAction(actionObjects.get(0));
074                return bean;
075            }
076            else {
077                throw new JPAExecutorException(ErrorCode.E0605, coordActionId);
078            }
079        } else {
080            List<CoordinatorActionBean> caBeans;
081            try {
082                Query q = em.createNamedQuery("GET_COORD_ACTION");
083                q.setParameter("id", coordActionId);
084                caBeans = q.getResultList();
085            }
086            catch (Exception e) {
087                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
088            }
089
090            if (caBeans != null && caBeans.size() > 0) {
091                CoordinatorActionBean bean = getBeanForCoordAction(caBeans.get(0));
092                return bean;
093            }
094            else {
095                throw new JPAExecutorException(ErrorCode.E0605, coordActionId);
096            }
097
098        }
099    }
100
101
102    private CoordinatorActionBean getBeanForCoordAction(CoordinatorActionBean a){
103        if (a != null) {
104            CoordinatorActionBean action = new CoordinatorActionBean();
105            action.setId(a.getId());
106            action.setActionNumber(a.getActionNumber());
107            action.setActionXml(a.getActionXml());
108            action.setConsoleUrl(a.getConsoleUrl());
109            action.setCreatedConf(a.getCreatedConf());
110            action.setExternalStatus(a.getExternalStatus());
111            action.setMissingDependencies(a.getMissingDependencies());
112            action.setPushMissingDependencies(a.getPushMissingDependencies());
113            action.setRunConf(a.getRunConf());
114            action.setTimeOut(a.getTimeOut());
115            action.setTrackerUri(a.getTrackerUri());
116            action.setType(a.getType());
117            action.setCreatedTime(a.getCreatedTime());
118            action.setExternalId(a.getExternalId());
119            action.setJobId(a.getJobId());
120            action.setLastModifiedTime(a.getLastModifiedTime());
121            action.setNominalTime(a.getNominalTime());
122            action.setSlaXml(a.getSlaXml());
123            action.setStatus(a.getStatus());
124            action.setPending(a.getPending());
125            action.setRerunTime(a.getRerunTime());
126            return action;
127        }
128        return null;
129    }
130
131    private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
132        CoordinatorActionBean bean = new CoordinatorActionBean();
133        if (arr[0] != null) {
134            bean.setId((String) arr[0]);
135        }
136        if (arr[1] != null) {
137            bean.setJobId((String) arr[1]);
138        }
139        if (arr[2] != null) {
140            bean.setActionNumber((Integer) arr[2]);
141        }
142        if (arr[3] != null) {
143            bean.setConsoleUrl((String) arr[3]);
144        }
145        if (arr[4] != null) {
146            bean.setErrorCode((String) arr[4]);
147        }
148        if (arr[5] != null) {
149            bean.setErrorMessage((String) arr[5]);
150        }
151        if (arr[6] != null) {
152            bean.setExternalId((String) arr[6]);
153        }
154        if (arr[7] != null) {
155            bean.setExternalStatus((String) arr[7]);
156        }
157        if (arr[8] != null) {
158            bean.setTrackerUri((String) arr[8]);
159        }
160        if (arr[9] != null) {
161            bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[9]));
162        }
163        if (arr[10] != null) {
164            bean.setNominalTime(DateUtils.toDate((Timestamp) arr[10]));
165        }
166        if (arr[11] != null) {
167            bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[11]));
168        }
169        if (arr[12] != null) {
170            bean.setLastModifiedTime(DateUtils.toDate((Timestamp) arr[12]));
171        }
172        if (arr[13] != null) {
173            bean.setMissingDependencies((String) arr[13]);
174        }
175        if (arr[14] != null) {
176            bean.setPushMissingDependencies((String) arr[14]);
177        }
178        return bean;
179    }
180}