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