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
019
020package org.apache.oozie.executor.jpa;
021
022import java.sql.Timestamp;
023import java.util.Date;
024import java.util.List;
025
026import javax.persistence.EntityManager;
027import javax.persistence.Query;
028
029import org.apache.oozie.ErrorCode;
030import org.apache.oozie.util.DateUtils;
031import org.apache.oozie.util.ParamChecker;
032
033public class CoordJobGetActionModifiedDateForRangeJPAExecutor implements JPAExecutor<Date> {
034
035    private String jobId = null;
036    private String startAction, endAction;
037
038    public CoordJobGetActionModifiedDateForRangeJPAExecutor(String jobId, String startAction, String endAction) {
039        ParamChecker.notNull(jobId, "jobId");
040        this.jobId = jobId;
041        this.startAction = startAction;
042        this.endAction = endAction;
043    }
044
045    @Override
046    public String getName() {
047        return "CoordJobGetActionModifiedDateForRangeJPAExecutor";
048    }
049
050    @Override
051    @SuppressWarnings("unchecked")
052    public Date execute(EntityManager em) throws JPAExecutorException {
053        try {
054            Query q = em.createNamedQuery("GET_COORD_ACTIONS_MAX_MODIFIED_DATE_FOR_RANGE");
055            q.setParameter("jobId", jobId);
056            q.setParameter("startAction", startAction);
057            q.setParameter("endAction", endAction);
058            List<Timestamp> coordActionIds = q.getResultList();
059            return coordActionIds.isEmpty() ? new Date() : DateUtils.toDate(coordActionIds.get(0));
060        }
061        catch (Exception e) {
062            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
063        }
064    }
065
066}