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 */
018package org.apache.oozie.client.rest;
019
020import org.apache.oozie.client.WorkflowAction;
021import org.json.simple.JSONArray;
022import org.json.simple.JSONObject;
023
024import java.text.MessageFormat;
025import java.util.Date;
026import java.util.List;
027
028import javax.persistence.*;
029
030/**
031 * Json Bean that represents an Oozie workflow node.
032 */
033@Entity
034@Table(name = "WF_ACTIONS")
035@DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
036
037public class JsonWorkflowAction implements WorkflowAction, JsonBean {
038    @Id
039    private String id;
040
041    @Basic
042    @Column(name = "name")
043    private String name = null;
044
045    @Basic
046    @Column(name = "cred")
047    private String cred = null;
048
049    @Basic
050    @Column(name = "type")
051    private String type = null;
052
053    @Basic
054    @Column(name = "conf")
055    @Lob
056    private String conf = null;
057
058    @Transient
059    private Status status = WorkflowAction.Status.PREP;
060
061    @Basic
062    @Column(name = "retries")
063    private int retries;
064
065    @Basic
066    @Column(name = "user_retry_count")
067    private int userRetryCount;
068
069    @Basic
070    @Column(name = "user_retry_max")
071    private int userRetryMax;
072
073    @Basic
074    @Column(name = "user_retry_interval")
075    private int userRetryInterval;
076
077    @Transient
078    private Date startTime;
079
080    @Transient
081    private Date endTime;
082
083    @Basic
084    @Column(name = "transition")
085    private String transition = null;
086
087    @Column(name = "data")
088    @Lob
089    private String data = null;
090
091    @Column(name = "stats")
092    @Lob
093    private String stats = null;
094
095    @Column(name = "external_child_ids")
096    @Lob
097    private String externalChildIDs = null;
098
099    @Basic
100    @Column(name = "external_id")
101    private String externalId = null;
102
103    @Basic
104    @Column(name = "external_status")
105    private String externalStatus = null;
106
107    @Basic
108    @Column(name = "tracker_uri")
109    private String trackerUri = null;
110
111    @Basic
112    @Column(name = "console_url")
113    private String consoleUrl = null;
114
115    @Basic
116    @Column(name = "error_code")
117    private String errorCode = null;
118
119    @Column(name = "error_message", length = 500)
120    private String errorMessage = null;
121
122    public JsonWorkflowAction() {
123    }
124
125    @SuppressWarnings("unchecked")
126    public JSONObject toJSONObject() {
127        return toJSONObject("GMT");
128    }
129
130    @SuppressWarnings("unchecked")
131    public JSONObject toJSONObject(String timeZoneId) {
132        JSONObject json = new JSONObject();
133        json.put(JsonTags.WORKFLOW_ACTION_ID, id);
134        json.put(JsonTags.WORKFLOW_ACTION_NAME, name);
135        json.put(JsonTags.WORKFLOW_ACTION_AUTH, cred);
136        json.put(JsonTags.WORKFLOW_ACTION_TYPE, type);
137        json.put(JsonTags.WORKFLOW_ACTION_CONF, conf);
138        json.put(JsonTags.WORKFLOW_ACTION_STATUS, status.toString());
139        json.put(JsonTags.WORKFLOW_ACTION_RETRIES, (long) retries);
140        json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(startTime, timeZoneId));
141        json.put(JsonTags.WORKFLOW_ACTION_END_TIME, JsonUtils.formatDateRfc822(endTime, timeZoneId));
142        json.put(JsonTags.WORKFLOW_ACTION_TRANSITION, transition);
143        json.put(JsonTags.WORKFLOW_ACTION_DATA, data);
144        json.put(JsonTags.WORKFLOW_ACTION_STATS, stats);
145        json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_CHILD_IDS, externalChildIDs);
146        json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, externalId);
147        json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, externalStatus);
148        json.put(JsonTags.WORKFLOW_ACTION_TRACKER_URI, trackerUri);
149        json.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, consoleUrl);
150        json.put(JsonTags.WORKFLOW_ACTION_ERROR_CODE, errorCode);
151        json.put(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, errorMessage);
152        json.put(JsonTags.TO_STRING, toString());
153        return json;
154    }
155
156    public String getId() {
157        return id;
158    }
159
160    public void setId(String id) {
161        this.id = id;
162    }
163
164    public String getName() {
165        return name;
166    }
167
168    public void setName(String name) {
169        this.name = name;
170    }
171
172    public String getCred() {
173        return cred;
174    }
175
176    public void setCred(String cred) {
177        this.cred = cred;
178    }
179
180    public String getType() {
181        return type;
182    }
183
184    public void setType(String type) {
185        this.type = type;
186    }
187
188    public String getConf() {
189        return conf;
190    }
191
192    public void setConf(String conf) {
193        this.conf = conf;
194    }
195
196    public Status getStatus() {
197        return status;
198    }
199
200    public void setStatus(Status status) {
201        this.status = status;
202    }
203
204    public int getRetries() {
205        return retries;
206    }
207
208    public void setRetries(int retries) {
209        this.retries = retries;
210    }
211
212    public int getUserRetryCount() {
213        return userRetryCount;
214    }
215
216    public void setUserRetryCount(int retryCount) {
217        this.userRetryCount = retryCount;
218    }
219
220    public void incrmentUserRetryCount() {
221        this.userRetryCount++;
222    }
223
224    public int getUserRetryMax() {
225        return userRetryMax;
226    }
227
228    public void setUserRetryMax(int retryMax) {
229        this.userRetryMax = retryMax;
230    }
231
232    public int getUserRetryInterval() {
233        return userRetryInterval;
234    }
235
236    public void setUserRetryInterval(int retryInterval) {
237        this.userRetryInterval = retryInterval;
238    }
239
240    public Date getStartTime() {
241        return startTime;
242    }
243
244    public void setStartTime(Date startTime) {
245        this.startTime = startTime;
246    }
247
248    public Date getEndTime() {
249        return endTime;
250    }
251
252    public void setEndTime(Date endTime) {
253        this.endTime = endTime;
254    }
255
256    public String getTransition() {
257        return transition;
258    }
259
260    public void setTransition(String transition) {
261        this.transition = transition;
262    }
263
264    public String getData() {
265        return data;
266    }
267
268    public void setData(String data) {
269        this.data = data;
270    }
271
272    public String getStats() {
273        return stats;
274    }
275
276    public void setStats(String stats) {
277        this.stats = stats;
278    }
279
280    public String getExternalChildIDs() {
281        return externalChildIDs;
282    }
283
284    public void setExternalChildIDs(String externalChildIDs) {
285        this.externalChildIDs = externalChildIDs;
286    }
287
288    public String getExternalId() {
289        return externalId;
290    }
291
292    public void setExternalId(String externalId) {
293        this.externalId = externalId;
294    }
295
296    public String getExternalStatus() {
297        return externalStatus;
298    }
299
300    public void setExternalStatus(String externalStatus) {
301        this.externalStatus = externalStatus;
302    }
303
304    public String getTrackerUri() {
305        return trackerUri;
306    }
307
308    public void setTrackerUri(String trackerUri) {
309        this.trackerUri = trackerUri;
310    }
311
312    public String getConsoleUrl() {
313        return consoleUrl;
314    }
315
316    public void setConsoleUrl(String consoleUrl) {
317        this.consoleUrl = consoleUrl;
318    }
319
320    public String getErrorCode() {
321        return errorCode;
322    }
323
324    public String getErrorMessage() {
325        return errorMessage;
326    }
327
328    public void setErrorInfo(String errorCode, String errorMessage) {
329        this.errorCode = errorCode;
330        if(errorMessage != null && errorMessage.length() > 500){
331            errorMessage = errorMessage.substring(0, 500);
332        }
333        this.errorMessage = errorMessage;
334    }
335
336    @Override
337    public String toString() {
338        return MessageFormat.format("Action name[{0}] status[{1}]", getName(), getStatus());
339    }
340
341    /**
342     * Convert a nodes list into a JSONArray.
343     *
344     * @param nodes nodes list.
345     * @param timeZoneId time zone to use for dates in the JSON array.
346     * @return the corresponding JSON array.
347     */
348    @SuppressWarnings("unchecked")
349    public static JSONArray toJSONArray(List<? extends JsonWorkflowAction> nodes, String timeZoneId) {
350        JSONArray array = new JSONArray();
351        for (JsonWorkflowAction node : nodes) {
352            array.add(node.toJSONObject(timeZoneId));
353        }
354        return array;
355    }
356
357}