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