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.sql.Timestamp;
018    import java.util.ArrayList;
019    import java.util.Date;
020    import java.util.List;
021    
022    import javax.persistence.EntityManager;
023    import javax.persistence.Query;
024    
025    import org.apache.oozie.CoordinatorActionBean;
026    import org.apache.oozie.ErrorCode;
027    import org.apache.oozie.util.ParamChecker;
028    
029    /**
030     * Load coordinator actions by dates.
031     */
032    public class CoordJobGetActionsForDatesJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
033    
034        private String jobId = null;
035        private Date startDate, endDate;
036    
037        public CoordJobGetActionsForDatesJPAExecutor(String jobId, Date startDate, Date endDate) {
038            ParamChecker.notNull(jobId, "jobId");
039            this.jobId = jobId;
040            this.startDate = startDate;
041            this.endDate = endDate;
042        }
043    
044        @Override
045        public String getName() {
046            return "CoordJobGetActionsForDatesJPAExecutor";
047        }
048    
049        @Override
050        @SuppressWarnings("unchecked")
051        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
052            List<CoordinatorActionBean> actions;
053            try {
054                Query q = em.createNamedQuery("GET_ACTIONS_FOR_DATES");
055                q.setParameter("jobId", jobId);
056                q.setParameter("startTime", new Timestamp(startDate.getTime()));
057                q.setParameter("endTime", new Timestamp(endDate.getTime()));
058                actions = q.getResultList();
059    
060                List<CoordinatorActionBean> actionList = new ArrayList<CoordinatorActionBean>();
061                for (CoordinatorActionBean a : actions) {
062                    CoordinatorActionBean aa = getBeanForRunningCoordAction(a);
063                    actionList.add(aa);
064                }
065                return actionList;
066            }
067            catch (Exception e) {
068                throw new JPAExecutorException(ErrorCode.E0603, e);
069            }       
070        }
071    
072        private CoordinatorActionBean getBeanForRunningCoordAction(CoordinatorActionBean a) {
073            if (a != null) {
074                CoordinatorActionBean action = new CoordinatorActionBean();
075                action.setId(a.getId());
076                action.setActionNumber(a.getActionNumber());
077                action.setActionXml(a.getActionXml());
078                action.setConsoleUrl(a.getConsoleUrl());
079                action.setCreatedConf(a.getCreatedConf());
080                //action.setErrorCode(a.getErrorCode());
081                //action.setErrorMessage(a.getErrorMessage());
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    }