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