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