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.service.Services;
031import org.apache.oozie.util.DateUtils;
032import org.apache.oozie.util.ParamChecker;
033
034/**
035 * Load the CoordinatorAction into a Bean and return it.
036 */
037public class CoordActionGetForInfoJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
038
039    private String coordActionId = null;
040    public static final String COORD_GET_ALL_COLS_FOR_ACTION = "oozie.coord.action.get.all.attributes";
041
042    public CoordActionGetForInfoJPAExecutor(String coordActionId) {
043        ParamChecker.notNull(coordActionId, "coordActionId");
044        this.coordActionId = coordActionId;
045    }
046
047    /* (non-Javadoc)
048     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
049     */
050    @Override
051    public String getName() {
052        return "CoordActionGetForInfoJPAExecutor";
053    }
054
055    /* (non-Javadoc)
056     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
057     */
058    @Override
059    @SuppressWarnings("unchecked")
060    public CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
061        // Maintain backward compatibility for action info cmd
062        if (!(Services.get().getConf().getBoolean(COORD_GET_ALL_COLS_FOR_ACTION, false))) {
063            List<Object[]> actionObjects;
064            try {
065                Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_INFO");
066                q.setParameter("id", coordActionId);
067                actionObjects = q.getResultList();
068            }
069            catch (Exception e) {
070                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
071            }
072
073            if (actionObjects != null && actionObjects.size() > 0) {
074                CoordinatorActionBean bean = getBeanForRunningCoordAction(actionObjects.get(0));
075                return bean;
076            }
077            else {
078                throw new JPAExecutorException(ErrorCode.E0605, coordActionId);
079            }
080        } else {
081            List<CoordinatorActionBean> caBeans;
082            try {
083                Query q = em.createNamedQuery("GET_COORD_ACTION");
084                q.setParameter("id", coordActionId);
085                caBeans = q.getResultList();
086            }
087            catch (Exception e) {
088                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
089            }
090
091            if (caBeans != null && caBeans.size() > 0) {
092                CoordinatorActionBean bean = caBeans.get(0);
093                return bean;
094            }
095            else {
096                throw new JPAExecutorException(ErrorCode.E0605, coordActionId);
097            }
098
099        }
100    }
101
102
103
104    private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
105        CoordinatorActionBean bean = new CoordinatorActionBean();
106        if (arr[0] != null) {
107            bean.setId((String) arr[0]);
108        }
109        if (arr[1] != null) {
110            bean.setJobId((String) arr[1]);
111        }
112        if (arr[2] != null) {
113            bean.setActionNumber((Integer) arr[2]);
114        }
115        if (arr[3] != null) {
116            bean.setConsoleUrl((String) arr[3]);
117        }
118        if (arr[4] != null) {
119            bean.setErrorCode((String) arr[4]);
120        }
121        if (arr[5] != null) {
122            bean.setErrorMessage((String) arr[5]);
123        }
124        if (arr[6] != null) {
125            bean.setExternalId((String) arr[6]);
126        }
127        if (arr[7] != null) {
128            bean.setExternalStatus((String) arr[7]);
129        }
130        if (arr[8] != null) {
131            bean.setTrackerUri((String) arr[8]);
132        }
133        if (arr[9] != null) {
134            bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[9]));
135        }
136        if (arr[10] != null) {
137            bean.setNominalTime(DateUtils.toDate((Timestamp) arr[10]));
138        }
139        if (arr[11] != null) {
140            bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[11]));
141        }
142        if (arr[12] != null) {
143            bean.setLastModifiedTime(DateUtils.toDate((Timestamp) arr[12]));
144        }
145        if (arr[13] != null) {
146            bean.setMissingDependenciesBlob((StringBlob) arr[13]);
147        }
148        if (arr[14] != null) {
149            bean.setPushMissingDependenciesBlob((StringBlob) arr[14]);
150        }
151        return bean;
152    }
153}