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.Date;
021 import java.sql.Timestamp;
022 import java.util.ArrayList;
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.service.Services;
032 import org.apache.oozie.util.DateUtils;
033 import org.apache.oozie.util.ParamChecker;
034
035 /**
036 * Load coordinator actions by start and len (a subset) for a coordinator job.
037 */
038 public class CoordJobGetActionsSubsetJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
039
040 private String coordJobId = null;
041 private int start = 1;
042 private int len = 50;
043 private List<String> filterList;
044
045 public CoordJobGetActionsSubsetJPAExecutor(String coordJobId) {
046 ParamChecker.notNull(coordJobId, "coordJobId");
047 this.coordJobId = coordJobId;
048 }
049
050 public CoordJobGetActionsSubsetJPAExecutor(String coordJobId, List<String> filterList, int start, int len) {
051 this(coordJobId);
052 ParamChecker.notNull(filterList, "filterList");
053 this.filterList = filterList;
054 this.start = start;
055 this.len = len;
056 }
057
058 @Override
059 public String getName() {
060 return "CoordJobGetActionsSubsetJPAExecutor";
061 }
062
063 @Override
064 @SuppressWarnings("unchecked")
065 public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
066 List<CoordinatorActionBean> actionList = new ArrayList<CoordinatorActionBean>();
067 try {
068 if (!Services.get().getConf()
069 .getBoolean(CoordActionGetForInfoJPAExecutor.COORD_GET_ALL_COLS_FOR_ACTION, false)) {
070 Query q = em.createNamedQuery("GET_ACTIONS_FOR_COORD_JOB_ORDER_BY_NOMINAL_TIME");
071 q = setQueryParameters(q, em);
072 List<Object[]> actions = q.getResultList();
073
074 for (Object[] a : actions) {
075 CoordinatorActionBean aa = getBeanForRunningCoordAction(a);
076 actionList.add(aa);
077 }
078 } else {
079 Query q = em.createNamedQuery("GET_ALL_COLS_FOR_ACTIONS_FOR_COORD_JOB_ORDER_BY_NOMINAL_TIME");
080 q = setQueryParameters(q, em);
081 List<CoordinatorActionBean> caActions = q.getResultList();
082
083 for (CoordinatorActionBean a : caActions) {
084 CoordinatorActionBean aa = getBeanForCoordAction(a);
085 actionList.add(aa);
086 }
087 }
088 }
089 catch (Exception e) {
090 throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
091 }
092 return actionList;
093 }
094
095 private Query setQueryParameters(Query q, EntityManager em){
096 if (!filterList.isEmpty()) {
097 // Add the filter clause
098 String query = q.toString();
099 StringBuilder sbTotal = new StringBuilder(query);
100 int offset = query.lastIndexOf("order");
101 // Get the 'where' clause for status filters
102 StringBuilder statusClause = getStatusClause(filterList);
103 // Insert 'where' before 'order by'
104 sbTotal.insert(offset, statusClause);
105 q = em.createQuery(sbTotal.toString());
106 }
107 q.setParameter("jobId", coordJobId);
108 q.setFirstResult(start - 1);
109 q.setMaxResults(len);
110 return q;
111 }
112
113 // Form the where clause to filter by status values
114 private StringBuilder getStatusClause(List<String> filterList) {
115 StringBuilder sb = new StringBuilder();
116 boolean isStatus = false;
117 for (String statusVal : filterList) {
118 if (!isStatus) {
119 sb.append(" and a.status IN (\'" + statusVal + "\'");
120 isStatus = true;
121 }
122 else {
123 sb.append(",\'" + statusVal + "\'");
124 }
125 }
126 sb.append(") ");
127 return sb;
128 }
129
130 private CoordinatorActionBean getBeanForCoordAction(CoordinatorActionBean a){
131 if (a != null) {
132 CoordinatorActionBean action = new CoordinatorActionBean();
133 action.setId(a.getId());
134 action.setActionNumber(a.getActionNumber());
135 action.setActionXml(a.getActionXml());
136 action.setConsoleUrl(a.getConsoleUrl());
137 action.setCreatedConf(a.getCreatedConf());
138 action.setExternalStatus(a.getExternalStatus());
139 action.setMissingDependencies(a.getMissingDependencies());
140 action.setRunConf(a.getRunConf());
141 action.setTimeOut(a.getTimeOut());
142 action.setTrackerUri(a.getTrackerUri());
143 action.setType(a.getType());
144 action.setCreatedTime(a.getCreatedTime());
145 action.setExternalId(a.getExternalId());
146 action.setJobId(a.getJobId());
147 action.setLastModifiedTime(a.getLastModifiedTime());
148 action.setNominalTime(a.getNominalTime());
149 action.setSlaXml(a.getSlaXml());
150 action.setStatus(a.getStatus());
151 return action;
152 }
153 return null;
154 }
155
156 private CoordinatorActionBean getBeanForRunningCoordAction(Object arr[]) {
157 CoordinatorActionBean bean = new CoordinatorActionBean();
158 if (arr[0] != null) {
159 bean.setId((String) arr[0]);
160 }
161 if (arr[1] != null) {
162 bean.setActionNumber((Integer) arr[1]);
163 }
164 if (arr[2] != null) {
165 bean.setConsoleUrl((String) arr[2]);
166 }
167 if (arr[3] != null) {
168 bean.setErrorCode((String) arr[3]);
169 }
170 if (arr[4] != null) {
171 bean.setErrorMessage((String) arr[4]);
172 }
173 if (arr[5] != null) {
174 bean.setExternalId((String) arr[5]);
175 }
176 if (arr[6] != null) {
177 bean.setExternalStatus((String) arr[6]);
178 }
179 if (arr[7] != null) {
180 bean.setJobId((String) arr[7]);
181 }
182 if (arr[8] != null) {
183 bean.setTrackerUri((String) arr[8]);
184 }
185 if (arr[9] != null) {
186 bean.setCreatedTime(DateUtils.toDate((Timestamp) arr[9]));
187 }
188 if (arr[10] != null) {
189 bean.setNominalTime(DateUtils.toDate((Timestamp) arr[10]));
190 }
191 if (arr[11] != null) {
192 bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[11]));
193 }
194 if (arr[12] != null) {
195 bean.setLastModifiedTime(DateUtils.toDate((Timestamp) arr[12]));
196 }
197 if (arr[13] != null) {
198 bean.setMissingDependencies((String) arr[13]);
199 }
200 if (arr[14] != null) {
201 bean.setTimeOut((Integer) arr[14]);
202 }
203 return bean;
204
205 }
206
207 }