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;
019
020import java.io.DataInput;
021import java.io.DataOutput;
022import java.io.IOException;
023import java.sql.Timestamp;
024import java.util.Date;
025
026import javax.persistence.Basic;
027import javax.persistence.Column;
028import javax.persistence.Entity;
029import javax.persistence.Lob;
030import javax.persistence.NamedQueries;
031import javax.persistence.NamedQuery;
032
033import org.apache.hadoop.io.Writable;
034import org.apache.oozie.client.BundleJob;
035import org.apache.oozie.client.Job;
036import org.apache.oozie.client.rest.JsonBundleJob;
037import org.apache.oozie.util.DateUtils;
038import org.apache.oozie.util.WritableUtils;
039import org.apache.openjpa.persistence.jdbc.Index;
040
041@Entity
042@NamedQueries( {
043        @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 = :createdTimestamp, w.endTimestamp = :endTimestamp, w.jobXml = :jobXml, w.lastModifiedTimestamp = :lastModifiedTimestamp, w.origJobXml = :origJobXml, w.startTimestamp = :startTimestamp, w.status = :status, w.timeUnitStr = :timeUnit, w.pending = :pending where w.id = :id"),
044
045        @NamedQuery(name = "UPDATE_BUNDLE_JOB_STATUS", query = "update BundleJobBean w set w.status = :status, w.lastModifiedTimestamp = :lastModifiedTimestamp, w.pending = :pending where w.id = :id"),
046
047        @NamedQuery(name = "DELETE_BUNDLE_JOB", query = "delete from BundleJobBean w where w.id = :id"),
048
049        @NamedQuery(name = "GET_BUNDLE_JOBS", query = "select OBJECT(w) from BundleJobBean w"),
050
051        @NamedQuery(name = "GET_BUNDLE_JOB", query = "select OBJECT(w) from BundleJobBean w where w.id = :id"),
052
053        @NamedQuery(name = "GET_BUNDLE_JOBS_COUNT", query = "select count(w) from BundleJobBean w"),
054
055        @NamedQuery(name = "GET_BUNDLE_JOBS_COLUMNS", query = "select w.id, w.appName, w.appPath, w.conf, w.status, 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"),
056
057        @NamedQuery(name = "GET_BUNDLE_JOBS_RUNNING_OR_PENDING", query = "select OBJECT(w) from BundleJobBean w where w.status = 'RUNNING' OR w.status = 'RUNNINGWITHERROR' OR w.pending = 1 order by w.lastModifiedTimestamp"),
058
059        @NamedQuery(name = "GET_BUNDLE_JOBS_NEED_START", query = "select OBJECT(w) from BundleJobBean w where w.status = 'PREP' AND (w.kickoffTimestamp IS NULL OR (w.kickoffTimestamp IS NOT NULL AND w.kickoffTimestamp <= :currentTime)) order by w.lastModifiedTimestamp"),
060
061        @NamedQuery(name = "GET_BUNDLE_JOBS_PAUSED", query = "select OBJECT(w) from BundleJobBean w where w.status = 'PAUSED' OR w.status = 'PAUSEDWITHERROR' OR w.status = 'PREPPAUSED' order by w.lastModifiedTimestamp"),
062
063        @NamedQuery(name = "GET_BUNDLE_JOBS_UNPAUSED", query = "select OBJECT(w) from BundleJobBean w where w.status = 'RUNNING' OR w.status = 'RUNNINGWITHERROR' OR w.status = 'PREP' order by w.lastModifiedTimestamp"),
064
065        @NamedQuery(name = "GET_BUNDLE_JOBS_OLDER_THAN", query = "select OBJECT(w) from BundleJobBean w where w.startTimestamp <= :matTime AND (w.status = 'PREP' OR w.status = 'RUNNING' or w.status = 'RUNNINGWITHERROR')  order by w.lastModifiedTimestamp"),
066
067        @NamedQuery(name = "GET_BUNDLE_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from BundleJobBean w where w.status = :status AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
068
069        @NamedQuery(name = "GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN", query = "select w.id from BundleJobBean w where ( w.status = 'SUCCEEDED' OR w.status = 'FAILED' OR w.status = 'KILLED' OR w.status = 'DONEWITHERROR') AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
070
071        @NamedQuery(name = "BULK_MONITOR_BUNDLE_QUERY", query = "SELECT b.id, b.status, b.user FROM BundleJobBean b WHERE b.appName = :appName"),
072
073        // Join query
074        @NamedQuery(name = "BULK_MONITOR_ACTIONS_QUERY", query = "SELECT a.id, a.actionNumber, a.errorCode, a.errorMessage, a.externalId, " +
075                "a.externalStatus, a.status, a.createdTimestamp, a.nominalTimestamp, a.missingDependencies, " +
076                "c.id, c.appName, c.status FROM CoordinatorActionBean a, CoordinatorJobBean c " +
077                "WHERE a.jobId = c.id AND c.bundleId = :bundleId ORDER BY a.jobId, a.createdTimestamp"),
078
079        @NamedQuery(name = "BULK_MONITOR_COUNT_QUERY", query = "SELECT COUNT(a) FROM CoordinatorActionBean a, CoordinatorJobBean c"),
080
081        @NamedQuery(name = "GET_BUNDLE_JOB_FOR_USER", query = "select w.user from BundleJobBean w where w.id = :id") })
082public class BundleJobBean extends JsonBundleJob implements Writable {
083
084    @Basic
085    @Index
086    @Column(name = "status")
087    private String status = Job.Status.PREP.toString();
088
089    @Basic
090    @Column(name = "kickoff_time")
091    private java.sql.Timestamp kickoffTimestamp = null;
092
093    @Basic
094    @Column(name = "start_time")
095    private java.sql.Timestamp startTimestamp = null;
096
097    @Basic
098    @Column(name = "end_time")
099    private java.sql.Timestamp endTimestamp = null;
100
101    @Basic
102    @Column(name = "pause_time")
103    private java.sql.Timestamp pauseTimestamp = null;
104
105    @Basic
106    @Index
107    @Column(name = "created_time")
108    private java.sql.Timestamp createdTimestamp = null;
109
110    @Basic
111    @Column(name = "time_unit")
112    private String timeUnitStr = BundleJob.Timeunit.NONE.toString();
113
114    @Basic
115    @Column(name = "pending")
116    private int pending = 0;
117
118    @Basic
119    @Index
120    @Column(name = "last_modified_time")
121    private java.sql.Timestamp lastModifiedTimestamp = null;
122
123    @Basic
124    @Index
125    @Column(name = "suspended_time")
126    private java.sql.Timestamp suspendedTimestamp = null;
127
128    @Column(name = "job_xml")
129    @Lob
130    private String jobXml = null;
131
132    @Column(name = "orig_job_xml")
133    @Lob
134    private String origJobXml = null;
135
136    /**
137     * @return the kickoffTimestamp
138     */
139    public java.sql.Timestamp getKickoffTimestamp() {
140        return kickoffTimestamp;
141    }
142
143    /**
144     * @return the startTimestamp
145     */
146    public java.sql.Timestamp getstartTimestamp() {
147        return startTimestamp;
148    }
149
150    /**
151     * @param kickoffTimestamp the kickoffTimestamp to set
152     */
153    public void setKickoffTimestamp(java.sql.Timestamp kickoffTimestamp) {
154        super.setKickoffTime(DateUtils.toDate(kickoffTimestamp));
155        this.kickoffTimestamp = kickoffTimestamp;
156    }
157
158    /**
159     * @param startTimestamp the startTimestamp to set
160     */
161    public void setStartTimestamp(java.sql.Timestamp startTimestamp) {
162        super.setStartTime(DateUtils.toDate(startTimestamp));
163        this.startTimestamp = startTimestamp;
164    }
165
166    /**
167     * Set startTime
168     *
169     * @param startTime the startTime to set
170     */
171    @Override
172    public void setStartTime(Date startTime) {
173        super.setStartTime(startTime);
174        this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
175    }
176
177    /**
178     * @return the endTimestamp
179     */
180    public java.sql.Timestamp getEndTimestamp() {
181        return endTimestamp;
182    }
183
184    /**
185     * @param endTimestamp the endTimestamp to set
186     */
187    public void setEndTimestamp(java.sql.Timestamp endTimestamp) {
188        super.setEndTime(DateUtils.toDate(endTimestamp));
189        this.endTimestamp = endTimestamp;
190    }
191
192    /**
193     * @return the pauseTimestamp
194     */
195    public java.sql.Timestamp getPauseTimestamp() {
196        return pauseTimestamp;
197    }
198
199    /**
200     * @param pauseTimestamp the pauseTimestamp to set
201     */
202    public void setPauseTimestamp(java.sql.Timestamp pauseTimestamp) {
203        super.setPauseTime(DateUtils.toDate(pauseTimestamp));
204        this.pauseTimestamp = pauseTimestamp;
205    }
206
207    /**
208     * @return the createdTimestamp
209     */
210    public java.sql.Timestamp getCreatedTimestamp() {
211        return createdTimestamp;
212    }
213
214    /**
215     * @return the createdTime
216     */
217    @Override
218    public Date getCreatedTime() {
219        return DateUtils.toDate(createdTimestamp);
220    }
221
222    /**
223     * @return the timeUnitStr
224     */
225    public String getTimeUnitStr() {
226        return timeUnitStr;
227    }
228
229    /**
230     * @return the pending
231     */
232    public int getPending() {
233        return pending;
234    }
235
236    /**
237     * Set pending to true
238     *
239     * @param pending set pending to true
240     */
241    @Override
242    public void setPending() {
243        super.setPending();
244        this.pending = 1;
245    }
246
247    /**
248     * Set pending to false
249     *
250     * @param pending set pending to false
251     */
252    @Override
253    public void resetPending() {
254        super.resetPending();
255        this.pending = 0;
256    }
257
258    /**
259     * Return if the action is pending.
260     *
261     * @return if the action is pending.
262     */
263    public boolean isPending() {
264        return pending == 1 ? true : false;
265    }
266
267    /**
268     * @return the lastModifiedTimestamp
269     */
270    public java.sql.Timestamp getLastModifiedTimestamp() {
271        return lastModifiedTimestamp;
272    }
273
274    /**
275     * @param lastModifiedTimestamp the lastModifiedTimestamp to set
276     */
277    public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) {
278        this.lastModifiedTimestamp = lastModifiedTimestamp;
279    }
280
281    /**
282     * @return the suspendedTimestamp
283     */
284    public Timestamp getSuspendedTimestamp() {
285        return suspendedTimestamp;
286    }
287
288    /**
289     * @param suspendedTimestamp the suspendedTimestamp to set
290     */
291    public void setSuspendedTimestamp(Timestamp suspendedTimestamp) {
292        this.suspendedTimestamp = suspendedTimestamp;
293    }
294
295    /**
296     * @return the jobXml
297     */
298    public String getJobXml() {
299        return jobXml;
300    }
301
302    /**
303     * @param jobXml the jobXml to set
304     */
305    public void setJobXml(String jobXml) {
306        this.jobXml = jobXml;
307    }
308
309    /**
310     * @return the origJobXml
311     */
312    public String getOrigJobXml() {
313        return origJobXml;
314    }
315
316    /**
317     * @param origJobXml the origJobXml to set
318     */
319    public void setOrigJobXml(String origJobXml) {
320        this.origJobXml = origJobXml;
321    }
322
323    /**
324     * @param createTime the createdTime to set
325     */
326    @Override
327    public void setCreatedTime(Date createTime) {
328        super.setCreatedTime(createTime);
329        this.createdTimestamp = DateUtils.convertDateToTimestamp(createTime);
330    }
331
332    /**
333     * @param lastModifiedTime
334     */
335    public void setLastModifiedTime(Date lastModifiedTime) {
336        this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
337    }
338
339    /**
340     * Get last modified time
341     *
342     * @return last modified time
343     */
344    public Date getLastModifiedTime() {
345        return DateUtils.toDate(lastModifiedTimestamp);
346    }
347
348    /* (non-Javadoc)
349     * @see org.apache.hadoop.io.Writable#write(java.io.DataOutput)
350     */
351    @Override
352    public void write(DataOutput dataOutput) throws IOException {
353        WritableUtils.writeStr(dataOutput, getAppPath());
354        WritableUtils.writeStr(dataOutput, getAppName());
355        WritableUtils.writeStr(dataOutput, getId());
356        WritableUtils.writeStr(dataOutput, getConf());
357        WritableUtils.writeStr(dataOutput, getStatusStr());
358        WritableUtils.writeStr(dataOutput, getTimeUnit().toString());
359        dataOutput.writeLong((getKickoffTime() != null) ? getKickoffTime().getTime() : -1);
360        dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1);
361        dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1);
362        WritableUtils.writeStr(dataOutput, getUser());
363        WritableUtils.writeStr(dataOutput, getGroup());
364        WritableUtils.writeStr(dataOutput, getExternalId());
365        dataOutput.writeInt(getTimeout());
366    }
367
368    /* (non-Javadoc)
369     * @see org.apache.hadoop.io.Writable#readFields(java.io.DataInput)
370     */
371    @Override
372    public void readFields(DataInput dataInput) throws IOException {
373
374        setAppPath(WritableUtils.readStr(dataInput));
375        setAppName(WritableUtils.readStr(dataInput));
376        setId(WritableUtils.readStr(dataInput));
377        setConf(WritableUtils.readStr(dataInput));
378        setStatus(BundleJob.Status.valueOf(WritableUtils.readStr(dataInput)));
379        setTimeUnit(BundleJob.Timeunit.valueOf(WritableUtils.readStr(dataInput)));
380
381        long d = dataInput.readLong();
382        if (d != -1) {
383            setKickoffTime(new Date(d));
384        }
385        d = dataInput.readLong();
386        if (d != -1) {
387            setStartTime(new Date(d));
388        }
389        d = dataInput.readLong();
390        if (d != -1) {
391            setEndTime(new Date(d));
392        }
393        setUser(WritableUtils.readStr(dataInput));
394        setGroup(WritableUtils.readStr(dataInput));
395        setExternalId(WritableUtils.readStr(dataInput));
396        setTimeOut(dataInput.readInt());
397    }
398
399    /* (non-Javadoc)
400     * @see org.apache.oozie.client.rest.JsonBundleJob#getStatus()
401     */
402    @Override
403    public Status getStatus() {
404        return Status.valueOf(this.status);
405    }
406
407    /**
408     * @return status string
409     */
410    public String getStatusStr() {
411        return status;
412    }
413
414    /* (non-Javadoc)
415     * @see org.apache.oozie.client.rest.JsonBundleJob#getEndTime()
416     */
417    @Override
418    public Date getEndTime() {
419        return DateUtils.toDate(endTimestamp);
420    }
421
422    /* (non-Javadoc)
423     * @see org.apache.oozie.client.rest.JsonBundleJob#getKickoffTime()
424     */
425    @Override
426    public Date getKickoffTime() {
427        return DateUtils.toDate(kickoffTimestamp);
428    }
429
430    /* (non-Javadoc)
431     * @see org.apache.oozie.client.rest.JsonBundleJob#getTimeUnit()
432     */
433    @Override
434    public Timeunit getTimeUnit() {
435        return Timeunit.valueOf(this.timeUnitStr);
436    }
437
438    /* (non-Javadoc)
439     * @see org.apache.oozie.client.rest.JsonBundleJob#setEndTime(java.util.Date)
440     */
441    @Override
442    public void setEndTime(Date endTime) {
443        super.setEndTime(endTime);
444        this.endTimestamp = DateUtils.convertDateToTimestamp(endTime);
445    }
446
447    /* (non-Javadoc)
448     * @see org.apache.oozie.client.rest.JsonBundleJob#setKickoffTime(java.util.Date)
449     */
450    @Override
451    public void setKickoffTime(Date kickoffTime) {
452        super.setKickoffTime(kickoffTime);
453        this.kickoffTimestamp = DateUtils.convertDateToTimestamp(kickoffTime);
454    }
455
456    @Override
457    /* (non-Javadoc)
458     * @see org.apache.oozie.client.rest.JsonBundleJob#getPauseTime()
459     */
460    public Date getPauseTime() {
461        return DateUtils.toDate(pauseTimestamp);
462    }
463
464    /* (non-Javadoc)
465     * @see org.apache.oozie.client.rest.JsonBundleJob#setPauseTime(java.util.Date)
466     */
467    @Override
468    public void setPauseTime(Date pauseTime) {
469        super.setPauseTime(pauseTime);
470        this.pauseTimestamp = DateUtils.convertDateToTimestamp(pauseTime);
471    }
472
473    /* (non-Javadoc)
474     * @see org.apache.oozie.client.rest.JsonBundleJob#setStatus(org.apache.oozie.client.BundleJob.Status)
475     */
476    @Override
477    public void setStatus(org.apache.oozie.client.BundleJob.Status val) {
478        super.setStatus(val);
479        this.status = val.toString();
480    }
481
482    /* (non-Javadoc)
483     * @see org.apache.oozie.client.rest.JsonBundleJob#setTimeUnit(org.apache.oozie.client.BundleJob.Timeunit)
484     */
485    @Override
486    public void setTimeUnit(Timeunit timeUnit) {
487        super.setTimeUnit(timeUnit);
488        this.timeUnitStr = timeUnit.toString();
489    }
490
491    /**
492     * @param return the suspendTime
493     */
494    public void setSuspendedTime(Date suspendTime) {
495        this.suspendedTimestamp = DateUtils.convertDateToTimestamp(suspendTime);
496    }
497
498}