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    package org.apache.oozie;
019    
020    import java.io.DataInput;
021    import java.io.DataOutput;
022    import java.io.IOException;
023    import java.sql.Timestamp;
024    import java.util.Date;
025    
026    import javax.persistence.Basic;
027    import javax.persistence.Column;
028    import javax.persistence.Entity;
029    import javax.persistence.Lob;
030    import javax.persistence.NamedQueries;
031    import javax.persistence.NamedQuery;
032    
033    import org.apache.hadoop.io.Writable;
034    import org.apache.oozie.client.BundleJob;
035    import org.apache.oozie.client.Job;
036    import org.apache.oozie.client.rest.JsonBundleJob;
037    import org.apache.oozie.util.DateUtils;
038    import org.apache.oozie.util.WritableUtils;
039    import 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.authToken = :authToken, 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_PENDING", query = "select OBJECT(w) from BundleJobBean w where w.pending = 1 order by w.lastModifiedTimestamp"),
058    
059            @NamedQuery(name = "GET_BUNDLE_JOBS_RUNNING", query = "select OBJECT(w) from BundleJobBean w where w.status = 'RUNNING' OR w.status = 'RUNNINGWITHERROR' order by w.lastModifiedTimestamp"),
060    
061            @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"),
062    
063            @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"),
064    
065            @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"),
066    
067            @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')  order by w.lastModifiedTimestamp"),
068    
069            @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"),
070    
071            @NamedQuery(name = "GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN", query = "select OBJECT(w) 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")})
072    public class BundleJobBean extends JsonBundleJob implements Writable {
073    
074        @Basic
075        @Index
076        @Column(name = "status")
077        private String status = Job.Status.PREP.toString();
078    
079        @Basic
080        @Column(name = "auth_token")
081        @Lob
082        private String authToken = null;
083    
084        @Basic
085        @Column(name = "kickoff_time")
086        private java.sql.Timestamp kickoffTimestamp = null;
087    
088        @Basic
089        @Column(name = "start_time")
090        private java.sql.Timestamp startTimestamp = null;
091    
092        @Basic
093        @Column(name = "end_time")
094        private java.sql.Timestamp endTimestamp = null;
095    
096        @Basic
097        @Column(name = "pause_time")
098        private java.sql.Timestamp pauseTimestamp = null;
099    
100        @Basic
101        @Index
102        @Column(name = "created_time")
103        private java.sql.Timestamp createdTimestamp = null;
104    
105        @Basic
106        @Column(name = "time_unit")
107        private String timeUnitStr = BundleJob.Timeunit.NONE.toString();
108    
109        @Basic
110        @Column(name = "pending")
111        private int pending = 0;
112    
113        @Basic
114        @Index
115        @Column(name = "last_modified_time")
116        private java.sql.Timestamp lastModifiedTimestamp = null;
117    
118        @Basic
119        @Index
120        @Column(name = "suspended_time")
121        private java.sql.Timestamp suspendedTimestamp = null;
122    
123        @Column(name = "job_xml")
124        @Lob
125        private String jobXml = null;
126    
127        @Column(name = "orig_job_xml")
128        @Lob
129        private String origJobXml = null;
130    
131        /**
132         * @return the authToken
133         */
134        public String getAuthToken() {
135            return authToken;
136        }
137    
138        /**
139         * @param authToken the authToken to set
140         */
141        public void setAuthToken(String authToken) {
142            this.authToken = authToken;
143        }
144    
145        /**
146         * @return the kickoffTimestamp
147         */
148        public java.sql.Timestamp getKickoffTimestamp() {
149            return kickoffTimestamp;
150        }
151    
152        /**
153         * @return the startTimestamp
154         */
155        public java.sql.Timestamp getstartTimestamp() {
156            return startTimestamp;
157        }
158    
159        /**
160         * @param kickoffTimestamp the kickoffTimestamp to set
161         */
162        public void setKickoffTimestamp(java.sql.Timestamp kickoffTimestamp) {
163            super.setKickoffTime(DateUtils.toDate(kickoffTimestamp));
164            this.kickoffTimestamp = kickoffTimestamp;
165        }
166    
167        /**
168         * @param startTimestamp the startTimestamp to set
169         */
170        public void setStartTimestamp(java.sql.Timestamp startTimestamp) {
171            super.setStartTime(DateUtils.toDate(startTimestamp));
172            this.startTimestamp = startTimestamp;
173        }
174    
175        /**
176         * Set startTime
177         *
178         * @param startTime the startTime to set
179         */
180        @Override
181        public void setStartTime(Date startTime) {
182            super.setStartTime(startTime);
183            this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
184        }
185    
186        /**
187         * @return the endTimestamp
188         */
189        public java.sql.Timestamp getEndTimestamp() {
190            return endTimestamp;
191        }
192    
193        /**
194         * @param endTimestamp the endTimestamp to set
195         */
196        public void setEndTimestamp(java.sql.Timestamp endTimestamp) {
197            super.setEndTime(DateUtils.toDate(endTimestamp));
198            this.endTimestamp = endTimestamp;
199        }
200    
201        /**
202         * @return the pauseTimestamp
203         */
204        public java.sql.Timestamp getPauseTimestamp() {
205            return pauseTimestamp;
206        }
207    
208        /**
209         * @param pauseTimestamp the pauseTimestamp to set
210         */
211        public void setPauseTimestamp(java.sql.Timestamp pauseTimestamp) {
212            super.setPauseTime(DateUtils.toDate(pauseTimestamp));
213            this.pauseTimestamp = pauseTimestamp;
214        }
215    
216        /**
217         * @return the createdTimestamp
218         */
219        public java.sql.Timestamp getCreatedTimestamp() {
220            return createdTimestamp;
221        }
222    
223        /**
224         * @return the createdTime
225         */
226        @Override
227        public Date getCreatedTime() {
228            return DateUtils.toDate(createdTimestamp);
229        }
230    
231        /**
232         * @return the timeUnitStr
233         */
234        public String getTimeUnitStr() {
235            return timeUnitStr;
236        }
237    
238        /**
239         * @return the pending
240         */
241        public int getPending() {
242            return pending;
243        }
244    
245        /**
246         * Set pending to true
247         *
248         * @param pending set pending to true
249         */
250        @Override
251        public void setPending() {
252            super.setPending();
253            this.pending = 1;
254        }
255    
256        /**
257         * Set pending to false
258         *
259         * @param pending set pending to false
260         */
261        @Override
262        public void resetPending() {
263            super.resetPending();
264            this.pending = 0;
265        }
266    
267        /**
268         * Return if the action is pending.
269         *
270         * @return if the action is pending.
271         */
272        public boolean isPending() {
273            return pending == 1 ? true : false;
274        }
275    
276        /**
277         * @return the lastModifiedTimestamp
278         */
279        public java.sql.Timestamp getLastModifiedTimestamp() {
280            return lastModifiedTimestamp;
281        }
282    
283        /**
284         * @param lastModifiedTimestamp the lastModifiedTimestamp to set
285         */
286        public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) {
287            this.lastModifiedTimestamp = lastModifiedTimestamp;
288        }
289    
290        /**
291         * @return the suspendedTimestamp
292         */
293        public Timestamp getSuspendedTimestamp() {
294            return suspendedTimestamp;
295        }
296    
297        /**
298         * @param suspendedTimestamp the suspendedTimestamp to set
299         */
300        public void setSuspendedTimestamp(Timestamp suspendedTimestamp) {
301            this.suspendedTimestamp = suspendedTimestamp;
302        }
303    
304        /**
305         * @return the jobXml
306         */
307        public String getJobXml() {
308            return jobXml;
309        }
310    
311        /**
312         * @param jobXml the jobXml to set
313         */
314        public void setJobXml(String jobXml) {
315            this.jobXml = jobXml;
316        }
317    
318        /**
319         * @return the origJobXml
320         */
321        public String getOrigJobXml() {
322            return origJobXml;
323        }
324    
325        /**
326         * @param origJobXml the origJobXml to set
327         */
328        public void setOrigJobXml(String origJobXml) {
329            this.origJobXml = origJobXml;
330        }
331    
332        /**
333         * @param createTime the createdTime to set
334         */
335        @Override
336        public void setCreatedTime(Date createTime) {
337            super.setCreatedTime(createTime);
338            this.createdTimestamp = DateUtils.convertDateToTimestamp(createTime);
339        }
340    
341        /**
342         * @param lastModifiedTime
343         */
344        public void setLastModifiedTime(Date lastModifiedTime) {
345            this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
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    }