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.ArrayList;
027import java.util.Date;
028import java.util.List;
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;
038import javax.persistence.Transient;
039
040import org.apache.hadoop.io.Writable;
041import org.apache.oozie.client.BundleJob;
042import org.apache.oozie.client.CoordinatorJob;
043import org.apache.oozie.client.Job;
044import org.apache.oozie.client.rest.JsonBean;
045import org.apache.oozie.client.rest.JsonTags;
046import org.apache.oozie.client.rest.JsonUtils;
047import org.apache.oozie.util.DateUtils;
048import org.apache.oozie.util.WritableUtils;
049import org.apache.openjpa.persistence.jdbc.Index;
050import org.apache.openjpa.persistence.jdbc.Strategy;
051import org.json.simple.JSONArray;
052import org.json.simple.JSONObject;
053
054@Entity
055@NamedQueries( {
056        @NamedQuery(name = "UPDATE_BUNDLE_JOB", query = "update BundleJobBean w set w.appName = :appName, w.appPath = :appPath, w.conf = :conf, w.externalId = :externalId, w.timeOut = :timeOut, w.createdTimestamp = :createdTime, w.endTimestamp = :endTime, w.jobXml = :jobXml, w.lastModifiedTimestamp = :lastModifiedTime, w.origJobXml = :origJobXml, w.startTimestamp = :startTime, w.statusStr = :status, w.timeUnitStr = :timeUnit, w.pending = :pending where w.id = :id"),
057
058        @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS", query = "update BundleJobBean w set w.statusStr = :status, w.lastModifiedTimestamp = :lastModifiedTime, w.pending = :pending where w.id = :id"),
059
060        @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS_PENDING", query = "update BundleJobBean w set w.statusStr = :status, w.pending = :pending where w.id = :id"),
061
062        @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS_PENDING_MODTIME", query = "update BundleJobBean w set w.statusStr = :status, w.lastModifiedTimestamp = :lastModifiedTime, w.pending = :pending where w.id = :id"),
063
064        @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS_PENDING_SUSP_MOD_TIME", query = "update BundleJobBean w set w.statusStr = :status, w.lastModifiedTimestamp = :lastModifiedTime, w.pending = :pending, w.suspendedTimestamp = :suspendedTime where w.id = :id"),
065
066        @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS_PAUSE_ENDTIME", query = "update BundleJobBean w set w.statusStr = :status, w.pauseTimestamp = :pauseTime, w.endTimestamp = :endTime where w.id = :id"),
067
068        @NamedQuery(name = "UPDATE_BUNDLE_JOB_PAUSE_KICKOFF", query = "update BundleJobBean w set w.kickoffTimestamp = :kickoffTime, w.pauseTimestamp = :pauseTime where w.id = :id"),
069
070        @NamedQuery(name = "DELETE_BUNDLE_JOB", query = "delete from BundleJobBean w where w.id IN (:id)"),
071
072        @NamedQuery(name = "GET_BUNDLE_JOBS", query = "select OBJECT(w) from BundleJobBean w"),
073
074        @NamedQuery(name = "GET_BUNDLE_JOB", query = "select OBJECT(w) from BundleJobBean w where w.id = :id"),
075
076        @NamedQuery(name = "GET_BUNDLE_JOB_STATUS", query = "select w.statusStr from BundleJobBean w where w.id = :id"),
077
078        @NamedQuery(name = "GET_BUNDLE_JOB_ID_STATUS_PENDING_MOD_PAUSE_SUSPEND_TIME", query = "select w.id, w.statusStr, w.pending, w.lastModifiedTimestamp, w.pauseTimestamp, w.suspendedTimestamp from BundleJobBean w where w.id = :id"),
079
080        @NamedQuery(name = "GET_BUNDLE_JOB_ID_JOBXML_CONF", query = "select w.id, w.jobXml, w.conf from BundleJobBean w where w.id = :id"),
081
082        @NamedQuery(name = "GET_BUNDLE_JOBS_COUNT", query = "select count(w) from BundleJobBean w"),
083
084        @NamedQuery(name = "GET_BUNDLE_JOBS_COLUMNS", query = "select w.id, w.appName, w.appPath, w.conf, w.statusStr, w.kickoffTimestamp, w.startTimestamp, w.endTimestamp, w.pauseTimestamp, w.createdTimestamp, w.user, w.group, w.timeUnitStr, w.timeOut from BundleJobBean w order by w.createdTimestamp desc"),
085
086        @NamedQuery(name = "GET_BUNDLE_JOBS_RUNNING_OR_PENDING", query = "select OBJECT(w) from BundleJobBean w where w.statusStr = 'RUNNING' OR w.statusStr = 'RUNNINGWITHERROR' OR w.pending = 1 order by w.lastModifiedTimestamp"),
087
088        @NamedQuery(name = "GET_BUNDLE_JOBS_NEED_START", query = "select OBJECT(w) from BundleJobBean w where w.statusStr = 'PREP' AND (w.kickoffTimestamp IS NULL OR (w.kickoffTimestamp IS NOT NULL AND w.kickoffTimestamp <= :currentTime)) order by w.lastModifiedTimestamp"),
089
090        @NamedQuery(name = "GET_BUNDLE_JOBS_PAUSED", query = "select OBJECT(w) from BundleJobBean w where w.statusStr = 'PAUSED' OR w.statusStr = 'PAUSEDWITHERROR' OR w.statusStr = 'PREPPAUSED' order by w.lastModifiedTimestamp"),
091
092        @NamedQuery(name = "GET_BUNDLE_JOBS_UNPAUSED", query = "select OBJECT(w) from BundleJobBean w where w.statusStr = 'RUNNING' OR w.statusStr = 'RUNNINGWITHERROR' OR w.statusStr = 'PREP' order by w.lastModifiedTimestamp"),
093
094        @NamedQuery(name = "GET_BUNDLE_JOBS_OLDER_THAN", query = "select OBJECT(w) from BundleJobBean w where w.startTimestamp <= :matTime AND (w.statusStr = 'PREP' OR w.statusStr = 'RUNNING' or w.statusStr = 'RUNNINGWITHERROR')  order by w.lastModifiedTimestamp"),
095
096        @NamedQuery(name = "GET_BUNDLE_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from BundleJobBean w where w.statusStr = :status AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
097
098        @NamedQuery(name = "GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN", query = "select w.id from BundleJobBean w where ( w.statusStr = 'SUCCEEDED' OR w.statusStr = 'FAILED' OR w.statusStr = 'KILLED' OR w.statusStr = 'DONEWITHERROR') AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
099
100        @NamedQuery(name = "BULK_MONITOR_BUNDLE_QUERY", query = "SELECT b.id, b.appName, b.statusStr, b.user FROM BundleJobBean b"),
101
102        // Join query
103        @NamedQuery(name = "BULK_MONITOR_ACTIONS_QUERY", query = "SELECT a.id, a.actionNumber, a.errorCode, a.errorMessage, a.externalId, " +
104                "a.externalStatus, a.statusStr, a.createdTimestamp, a.nominalTimestamp, a.missingDependencies, " +
105                "c.id, c.appName, c.statusStr FROM CoordinatorActionBean a, CoordinatorJobBean c " +
106                "WHERE a.jobId = c.id AND c.bundleId = :bundleId ORDER BY a.jobId, a.createdTimestamp"),
107
108        @NamedQuery(name = "BULK_MONITOR_COUNT_QUERY", query = "SELECT COUNT(a) FROM CoordinatorActionBean a, CoordinatorJobBean c"),
109
110        @NamedQuery(name = "GET_BUNDLE_IDS_FOR_STATUS_TRANSIT", query = "select DISTINCT w.id from BundleActionBean a , BundleJobBean w where a.lastModifiedTimestamp >= :lastModifiedTime and w.id = a.bundleId and (w.statusStr = 'RUNNING' OR w.statusStr = 'RUNNINGWITHERROR' OR w.statusStr = 'PAUSED' OR w.statusStr = 'PAUSEDWITHERROR' OR w.statusStr = 'SUSPENDED' OR w.statusStr = 'SUSPENDEDWITHERROR' OR w.pending = 1)"),
111
112
113        @NamedQuery(name = "GET_BUNDLE_JOB_FOR_USER", query = "select w.user from BundleJobBean w where w.id = :id") })
114@Table(name = "BUNDLE_JOBS")
115public class BundleJobBean implements Writable, BundleJob, JsonBean {
116
117    @Id
118    private String id;
119
120    @Basic
121    @Column(name = "app_path")
122    private String appPath = null;
123
124    @Basic
125    @Column(name = "app_name")
126    private String appName = null;
127
128    @Basic
129    @Column(name = "external_id")
130    private String externalId = null;
131
132    @Basic
133    @Column(name = "conf")
134    @Lob
135    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
136    private StringBlob conf;
137
138    @Basic
139    @Column(name = "time_out")
140    private int timeOut = 0;
141
142    @Basic
143    @Column(name = "user_name")
144    private String user = null;
145
146    @Basic
147    @Column(name = "group_name")
148    private String group = null;
149
150    @Transient
151    private String consoleUrl;
152
153    @Basic
154    @Index
155    @Column(name = "status")
156    private String statusStr = Job.Status.PREP.toString();
157
158    @Basic
159    @Column(name = "kickoff_time")
160    private java.sql.Timestamp kickoffTimestamp = null;
161
162    @Basic
163    @Column(name = "start_time")
164    private java.sql.Timestamp startTimestamp = null;
165
166    @Basic
167    @Column(name = "end_time")
168    private java.sql.Timestamp endTimestamp = null;
169
170    @Basic
171    @Column(name = "pause_time")
172    private java.sql.Timestamp pauseTimestamp = null;
173
174    @Basic
175    @Index
176    @Column(name = "created_time")
177    private java.sql.Timestamp createdTimestamp = null;
178
179    @Basic
180    @Column(name = "time_unit")
181    private String timeUnitStr = BundleJob.Timeunit.NONE.toString();
182
183    @Basic
184    @Column(name = "pending")
185    private int pending = 0;
186
187    @Basic
188    @Index
189    @Column(name = "last_modified_time")
190    private java.sql.Timestamp lastModifiedTimestamp = null;
191
192    @Basic
193    @Index
194    @Column(name = "suspended_time")
195    private java.sql.Timestamp suspendedTimestamp = null;
196
197    @Basic
198    @Column(name = "job_xml")
199    @Lob
200    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
201    private StringBlob jobXml;
202
203    @Basic
204    @Column(name = "orig_job_xml")
205    @Lob
206    @Strategy("org.apache.oozie.executor.jpa.StringBlobValueHandler")
207    private StringBlob origJobXml = null;
208
209
210    @Transient
211    private List<CoordinatorJobBean> coordJobs;
212
213    public BundleJobBean() {
214        coordJobs = new ArrayList<CoordinatorJobBean>();
215    }
216
217    /**
218     * @return the kickoffTimestamp
219     */
220    public java.sql.Timestamp getKickoffTimestamp() {
221        return kickoffTimestamp;
222    }
223
224    /**
225     * @return the startTimestamp
226     */
227    public java.sql.Timestamp getstartTimestamp() {
228        return startTimestamp;
229    }
230
231    /**
232     * @param kickoffTimestamp the kickoffTimestamp to set
233     */
234    public void setKickoffTimestamp(java.sql.Timestamp kickoffTimestamp) {
235        this.kickoffTimestamp = kickoffTimestamp;
236    }
237
238    /**
239     * @param startTimestamp the startTimestamp to set
240     */
241    public void setStartTimestamp(java.sql.Timestamp startTimestamp) {
242        this.startTimestamp = startTimestamp;
243    }
244
245    /**
246     * Set startTime
247     *
248     * @param startTime the startTime to set
249     */
250    public void setStartTime(Date startTime) {
251        this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
252    }
253
254    /**
255     * @return the endTimestamp
256     */
257    public java.sql.Timestamp getEndTimestamp() {
258        return endTimestamp;
259    }
260
261    /**
262     * @param endTimestamp the endTimestamp to set
263     */
264    public void setEndTimestamp(java.sql.Timestamp endTimestamp) {
265        this.endTimestamp = endTimestamp;
266    }
267
268    /**
269     * @return the pauseTimestamp
270     */
271    public java.sql.Timestamp getPauseTimestamp() {
272        return pauseTimestamp;
273    }
274
275    /**
276     * @param pauseTimestamp the pauseTimestamp to set
277     */
278    public void setPauseTimestamp(java.sql.Timestamp pauseTimestamp) {
279        this.pauseTimestamp = pauseTimestamp;
280    }
281
282    /**
283     * @return the createdTimestamp
284     */
285    public java.sql.Timestamp getCreatedTimestamp() {
286        return createdTimestamp;
287    }
288
289    /**
290     * @return the createdTime
291     */
292    @Override
293    public Date getCreatedTime() {
294        return DateUtils.toDate(createdTimestamp);
295    }
296
297    /**
298     * @return the timeUnitStr
299     */
300    public String getTimeUnitStr() {
301        return timeUnitStr;
302    }
303
304    /**
305     * @return the pending
306     */
307    public int getPending() {
308        return pending;
309    }
310
311    /**
312     * Set pending to true
313     */
314    @Override
315    public void setPending() {
316        this.pending = 1;
317    }
318
319    /**
320     * Set pending value
321     *
322     * @param pending set pending value
323     */
324    public void setPending(int pending) {
325        this.pending = pending;
326    }
327
328    /**
329     * Set pending to false
330     */
331    @Override
332    public void resetPending() {
333        this.pending = 0;
334    }
335
336    /**
337     * Return if the action is pending.
338     *
339     * @return if the action is pending.
340     */
341    public boolean isPending() {
342        return pending == 1 ? true : false;
343    }
344
345    /**
346     * @return the lastModifiedTimestamp
347     */
348    public java.sql.Timestamp getLastModifiedTimestamp() {
349        return lastModifiedTimestamp;
350    }
351
352    /**
353     * @param lastModifiedTimestamp the lastModifiedTimestamp to set
354     */
355    public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) {
356        this.lastModifiedTimestamp = lastModifiedTimestamp;
357    }
358
359    /**
360     * @return the suspendedTimestamp
361     */
362    public Timestamp getSuspendedTimestamp() {
363        return suspendedTimestamp;
364    }
365
366    /**
367     * @param suspendedTimestamp the suspendedTimestamp to set
368     */
369    public void setSuspendedTimestamp(Timestamp suspendedTimestamp) {
370        this.suspendedTimestamp = suspendedTimestamp;
371    }
372
373    /**
374     * @return the jobXml
375     */
376    public String getJobXml() {
377        return jobXml == null ? null : jobXml.getString();
378    }
379
380    /**
381     * @param jobXml the jobXml to set
382     */
383    public void setJobXml(String jobXml) {
384        if (this.jobXml == null) {
385            this.jobXml = new StringBlob(jobXml);
386        }
387        else {
388            this.jobXml.setString(jobXml);
389        }
390
391    }
392
393    public void setJobXmlBlob (StringBlob jobXmlBlob) {
394        this.jobXml = jobXmlBlob;
395    }
396
397    public StringBlob getJobXmlBlob() {
398        return jobXml;
399    }
400
401    /**
402     * @return the origJobXml
403     */
404    public String getOrigJobXml() {
405        return origJobXml == null ? null : origJobXml.getString();
406    }
407
408    /**
409     * @param origJobXml the origJobXml to set
410     */
411    public void setOrigJobXml(String origJobXml) {
412        if (this.origJobXml == null) {
413            this.origJobXml = new StringBlob(origJobXml);
414        }
415        else {
416            this.origJobXml.setString(origJobXml);
417        }
418    }
419
420    public void setOrigJobXmlBlob (StringBlob origJobXml) {
421        this.origJobXml = origJobXml;
422    }
423
424    public StringBlob getOrigJobXmlBlob() {
425        return origJobXml;
426    }
427
428    /**
429     * @param createTime the createdTime to set
430     */
431    public void setCreatedTime(Date createTime) {
432        this.createdTimestamp = DateUtils.convertDateToTimestamp(createTime);
433    }
434
435    /**
436     * @param lastModifiedTime
437     */
438    public void setLastModifiedTime(Date lastModifiedTime) {
439        this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
440    }
441
442    /**
443     * Get last modified time
444     *
445     * @return last modified time
446     */
447    public Date getLastModifiedTime() {
448        return DateUtils.toDate(lastModifiedTimestamp);
449    }
450
451    @Override
452    public void write(DataOutput dataOutput) throws IOException {
453        WritableUtils.writeStr(dataOutput, getAppPath());
454        WritableUtils.writeStr(dataOutput, getAppName());
455        WritableUtils.writeStr(dataOutput, getId());
456        WritableUtils.writeStr(dataOutput, getConf());
457        WritableUtils.writeStr(dataOutput, getStatusStr());
458        WritableUtils.writeStr(dataOutput, getTimeUnit().toString());
459        dataOutput.writeLong((getKickoffTime() != null) ? getKickoffTime().getTime() : -1);
460        dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1);
461        dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1);
462        WritableUtils.writeStr(dataOutput, getUser());
463        WritableUtils.writeStr(dataOutput, getGroup());
464        WritableUtils.writeStr(dataOutput, getExternalId());
465        dataOutput.writeInt(getTimeout());
466    }
467
468    @Override
469    public void readFields(DataInput dataInput) throws IOException {
470
471        setAppPath(WritableUtils.readStr(dataInput));
472        setAppName(WritableUtils.readStr(dataInput));
473        setId(WritableUtils.readStr(dataInput));
474        setConf(WritableUtils.readStr(dataInput));
475        setStatus(BundleJob.Status.valueOf(WritableUtils.readStr(dataInput)));
476        setTimeUnit(BundleJob.Timeunit.valueOf(WritableUtils.readStr(dataInput)));
477
478        long d = dataInput.readLong();
479        if (d != -1) {
480            setKickoffTime(new Date(d));
481        }
482        d = dataInput.readLong();
483        if (d != -1) {
484            setStartTime(new Date(d));
485        }
486        d = dataInput.readLong();
487        if (d != -1) {
488            setEndTime(new Date(d));
489        }
490        setUser(WritableUtils.readStr(dataInput));
491        setGroup(WritableUtils.readStr(dataInput));
492        setExternalId(WritableUtils.readStr(dataInput));
493        setTimeOut(dataInput.readInt());
494    }
495
496
497    public Date getEndTime() {
498        return DateUtils.toDate(endTimestamp);
499    }
500
501    @Override
502    public Date getKickoffTime() {
503        return DateUtils.toDate(kickoffTimestamp);
504    }
505
506    @Override
507    public Timeunit getTimeUnit() {
508        return Timeunit.valueOf(this.timeUnitStr);
509    }
510
511    public void setEndTime(Date endTime) {
512        this.endTimestamp = DateUtils.convertDateToTimestamp(endTime);
513    }
514
515    public void setKickoffTime(Date kickoffTime) {
516        this.kickoffTimestamp = DateUtils.convertDateToTimestamp(kickoffTime);
517    }
518
519    @Override
520    public Date getPauseTime() {
521        return DateUtils.toDate(pauseTimestamp);
522    }
523
524    public void setPauseTime(Date pauseTime) {
525        this.pauseTimestamp = DateUtils.convertDateToTimestamp(pauseTime);
526    }
527
528    /**
529     * @param suspendTime the suspendTime
530     */
531    public void setSuspendedTime(Date suspendTime) {
532        this.suspendedTimestamp = DateUtils.convertDateToTimestamp(suspendTime);
533    }
534
535    @Override
536    @SuppressWarnings("unchecked")
537    public JSONObject toJSONObject() {
538        return toJSONObject("GMT");
539    }
540
541    @Override
542    @SuppressWarnings("unchecked")
543    public JSONObject toJSONObject(String timeZoneId) {
544        JSONObject json = new JSONObject();
545        json.put(JsonTags.BUNDLE_JOB_PATH, appPath);
546        json.put(JsonTags.BUNDLE_JOB_NAME, appName);
547        json.put(JsonTags.BUNDLE_JOB_ID, id);
548        json.put(JsonTags.BUNDLE_JOB_EXTERNAL_ID, externalId);
549        json.put(JsonTags.BUNDLE_JOB_CONF, getConf());
550        json.put(JsonTags.BUNDLE_JOB_STATUS, getStatus().toString());
551        json.put(JsonTags.BUNDLE_JOB_TIMEUNIT, getTimeUnit().toString());
552        json.put(JsonTags.BUNDLE_JOB_TIMEOUT, timeOut);
553        json.put(JsonTags.BUNDLE_JOB_KICKOFF_TIME, JsonUtils.formatDateRfc822(getKickoffTime(), timeZoneId));
554        json.put(JsonTags.BUNDLE_JOB_START_TIME, JsonUtils.formatDateRfc822(getStartTime(), timeZoneId));
555        json.put(JsonTags.BUNDLE_JOB_END_TIME, JsonUtils.formatDateRfc822(getEndTime(), timeZoneId));
556        json.put(JsonTags.BUNDLE_JOB_PAUSE_TIME, JsonUtils.formatDateRfc822(getPauseTime(), timeZoneId));
557        json.put(JsonTags.BUNDLE_JOB_CREATED_TIME, JsonUtils.formatDateRfc822(getCreatedTime(), timeZoneId));
558        json.put(JsonTags.BUNDLE_JOB_USER, getUser());
559        json.put(JsonTags.BUNDLE_JOB_GROUP, getGroup());
560        json.put(JsonTags.BUNDLE_JOB_ACL, getAcl());
561        json.put(JsonTags.BUNDLE_JOB_CONSOLE_URL, getConsoleUrl());
562        json.put(JsonTags.BUNDLE_COORDINATOR_JOBS, CoordinatorJobBean.toJSONArray(coordJobs, timeZoneId));
563        json.put(JsonTags.TO_STRING, toString());
564
565        return json;
566    }
567
568    @Override
569    public String getAppName() {
570        return appName;
571    }
572
573    @Override
574    public String getAppPath() {
575        return appPath;
576    }
577
578    @Override
579    public String getConf() {
580        return conf == null ? null : conf.getString();
581    }
582
583    @Override
584    public String getConsoleUrl() {
585        return consoleUrl;
586    }
587
588    @Override
589    @SuppressWarnings("unchecked")
590    public List<CoordinatorJob> getCoordinators() {
591        return (List) coordJobs;
592    }
593
594    @Override
595    @Deprecated
596    public String getGroup() {
597        return group;
598    }
599
600    @Override
601    public String getAcl() {
602        return getGroup();
603    }
604
605    @Override
606    public String getId() {
607        return id;
608    }
609
610    public int getTimeout() {
611        return timeOut;
612    }
613
614    public String getUser() {
615        return user;
616    }
617
618    /**
619     * Set id
620     *
621     * @param id the id to set
622     */
623    public void setId(String id) {
624        this.id = id;
625    }
626
627    /**
628     * Set bundlePath
629     *
630     * @param bundlePath the bundlePath to set
631     */
632    public void setAppPath(String bundlePath) {
633        this.appPath = bundlePath;
634    }
635
636    /**
637     * Set bundleName
638     *
639     * @param bundleName the bundleName to set
640     */
641    public void setAppName(String bundleName) {
642        this.appName = bundleName;
643    }
644
645    /**
646     * Return externalId
647     *
648     * @return externalId
649     */
650    public String getExternalId() {
651        return this.externalId;
652    }
653
654    /**
655     * Set externalId
656     *
657     * @param externalId the externalId to set
658     */
659    public void setExternalId(String externalId) {
660        this.externalId = externalId;
661    }
662
663    /**
664     * Set conf
665     *
666     * @param conf the conf to set
667     */
668    public void setConf(String conf) {
669        if (this.conf == null) {
670            this.conf = new StringBlob(conf);
671        }
672        else {
673            this.conf.setString(conf);
674        }
675    }
676
677    public void setConfBlob(StringBlob conf) {
678        this.conf = conf;
679    }
680
681    public StringBlob getConfBlob() {
682        return conf;
683    }
684
685    /**
686     * Set status
687     *
688     * @param status the status to set
689     */
690    public void setStatus(Status status) {
691        this.statusStr = status.toString();
692    }
693
694
695    @Override
696    public Status getStatus() {
697        return Status.valueOf(this.statusStr);
698    }
699
700    /**
701     * Set status
702     *
703     * @param statusStr the status to set
704     */
705    public void setStatus(String statusStr) {
706        this.statusStr = statusStr;
707    }
708
709
710    /**
711     * @return status string
712     */
713    public String getStatusStr() {
714        return statusStr;
715    }
716
717
718    /**
719     * Set timeUnit
720     *
721     * @param timeUnit the timeUnit to set
722     */
723    public void setTimeUnit(Timeunit timeUnit) {
724        this.timeUnitStr = timeUnit.toString();
725    }
726
727    /**
728     * Set timeOut
729     *
730     * @param timeOut the timeOut to set
731     */
732    public void setTimeOut(int timeOut) {
733        this.timeOut = timeOut;
734    }
735
736    /**
737     * Set user
738     *
739     * @param user the user to set
740     */
741    public void setUser(String user) {
742        this.user = user;
743    }
744
745    /**
746     * Set group
747     *
748     * @param group the group to set
749     */
750    public void setGroup(String group) {
751        this.group = group;
752    }
753
754    /**
755     * Set consoleUrl
756     *
757     * @param consoleUrl the consoleUrl to set
758     */
759    public void setConsoleUrl(String consoleUrl) {
760        this.consoleUrl = consoleUrl;
761    }
762
763    /**
764     * Set coordJobs
765     *
766     * @param coordJobs the coordJobs to set
767     */
768    public void setCoordJobs(List<CoordinatorJobBean> coordJobs) {
769        this.coordJobs = (coordJobs != null) ? coordJobs : new ArrayList<CoordinatorJobBean>();
770    }
771
772    /**
773     * Convert a Bundle job list into a JSONArray.
774     *
775     * @param applications list of applications.
776     * @param timeZoneId time zone to use for dates in the JSON array.
777     * @return the corresponding JSON array.
778     */
779    @SuppressWarnings("unchecked")
780    public static JSONArray toJSONArray(List<BundleJobBean> applications, String timeZoneId) {
781        JSONArray array = new JSONArray();
782        if (applications != null) {
783            for (BundleJobBean application : applications) {
784                array.add(application.toJSONObject(timeZoneId));
785            }
786        }
787        return array;
788    }
789
790
791    @Override
792    public String toString() {
793        return MessageFormat.format("Bundle id[{0}] status[{1}]", getId(), getStatus());
794    }
795
796    @Override
797    public Date getStartTime() {
798        return DateUtils.toDate(startTimestamp);
799    }
800
801    /**
802     * @return true if in terminal status
803     */
804    public boolean isTerminalStatus() {
805        boolean isTerminal = false;
806        switch (getStatus()) {
807            case SUCCEEDED:
808            case FAILED:
809            case KILLED:
810            case DONEWITHERROR:
811                isTerminal = true;
812                break;
813            default:
814                isTerminal = false;
815                break;
816        }
817        return isTerminal;
818    }
819}