001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.executor.jpa;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    import javax.persistence.EntityManager;
021    import javax.persistence.Query;
022    
023    import org.apache.oozie.CoordinatorActionBean;
024    import org.apache.oozie.ErrorCode;
025    import org.apache.oozie.util.ParamChecker;
026    
027    /**
028     * Load coordinator actions for a coordinator job.
029     */
030    public class CoordJobGetActionsJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
031    
032        private String coordJobId = null;
033    
034        public CoordJobGetActionsJPAExecutor(String coordJobId) {
035            ParamChecker.notNull(coordJobId, "coordJobId");
036            this.coordJobId = coordJobId;
037        }
038    
039        @Override
040        public String getName() {
041            return "CoordJobGetActionsJPAExecutor";
042        }
043    
044        @Override
045        @SuppressWarnings("unchecked")
046        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
047            List<CoordinatorActionBean> actionBeans = new ArrayList<CoordinatorActionBean>();
048            try {
049                Query q = em.createNamedQuery("GET_ACTIONS_FOR_COORD_JOB");
050                q.setParameter("jobId", coordJobId);
051                List<CoordinatorActionBean> actions = q.getResultList();
052                for (CoordinatorActionBean a : actions) {
053                    CoordinatorActionBean aa = getBeanForRunningCoordAction(a);
054                    actionBeans.add(aa);
055                }
056                
057                return actionBeans;
058            }
059            catch (Exception e) {
060                throw new JPAExecutorException(ErrorCode.E0603, e);
061            }        
062        }
063    
064        private CoordinatorActionBean getBeanForRunningCoordAction(CoordinatorActionBean a) {
065            if (a != null) {
066                CoordinatorActionBean action = new CoordinatorActionBean();
067                action.setId(a.getId());
068                action.setActionNumber(a.getActionNumber());
069                action.setActionXml(a.getActionXml());
070                action.setConsoleUrl(a.getConsoleUrl());
071                action.setCreatedConf(a.getCreatedConf());
072                action.setExternalStatus(a.getExternalStatus());
073                action.setMissingDependencies(a.getMissingDependencies());
074                action.setRunConf(a.getRunConf());
075                action.setTimeOut(a.getTimeOut());
076                action.setTrackerUri(a.getTrackerUri());
077                action.setType(a.getType());
078                action.setCreatedTime(a.getCreatedTime());
079                action.setExternalId(a.getExternalId());
080                action.setJobId(a.getJobId());
081                action.setLastModifiedTime(a.getLastModifiedTime());
082                action.setNominalTime(a.getNominalTime());
083                action.setSlaXml(a.getSlaXml());
084                action.setStatus(a.getStatus());
085                return action;
086            }
087            return null;
088        }
089    }