This project has retired. For details please refer to its
Attic page.
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.List;
023
024 import javax.persistence.EntityManager;
025 import javax.persistence.Query;
026
027 import org.apache.oozie.CoordinatorActionBean;
028 import org.apache.oozie.ErrorCode;
029 import org.apache.oozie.client.CoordinatorAction;
030 import org.apache.oozie.util.DateUtils;
031 import org.apache.oozie.util.ParamChecker;
032
033 /**
034 * Load the Coordinator actions which are not completed for a given Coordinator job
035 */
036 public class CoordJobGetActionsNotCompletedJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
037
038 private String coordJobId = null;
039
040 public CoordJobGetActionsNotCompletedJPAExecutor(String coordJobId) {
041 ParamChecker.notNull(coordJobId, "coordJobId");
042 this.coordJobId = coordJobId;
043 }
044
045 @Override
046 public String getName() {
047 return "CoordJobGetActionsNotCompletedJPAExecutor";
048 }
049
050 @Override
051 @SuppressWarnings("unchecked")
052 public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
053 try {
054 List<CoordinatorActionBean> actionBeansList = new ArrayList<CoordinatorActionBean>();
055 Query q = em.createNamedQuery("GET_COORD_ACTIONS_NOT_COMPLETED");
056 q.setParameter("jobId", coordJobId);
057 List<Object[]> objectArrList = q.getResultList();
058
059 for (Object[] arr : objectArrList) {
060 CoordinatorActionBean caa = getBeanForCoordinatorActionFromArray(arr);
061 actionBeansList.add(caa);
062 }
063 return actionBeansList;
064 }
065 catch (Exception e) {
066 throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
067 }
068 }
069
070 /*
071 * A Coordinator action bean is constructed from an array of objects.
072 * ActionId, status, pending and externalId of the Coordinator action are
073 * updated. If values of id, status and externalId fetched from db are
074 * null, they will be automatically initialized to default values of null as
075 * they are Strings. If value of pending fetched from db is null, it will be
076 * initialized with default value of 0 as it is a int.
077 */
078 private CoordinatorActionBean getBeanForCoordinatorActionFromArray(Object[] arr) {
079 CoordinatorActionBean bean = new CoordinatorActionBean();
080 if (arr[0] != null) {
081 bean.setId((String) arr[0]);
082 }
083 if (arr[1] != null) {
084 bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[1]));
085 }
086 if (arr[2] != null) {
087 bean.setPending((Integer) arr[2]);
088 }
089 else{
090 bean.setPending(0);
091 }
092 if (arr[3] != null) {
093 bean.setExternalId((String) arr[3]);
094 }
095 if (arr[4] != null) {
096 bean.setPushMissingDependencies((String) arr[4]);
097 }
098 if (arr[5] != null){
099 bean.setNominalTime(DateUtils.toDate((Timestamp) arr[5]));
100 }
101 if (arr[6] != null){
102 bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[6]));
103 }
104 if (arr[7] != null) {
105 bean.setJobId((String) arr[7]);
106 }
107 return bean;
108 }
109
110 }