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.json.simple.JSONArray; 019 import org.json.simple.JSONObject; 020 021 import java.text.MessageFormat; 022 import java.util.ArrayList; 023 import java.util.Date; 024 import java.util.List; 025 026 import javax.persistence.*; 027 028 /** 029 * Json Bean that represents an Oozie workflow node. 030 */ 031 @Entity 032 @Table(name = "WF_ACTIONS") 033 @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING) 034 035 public class JsonWorkflowAction implements WorkflowAction, JsonBean { 036 @Id 037 private String id; 038 039 @Basic 040 @Column(name = "name") 041 private String name = null; 042 043 @Basic 044 @Column(name = "type") 045 private String type = null; 046 047 @Basic 048 @Column(name = "conf") 049 @Lob 050 private String conf = null; 051 052 @Transient 053 private Status status = WorkflowAction.Status.PREP; 054 055 @Basic 056 @Column(name = "retries") 057 private int retries; 058 059 @Transient 060 private Date startTime; 061 062 @Transient 063 private Date endTime; 064 065 @Basic 066 @Column(name = "transition") 067 private String transition = null; 068 069 @Column(name = "data") 070 @Lob 071 private String data = null; 072 073 @Basic 074 @Column(name = "external_id") 075 private String externalId = null; 076 077 @Basic 078 @Column(name = "external_status") 079 private String externalStatus = null; 080 081 @Basic 082 @Column(name = "tracker_uri") 083 private String trackerUri = null; 084 085 @Basic 086 @Column(name = "console_url") 087 private String consoleUrl = null; 088 089 @Basic 090 @Column(name = "error_code") 091 private String errorCode = null; 092 093 @Column(name = "error_message") 094 @Lob 095 private String errorMessage = null; 096 097 public JsonWorkflowAction() { 098 } 099 100 public JsonWorkflowAction(JSONObject jsonObject) { 101 id = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_ID); 102 name = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_NAME); 103 type = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_TYPE); 104 conf = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_CONF); 105 status = Status.valueOf((String) jsonObject.get(JsonTags.WORKFLOW_ACTION_STATUS)); 106 retries = (int) JsonUtils.getLongValue(jsonObject, JsonTags.WORKFLOW_ACTION_RETRIES); 107 startTime = JsonUtils.parseDateRfc822((String) jsonObject.get(JsonTags.WORKFLOW_ACTION_START_TIME)); 108 endTime = JsonUtils.parseDateRfc822((String) jsonObject.get(JsonTags.WORKFLOW_ACTION_END_TIME)); 109 transition = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_TRANSITION); 110 data = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_DATA); 111 externalId = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID); 112 externalStatus = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS); 113 trackerUri = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_TRACKER_URI); 114 consoleUrl = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_CONSOLE_URL); 115 errorCode = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_ERROR_CODE); 116 errorMessage = (String) jsonObject.get(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE); 117 } 118 119 @SuppressWarnings("unchecked") 120 public JSONObject toJSONObject() { 121 JSONObject json = new JSONObject(); 122 json.put(JsonTags.WORKFLOW_ACTION_ID, id); 123 json.put(JsonTags.WORKFLOW_ACTION_NAME, name); 124 json.put(JsonTags.WORKFLOW_ACTION_TYPE, type); 125 json.put(JsonTags.WORKFLOW_ACTION_CONF, conf); 126 json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(startTime)); 127 json.put(JsonTags.WORKFLOW_ACTION_STATUS, status.toString()); 128 json.put(JsonTags.WORKFLOW_ACTION_RETRIES, (long) retries); 129 json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(startTime)); 130 json.put(JsonTags.WORKFLOW_ACTION_END_TIME, JsonUtils.formatDateRfc822(endTime)); 131 json.put(JsonTags.WORKFLOW_ACTION_TRANSITION, transition); 132 json.put(JsonTags.WORKFLOW_ACTION_DATA, data); 133 json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, externalId); 134 json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, externalStatus); 135 json.put(JsonTags.WORKFLOW_ACTION_TRACKER_URI, trackerUri); 136 json.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, consoleUrl); 137 json.put(JsonTags.WORKFLOW_ACTION_ERROR_CODE, errorCode); 138 json.put(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, errorMessage); 139 return json; 140 } 141 142 public String getId() { 143 return id; 144 } 145 146 public void setId(String id) { 147 this.id = id; 148 } 149 150 public String getName() { 151 return name; 152 } 153 154 public void setName(String name) { 155 this.name = name; 156 } 157 158 public String getType() { 159 return type; 160 } 161 162 public void setType(String type) { 163 this.type = type; 164 } 165 166 public String getConf() { 167 return conf; 168 } 169 170 public void setConf(String conf) { 171 this.conf = conf; 172 } 173 174 public Status getStatus() { 175 return status; 176 } 177 178 public void setStatus(Status status) { 179 this.status = status; 180 } 181 182 public int getRetries() { 183 return retries; 184 } 185 186 public void setRetries(int retries) { 187 this.retries = retries; 188 } 189 190 public Date getStartTime() { 191 return startTime; 192 } 193 194 public void setStartTime(Date startTime) { 195 this.startTime = startTime; 196 } 197 198 public Date getEndTime() { 199 return endTime; 200 } 201 202 public void setEndTime(Date endTime) { 203 this.endTime = endTime; 204 } 205 206 public String getTransition() { 207 return transition; 208 } 209 210 public void setTransition(String transition) { 211 this.transition = transition; 212 } 213 214 public String getData() { 215 return data; 216 } 217 218 public void setData(String data) { 219 this.data = data; 220 } 221 222 public String getExternalId() { 223 return externalId; 224 } 225 226 public void setExternalId(String externalId) { 227 this.externalId = externalId; 228 } 229 230 public String getExternalStatus() { 231 return externalStatus; 232 } 233 234 public void setExternalStatus(String externalStatus) { 235 this.externalStatus = externalStatus; 236 } 237 238 public String getTrackerUri() { 239 return trackerUri; 240 } 241 242 public void setTrackerUri(String trackerUri) { 243 this.trackerUri = trackerUri; 244 } 245 246 public String getConsoleUrl() { 247 return consoleUrl; 248 } 249 250 public void setConsoleUrl(String consoleUrl) { 251 this.consoleUrl = consoleUrl; 252 } 253 254 public String getErrorCode() { 255 return errorCode; 256 } 257 258 public String getErrorMessage() { 259 return errorMessage; 260 } 261 262 public void setErrorInfo(String errorCode, String errorMessage) { 263 this.errorCode = errorCode; 264 this.errorMessage = errorMessage; 265 } 266 267 public String toString() { 268 return MessageFormat.format("Action name[{0}] status[{1}]", getName(), getStatus()); 269 } 270 271 /** 272 * Convert a nodes list into a JSONArray. 273 * 274 * @param nodes nodes list. 275 * @return the corresponding JSON array. 276 */ 277 @SuppressWarnings("unchecked") 278 public static JSONArray toJSONArray(List<? extends JsonWorkflowAction> nodes) { 279 JSONArray array = new JSONArray(); 280 for (JsonWorkflowAction node : nodes) { 281 array.add(node.toJSONObject()); 282 } 283 return array; 284 } 285 286 /** 287 * Convert a JSONArray into a nodes list. 288 * 289 * @param array JSON array. 290 * @return the corresponding nodes list. 291 */ 292 @SuppressWarnings("unchecked") 293 public static List<JsonWorkflowAction> fromJSONArray(JSONArray array) { 294 List<JsonWorkflowAction> list = new ArrayList<JsonWorkflowAction>(); 295 for (Object obj : array) { 296 list.add(new JsonWorkflowAction((JSONObject) obj)); 297 } 298 return list; 299 } 300 301 }