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