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 JSONObject toJSONObject() {
096            JSONObject json = new JSONObject();
097            json.put(JsonTags.WORKFLOW_APP_PATH, appPath);
098            json.put(JsonTags.WORKFLOW_APP_NAME, appName);
099            json.put(JsonTags.WORKFLOW_ID, id);
100            json.put(JsonTags.WORKFLOW_EXTERNAL_ID, externalId);
101            json.put(JsonTags.WORKFLOW_CONF, conf);
102            json.put(JsonTags.WORKFLOW_STATUS, status.toString());
103            json.put(JsonTags.WORKFLOW_LAST_MOD_TIME, JsonUtils.formatDateRfc822(lastModifiedTime));
104            json.put(JsonTags.WORKFLOW_CREATED_TIME, JsonUtils.formatDateRfc822(createdTime));
105            json.put(JsonTags.WORKFLOW_START_TIME, JsonUtils.formatDateRfc822(startTime));
106            json.put(JsonTags.WORKFLOW_END_TIME, JsonUtils.formatDateRfc822(endTime));
107            json.put(JsonTags.WORKFLOW_USER, user);
108            json.put(JsonTags.WORKFLOW_GROUP, group);
109            json.put(JsonTags.WORKFLOW_RUN, (long) run);
110            json.put(JsonTags.WORKFLOW_CONSOLE_URL, consoleUrl);
111            json.put(JsonTags.WORKFLOW_ACTIONS, JsonWorkflowAction.toJSONArray(actions));
112            json.put(JsonTags.TO_STRING, toString());
113            return json;
114        }
115    
116        public String getAppPath() {
117            return appPath;
118        }
119    
120        public void setAppPath(String appPath) {
121            this.appPath = appPath;
122        }
123    
124        public String getAppName() {
125            return appName;
126        }
127    
128        public void setAppName(String appName) {
129            this.appName = appName;
130        }
131    
132        public String getId() {
133            return id;
134        }
135    
136        public void setId(String id) {
137            this.id = id;
138        }
139    
140        public void setExternalId(String externalId) {
141            this.externalId = externalId;
142        }
143    
144        public String getExternalId() {
145            return externalId;
146        }
147    
148        public String getConf() {
149            return conf;
150        }
151    
152        public void setConf(String conf) {
153            this.conf = conf;
154        }
155    
156        public Status getStatus() {
157            return status;
158        }
159    
160        public void setStatus(Status status) {
161            this.status = status;
162        }
163    
164        public Date getLastModifiedTime() {
165            return lastModifiedTime;
166        }
167    
168        public void setLastModifiedTime(Date lastModTime) {
169            this.lastModifiedTime = lastModTime;
170        }
171    
172        public Date getCreatedTime() {
173            return createdTime;
174        }
175    
176        public void setCreatedTime(Date createdTime) {
177            this.createdTime = createdTime;
178        }
179    
180        public Date getStartTime() {
181            return startTime;
182        }
183    
184        public void setStartTime(Date startTime) {
185            this.startTime = startTime;
186        }
187    
188        public Date getEndTime() {
189            return endTime;
190        }
191    
192        public void setEndTime(Date endTime) {
193            this.endTime = endTime;
194        }
195    
196        public String getUser() {
197            return user;
198        }
199    
200        public void setUser(String user) {
201            this.user = user;
202        }
203    
204        public String getGroup() {
205            return group;
206        }
207    
208        public void setGroup(String group) {
209            this.group = group;
210        }
211    
212        public int getRun() {
213            return run;
214        }
215    
216        public void setRun(int run) {
217            this.run = run;
218        }
219    
220        /**
221         * Return the workflow job console URL.
222         *
223         * @return the workflow job console URL.
224         */
225        public String getConsoleUrl() {
226            return consoleUrl;
227        }
228    
229        /**
230         * Set the workflow job console URL.
231         *
232         * @param consoleUrl the workflow job console URL.
233         */
234        public void setConsoleUrl(String consoleUrl) {
235            this.consoleUrl = consoleUrl;
236        }
237    
238        @SuppressWarnings("unchecked")
239        public List<WorkflowAction> getActions() {
240            return (List) actions;
241        }
242    
243        public void setActions(List<? extends JsonWorkflowAction> nodes) {
244            this.actions = (nodes != null) ? nodes : new ArrayList<JsonWorkflowAction>();
245        }
246    
247        @Override
248        public String toString() {
249            return MessageFormat.format("Workflow id[{0}] status[{1}]", getId(), getStatus());
250        }
251    
252        /**
253         * Convert a workflows list into a JSONArray.
254         *
255         * @param workflows workflows list.
256         * @return the corresponding JSON array.
257         */
258        @SuppressWarnings("unchecked")
259        public static JSONArray toJSONArray(List<? extends JsonWorkflowJob> workflows) {
260            JSONArray array = new JSONArray();
261            if (workflows != null) {
262                for (JsonWorkflowJob node : workflows) {
263                    array.add(node.toJSONObject());
264                }
265            }
266            return array;
267        }
268    
269    }