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