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 org.apache.oozie.workflow.WorkflowInstance;
018    import org.apache.oozie.workflow.lite.LiteWorkflowInstance;
019    import org.apache.oozie.client.rest.JsonWorkflowJob;
020    import org.apache.oozie.client.WorkflowJob;
021    import org.apache.oozie.util.DateUtils;
022    import org.apache.oozie.util.WritableUtils;
023    import org.apache.hadoop.io.Writable;
024    
025    import java.io.DataInput;
026    import java.io.IOException;
027    import java.io.DataOutput;
028    import java.util.Date;
029    
030    import javax.persistence.Entity;
031    import javax.persistence.Column;
032    import javax.persistence.NamedQueries;
033    import javax.persistence.NamedQuery;
034    import javax.persistence.Basic;
035    import javax.persistence.Lob;
036    
037    import java.sql.Timestamp;
038    
039    import org.apache.openjpa.persistence.jdbc.Index;
040    
041    @Entity
042    @NamedQueries({
043    
044        @NamedQuery(name = "UPDATE_WORKFLOW", query = "update WorkflowJobBean w set w.appName = :appName, w.appPath = :appPath, w.conf = :conf, w.group = :groupName, w.run = :run, w.user = :user, w.authToken = :authToken, w.createdTimestamp = :createdTime, w.endTimestamp = :endTime, w.externalId = :externalId, w.lastModifiedTimestamp = :lastModTime, w.logToken = :logToken, w.protoActionConf = :protoActionConf, w.slaXml =:slaXml, w.startTimestamp = :startTime, w.status = :status, w.wfInstance = :wfInstance where w.id = :id"),
045    
046        @NamedQuery(name = "DELETE_WORKFLOW", query = "delete from WorkflowJobBean w where w.id = :id"),
047    
048        @NamedQuery(name = "GET_WORKFLOWS", query = "select OBJECT(w) from WorkflowJobBean w order by w.startTimestamp desc"),
049    
050        @NamedQuery(name = "GET_WORKFLOWS_COLUMNS", query = "select w.id, w.appName, w.status, w.run, w.user, w.group, w.createdTimestamp, "
051                + "w.startTimestamp, w.lastModifiedTimestamp, w.endTimestamp from WorkflowJobBean w order by w.createdTimestamp desc"),
052    
053        @NamedQuery(name = "GET_WORKFLOWS_COUNT", query = "select count(w) from WorkflowJobBean w"),
054    
055        @NamedQuery(name = "GET_COMPLETED_WORKFLOWS_OLDER_THAN", query = "select w from WorkflowJobBean w where w.endTimestamp < :endTime"),
056    
057        @NamedQuery(name = "GET_WORKFLOW", query = "select OBJECT(w) from WorkflowJobBean w where w.id = :id"),
058    
059        @NamedQuery(name = "GET_WORKFLOW_FOR_UPDATE", query = "select OBJECT(w) from WorkflowJobBean w where w.id = :id"),
060    
061        @NamedQuery(name = "GET_WORKFLOW_ID_FOR_EXTERNAL_ID", query = "select  w.id from WorkflowJobBean w where w.externalId = :externalId"),
062    
063        @NamedQuery(name = "GET_WORKFLOWS_COUNT_WITH_STATUS", query = "select count(w) from WorkflowJobBean w where w.status = :status"),
064    
065        @NamedQuery(name = "GET_WORKFLOWS_COUNT_WITH_STATUS_IN_LAST_N_SECS", query = "select count(w) from WorkflowJobBean w where w.status = :status and w.lastModifiedTimestamp > :lastModTime")
066    
067            })
068    public class WorkflowJobBean extends JsonWorkflowJob implements Writable {
069    
070        @Column(name = "proto_action_conf")
071        @Lob
072        private String protoActionConf = null;
073    
074        @Basic
075        @Column(name = "log_token")
076        private String logToken = null;
077    
078        @Basic
079        @Index
080        @Column(name = "external_id")
081        private String externalId = null;
082    
083        @Basic
084        @Index
085        @Column(name = "status")
086        private String status = WorkflowJob.Status.PREP.toString();
087    
088        @Basic
089        @Column(name = "created_time")
090        private java.sql.Timestamp createdTimestamp = null;
091    
092        @Basic
093        @Column(name = "start_time")
094        private java.sql.Timestamp startTimestamp = null;
095    
096        @Basic
097        @Index
098        @Column(name = "end_time")
099        private java.sql.Timestamp endTimestamp = null;
100    
101        @Column(name = "auth_token")
102        @Lob
103        private String authToken = null;
104    
105        @Basic
106        @Index
107        @Column(name = "last_modified_time")
108        private java.sql.Timestamp lastModifiedTimestamp = null;
109    
110        // @Basic(fetch = FetchType.LAZY)
111        // @Column(name="wfinstance",columnDefinition="blob")
112        @Column(name = "wf_instance")
113        @Lob
114        private byte[] wfInstance = null;
115    
116        @Column(name = "sla_xml")
117        @Lob
118        private String slaXml = null;
119    
120        /**
121         * Default constructor.
122         */
123        public WorkflowJobBean() {
124        }
125    
126        /**
127         * Serialize the workflow bean to a data output.
128         *
129         * @param dataOutput data output.
130         * @throws IOException thrown if the workflow bean could not be serialized.
131         */
132        public void write(DataOutput dataOutput) throws IOException {
133            WritableUtils.writeStr(dataOutput, getAppPath());
134            WritableUtils.writeStr(dataOutput, getAppName());
135            WritableUtils.writeStr(dataOutput, getId());
136            WritableUtils.writeStr(dataOutput, getParentId());
137            WritableUtils.writeStr(dataOutput, getConf());
138            WritableUtils.writeStr(dataOutput, getStatusStr());
139            dataOutput.writeLong((getCreatedTime() != null) ? getCreatedTime().getTime() : -1);
140            dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1);
141            dataOutput.writeLong((getLastModifiedTime() != null) ? getLastModifiedTime().getTime() : -1);
142            dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1);
143            WritableUtils.writeStr(dataOutput, getUser());
144            WritableUtils.writeStr(dataOutput, getGroup());
145            dataOutput.writeInt(getRun());
146            WritableUtils.writeStr(dataOutput, authToken);
147            WritableUtils.writeStr(dataOutput, logToken);
148            WritableUtils.writeStr(dataOutput, protoActionConf);
149        }
150    
151        /**
152         * Deserialize a workflow bean from a data input.
153         *
154         * @param dataInput data input.
155         * @throws IOException thrown if the workflow bean could not be deserialized.
156         */
157        public void readFields(DataInput dataInput) throws IOException {
158            setAppPath(WritableUtils.readStr(dataInput));
159            setAppName(WritableUtils.readStr(dataInput));
160            setId(WritableUtils.readStr(dataInput));
161            setParentId(WritableUtils.readStr(dataInput));
162            setConf(WritableUtils.readStr(dataInput));
163            setStatus(WorkflowJob.Status.valueOf(WritableUtils.readStr(dataInput)));
164            // setStatus(WritableUtils.readStr(dataInput));
165            long d = dataInput.readLong();
166            if (d != -1) {
167                setCreatedTime(new Date(d));
168            }
169            d = dataInput.readLong();
170            if (d != -1) {
171            }
172            setStartTime(new Date(d));
173            d = dataInput.readLong();
174            if (d != -1) {
175                setLastModifiedTime(new Date(d));
176            }
177            d = dataInput.readLong();
178            if (d != -1) {
179                setEndTime(new Date(d));
180            }
181            setUser(WritableUtils.readStr(dataInput));
182            setGroup(WritableUtils.readStr(dataInput));
183            setRun(dataInput.readInt());
184            authToken = WritableUtils.readStr(dataInput);
185            logToken = WritableUtils.readStr(dataInput);
186            protoActionConf = WritableUtils.readStr(dataInput);
187            setExternalId(getExternalId());
188            setProtoActionConf(protoActionConf);
189        }
190    
191        public String getAuthToken() {
192            return authToken;
193        }
194    
195        public void setAuthToken(String authToken) {
196            this.authToken = authToken;
197        }
198    
199        public String getLogToken() {
200            return logToken;
201        }
202    
203        public void setLogToken(String logToken) {
204            this.logToken = logToken;
205        }
206    
207        public String getSlaXml() {
208            return slaXml;
209        }
210    
211        public void setSlaXml(String slaXml) {
212            this.slaXml = slaXml;
213        }
214    
215        public WorkflowInstance getWorkflowInstance() {
216            return get(this.wfInstance);
217        }
218    
219        public byte[] getWfInstance() {
220            return wfInstance;
221        }
222    
223        public void setWorkflowInstance(WorkflowInstance workflowInstance) {
224            setWfInstance(workflowInstance);
225        }
226    
227        public void setWfInstance(byte[] wfInstance) {
228            this.wfInstance = wfInstance;
229        }
230    
231        public void setWfInstance(WorkflowInstance wfInstance) {
232            this.wfInstance = WritableUtils.toByteArray((LiteWorkflowInstance) wfInstance);
233        }
234    
235        public String getProtoActionConf() {
236            return protoActionConf;
237        }
238    
239        public void setProtoActionConf(String protoActionConf) {
240            this.protoActionConf = protoActionConf;
241        }
242    
243        public String getprotoActionConf() {
244            return protoActionConf;
245        }
246    
247        public String getlogToken() {
248            return logToken;
249        }
250    
251        public String getStatusStr() {
252            return status;
253        }
254    
255        public Timestamp getLastModifiedTimestamp() {
256            return lastModifiedTimestamp;
257        }
258    
259        public Timestamp getStartTimestamp() {
260            return startTimestamp;
261        }
262    
263        public Timestamp getCreatedTimestamp() {
264            return createdTimestamp;
265        }
266    
267        public Timestamp getEndTimestamp() {
268            return endTimestamp;
269        }
270    
271        @Override
272        public void setAppName(String val) {
273            super.setAppName(val);
274        }
275    
276        @Override
277        public void setAppPath(String val) {
278            super.setAppPath(val);
279        }
280    
281        @Override
282        public void setConf(String val) {
283            super.setConf(val);
284        }
285    
286        @Override
287        public void setStatus(Status val) {
288            super.setStatus(val);
289            this.status = val.toString();
290        }
291    
292        @Override
293        public Status getStatus() {
294            return Status.valueOf(this.status);
295        }
296    
297        @Override
298        public void setExternalId(String externalId) {
299            super.setExternalId(externalId);
300            this.externalId = externalId;
301        }
302    
303        @Override
304        public String getExternalId() {
305            return externalId;
306        }
307    
308        @Override
309        public void setLastModifiedTime(Date lastModifiedTime) {
310            super.setLastModifiedTime(lastModifiedTime);
311            this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime);
312        }
313    
314        @Override
315        public Date getLastModifiedTime() {
316            return DateUtils.toDate(lastModifiedTimestamp);
317        }
318    
319        @Override
320        public Date getCreatedTime() {
321            return DateUtils.toDate(createdTimestamp);
322        }
323    
324        @Override
325        public void setCreatedTime(Date createdTime) {
326            super.setCreatedTime(createdTime);
327            this.createdTimestamp = DateUtils.convertDateToTimestamp(createdTime);
328        }
329    
330        @Override
331        public Date getStartTime() {
332            return DateUtils.toDate(startTimestamp);
333        }
334    
335        @Override
336        public void setStartTime(Date startTime) {
337            super.setStartTime(startTime);
338            this.startTimestamp = DateUtils.convertDateToTimestamp(startTime);
339        }
340    
341        @Override
342        public Date getEndTime() {
343            return DateUtils.toDate(endTimestamp);
344        }
345    
346        @Override
347        public void setEndTime(Date endTime) {
348            super.setEndTime(endTime);
349            this.endTimestamp = DateUtils.convertDateToTimestamp(endTime);
350        }
351    
352        private WorkflowInstance get(byte[] array) {
353            LiteWorkflowInstance pInstance = WritableUtils.fromByteArray(array, LiteWorkflowInstance.class);
354            return pInstance;
355        }
356    
357    }