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 */
018
019package org.apache.oozie;
020
021import java.io.DataInput;
022import java.io.DataOutput;
023import java.io.IOException;
024import java.sql.Timestamp;
025import java.text.MessageFormat;
026import java.util.Date;
027import java.util.List;
028import java.util.Properties;
029
030import javax.persistence.Basic;
031import javax.persistence.Column;
032import javax.persistence.Entity;
033import javax.persistence.Id;
034import javax.persistence.Lob;
035import javax.persistence.NamedQueries;
036import javax.persistence.NamedQuery;
037import javax.persistence.Table;
038
039import org.apache.hadoop.io.Writable;
040import org.apache.oozie.client.WorkflowAction;
041import org.apache.oozie.client.rest.JsonBean;
042import org.apache.oozie.client.rest.JsonTags;
043import org.apache.oozie.client.rest.JsonUtils;
044import org.apache.oozie.util.DateUtils;
045import org.apache.oozie.util.ParamChecker;
046import org.apache.oozie.util.PropertiesUtils;
047import org.apache.oozie.util.StringUtils;
048import org.apache.oozie.util.WritableUtils;
049import org.apache.openjpa.persistence.jdbc.Index;
050import org.apache.openjpa.persistence.jdbc.Strategy;
051
052import org.json.simple.JSONArray;
053import org.json.simple.JSONObject;
054
055/**
056 * Bean that contains all the information to start an action for a workflow
057 * node.
058 */
059@Entity
060@NamedQueries({
061
062    @NamedQuery(name = "UPDATE_ACTION", query = "update WorkflowActionBean a set a.conf = :conf, a.consoleUrl = :consoleUrl,"
063            + " a.data = :data, a.stats = :stats, a.externalChildIDs = :externalChildIDs, a.errorCode = :errorCode,"
064            + " a.errorMessage = :errorMessage, a.externalId = :externalId, a.externalStatus = :externalStatus, a.name = :name,"
065            + " a.cred = :cred , a.retries = :retries, a.trackerUri = :trackerUri, a.transition = :transition, a.type = :type,"
066            + " a.endTimestamp = :endTime, a.executionPath = :executionPath, a.lastCheckTimestamp = :lastCheckTime, a.logToken "
067            + "= :logToken, a.pending = :pending, a.pendingAgeTimestamp = :pendingAge, a.signalValue = :signalValue, a.slaXml "
068            + "= :slaXml, a.startTimestamp = :startTime, a.statusStr = :status, a.wfId=:wfId where a.id = :id"),
069
070    @NamedQuery(name = "UPDATE_ACTION_FOR_LAST_CHECKED_TIME", query = "update WorkflowActionBean a set a.lastCheckTimestamp "
071            + "= :lastCheckTime where a.id = :id"),
072
073    @NamedQuery(name = "UPDATE_ACTION_START", query = "update WorkflowActionBean a set a.startTimestamp = :startTime,"
074            + " a.externalChildIDs = :externalChildIDs, a.conf = :conf, a.errorCode = :errorCode, a.errorMessage = :errorMessage,"
075            + " a.startTimestamp = :startTime, a.externalId = :externalId, a.trackerUri = :trackerUri, a.consoleUrl "
076            + "= :consoleUrl, a.lastCheckTimestamp = :lastCheckTime, a.statusStr = :status, a.externalStatus = :externalStatus,"
077            + " a.data = :data, a.retries = :retries, a.pending = :pending, a.pendingAgeTimestamp = :pendingAge, a.userRetryCount"
078            + " = :userRetryCount where a.id = :id"),
079
080    @NamedQuery(name = "UPDATE_ACTION_CHECK", query = "update WorkflowActionBean a set a.userRetryCount = :userRetryCount,"
081            + " a.stats = :stats, a.externalChildIDs = :externalChildIDs, a.externalStatus = :externalStatus, a.statusStr "
082            + "= :status, a.data = :data, a.pending = :pending, a.errorCode = :errorCode, a.errorMessage = :errorMessage,"
083            + " a.lastCheckTimestamp = :lastCheckTime, a.retries = :retries, a.pendingAgeTimestamp = :pendingAge,"
084            + " a.startTimestamp = :startTime where a.id = :id"),
085
086    @NamedQuery(name = "UPDATE_ACTION_END", query = "update WorkflowActionBean a set a.stats = :stats, a.errorCode = :errorCode,"
087            + " a.errorMessage = :errorMessage, a.retries = :retries, a.endTimestamp = :endTime, a.statusStr = :status, a.pending"
088            + " = :pending, a.pendingAgeTimestamp = :pendingAge, a.signalValue = :signalValue, a.userRetryCount "
089            + "= :userRetryCount, a.externalStatus = :externalStatus where a.id = :id"),
090
091    @NamedQuery(name = "UPDATE_ACTION_PENDING", query = "update WorkflowActionBean a set a.pending = :pending,"
092            + " a.pendingAgeTimestamp = :pendingAge, a.executionPath = :executionPath where a.id = :id"),
093
094    @NamedQuery(name = "UPDATE_ACTION_STATUS_PENDING", query = "update WorkflowActionBean a set a.statusStr = :status, a.pending"
095            + " = :pending, a.pendingAgeTimestamp = :pendingAge where a.id = :id"),
096
097    @NamedQuery(name = "UPDATE_ACTION_PENDING_TRANS", query = "update WorkflowActionBean a set a.pending = :pending,"
098            + " a.pendingAgeTimestamp = :pendingAge, a.transition = :transition where a.id = :id"),
099
100    @NamedQuery(name = "UPDATE_ACTION_PENDING_TRANS_ERROR", query = "update WorkflowActionBean a set a.pending = :pending,"
101            + " a.pendingAgeTimestamp = :pendingAge, a.transition = :transition, a.errorCode = :errorCode, a.errorMessage "
102            + "= :errorMessage, a.statusStr = :status where a.id = :id"),
103
104    @NamedQuery(name = "DELETE_ACTION", query = "delete from WorkflowActionBean a where a.id IN (:id)"),
105
106    @NamedQuery(name = "DELETE_ACTIONS_FOR_WORKFLOW", query = "delete from WorkflowActionBean a where a.wfId IN (:wfId)"),
107
108    @NamedQuery(name = "GET_ACTIONS", query = "select OBJECT(a) from WorkflowActionBean a"),
109
110    @NamedQuery(name = "GET_ACTION", query = "select OBJECT(a) from WorkflowActionBean a where a.id = :id"),
111
112    @NamedQuery(name = "GET_ACTION_ID_TYPE_LASTCHECK", query = "select a.id, a.type, a.lastCheckTimestamp "
113            + "from WorkflowActionBean a where a.id = :id"),
114
115    @NamedQuery(name = "GET_ACTION_FAIL", query = "select a.id, a.wfId, a.name, a.statusStr, a.pending, a.type, a.logToken,"
116            + " a.transition, a.errorCode, a.errorMessage from WorkflowActionBean a where a.id = :id"),
117
118    @NamedQuery(name = "GET_ACTION_SIGNAL", query = "select a.id, a.wfId, a.name, a.statusStr, a.pending, a.pendingAgeTimestamp,"
119            + " a.type, a.logToken, a.transition, a.errorCode, a.errorMessage, a.executionPath, a.signalValue, a.slaXml,"
120            + " a.externalId from WorkflowActionBean a where a.id = :id"),
121
122    @NamedQuery(name = "GET_ACTION_CHECK", query = "select a.id, a.wfId, a.name, a.statusStr, a.pending, a.pendingAgeTimestamp,"
123            + " a.type, a.logToken, a.transition, a.retries, a.userRetryCount, a.userRetryMax, a.userRetryInterval, a.trackerUri,"
124            + " a.startTimestamp, a.endTimestamp, a.lastCheckTimestamp, a.errorCode, a.errorMessage, a.externalId,"
125            + " a.externalStatus, a.externalChildIDs, a.conf from WorkflowActionBean a where a.id = :id"),
126
127    @NamedQuery(name = "GET_ACTION_END", query = "select a.id, a.wfId, a.name, a.statusStr, a.pending, a.pendingAgeTimestamp,"
128            + " a.type, a.logToken, a.transition, a.retries, a.trackerUri, a.userRetryCount, a.userRetryMax, a.userRetryInterval,"
129            + " a.startTimestamp, a.endTimestamp, a.errorCode, a.errorMessage, a.externalId, a.externalStatus,"
130            + " a.externalChildIDs, a.conf, a.data, a.stats from WorkflowActionBean a where a.id = :id"),
131
132    @NamedQuery(name = "GET_ACTION_COMPLETED", query = "select a.id, a.wfId, a.statusStr, a.type, a.logToken "
133            + "from WorkflowActionBean a where a.id = :id"),
134
135    @NamedQuery(name = "GET_ACTION_FOR_UPDATE", query = "select OBJECT(a) from WorkflowActionBean a where a.id = :id"),
136
137    @NamedQuery(name = "GET_ACTION_FOR_SLA", query = "select a.id, a.statusStr, a.startTimestamp, a.endTimestamp "
138            + "from WorkflowActionBean a where a.id = :id"),
139
140    @NamedQuery(name = "GET_ACTIONS_FOR_WORKFLOW", query = "select OBJECT(a) from WorkflowActionBean a where a.wfId = :wfId "
141            + "order by a.startTimestamp"),
142
143    @NamedQuery(name = "GET_ACTIONS_OF_WORKFLOW_FOR_UPDATE", query = "select OBJECT(a) from WorkflowActionBean a where a.wfId "
144            + "= :wfId order by a.startTimestamp"),
145
146    @NamedQuery(name = "GET_PENDING_ACTIONS", query = "select a.id, a.wfId, a.statusStr, a.type, a.pendingAgeTimestamp from"
147            + " WorkflowActionBean a where a.pending = 1 AND a.pendingAgeTimestamp < :pendingAge AND a.statusStr <> 'RUNNING' "
148            + "AND a.createdTimeTS >= :createdTime"),
149
150    @NamedQuery(name = "GET_RUNNING_ACTIONS", query = "select a.id from WorkflowActionBean a where a.pending = 1 AND a.statusStr"
151            + " = 'RUNNING' AND a.lastCheckTimestamp < :lastCheckTime"),
152
153    @NamedQuery(name = "GET_RETRY_MANUAL_ACTIONS", query = "select OBJECT(a) from WorkflowActionBean a where a.wfId = :wfId "
154            + "AND (a.statusStr = 'START_RETRY' OR a.statusStr = 'START_MANUAL' OR a.statusStr = 'END_RETRY' OR a.statusStr "
155            + "= 'END_MANUAL')"),
156
157    @NamedQuery(name = "GET_ACTIONS_FOR_WORKFLOW_RERUN", query = "select a.id, a.name, a.statusStr, a.endTimestamp, a.type "
158            + "from WorkflowActionBean a where a.wfId = :wfId order by a.startTimestamp") })
159@Table(name = "WF_ACTIONS")
160public class WorkflowActionBean implements Writable, WorkflowAction, JsonBean {
161    @Id
162    private String id;
163
164    @Basic
165    @Index
166    @Column(name = "wf_id")
167    private String wfId = null;
168
169    @Basic
170    @Column(name = "created_time")
171    private Timestamp createdTimeTS = null;
172
173    @Basic
174    @Index
175    @Column(name = "status")
176    private String statusStr = WorkflowAction.Status.PREP.toString();
177
178    @Basic
179    @Column(name = "last_check_time")
180    private Timestamp lastCheckTimestamp;
181
182    @Basic
183    @Column(name = "end_time")
184    private Timestamp endTimestamp = null;
185
186    @Basic
187    @Column(name = "start_time")
188    private Timestamp startTimestamp = null;
189
190    @Basic
191    @Column(name = "execution_path", length = 1024)
192    private String executionPath = null;
193
194    @Basic
195    @Column(name = "pending")
196    private int pending = 0;
197
198    @Basic
199    @Index
200    @Column(name = "pending_age")
201    private Timestamp pendingAgeTimestamp = null;
202
203    @Basic
204    @Column(name = "signal_value")
205    private String signalValue = null;
206
207    @Basic
208    @Column(name = "log_token")
209    private String logToken = null;
210
211    @Basic
212    @Column(name = "sla_xml")
213    @Lob
214    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
215    private StringBlob slaXml;
216
217    @Basic
218    @Column(name = "name")
219    private String name = null;
220
221    @Basic
222    @Column(name = "cred")
223    private String cred = null;
224
225    @Basic
226    @Column(name = "type")
227    private String type = null;
228
229    @Basic
230    @Column(name = "conf")
231    @Lob
232    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
233    private StringBlob conf;
234
235    @Basic
236    @Column(name = "retries")
237    private int retries;
238
239    @Basic
240    @Column(name = "user_retry_count")
241    private int userRetryCount;
242
243    @Basic
244    @Column(name = "user_retry_max")
245    private int userRetryMax;
246
247    @Basic
248    @Column(name = "user_retry_interval")
249    private int userRetryInterval;
250
251    @Basic
252    @Column(name = "transition")
253    private String transition = null;
254
255    @Basic
256    @Column(name = "data")
257    @Lob
258    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
259    private StringBlob data;
260
261    @Basic
262    @Column(name = "stats")
263    @Lob
264    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
265    private StringBlob stats;
266
267    @Basic
268    @Column(name = "external_child_ids")
269    @Lob
270    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
271    private StringBlob externalChildIDs;
272
273    @Basic
274    @Column(name = "external_id")
275    private String externalId = null;
276
277    @Basic
278    @Column(name = "external_status")
279    private String externalStatus = null;
280
281    @Basic
282    @Column(name = "tracker_uri")
283    private String trackerUri = null;
284
285    @Basic
286    @Column(name = "console_url")
287    private String consoleUrl = null;
288
289    @Basic
290    @Column(name = "error_code")
291    private String errorCode = null;
292
293    @Column(name = "error_message", length = 500)
294    private String errorMessage = null;
295
296    /**
297     * Default constructor.
298     */
299    public WorkflowActionBean() {
300    }
301
302    /**
303     * Serialize the action bean to a data output.
304     *
305     * @param dataOutput data output.
306     * @throws IOException thrown if the action bean could not be serialized.
307     */
308
309    public void write(DataOutput dataOutput) throws IOException {
310        WritableUtils.writeStr(dataOutput, getId());
311        WritableUtils.writeStr(dataOutput, getName());
312        WritableUtils.writeStr(dataOutput, getCred());
313        WritableUtils.writeStr(dataOutput, getType());
314        WritableUtils.writeStr(dataOutput, getConf());
315        WritableUtils.writeStr(dataOutput, getStatusStr());
316        dataOutput.writeInt(getRetries());
317        dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1);
318        dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1);
319        dataOutput.writeLong((getLastCheckTime() != null) ? getLastCheckTime().getTime() : -1);
320        WritableUtils.writeStr(dataOutput, getTransition());
321        WritableUtils.writeStr(dataOutput, getData());
322        WritableUtils.writeStr(dataOutput, getStats());
323        WritableUtils.writeStr(dataOutput, getExternalChildIDs());
324        WritableUtils.writeStr(dataOutput, getExternalId());
325        WritableUtils.writeStr(dataOutput, getExternalStatus());
326        WritableUtils.writeStr(dataOutput, getTrackerUri());
327        WritableUtils.writeStr(dataOutput, getConsoleUrl());
328        WritableUtils.writeStr(dataOutput, getErrorCode());
329        WritableUtils.writeStr(dataOutput, getErrorMessage());
330        WritableUtils.writeStr(dataOutput, wfId);
331        WritableUtils.writeStr(dataOutput, executionPath);
332        dataOutput.writeInt(pending);
333        dataOutput.writeLong((getPendingAge() != null) ? getPendingAge().getTime() : -1);
334        WritableUtils.writeStr(dataOutput, signalValue);
335        WritableUtils.writeStr(dataOutput, logToken);
336        dataOutput.writeInt(getUserRetryCount());
337        dataOutput.writeInt(getUserRetryInterval());
338        dataOutput.writeInt(getUserRetryMax());
339    }
340
341    /**
342     * Deserialize an action bean from a data input.
343     *
344     * @param dataInput data input.
345     * @throws IOException thrown if the action bean could not be deserialized.
346     */
347    public void readFields(DataInput dataInput) throws IOException {
348        setId(WritableUtils.readStr(dataInput));
349        setName(WritableUtils.readStr(dataInput));
350        setCred(WritableUtils.readStr(dataInput));
351        setType(WritableUtils.readStr(dataInput));
352        setConf(WritableUtils.readStr(dataInput));
353        setStatus(WorkflowAction.Status.valueOf(WritableUtils.readStr(dataInput)));
354        setRetries(dataInput.readInt());
355        long d = dataInput.readLong();
356        if (d != -1) {
357            setStartTime(new Date(d));
358        }
359        d = dataInput.readLong();
360        if (d != -1) {
361            setEndTime(new Date(d));
362        }
363        d = dataInput.readLong();
364        if (d != -1) {
365            setLastCheckTime(new Date(d));
366        }
367        setTransition(WritableUtils.readStr(dataInput));
368        setData(WritableUtils.readStr(dataInput));
369        setStats(WritableUtils.readStr(dataInput));
370        setExternalChildIDs(WritableUtils.readStr(dataInput));
371        setExternalId(WritableUtils.readStr(dataInput));
372        setExternalStatus(WritableUtils.readStr(dataInput));
373        setTrackerUri(WritableUtils.readStr(dataInput));
374        setConsoleUrl(WritableUtils.readStr(dataInput));
375        setErrorInfo(WritableUtils.readStr(dataInput), WritableUtils.readStr(dataInput));
376        setJobId(WritableUtils.readStr(dataInput));
377        executionPath = WritableUtils.readStr(dataInput);
378        pending = dataInput.readInt();
379        d = dataInput.readLong();
380        if (d != -1) {
381            pendingAgeTimestamp = DateUtils.convertDateToTimestamp(new Date(d));
382        }
383        signalValue = WritableUtils.readStr(dataInput);
384        logToken = WritableUtils.readStr(dataInput);
385        setUserRetryCount(dataInput.readInt());
386        setUserRetryInterval(dataInput.readInt());
387        setUserRetryMax(dataInput.readInt());
388    }
389
390    /**
391     * Return whether workflow action in terminal state or not
392     *
393     * @return isTerminalState Return whether workflow action in terminal state or not
394     */
395    public boolean inTerminalState() {
396        boolean isTerminalState = false;
397        switch (WorkflowAction.Status.valueOf(statusStr)) {
398            case ERROR:
399            case FAILED:
400            case KILLED:
401            case OK:
402                isTerminalState = true;
403                break;
404            default:
405                break;
406        }
407        return isTerminalState;
408    }
409
410    /**
411     * Return if the action execution is complete.
412     *
413     * @return if the action start is complete.
414     */
415    public boolean isExecutionComplete() {
416        return getStatus() == WorkflowAction.Status.DONE;
417    }
418
419    /**
420     * Return if the action is START_RETRY or START_MANUAL or END_RETRY or
421     * END_MANUAL.
422     *
423     * @return boolean true if status is START_RETRY or START_MANUAL or
424     *         END_RETRY or END_MANUAL
425     */
426    public boolean isRetryOrManual() {
427        return (getStatus() == WorkflowAction.Status.START_RETRY || getStatus() == WorkflowAction.Status.START_MANUAL
428                || getStatus() == WorkflowAction.Status.END_RETRY || getStatus() == WorkflowAction.Status.END_MANUAL);
429    }
430
431    /**
432     * Return true if the action is USER_RETRY
433     *
434     * @return boolean true if status is USER_RETRY
435     */
436    public boolean isUserRetry() {
437        return (getStatus() == WorkflowAction.Status.USER_RETRY);
438    }
439
440    /**
441     * Return if the action is complete.
442     *
443     * @return if the action is complete.
444     */
445    public boolean isComplete() {
446        return getStatus() == WorkflowAction.Status.OK || getStatus() == WorkflowAction.Status.KILLED
447                || getStatus() == WorkflowAction.Status.ERROR;
448    }
449
450    /**
451     * Return if the action is complete with failure.
452     *
453     * @return if the action is complete with failure.
454     */
455    public boolean isTerminalWithFailure() {
456        boolean result = false;
457        switch (getStatus()) {
458            case FAILED:
459            case KILLED:
460            case ERROR:
461                result = true;
462        }
463        return result;
464    }
465
466    /**
467     * Set the action pending flag to true.
468     */
469    public void setPendingOnly() {
470        pending = 1;
471    }
472
473    /**
474     * Set the action as pending and the current time as pending.
475     */
476    public void setPending() {
477        pending = 1;
478        pendingAgeTimestamp = DateUtils.convertDateToTimestamp(new Date());
479    }
480
481    /**
482     * Set pending flag
483     * @param i the flag
484     */
485    public void setPending(int i) {
486        pending = i;
487    }
488
489    /**
490     * Set a time when the action will be pending, normally a time in the
491     * future.
492     *
493     * @param pendingAge the time when the action will be pending.
494     */
495    public void setPendingAge(Date pendingAge) {
496        this.pendingAgeTimestamp = DateUtils.convertDateToTimestamp(pendingAge);
497    }
498
499    /**
500     * Return the pending age of the action.
501     *
502     * @return the pending age of the action, <code>null</code> if the action is
503     *         not pending.
504     */
505    public Date getPendingAge() {
506        return DateUtils.toDate(pendingAgeTimestamp);
507    }
508
509    /**
510     * Return if the action is pending.
511     *
512     * @return if the action is pending.
513     */
514    public boolean isPending() {
515        return pending == 1 ? true : false;
516    }
517
518    /**
519     * Removes the pending flag and pendingAge from the action.
520     */
521    public void resetPending() {
522        pending = 0;
523        pendingAgeTimestamp = null;
524    }
525
526    /**
527     * Removes the pending flag from the action.
528     */
529    public void resetPendingOnly() {
530        pending = 0;
531    }
532
533    /**
534     * Increments the number of retries for the action.
535     */
536    public void incRetries() {
537        setRetries(getRetries() + 1);
538    }
539
540    /**
541     * Set a tracking information for an action, and set the action status to
542     * {@link org.apache.oozie.client.WorkflowAction.Status#DONE}
543     *
544     * @param externalId external ID for the action.
545     * @param trackerUri tracker URI for the action.
546     * @param consoleUrl console URL for the action.
547     */
548    public void setStartData(String externalId, String trackerUri, String consoleUrl) {
549        setExternalId(ParamChecker.notEmpty(externalId, "externalId"));
550        setTrackerUri(ParamChecker.notEmpty(trackerUri, "trackerUri"));
551        setConsoleUrl(ParamChecker.notEmpty(consoleUrl, "consoleUrl"));
552        Date now = new Date();
553        if (this.startTimestamp == null) {
554            setStartTime(now);
555        }
556        setLastCheckTime(now);
557        setStatus(Status.RUNNING);
558    }
559
560    /**
561     * Set the completion information for an action start. Sets the Action
562     * status to {@link org.apache.oozie.client.WorkflowAction.Status#DONE}
563     *
564     * @param externalStatus action external end status.
565     * @param actionData action output data, <code>null</code> if there is no
566     *        action output data.
567     */
568    public void setExecutionData(String externalStatus, Properties actionData) {
569        setStatus(Status.DONE);
570        setExternalStatus(ParamChecker.notEmpty(externalStatus, "externalStatus"));
571        if (actionData != null) {
572            setData(PropertiesUtils.propertiesToString(actionData));
573        }
574    }
575
576    /**
577     * Return the action statistics info.
578     *
579     * @return Json representation of the stats.
580     */
581    public String getExecutionStats() {
582        return getStats();
583    }
584
585    /**
586     * Set the action statistics info for the workflow action.
587     *
588     * @param jsonStats representation of the stats.
589     */
590    public void setExecutionStats(String jsonStats) {
591        setStats(jsonStats);
592    }
593
594    /**
595     * Return the external child IDs.
596     *
597     * @return externalChildIDs as a string.
598     */
599    @Override
600    public String getExternalChildIDs() {
601        return externalChildIDs == null ? null : externalChildIDs.getString();
602    }
603
604    /**
605     * Set the external child IDs for the workflow action.
606     *
607     * @param externalChildIDs as a string.
608     */
609    public void setExternalChildIDs(String externalChildIDs) {
610        if (this.externalChildIDs == null) {
611            this.externalChildIDs = new StringBlob(externalChildIDs);
612        }
613        else {
614            this.externalChildIDs.setString(externalChildIDs);
615        }
616    }
617
618    /**
619     * Set external child ids
620     *
621     * @param externalChildIDs the external child ids
622     */
623    public void setExternalChildIDsBlob(StringBlob externalChildIDs) {
624        this.externalChildIDs = externalChildIDs;
625    }
626
627    /**
628     * Get external ChildIds
629     *
630     * @return externalChildIDs Get external ChildIds
631     */
632    public StringBlob getExternalChildIDsBlob() {
633        return externalChildIDs;
634    }
635
636    /**
637     * Set the completion information for an action end.
638     *
639     * @param status action status, {@link org.apache.oozie.client.WorkflowAction.Status#OK} or
640     *        {@link org.apache.oozie.client.WorkflowAction.Status#ERROR} or
641     *        {@link org.apache.oozie.client.WorkflowAction.Status#KILLED}
642     * @param signalValue the signal value. In most cases, the value should be
643     *        OK or ERROR.
644     */
645    public void setEndData(Status status, String signalValue) {
646        if (status == null || (status != Status.OK && status != Status.ERROR && status != Status.KILLED)) {
647            throw new IllegalArgumentException("Action status must be OK, ERROR or KILLED. Received ["
648                    + (status == null ? "null" : status.toString()) + "]");
649        }
650        if (status == Status.OK) {
651            setErrorInfo(null, null);
652        }
653        setStatus(status);
654        setSignalValue(ParamChecker.notEmpty(signalValue, "signalValue"));
655    }
656
657    /**
658     * Return the job Id.
659     *
660     * @return the job Id.
661     */
662    public String getJobId() {
663        return wfId;
664    }
665
666    /**
667     * Return the job Id.
668     *
669     * @return the job Id.
670     */
671    public String getWfId() {
672        return wfId;
673    }
674
675    /**
676     * Set the job id.
677     *
678     * @param id jobId;
679     */
680    public void setJobId(String id) {
681        this.wfId = StringUtils.intern(id);
682    }
683
684    public void setSlaXml(String slaXmlStr) {
685        if (this.slaXml == null) {
686            this.slaXml = new StringBlob(slaXmlStr);
687        }
688        else {
689            this.slaXml.setString(slaXmlStr);
690        }
691    }
692
693    public String getSlaXml() {
694        return slaXml == null ? null : slaXml.getString();
695    }
696
697    public void setSlaXmlBlob(StringBlob slaXml) {
698        this.slaXml = slaXml;
699    }
700
701    public StringBlob getSlaXmlBlob() {
702        return slaXml;
703    }
704
705    /**
706     * Set status of job
707     *
708     * @param val the status
709     */
710    public void setStatus(Status val) {
711        this.statusStr = StringUtils.intern(val.toString());
712    }
713
714    @Override
715    public Status getStatus() {
716        return Status.valueOf(this.statusStr);
717    }
718
719    /**
720     * Set status
721     *
722     * @param statusStr the status
723     */
724    public void setStatusStr(String statusStr) {
725        this.statusStr = StringUtils.intern(statusStr);
726    }
727
728    /**
729     * Get status
730     *
731     * @return statusStr Get status
732     */
733    public String getStatusStr() {
734        return statusStr;
735    }
736
737    /**
738     * Return the node execution path.
739     *
740     * @return the node execution path.
741     */
742    public String getExecutionPath() {
743        return executionPath;
744    }
745
746    /**
747     * Set the node execution path.
748     *
749     * @param executionPath the node execution path.
750     */
751    public void setExecutionPath(String executionPath) {
752        this.executionPath = executionPath;
753    }
754
755    /**
756     * Return the signal value for the action.
757     * <p>
758     * For decision nodes it is the choosen transition, for actions it is OK or
759     * ERROR.
760     *
761     * @return the action signal value.
762     */
763    public String getSignalValue() {
764        return signalValue;
765    }
766
767    /**
768     * Set the signal value for the action.
769     * <p>
770     * For decision nodes it is the choosen transition, for actions it is OK or
771     * ERROR.
772     *
773     * @param signalValue the action signal value.
774     */
775    public void setSignalValue(String signalValue) {
776        this.signalValue = signalValue;
777    }
778
779    /**
780     * Return the job log token.
781     *
782     * @return the job log token.
783     */
784    public String getLogToken() {
785        return logToken;
786    }
787
788    /**
789     * Set the job log token.
790     *
791     * @param logToken the job log token.
792     */
793    public void setLogToken(String logToken) {
794        this.logToken = logToken;
795    }
796
797    /**
798     * Return the action last check time
799     *
800     * @return the last check time
801     */
802    public Date getLastCheckTime() {
803        return DateUtils.toDate(lastCheckTimestamp);
804    }
805
806    /**
807     * Return the action last check time
808     *
809     * @return the last check time
810     */
811    public Timestamp getLastCheckTimestamp() {
812        return lastCheckTimestamp;
813    }
814
815    /**
816     * Return the action last check time
817     *
818     * @return the last check time
819     */
820    public Timestamp getStartTimestamp() {
821        return startTimestamp;
822    }
823
824    /**
825     * Return the action last check time
826     *
827     * @return the last check time
828     */
829    public Timestamp getEndTimestamp() {
830        return endTimestamp;
831    }
832
833    /**
834     * Return the action last check time
835     *
836     * @return the last check time
837     */
838    public Timestamp getPendingAgeTimestamp() {
839        return pendingAgeTimestamp;
840    }
841
842    /**
843     * Sets the action last check time
844     *
845     * @param lastCheckTime the last check time to set.
846     */
847    public void setLastCheckTime(Date lastCheckTime) {
848        this.lastCheckTimestamp = DateUtils.convertDateToTimestamp(lastCheckTime);
849    }
850
851    public int getPending() {
852        return this.pending;
853    }
854
855    @Override
856    public Date getStartTime() {
857        return DateUtils.toDate(startTimestamp);
858    }
859
860    /**
861     * Set start time
862     *
863     * @param startTime the start time
864     */
865    public void setStartTime(Date startTime) {
866        this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
867    }
868
869    @Override
870    public Date getEndTime() {
871        return DateUtils.toDate(endTimestamp);
872    }
873
874    /**
875     * Set end time
876     *
877     * @param endTime the end time
878     */
879    public void setEndTime(Date endTime) {
880        this.endTimestamp = DateUtils.convertDateToTimestamp(endTime);
881    }
882
883    @SuppressWarnings("unchecked")
884    public JSONObject toJSONObject() {
885        return toJSONObject("GMT");
886    }
887
888    @SuppressWarnings("unchecked")
889    public JSONObject toJSONObject(String timeZoneId) {
890        JSONObject json = new JSONObject();
891        json.put(JsonTags.WORKFLOW_ACTION_ID, id);
892        json.put(JsonTags.WORKFLOW_ACTION_NAME, name);
893        json.put(JsonTags.WORKFLOW_ACTION_AUTH, cred);
894        json.put(JsonTags.WORKFLOW_ACTION_TYPE, type);
895        json.put(JsonTags.WORKFLOW_ACTION_CONF, getConf());
896        json.put(JsonTags.WORKFLOW_ACTION_STATUS, statusStr);
897        json.put(JsonTags.WORKFLOW_ACTION_RETRIES, (long) retries);
898        json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(getStartTime(), timeZoneId));
899        json.put(JsonTags.WORKFLOW_ACTION_END_TIME, JsonUtils.formatDateRfc822(getEndTime(), timeZoneId));
900        json.put(JsonTags.WORKFLOW_ACTION_TRANSITION, transition);
901        json.put(JsonTags.WORKFLOW_ACTION_DATA, getData());
902        json.put(JsonTags.WORKFLOW_ACTION_STATS, getStats());
903        json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_CHILD_IDS, getExternalChildIDs());
904        json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, externalId);
905        json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, externalStatus);
906        json.put(JsonTags.WORKFLOW_ACTION_TRACKER_URI, trackerUri);
907        json.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, consoleUrl);
908        json.put(JsonTags.WORKFLOW_ACTION_ERROR_CODE, errorCode);
909        json.put(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, errorMessage);
910        json.put(JsonTags.TO_STRING, toString());
911        json.put(JsonTags.WORKFLOW_ACTION_USER_RETRY_INTERVAL, userRetryInterval);
912        json.put(JsonTags.WORKFLOW_ACTION_USER_RETRY_COUNT, userRetryCount);
913        json.put(JsonTags.WORKFLOW_ACTION_USER_RETRY_MAX, userRetryMax);
914        json.put(JsonTags.WORKFLOW_ACTION_CRED, cred);
915        return json;
916    }
917
918    @Override
919    public String getId() {
920        return id;
921    }
922
923    public void setId(String id) {
924        this.id = StringUtils.intern(id);
925    }
926
927    public Timestamp getCreatedTimestamp() {
928        return createdTimeTS;
929    }
930
931    public Date getCreatedTime() {
932        return DateUtils.toDate(createdTimeTS);
933    }
934
935    public void setCreatedTime(Date createdTime) {
936        this.createdTimeTS = DateUtils.convertDateToTimestamp(createdTime);
937    }
938
939    @Override
940    public String getName() {
941        return name;
942    }
943
944    public void setName(String name) {
945        this.name = StringUtils.intern(name);
946    }
947
948    @Override
949    public String getCred() {
950        return cred;
951    }
952
953    public void setCred(String cred) {
954        this.cred = cred;
955    }
956
957    @Override
958    public String getType() {
959        return type;
960    }
961
962    public void setType(String type) {
963        this.type = StringUtils.intern(type);
964    }
965
966    @Override
967    public String getConf() {
968        return conf == null ? null : conf.getString();
969    }
970
971    public void setConf(String conf) {
972        if (this.conf == null) {
973            this.conf = new StringBlob(conf);
974        }
975        else {
976            this.conf.setString(conf);
977        }
978    }
979
980    public void setConfBlob(StringBlob conf) {
981        this.conf = conf;
982    }
983
984    public StringBlob getConfBlob() {
985        return conf;
986    }
987
988    @Override
989    public int getRetries() {
990        return retries;
991    }
992
993    public void setRetries(int retries) {
994        this.retries = retries;
995    }
996
997    @Override
998    public int getUserRetryCount() {
999        return userRetryCount;
1000    }
1001
1002    public void setUserRetryCount(int retryCount) {
1003        this.userRetryCount = retryCount;
1004    }
1005
1006    public void incrmentUserRetryCount() {
1007        this.userRetryCount++;
1008    }
1009
1010    @Override
1011    public int getUserRetryMax() {
1012        return userRetryMax;
1013    }
1014
1015    /**
1016     * Set user retry max
1017     *
1018     * @param retryMax the maximum retry count
1019     */
1020    public void setUserRetryMax(int retryMax) {
1021        this.userRetryMax = retryMax;
1022    }
1023
1024    @Override
1025    public int getUserRetryInterval() {
1026        return userRetryInterval;
1027    }
1028
1029    public void setUserRetryInterval(int retryInterval) {
1030        this.userRetryInterval = retryInterval;
1031    }
1032
1033    @Override
1034    public String getTransition() {
1035        return transition;
1036    }
1037
1038    /**
1039     * Set transition
1040     *
1041     * @param transition the transition
1042     */
1043    public void setTransition(String transition) {
1044        this.transition = transition;
1045    }
1046
1047    @Override
1048    public String getData() {
1049        return data == null ? null : data.getString();
1050    }
1051
1052    /**
1053     * Set data
1054     *
1055     * @param data the data
1056     */
1057    public void setData(String data) {
1058        if (this.data == null) {
1059            this.data = new StringBlob(data);
1060        }
1061        else {
1062            this.data.setString(data);
1063        }
1064    }
1065
1066    public void setDataBlob(StringBlob data) {
1067        this.data = data;
1068    }
1069
1070    public StringBlob getDataBlob() {
1071        return data;
1072    }
1073
1074    @Override
1075    public String getStats() {
1076        return stats == null ? null : stats.getString();
1077    }
1078
1079    /**
1080     * Set stats
1081     *
1082     * @param stats the action stats
1083     */
1084    public void setStats(String stats) {
1085        if (this.stats == null) {
1086            this.stats = new StringBlob(stats);
1087        }
1088        else {
1089            this.stats.setString(stats);
1090        }
1091    }
1092
1093    public void setStatsBlob(StringBlob stats) {
1094        this.stats = stats;
1095    }
1096
1097    public StringBlob getStatsBlob() {
1098        return this.stats;
1099    }
1100
1101    @Override
1102    public String getExternalId() {
1103        return externalId;
1104    }
1105
1106    /**
1107     * Set external Id
1108     *
1109     * @param externalId the id
1110     */
1111    public void setExternalId(String externalId) {
1112        this.externalId = StringUtils.intern(externalId);
1113    }
1114
1115    @Override
1116    public String getExternalStatus() {
1117        return externalStatus;
1118    }
1119
1120    /**
1121     * Set external status
1122     *
1123     * @param externalStatus the external status
1124     */
1125    public void setExternalStatus(String externalStatus) {
1126        this.externalStatus = StringUtils.intern(externalStatus);
1127    }
1128
1129    @Override
1130    public String getTrackerUri() {
1131        return trackerUri;
1132    }
1133
1134    /**
1135     * Set tracker uri
1136     *
1137     * @param trackerUri the URI
1138     */
1139    public void setTrackerUri(String trackerUri) {
1140        this.trackerUri = StringUtils.intern(trackerUri);
1141    }
1142
1143    @Override
1144    public String getConsoleUrl() {
1145        return consoleUrl;
1146    }
1147
1148    /**
1149     * Set console URL
1150     *
1151     * @param consoleUrl the URL
1152     */
1153    public void setConsoleUrl(String consoleUrl) {
1154        this.consoleUrl = consoleUrl;
1155    }
1156
1157    @Override
1158    public String getErrorCode() {
1159        return errorCode;
1160    }
1161
1162    @Override
1163    public String getErrorMessage() {
1164        return errorMessage;
1165    }
1166
1167    /**
1168     * Set the error Info
1169     *
1170     * @param errorCode the error code
1171     * @param errorMessage the error message
1172     */
1173    public void setErrorInfo(String errorCode, String errorMessage) {
1174        this.errorCode = errorCode;
1175        if (errorMessage != null && errorMessage.length() > 500) {
1176            errorMessage = errorMessage.substring(0, 500);
1177        }
1178        this.errorMessage = errorMessage;
1179    }
1180
1181    @Override
1182    public String toString() {
1183        return MessageFormat.format("Action name[{0}] status[{1}]", getName(), getStatus());
1184    }
1185
1186    /**
1187     * Convert a nodes list into a JSONArray.
1188     *
1189     * @param nodes nodes list.
1190     * @param timeZoneId time zone to use for dates in the JSON array.
1191     * @return the corresponding JSON array.
1192     */
1193    @SuppressWarnings("unchecked")
1194    public static JSONArray toJSONArray(List<WorkflowActionBean> nodes, String timeZoneId) {
1195        JSONArray array = new JSONArray();
1196        for (WorkflowActionBean node : nodes) {
1197            array.add(node.toJSONObject(timeZoneId));
1198        }
1199        return array;
1200    }
1201
1202}