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 */
018package org.apache.oozie.executor.jpa;
019
020import java.sql.Timestamp;
021import java.util.Date;
022import java.util.List;
023
024import javax.persistence.EntityManager;
025import javax.persistence.Query;
026
027import org.apache.oozie.CoordinatorActionBean;
028import org.apache.oozie.ErrorCode;
029import org.apache.oozie.util.DateUtils;
030import org.apache.oozie.util.ParamChecker;
031
032/**
033 * Load coordinator action by nominal time.
034 */
035public 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.formatDateOozieTZ(nominalTime));
066        }
067        return action;
068    }
069
070}