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 by start and len (a subset) for a coordinator job.
029     */
030    public class CoordJobGetActionsSubsetJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
031    
032        private String coordJobId = null;
033        private int start = 1;
034        private int len = 50;
035    
036        public CoordJobGetActionsSubsetJPAExecutor(String coordJobId) {
037            ParamChecker.notNull(coordJobId, "coordJobId");
038            this.coordJobId = coordJobId;
039        }
040    
041        public CoordJobGetActionsSubsetJPAExecutor(String coordJobId, int start, int len) {
042            this(coordJobId);
043            this.start = start;
044            this.len = len;
045        }
046    
047        @Override
048        public String getName() {
049            return "CoordJobGetActionsSubsetJPAExecutor";
050        }
051    
052        @Override
053        @SuppressWarnings("unchecked")
054        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
055            List<CoordinatorActionBean> actions;
056            List<CoordinatorActionBean> actionList = new ArrayList<CoordinatorActionBean>();
057            try {
058                Query q = em.createNamedQuery("GET_ACTIONS_FOR_COORD_JOB");
059                q.setParameter("jobId", coordJobId);
060                q.setFirstResult(start - 1);
061                q.setMaxResults(len);
062                actions = q.getResultList();
063                for (CoordinatorActionBean a : actions) {
064                    CoordinatorActionBean aa = getBeanForRunningCoordAction(a);
065                    actionList.add(aa);
066                }
067            }
068            catch (Exception e) {
069                throw new JPAExecutorException(ErrorCode.E0603, e);
070            }
071            return actionList;
072        }
073    
074        private CoordinatorActionBean getBeanForRunningCoordAction(CoordinatorActionBean a) {
075            if (a != null) {
076                CoordinatorActionBean action = new CoordinatorActionBean();
077                action.setId(a.getId());
078                action.setActionNumber(a.getActionNumber());
079                action.setActionXml(a.getActionXml());
080                action.setConsoleUrl(a.getConsoleUrl());
081                action.setCreatedConf(a.getCreatedConf());
082                action.setExternalStatus(a.getExternalStatus());
083                action.setMissingDependencies(a.getMissingDependencies());
084                action.setRunConf(a.getRunConf());
085                action.setTimeOut(a.getTimeOut());
086                action.setTrackerUri(a.getTrackerUri());
087                action.setType(a.getType());
088                action.setCreatedTime(a.getCreatedTime());
089                action.setExternalId(a.getExternalId());
090                action.setJobId(a.getJobId());
091                action.setLastModifiedTime(a.getLastModifiedTime());
092                action.setNominalTime(a.getNominalTime());
093                action.setSlaXml(a.getSlaXml());
094                action.setStatus(a.getStatus());
095                return action;
096            }
097            return null;
098        }
099    
100    }