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