001 /**
002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License. See accompanying LICENSE file.
014 */
015 package org.apache.oozie.client.rest;
016
017 import org.apache.oozie.client.CoordinatorJob;
018 import org.apache.oozie.client.CoordinatorAction;
019 import org.json.simple.JSONArray;
020 import org.json.simple.JSONObject;
021
022 import java.text.MessageFormat;
023 import java.util.Date;
024 import java.util.List;
025 import java.util.ArrayList;
026
027 import javax.persistence.*;
028
029 @Entity
030 @Table(name = "COORD_JOBS")
031 @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
032 public class JsonCoordinatorJob implements CoordinatorJob, JsonBean {
033
034 @Id
035 private String id;
036
037 @Basic
038 @Column(name = "app_path")
039 private String appPath = null;
040
041 @Basic
042 @Column(name = "app_name")
043 private String appName = null;
044
045 @Basic
046 @Column(name = "external_id")
047 private String externalId = null;
048
049 @Column(name = "conf")
050 @Lob
051 private String conf = null;
052
053 @Transient
054 private Status status = CoordinatorJob.Status.PREP;
055
056 @Transient
057 private Execution executionOrder = CoordinatorJob.Execution.LIFO;
058
059 @Transient
060 private Date startTime;
061
062 @Transient
063 private Date endTime;
064
065 @Transient
066 private Date pauseTime;
067
068 @Basic
069 @Column(name = "frequency")
070 private int frequency = 0;
071
072 @Basic
073 @Column(name = "time_zone")
074 private String timeZone = null;
075
076 @Basic
077 @Column(name = "concurrency")
078 private int concurrency = 0;
079
080 @Transient
081 private Timeunit timeUnit = CoordinatorJob.Timeunit.MINUTE;
082
083 @Basic
084 @Column(name = "time_out")
085 private int timeOut = 0;
086
087 @Transient
088 private Date lastAction;
089
090 @Basic
091 @Column(name = "last_action_number")
092 private int lastActionNumber;
093
094 @Transient
095 private Date nextMaterializedTime;
096
097 @Basic
098 @Column(name = "user_name")
099 private String user = null;
100
101 @Basic
102 @Column(name = "group_name")
103 private String group = null;
104
105 @Basic
106 @Column(name = "bundle_id")
107 private String bundleId = null;
108
109 @Transient
110 private String consoleUrl;
111
112 @Transient
113 private List<? extends JsonCoordinatorAction> actions;
114
115 public JsonCoordinatorJob() {
116 actions = new ArrayList<JsonCoordinatorAction>();
117 }
118
119 public JsonCoordinatorJob(JSONObject json) {
120 appPath = (String) json.get(JsonTags.COORDINATOR_JOB_PATH);
121 appName = (String) json.get(JsonTags.COORDINATOR_JOB_NAME);
122 id = (String) json.get(JsonTags.COORDINATOR_JOB_ID);
123 externalId = (String) json.get(JsonTags.COORDINATOR_JOB_EXTERNAL_ID);
124 conf = (String) json.get(JsonTags.COORDINATOR_JOB_CONF);
125 status = Status.valueOf((String) json.get(JsonTags.COORDINATOR_JOB_STATUS));
126 executionOrder = Execution.valueOf((String) json.get(JsonTags.COORDINATOR_JOB_EXECUTIONPOLICY));
127 startTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.COORDINATOR_JOB_START_TIME));
128 endTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.COORDINATOR_JOB_END_TIME));
129 pauseTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.COORDINATOR_JOB_PAUSE_TIME));
130 frequency = (int) JsonUtils.getLongValue(json, JsonTags.COORDINATOR_JOB_FREQUENCY);
131 timeUnit = Timeunit.valueOf((String) json.get(JsonTags.COORDINATOR_JOB_TIMEUNIT));
132 timeZone = (String) json.get(JsonTags.COORDINATOR_JOB_TIMEZONE);
133 concurrency = (int) JsonUtils.getLongValue(json, JsonTags.COORDINATOR_JOB_CONCURRENCY);
134 timeOut = (int) JsonUtils.getLongValue(json, JsonTags.COORDINATOR_JOB_TIMEOUT);
135 lastAction = JsonUtils.parseDateRfc822((String) json.get(JsonTags.COORDINATOR_JOB_LAST_ACTION_TIME));
136 nextMaterializedTime = JsonUtils.parseDateRfc822((String) json
137 .get(JsonTags.COORDINATOR_JOB_NEXT_MATERIALIZED_TIME));
138 user = (String) json.get(JsonTags.COORDINATOR_JOB_USER);
139 group = (String) json.get(JsonTags.COORDINATOR_JOB_GROUP);
140 consoleUrl = (String) json.get(JsonTags.COORDINATOR_JOB_CONSOLE_URL);
141 actions = JsonCoordinatorAction.fromJSONArray((JSONArray) json.get(JsonTags.COORDINATOR_ACTIONS));
142 }
143
144 @SuppressWarnings("unchecked")
145 public JSONObject toJSONObject() {
146 JSONObject json = new JSONObject();
147 json.put(JsonTags.COORDINATOR_JOB_PATH, appPath);
148 json.put(JsonTags.COORDINATOR_JOB_NAME, appName);
149 json.put(JsonTags.COORDINATOR_JOB_ID, id);
150 json.put(JsonTags.COORDINATOR_JOB_EXTERNAL_ID, externalId);
151 json.put(JsonTags.COORDINATOR_JOB_CONF, conf);
152 json.put(JsonTags.COORDINATOR_JOB_STATUS, status.toString());
153 json.put(JsonTags.COORDINATOR_JOB_EXECUTIONPOLICY, executionOrder.toString());
154 json.put(JsonTags.COORDINATOR_JOB_FREQUENCY, frequency);
155 json.put(JsonTags.COORDINATOR_JOB_TIMEUNIT, timeUnit.toString());
156 json.put(JsonTags.COORDINATOR_JOB_TIMEZONE, timeZone);
157 json.put(JsonTags.COORDINATOR_JOB_CONCURRENCY, concurrency);
158 json.put(JsonTags.COORDINATOR_JOB_TIMEOUT, timeOut);
159 json.put(JsonTags.COORDINATOR_JOB_LAST_ACTION_TIME, JsonUtils.formatDateRfc822(lastAction));
160 json.put(JsonTags.COORDINATOR_JOB_NEXT_MATERIALIZED_TIME, JsonUtils.formatDateRfc822(nextMaterializedTime));
161 json.put(JsonTags.COORDINATOR_JOB_START_TIME, JsonUtils.formatDateRfc822(startTime));
162 json.put(JsonTags.COORDINATOR_JOB_END_TIME, JsonUtils.formatDateRfc822(endTime));
163 json.put(JsonTags.COORDINATOR_JOB_PAUSE_TIME, JsonUtils.formatDateRfc822(pauseTime));
164 json.put(JsonTags.COORDINATOR_JOB_USER, user);
165 json.put(JsonTags.COORDINATOR_JOB_GROUP, group);
166 json.put(JsonTags.COORDINATOR_JOB_CONSOLE_URL, consoleUrl);
167 json.put(JsonTags.COORDINATOR_ACTIONS, JsonCoordinatorAction.toJSONArray(actions));
168
169 return json;
170 }
171
172 public String getAppPath() {
173 return appPath;
174 }
175
176 public void setAppPath(String appPath) {
177 this.appPath = appPath;
178 }
179
180 public String getAppName() {
181 return appName;
182 }
183
184 public void setAppName(String appName) {
185 this.appName = appName;
186 }
187
188 public String getId() {
189 return id;
190 }
191
192 public void setId(String id) {
193 this.id = id;
194 }
195
196 public void setExternalId(String externalId) {
197 this.externalId = externalId;
198 }
199
200 public String getExternalId() {
201 return externalId;
202 }
203
204 public String getConf() {
205 return conf;
206 }
207
208 public void setConf(String conf) {
209 this.conf = conf;
210 }
211
212 public Status getStatus() {
213 return status;
214 }
215
216 public void setStatus(Status status) {
217 this.status = status;
218 }
219
220 public void setFrequency(int frequency) {
221 this.frequency = frequency;
222 }
223
224 public int getFrequency() {
225 return frequency;
226 }
227
228 public void setTimeUnit(Timeunit timeUnit) {
229 this.timeUnit = timeUnit;
230 }
231
232 public Timeunit getTimeUnit() {
233 return timeUnit;
234 }
235
236 public void setTimeZone(String timeZone) {
237 this.timeZone = timeZone;
238 }
239
240 public String getTimeZone() {
241 return timeZone;
242 }
243
244 public void setConcurrency(int concurrency) {
245 this.concurrency = concurrency;
246 }
247
248 public int getConcurrency() {
249 return concurrency;
250 }
251
252 public void setExecutionOrder(Execution order) {
253 this.executionOrder = order;
254 }
255
256 public Execution getExecutionOrder() {
257 return executionOrder;
258 }
259
260 public void setTimeout(int timeOut) {
261 this.timeOut = timeOut;
262 }
263
264 public int getTimeout() {
265 return timeOut;
266 }
267
268 public void setLastActionTime(Date lastAction) {
269 this.lastAction = lastAction;
270 }
271
272 public Date getLastActionTime() {
273 return lastAction;
274 }
275
276 public Date getNextMaterializedTime() {
277 return nextMaterializedTime;
278 }
279
280 public void setNextMaterializedTime(Date nextMaterializedTime) {
281 this.nextMaterializedTime = nextMaterializedTime;
282 }
283
284 public Date getStartTime() {
285 return startTime;
286 }
287
288 public void setStartTime(Date startTime) {
289 this.startTime = startTime;
290 }
291
292 public Date getEndTime() {
293 return endTime;
294 }
295
296 public void setEndTime(Date endTime) {
297 this.endTime = endTime;
298 }
299
300 public Date getPauseTime() {
301 return pauseTime;
302 }
303
304 public void setPauseTime(Date pauseTime) {
305 this.pauseTime = pauseTime;
306 }
307
308 public String getUser() {
309 return user;
310 }
311
312 public void setUser(String user) {
313 this.user = user;
314 }
315
316 public String getGroup() {
317 return group;
318 }
319
320 public void setGroup(String group) {
321 this.group = group;
322 }
323
324 public String getBundleId() {
325 return bundleId;
326 }
327
328 public void setBundleId(String bundleId) {
329 this.bundleId = bundleId;
330 }
331
332 /**
333 * Return the coordinate application console URL.
334 *
335 * @return the coordinate application console URL.
336 */
337 public String getConsoleUrl() {
338 return consoleUrl;
339 }
340
341 /**
342 * Set the coordinate application console URL.
343 *
344 * @param consoleUrl the coordinate application console URL.
345 */
346 public void setConsoleUrl(String consoleUrl) {
347 this.consoleUrl = consoleUrl;
348 }
349
350 public String toString() {
351 return MessageFormat.format("Coornidator application id[{0}] status[{1}]", getId(), getStatus());
352 }
353
354 public void setActions(List<? extends JsonCoordinatorAction> nodes) {
355 this.actions = (nodes != null) ? nodes : new ArrayList<JsonCoordinatorAction>();
356 }
357
358 @SuppressWarnings("unchecked")
359 public List<CoordinatorAction> getActions() {
360 return (List) actions;
361 }
362
363 /**
364 * Convert a coordinator application list into a JSONArray.
365 *
366 * @param application list.
367 * @return the corresponding JSON array.
368 */
369 @SuppressWarnings("unchecked")
370 public static JSONArray toJSONArray(List<? extends JsonCoordinatorJob> applications) {
371 JSONArray array = new JSONArray();
372 if (applications != null) {
373 for (JsonCoordinatorJob application : applications) {
374 array.add(application.toJSONObject());
375 }
376 }
377 return array;
378 }
379
380 /**
381 * Convert a JSONArray into a application list.
382 *
383 * @param array JSON array.
384 * @return the corresponding application list.
385 */
386 @SuppressWarnings("unchecked")
387 public static List<CoordinatorJob> fromJSONArray(JSONArray applications) {
388 List<CoordinatorJob> list = new ArrayList<CoordinatorJob>();
389 for (Object obj : applications) {
390 list.add(new JsonCoordinatorJob((JSONObject) obj));
391 }
392 return list;
393 }
394
395 public int getLastActionNumber() {
396 return lastActionNumber;
397 }
398
399 public void setLastActionNumber(int lastActionNumber) {
400 this.lastActionNumber = lastActionNumber;
401 }
402 }