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.Date;
019    import java.util.List;
020    
021    import javax.persistence.EntityManager;
022    import javax.persistence.Query;
023    
024    import org.apache.oozie.CoordinatorActionBean;
025    import org.apache.oozie.ErrorCode;
026    import org.apache.oozie.util.DateUtils;
027    import org.apache.oozie.util.ParamChecker;
028    
029    /**
030     * Load coordinator action by nominal time.
031     */
032    public class CoordJobGetActionForNominalTimeJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
033    
034        private String jobId = null;
035        private Date nominalTime = null;
036    
037        public CoordJobGetActionForNominalTimeJPAExecutor(String jobId, Date nominalTime) {
038            ParamChecker.notNull(jobId, "jobId");
039            this.jobId = jobId;
040            this.nominalTime = nominalTime;
041        }
042    
043        @Override
044        public String getName() {
045            return "CoordJobGetActionForNominalTimeJPAExecutor";
046        }
047    
048        @Override
049        @SuppressWarnings("unchecked")
050        public CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
051            List<CoordinatorActionBean> actions;
052            Query q = em.createNamedQuery("GET_ACTION_FOR_NOMINALTIME");
053            q.setParameter("jobId", jobId);
054            q.setParameter("nominalTime", new Timestamp(nominalTime.getTime()));
055            actions = q.getResultList();
056    
057            CoordinatorActionBean action = null;
058            if (actions.size() > 0) {
059                action = actions.get(0);
060            }
061            else {
062                throw new JPAExecutorException(ErrorCode.E0605, DateUtils.convertDateToString(nominalTime));
063            }
064            return getBeanForRunningCoordAction(action);
065        }
066    
067        private CoordinatorActionBean getBeanForRunningCoordAction(CoordinatorActionBean a) {
068            if (a != null) {
069                CoordinatorActionBean action = new CoordinatorActionBean();
070                action.setId(a.getId());
071                action.setActionNumber(a.getActionNumber());
072                action.setActionXml(a.getActionXml());
073                action.setConsoleUrl(a.getConsoleUrl());
074                action.setCreatedConf(a.getCreatedConf());
075                action.setExternalStatus(a.getExternalStatus());
076                action.setMissingDependencies(a.getMissingDependencies());
077                action.setRunConf(a.getRunConf());
078                action.setTimeOut(a.getTimeOut());
079                action.setTrackerUri(a.getTrackerUri());
080                action.setType(a.getType());
081                action.setCreatedTime(a.getCreatedTime());
082                action.setExternalId(a.getExternalId());
083                action.setJobId(a.getJobId());
084                action.setLastModifiedTime(a.getLastModifiedTime());
085                action.setNominalTime(a.getNominalTime());
086                action.setSlaXml(a.getSlaXml());
087                action.setStatus(a.getStatus());
088                return action;
089            }
090            return null;
091        }
092    }