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.client.CoordinatorAction;
032import org.apache.oozie.util.DateUtils;
033import org.apache.oozie.util.ParamChecker;
034
035/**
036 * Load non-terminal coordinator actions by dates.
037 */
038public class CoordJobGetActionsByDatesForKillJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
039
040    private String jobId = null;
041    private Date startDate, endDate;
042
043    public CoordJobGetActionsByDatesForKillJPAExecutor(String jobId, Date startDate, Date endDate) {
044        ParamChecker.notNull(jobId, "jobId");
045        this.jobId = jobId;
046        this.startDate = startDate;
047        this.endDate = endDate;
048    }
049
050    @Override
051    public String getName() {
052        return "CoordJobGetActionsByDatesForKillJPAExecutor";
053    }
054
055    @Override
056    @SuppressWarnings("unchecked")
057    public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
058        List<CoordinatorActionBean> actionList = new ArrayList<CoordinatorActionBean>();
059        try {
060            Query q = em.createNamedQuery("GET_ACTIONS_BY_DATES_FOR_KILL");
061            q.setParameter("jobId", jobId);
062            q.setParameter("startTime", new Timestamp(startDate.getTime()));
063            q.setParameter("endTime", new Timestamp(endDate.getTime()));
064            List<Object[]> actions = q.getResultList();
065
066            for (Object[] a : actions) {
067                CoordinatorActionBean aa = getBeanForRunningCoordAction(a);
068                actionList.add(aa);
069            }
070            return actionList;
071        }
072        catch (Exception e) {
073            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
074        }
075    }
076
077    private CoordinatorActionBean getBeanForRunningCoordAction(Object[] arr) {
078        CoordinatorActionBean action = new CoordinatorActionBean();
079        if (arr[0] != null) {
080            action.setId((String) arr[0]);
081        }
082
083        if (arr[1] != null) {
084            action.setJobId((String) arr[1]);
085        }
086
087        if (arr[2] != null) {
088            action.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
089        }
090
091        if (arr[3] != null) {
092            action.setExternalId((String) arr[3]);
093        }
094
095        if (arr[4] != null) {
096            action.setPending((Integer) arr[4]);
097        }
098
099        if (arr[5] != null) {
100            action.setNominalTime(DateUtils.toDate((Timestamp) arr[5]));
101        }
102
103        if (arr[6] != null) {
104            action.setCreatedTime(DateUtils.toDate((Timestamp) arr[6]));
105        }
106        return action;
107    }
108}