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.WorkflowAction; 018 import org.apache.oozie.client.WorkflowJob; 019 import org.json.simple.JSONArray; 020 import org.json.simple.JSONObject; 021 022 import java.text.MessageFormat; 023 import java.util.ArrayList; 024 import java.util.Date; 025 import java.util.List; 026 027 import javax.persistence.*; 028 029 /** 030 * Json Bean that represents an Oozie workflow job. 031 */ 032 033 @Entity 034 @Table(name = "WF_JOBS") 035 @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 036 @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING) 037 public class JsonWorkflowJob implements WorkflowJob, JsonBean { 038 039 @Id 040 private String id; 041 042 @Basic 043 @Column(name = "app_name") 044 private String appName = null; 045 046 @Basic 047 @Column(name = "app_path") 048 private String appPath = null; 049 050 @Transient 051 private String externalId = null; 052 053 @Column(name = "conf") 054 @Lob 055 private String conf = null; 056 057 @Transient 058 private Status status = WorkflowJob.Status.PREP; 059 060 @Transient 061 private Date createdTime; 062 063 @Transient 064 private Date startTime; 065 066 @Transient 067 private Date endTime; 068 069 @Transient 070 private Date lastModifiedTime; 071 072 @Basic 073 @Column(name = "user_name") 074 private String user = null; 075 076 @Basic 077 @Column(name = "group_name") 078 private String group; 079 080 @Basic 081 @Column(name = "run") 082 private int run = 1; 083 084 @Basic 085 @Column(name = "parent_id") 086 private String parentId; 087 088 @Transient 089 private String consoleUrl; 090 091 @Transient 092 private List<? extends JsonWorkflowAction> actions; 093 094 public JsonWorkflowJob() { 095 actions = new ArrayList<JsonWorkflowAction>(); 096 } 097 098 @SuppressWarnings("unchecked") 099 public JSONObject toJSONObject() { 100 JSONObject json = new JSONObject(); 101 json.put(JsonTags.WORKFLOW_APP_PATH, getAppPath()); 102 json.put(JsonTags.WORKFLOW_APP_NAME, getAppName()); 103 json.put(JsonTags.WORKFLOW_ID, getId()); 104 json.put(JsonTags.WORKFLOW_EXTERNAL_ID, getExternalId()); 105 json.put(JsonTags.WORKFLOW_PARENT_ID, getParentId()); 106 json.put(JsonTags.WORKFLOW_CONF, getConf()); 107 json.put(JsonTags.WORKFLOW_STATUS, getStatus().toString()); 108 json.put(JsonTags.WORKFLOW_LAST_MOD_TIME, JsonUtils.formatDateRfc822(getLastModifiedTime())); 109 json.put(JsonTags.WORKFLOW_CREATED_TIME, JsonUtils.formatDateRfc822(getCreatedTime())); 110 json.put(JsonTags.WORKFLOW_START_TIME, JsonUtils.formatDateRfc822(getStartTime())); 111 json.put(JsonTags.WORKFLOW_END_TIME, JsonUtils.formatDateRfc822(getEndTime())); 112 json.put(JsonTags.WORKFLOW_USER, getUser()); 113 json.put(JsonTags.WORKFLOW_GROUP, getGroup()); 114 json.put(JsonTags.WORKFLOW_RUN, (long) getRun()); 115 json.put(JsonTags.WORKFLOW_CONSOLE_URL, getConsoleUrl()); 116 json.put(JsonTags.WORKFLOW_ACTIONS, JsonWorkflowAction.toJSONArray(actions)); 117 json.put(JsonTags.TO_STRING, toString()); 118 return json; 119 } 120 121 public String getAppPath() { 122 return appPath; 123 } 124 125 public void setAppPath(String appPath) { 126 this.appPath = appPath; 127 } 128 129 public String getAppName() { 130 return appName; 131 } 132 133 public void setAppName(String appName) { 134 this.appName = appName; 135 } 136 137 public String getId() { 138 return id; 139 } 140 141 public void setId(String id) { 142 this.id = id; 143 } 144 145 public void setExternalId(String externalId) { 146 this.externalId = externalId; 147 } 148 149 public String getExternalId() { 150 return externalId; 151 } 152 153 public String getConf() { 154 return conf; 155 } 156 157 public void setConf(String conf) { 158 this.conf = conf; 159 } 160 161 public Status getStatus() { 162 return status; 163 } 164 165 public void setStatus(Status status) { 166 this.status = status; 167 } 168 169 public Date getLastModifiedTime() { 170 return lastModifiedTime; 171 } 172 173 public void setLastModifiedTime(Date lastModTime) { 174 this.lastModifiedTime = lastModTime; 175 } 176 177 public Date getCreatedTime() { 178 return createdTime; 179 } 180 181 public void setCreatedTime(Date createdTime) { 182 this.createdTime = createdTime; 183 } 184 185 public Date getStartTime() { 186 return startTime; 187 } 188 189 public void setStartTime(Date startTime) { 190 this.startTime = startTime; 191 } 192 193 public Date getEndTime() { 194 return endTime; 195 } 196 197 public void setEndTime(Date endTime) { 198 this.endTime = endTime; 199 } 200 201 public String getUser() { 202 return user; 203 } 204 205 public void setUser(String user) { 206 this.user = user; 207 } 208 209 public String getGroup() { 210 return group; 211 } 212 213 public void setGroup(String group) { 214 this.group = group; 215 } 216 217 public int getRun() { 218 return run; 219 } 220 221 public void setRun(int run) { 222 this.run = run; 223 } 224 225 /** 226 * Return the workflow job console URL. 227 * 228 * @return the workflow job console URL. 229 */ 230 public String getConsoleUrl() { 231 return consoleUrl; 232 } 233 234 /** 235 * Return the corresponding Action ID, if any. 236 * 237 * @return the coordinator Action Id. 238 */ 239 public String getParentId() { 240 return parentId; 241 } 242 243 /** 244 * Set coordinator action id 245 * 246 * @param parentId : coordinator action id 247 */ 248 public void setParentId(String parentId) { 249 this.parentId = parentId; 250 } 251 252 /** 253 * Set the workflow job console URL. 254 * 255 * @param consoleUrl the workflow job console URL. 256 */ 257 public void setConsoleUrl(String consoleUrl) { 258 this.consoleUrl = consoleUrl; 259 } 260 261 @SuppressWarnings("unchecked") 262 public List<WorkflowAction> getActions() { 263 return (List) actions; 264 } 265 266 public void setActions(List<? extends JsonWorkflowAction> nodes) { 267 this.actions = (nodes != null) ? nodes : new ArrayList<JsonWorkflowAction>(); 268 } 269 270 @Override 271 public String toString() { 272 return MessageFormat.format("Workflow id[{0}] status[{1}]", getId(), getStatus()); 273 } 274 275 /** 276 * Convert a workflows list into a JSONArray. 277 * 278 * @param workflows workflows list. 279 * @return the corresponding JSON array. 280 */ 281 @SuppressWarnings("unchecked") 282 public static JSONArray toJSONArray(List<? extends JsonWorkflowJob> workflows) { 283 JSONArray array = new JSONArray(); 284 if (workflows != null) { 285 for (JsonWorkflowJob node : workflows) { 286 array.add(node.toJSONObject()); 287 } 288 } 289 return array; 290 } 291 292 }