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