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.client.rest;
019    
020    import org.apache.oozie.client.WorkflowAction;
021    import org.apache.oozie.client.WorkflowJob;
022    import org.json.simple.JSONArray;
023    import org.json.simple.JSONObject;
024    
025    import java.text.MessageFormat;
026    import java.util.ArrayList;
027    import java.util.Date;
028    import java.util.List;
029    
030    import javax.persistence.*;
031    
032    /**
033     * Json Bean that represents an Oozie workflow job.
034     */
035    
036    @Entity
037    @Table(name = "WF_JOBS")
038    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
039    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
040    public class JsonWorkflowJob implements WorkflowJob, JsonBean {
041    
042        @Id
043        private String id;
044    
045        @Basic
046        @Column(name = "app_name")
047        private String appName = null;
048    
049        @Basic
050        @Column(name = "app_path")
051        private String appPath = null;
052    
053        @Transient
054        private String externalId = null;
055    
056        @Column(name = "conf")
057        @Lob
058        private String conf = null;
059    
060        @Transient
061        private Status status = WorkflowJob.Status.PREP;
062    
063        @Transient
064        private Date createdTime;
065    
066        @Transient
067        private Date startTime;
068    
069        @Transient
070        private Date endTime;
071    
072        @Transient
073        private Date lastModifiedTime;
074    
075        @Basic
076        @Column(name = "user_name")
077        private String user = null;
078    
079        @Basic
080        @Column(name = "group_name")
081        private String group;
082    
083        @Basic
084        @Column(name = "run")
085        private int run = 1;
086    
087        @Basic
088        @Column(name = "parent_id")
089        private String parentId;
090    
091        @Transient
092        private String consoleUrl;
093    
094        @Transient
095        private List<? extends JsonWorkflowAction> actions;
096    
097        public JsonWorkflowJob() {
098            actions = new ArrayList<JsonWorkflowAction>();
099        }
100    
101        @SuppressWarnings("unchecked")
102        public JSONObject toJSONObject() {
103            JSONObject json = new JSONObject();
104            json.put(JsonTags.WORKFLOW_APP_PATH, getAppPath());
105            json.put(JsonTags.WORKFLOW_APP_NAME, getAppName());
106            json.put(JsonTags.WORKFLOW_ID, getId());
107            json.put(JsonTags.WORKFLOW_EXTERNAL_ID, getExternalId());
108            json.put(JsonTags.WORKFLOW_PARENT_ID, getParentId());
109            json.put(JsonTags.WORKFLOW_CONF, getConf());
110            json.put(JsonTags.WORKFLOW_STATUS, getStatus().toString());
111            json.put(JsonTags.WORKFLOW_LAST_MOD_TIME, JsonUtils.formatDateRfc822(getLastModifiedTime()));
112            json.put(JsonTags.WORKFLOW_CREATED_TIME, JsonUtils.formatDateRfc822(getCreatedTime()));
113            json.put(JsonTags.WORKFLOW_START_TIME, JsonUtils.formatDateRfc822(getStartTime()));
114            json.put(JsonTags.WORKFLOW_END_TIME, JsonUtils.formatDateRfc822(getEndTime()));
115            json.put(JsonTags.WORKFLOW_USER, getUser());
116            json.put(JsonTags.WORKFLOW_GROUP, getGroup());
117            json.put(JsonTags.WORKFLOW_RUN, (long) getRun());
118            json.put(JsonTags.WORKFLOW_CONSOLE_URL, getConsoleUrl());
119            json.put(JsonTags.WORKFLOW_ACTIONS, JsonWorkflowAction.toJSONArray(actions));
120            json.put(JsonTags.TO_STRING, toString());
121            return json;
122        }
123    
124        public String getAppPath() {
125            return appPath;
126        }
127    
128        public void setAppPath(String appPath) {
129            this.appPath = appPath;
130        }
131    
132        public String getAppName() {
133            return appName;
134        }
135    
136        public void setAppName(String appName) {
137            this.appName = appName;
138        }
139    
140        public String getId() {
141            return id;
142        }
143    
144        public void setId(String id) {
145            this.id = id;
146        }
147    
148        public void setExternalId(String externalId) {
149            this.externalId = externalId;
150        }
151    
152        public String getExternalId() {
153            return externalId;
154        }
155    
156        public String getConf() {
157            return conf;
158        }
159    
160        public void setConf(String conf) {
161            this.conf = conf;
162        }
163    
164        public Status getStatus() {
165            return status;
166        }
167    
168        public void setStatus(Status status) {
169            this.status = status;
170        }
171    
172        public Date getLastModifiedTime() {
173            return lastModifiedTime;
174        }
175    
176        public void setLastModifiedTime(Date lastModTime) {
177            this.lastModifiedTime = lastModTime;
178        }
179    
180        public Date getCreatedTime() {
181            return createdTime;
182        }
183    
184        public void setCreatedTime(Date createdTime) {
185            this.createdTime = createdTime;
186        }
187    
188        public Date getStartTime() {
189            return startTime;
190        }
191    
192        public void setStartTime(Date startTime) {
193            this.startTime = startTime;
194        }
195    
196        public Date getEndTime() {
197            return endTime;
198        }
199    
200        public void setEndTime(Date endTime) {
201            this.endTime = endTime;
202        }
203    
204        public String getUser() {
205            return user;
206        }
207    
208        public void setUser(String user) {
209            this.user = user;
210        }
211    
212        public String getGroup() {
213            return group;
214        }
215    
216        public void setGroup(String group) {
217            this.group = group;
218        }
219    
220        public int getRun() {
221            return run;
222        }
223    
224        public void setRun(int run) {
225            this.run = run;
226        }
227    
228        /**
229         * Return the workflow job console URL.
230         *
231         * @return the workflow job console URL.
232         */
233        public String getConsoleUrl() {
234            return consoleUrl;
235        }
236    
237        /**
238         * Return the corresponding Action ID, if any.
239         *
240         * @return the coordinator Action Id.
241         */
242        public String getParentId() {
243            return parentId;
244        }
245    
246        /**
247         * Set coordinator action id
248         *
249         * @param parentId : coordinator action id
250         */
251        public void setParentId(String parentId) {
252            this.parentId = parentId;
253        }
254    
255        /**
256         * Set the workflow job console URL.
257         *
258         * @param consoleUrl the workflow job console URL.
259         */
260        public void setConsoleUrl(String consoleUrl) {
261            this.consoleUrl = consoleUrl;
262        }
263    
264        @SuppressWarnings("unchecked")
265        public List<WorkflowAction> getActions() {
266            return (List) actions;
267        }
268    
269        public void setActions(List<? extends JsonWorkflowAction> nodes) {
270            this.actions = (nodes != null) ? nodes : new ArrayList<JsonWorkflowAction>();
271        }
272    
273        @Override
274        public String toString() {
275            return MessageFormat.format("Workflow id[{0}] status[{1}]", getId(), getStatus());
276        }
277    
278        /**
279         * Convert a workflows list into a JSONArray.
280         *
281         * @param workflows workflows list.
282         * @return the corresponding JSON array.
283         */
284        @SuppressWarnings("unchecked")
285        public static JSONArray toJSONArray(List<? extends JsonWorkflowJob> workflows) {
286            JSONArray array = new JSONArray();
287            if (workflows != null) {
288                for (JsonWorkflowJob node : workflows) {
289                    array.add(node.toJSONObject());
290                }
291            }
292            return array;
293        }
294    
295    }