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