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.CoordinatorJob;
032    import org.apache.oozie.client.rest.JsonCoordinatorJob;
033    import org.apache.oozie.util.DateUtils;
034    import org.apache.oozie.util.WritableUtils;
035    import org.apache.openjpa.persistence.jdbc.Index;
036    
037    @Entity
038    @NamedQueries({
039        @NamedQuery(name = "UPDATE_COORD_JOB", query = "update CoordinatorJobBean w set w.appName = :appName, w.appPath = :appPath, w.concurrency = :concurrency, w.conf = :conf, w.externalId = :externalId, w.frequency = :frequency, w.lastActionNumber = :lastActionNumber, w.timeOut = :timeOut, w.timeZone = :timeZone, w.authToken = :authToken, w.createdTimestamp = :createdTime, w.endTimestamp = :endTime, w.execution = :execution, w.jobXml = :jobXml, w.lastActionTimestamp = :lastAction, w.lastModifiedTimestamp = :lastModifiedTime, w.nextMaterializedTimestamp = :nextMaterializedTime, w.origJobXml = :origJobXml, w.slaXml=:slaXml, w.startTimestamp = :startTime, w.status = :status, w.timeUnitStr = :timeUnit where w.id = :id"),
040    
041        @NamedQuery(name = "UPDATE_COORD_JOB_STATUS", query = "update CoordinatorJobBean w set w.status = :status, w.lastModifiedTimestamp = :lastModifiedTime where w.id = :id"),
042    
043        @NamedQuery(name = "DELETE_COORD_JOB", query = "delete from CoordinatorJobBean w where w.id = :id"),
044    
045        @NamedQuery(name = "GET_COORD_JOBS", query = "select OBJECT(w) from CoordinatorJobBean w"),
046    
047        @NamedQuery(name = "GET_COORD_JOB", query = "select OBJECT(w) from CoordinatorJobBean w where w.id = :id"),
048    
049        @NamedQuery(name = "GET_COORD_JOBS_COUNT", query = "select count(w) from CoordinatorJobBean w"),
050    
051        @NamedQuery(name = "GET_COORD_JOBS_COLUMNS", query = "select w.id, w.appName, w.status, w.user, w.group, w.startTimestamp, w.endTimestamp, w.appPath, w.concurrency, w.frequency, w.lastActionTimestamp, w.nextMaterializedTimestamp, w.createdTimestamp, w.timeUnitStr, w.timeZone, w.timeOut from CoordinatorJobBean w order by w.createdTimestamp desc"),
052    
053        @NamedQuery(name = "GET_COORD_JOBS_OLDER_THAN", query = "select OBJECT(w) from CoordinatorJobBean w where w.startTimestamp <= :matTime AND (w.status = 'PREP' OR w.status = 'RUNNING') AND (w.nextMaterializedTimestamp < :matTime OR w.nextMaterializedTimestamp IS NULL) AND (w.nextMaterializedTimestamp IS NULL OR (w.endTimestamp > w.nextMaterializedTimestamp AND (w.pauseTimestamp IS NULL OR w.pauseTimestamp > w.nextMaterializedTimestamp))) order by w.lastModifiedTimestamp"),
054    
055        @NamedQuery(name = "GET_COORD_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from CoordinatorJobBean w where w.status = :status AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp"),
056    
057        @NamedQuery(name = "GET_COMPLETED_COORD_JOBS_OLDER_THAN_STATUS", query = "select OBJECT(w) from CoordinatorJobBean w where ( w.status = 'SUCCEEDED' OR w.status = 'FAILED' or w.status = 'KILLED') AND w.lastModifiedTimestamp <= :lastModTime order by w.lastModifiedTimestamp")})
058    public class CoordinatorJobBean extends JsonCoordinatorJob implements Writable {
059    
060        @Basic
061        @Index
062        @Column(name = "status")
063        private String status = CoordinatorJob.Status.PREP.toString();
064    
065        @Basic
066        @Column(name = "auth_token")
067        @Lob
068        private String authToken = null;
069    
070        @Basic
071        @Column(name = "start_time")
072        private java.sql.Timestamp startTimestamp = null;
073    
074        @Basic
075        @Column(name = "end_time")
076        private java.sql.Timestamp endTimestamp = null;
077    
078        @Basic
079        @Column(name = "pause_time")
080        private java.sql.Timestamp pauseTimestamp = null;
081    
082        @Basic
083        @Index
084        @Column(name = "created_time")
085        private java.sql.Timestamp createdTimestamp = null;
086    
087        @Basic
088        @Column(name = "time_unit")
089        private String timeUnitStr = CoordinatorJob.Timeunit.NONE.toString();
090    
091        @Basic
092        @Column(name = "execution")
093        private String execution = null;
094    
095        @Basic
096        @Column(name = "last_action")
097        private java.sql.Timestamp lastActionTimestamp = null;
098    
099        @Basic
100        @Index
101        @Column(name = "next_matd_time")
102        private java.sql.Timestamp nextMaterializedTimestamp = null;
103    
104        @Basic
105        @Index
106        @Column(name = "last_modified_time")
107        private java.sql.Timestamp lastModifiedTimestamp = null;
108    
109        @Basic
110        @Index
111        @Column(name = "suspended_time")
112        private java.sql.Timestamp suspendedTimestamp = null;
113    
114        @Column(name = "job_xml")
115        @Lob
116        private String jobXml = null;
117    
118        @Column(name = "orig_job_xml")
119        @Lob
120        private String origJobXml = null;
121    
122        @Column(name = "sla_xml")
123        @Lob
124        private String slaXml = null;
125    
126        public java.sql.Timestamp getStartTimestamp() {
127            return startTimestamp;
128        }
129    
130        public void setStartTimestamp(java.sql.Timestamp startTimestamp) {
131            super.setStartTime(DateUtils.toDate(startTimestamp));
132            this.startTimestamp = startTimestamp;
133        }
134    
135        public java.sql.Timestamp getEndTimestamp() {
136            return endTimestamp;
137        }
138    
139        public void setEndTimestamp(java.sql.Timestamp endTimestamp) {
140            super.setEndTime(DateUtils.toDate(endTimestamp));
141            this.endTimestamp = endTimestamp;
142        }
143    
144        public Timestamp getNextMaterializedTimestamp() {
145            return nextMaterializedTimestamp;
146        }
147    
148        public void setNextMaterializedTimestamp(java.sql.Timestamp nextMaterializedTimestamp) {
149            super.setNextMaterializedTime(DateUtils.toDate(nextMaterializedTimestamp));
150            this.nextMaterializedTimestamp = nextMaterializedTimestamp;
151        }
152    
153        public Timestamp getLastModifiedTimestamp() {
154            return lastModifiedTimestamp;
155        }
156    
157        public void setLastModifiedTimestamp(java.sql.Timestamp lastModifiedTimestamp) {
158            this.lastModifiedTimestamp = lastModifiedTimestamp;
159        }
160    
161        public Timestamp getSuspendedTimestamp() {
162            return suspendedTimestamp;
163        }
164    
165        public void setSuspendedTimestamp(java.sql.Timestamp suspendedTimestamp) {
166            this.suspendedTimestamp = suspendedTimestamp;
167        }
168    
169        public String getJobXml() {
170            return jobXml;
171        }
172    
173        public void setJobXml(String jobXml) {
174            this.jobXml = jobXml;
175        }
176    
177        public String getOrigJobXml() {
178            return origJobXml;
179        }
180    
181        public void setOrigJobXml(String origJobXml) {
182            this.origJobXml = origJobXml;
183        }
184    
185        public String getSlaXml() {
186            return slaXml;
187        }
188    
189        public void setSlaXml(String slaXml) {
190            this.slaXml = slaXml;
191        }
192    
193        @Override
194        public void setTimeUnit(Timeunit timeUnit) {
195            super.setTimeUnit(timeUnit);
196            this.timeUnitStr = timeUnit.toString();
197        }
198    
199        public void setExecution(String execution) {
200            this.execution = execution;
201        }
202    
203        public void setLastActionTimestamp(java.sql.Timestamp lastActionTimestamp) {
204            super.setLastActionTime(DateUtils.toDate(lastActionTimestamp));
205            this.lastActionTimestamp = lastActionTimestamp;
206        }
207    
208        public void setAuthToken(String authToken) {
209            this.authToken = authToken;
210        }
211    
212        public CoordinatorJobBean() {
213        }
214    
215        /*
216         * Serialize the coordinator bean to a data output. @param dataOutput data
217         * output. @throws IOException thrown if the coordinator bean could not be
218         * serialized.
219         */
220        public void write(DataOutput dataOutput) throws IOException {
221            WritableUtils.writeStr(dataOutput, getAppPath());
222            WritableUtils.writeStr(dataOutput, getAppName());
223            WritableUtils.writeStr(dataOutput, getId());
224            WritableUtils.writeStr(dataOutput, getConf());
225            WritableUtils.writeStr(dataOutput, getStatusStr());
226            dataOutput.writeInt(getFrequency());
227            WritableUtils.writeStr(dataOutput, getTimeUnit().toString());
228            WritableUtils.writeStr(dataOutput, getTimeZone());
229            dataOutput.writeInt(getConcurrency());
230            WritableUtils.writeStr(dataOutput, getExecutionOrder().toString());
231            dataOutput.writeLong((getStartTime() != null) ? getLastActionTime().getTime() : -1);
232            dataOutput.writeLong((getStartTime() != null) ? getNextMaterializedTime().getTime() : -1);
233            dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1);
234            dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1);
235            WritableUtils.writeStr(dataOutput, getUser());
236            WritableUtils.writeStr(dataOutput, getGroup());
237            WritableUtils.writeStr(dataOutput, getExternalId());
238            dataOutput.writeInt(getTimeout());
239        }
240    
241        /**
242         * Deserialize a coordinator bean from a data input.
243         *
244         * @param dataInput data input.
245         * @throws IOException thrown if the workflow bean could not be deserialized.
246         */
247        public void readFields(DataInput dataInput) throws IOException {
248            setAppPath(WritableUtils.readStr(dataInput));
249            setAppName(WritableUtils.readStr(dataInput));
250            setId(WritableUtils.readStr(dataInput));
251            setConf(WritableUtils.readStr(dataInput));
252            setStatus(CoordinatorJob.Status.valueOf(WritableUtils.readStr(dataInput)));
253            setFrequency(dataInput.readInt());
254            setTimeUnit(CoordinatorJob.Timeunit.valueOf(WritableUtils.readStr(dataInput)));
255            setTimeZone(WritableUtils.readStr(dataInput));
256            setConcurrency(dataInput.readInt());
257            setExecutionOrder(Execution.valueOf(WritableUtils.readStr(dataInput)));
258    
259            long d = dataInput.readLong();
260            if (d != -1) {
261                setLastActionTime(new Date(d));
262            }
263            d = dataInput.readLong();
264            if (d != -1) {
265                setNextMaterializedTime(new Date(d));
266            }
267            d = dataInput.readLong();
268            if (d != -1) {
269                setStartTime(new Date(d));
270            }
271    
272            d = dataInput.readLong();
273            if (d != -1) {
274                setEndTime(new Date(d));
275            }
276            setUser(WritableUtils.readStr(dataInput));
277            setGroup(WritableUtils.readStr(dataInput));
278            setExternalId(WritableUtils.readStr(dataInput));
279            setTimeout(dataInput.readInt());
280        }
281    
282        @Override
283        public Status getStatus() {
284            return Status.valueOf(this.status);
285        }
286    
287        public String getStatusStr() {
288            return status;
289        }
290    
291        @Override
292        public void setStatus(Status val) {
293            super.setStatus(val);
294            this.status = val.toString();
295        }
296    
297        public String getTimeUnitStr() {
298            return timeUnitStr;
299        }
300    
301        @Override
302        public Timeunit getTimeUnit() {
303            return Timeunit.valueOf(this.timeUnitStr);
304        }
305    
306        public void setExecution(Execution order) {
307            this.execution = order.toString();
308            super.setExecutionOrder(order);
309        }
310    
311        @Override
312        public Execution getExecutionOrder() {
313            return Execution.valueOf(this.execution);
314        }
315    
316        public String getExecution() {
317            return execution;
318        }
319    
320        @Override
321        public void setLastActionTime(Date lastAction) {
322            this.lastActionTimestamp = DateUtils.convertDateToTimestamp(lastAction);
323            super.setLastActionTime(lastAction);
324        }
325    
326        @Override
327        public Date getLastActionTime() {
328            return DateUtils.toDate(lastActionTimestamp);
329        }
330    
331        public Timestamp getLastActionTimestamp() {
332            return lastActionTimestamp;
333        }
334    
335        @Override
336        public void setNextMaterializedTime(Date nextMaterializedTime) {
337            super.setNextMaterializedTime(nextMaterializedTime);
338            this.nextMaterializedTimestamp = DateUtils.convertDateToTimestamp(nextMaterializedTime);
339        }
340    
341        @Override
342        public Date getNextMaterializedTime() {
343            return DateUtils.toDate(nextMaterializedTimestamp);
344        }
345    
346        public void setLastModifiedTime(Date lastModifiedTime) {
347            this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
348        }
349    
350        public Date getLastModifiedTime() {
351            return DateUtils.toDate(lastModifiedTimestamp);
352        }
353    
354        public void setSuspendedTime(Date suspendedTime) {
355            this.suspendedTimestamp = DateUtils.convertDateToTimestamp(suspendedTime);
356        }
357    
358        public Date getSuspendedTime() {
359            return DateUtils.toDate(suspendedTimestamp);
360        }
361    
362        @Override
363        public void setStartTime(Date startTime) {
364            super.setStartTime(startTime);
365            this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
366        }
367    
368        @Override
369        public Date getStartTime() {
370            return DateUtils.toDate(startTimestamp);
371        }
372    
373        @Override
374        public void setEndTime(Date endTime) {
375            super.setEndTime(endTime);
376            this.endTimestamp = DateUtils.convertDateToTimestamp(endTime);
377        }
378    
379        @Override
380        public void setPauseTime(Date pauseTime) {
381            super.setPauseTime(pauseTime);
382            this.pauseTimestamp = DateUtils.convertDateToTimestamp(pauseTime);
383        }
384    
385        @Override
386        public Date getEndTime() {
387            return DateUtils.toDate(endTimestamp);
388        }
389    
390        @Override
391        public Date getPauseTime() {
392            return DateUtils.toDate(pauseTimestamp);
393        }
394    
395        public void setCreatedTime(Date createTime) {
396            this.createdTimestamp = DateUtils.convertDateToTimestamp(createTime);
397        }
398    
399        public Date getCreatedTime() {
400            return DateUtils.toDate(createdTimestamp);
401        }
402    
403        public Timestamp getCreatedTimestamp() {
404            return createdTimestamp;
405        }
406    
407        public String getAuthToken() {
408            // TODO Auto-generated method stub
409            return this.authToken;
410        }
411    
412    }