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    package org.apache.oozie.executor.jpa;
019    
020    import java.sql.Timestamp;
021    import java.util.List;
022    
023    import javax.persistence.EntityManager;
024    import javax.persistence.Query;
025    
026    import org.apache.oozie.CoordinatorActionBean;
027    import org.apache.oozie.ErrorCode;
028    import org.apache.oozie.client.CoordinatorAction;
029    import org.apache.oozie.service.Services;
030    import org.apache.oozie.util.DateUtils;
031    import org.apache.oozie.util.ParamChecker;
032    
033    /**
034     * Load the CoordinatorAction into a Bean and return it.
035     */
036    public 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.setRunConf(a.getRunConf());
113                action.setTimeOut(a.getTimeOut());
114                action.setTrackerUri(a.getTrackerUri());
115                action.setType(a.getType());
116                action.setCreatedTime(a.getCreatedTime());
117                action.setExternalId(a.getExternalId());
118                action.setJobId(a.getJobId());
119                action.setLastModifiedTime(a.getLastModifiedTime());
120                action.setNominalTime(a.getNominalTime());
121                action.setSlaXml(a.getSlaXml());
122                action.setStatus(a.getStatus());
123                action.setPending(a.getPending());
124                action.setRerunTime(a.getRerunTime());
125                return action;
126            }
127            return null;
128        }
129    
130        private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
131            CoordinatorActionBean bean = new CoordinatorActionBean();
132            if (arr[0] != null) {
133                bean.setId((String) arr[0]);
134            }
135            if (arr[1] != null) {
136                bean.setJobId((String) arr[1]);
137            }
138            if (arr[2] != null) {
139                bean.setActionNumber((Integer) arr[2]);
140            }
141            if (arr[3] != null) {
142                bean.setConsoleUrl((String) arr[3]);
143            }
144            if (arr[4] != null) {
145                bean.setErrorCode((String) arr[4]);
146            }
147            if (arr[5] != null) {
148                bean.setErrorMessage((String) arr[5]);
149            }
150            if (arr[6] != null) {
151                bean.setExternalId((String) arr[6]);
152            }
153            if (arr[7] != null) {
154                bean.setExternalStatus((String) arr[7]);
155            }
156            if (arr[8] != null) {
157                bean.setTrackerUri((String) arr[8]);
158            }
159            if (arr[9] != null) {
160                bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[9]));
161            }
162            if (arr[10] != null) {
163                bean.setNominalTime(DateUtils.toDate((Timestamp) arr[10]));
164            }
165            if (arr[11] != null) {
166                bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[11]));
167            }
168            if (arr[12] != null) {
169                bean.setLastModifiedTime(DateUtils.toDate((Timestamp) arr[12]));
170            }
171            if (arr[13] != null) {
172                bean.setMissingDependencies((String) arr[13]);
173            }
174            return bean;
175        }
176    }