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
019package org.apache.oozie.executor.jpa;
020
021import java.sql.Timestamp;
022import java.util.ArrayList;
023import java.util.Date;
024import java.util.List;
025
026import javax.persistence.EntityManager;
027import javax.persistence.Query;
028
029import org.apache.oozie.CoordinatorActionBean;
030import org.apache.oozie.ErrorCode;
031import org.apache.oozie.util.ParamChecker;
032
033/**
034 * Load coordinator actions by dates.
035 */
036public class CoordJobGetActionsForDatesJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
037
038    private String jobId = null;
039    private Date startDate, endDate;
040
041    public CoordJobGetActionsForDatesJPAExecutor(String jobId, Date startDate, Date endDate) {
042        ParamChecker.notNull(jobId, "jobId");
043        this.jobId = jobId;
044        this.startDate = startDate;
045        this.endDate = endDate;
046    }
047
048    @Override
049    public String getName() {
050        return "CoordJobGetActionsForDatesJPAExecutor";
051    }
052
053    @Override
054    @SuppressWarnings("unchecked")
055    public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
056        List<CoordinatorActionBean> actions;
057        try {
058            Query q = em.createNamedQuery("GET_ACTIONS_FOR_DATES");
059            q.setParameter("jobId", jobId);
060            q.setParameter("startTime", new Timestamp(startDate.getTime()));
061            q.setParameter("endTime", new Timestamp(endDate.getTime()));
062            actions = q.getResultList();
063            return actions;
064        }
065        catch (Exception e) {
066            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
067        }
068    }
069
070}