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_ACL, getAcl());
118            json.put(JsonTags.WORKFLOW_RUN, (long) getRun());
119            json.put(JsonTags.WORKFLOW_CONSOLE_URL, getConsoleUrl());
120            json.put(JsonTags.WORKFLOW_ACTIONS, JsonWorkflowAction.toJSONArray(actions));
121            json.put(JsonTags.TO_STRING, toString());
122            return json;
123        }
124    
125        public String getAppPath() {
126            return appPath;
127        }
128    
129        public void setAppPath(String appPath) {
130            this.appPath = appPath;
131        }
132    
133        public String getAppName() {
134            return appName;
135        }
136    
137        public void setAppName(String appName) {
138            this.appName = appName;
139        }
140    
141        public String getId() {
142            return id;
143        }
144    
145        public void setId(String id) {
146            this.id = id;
147        }
148    
149        public void setExternalId(String externalId) {
150            this.externalId = externalId;
151        }
152    
153        public String getExternalId() {
154            return externalId;
155        }
156    
157        public String getConf() {
158            return conf;
159        }
160    
161        public void setConf(String conf) {
162            this.conf = conf;
163        }
164    
165        public Status getStatus() {
166            return status;
167        }
168    
169        public void setStatus(Status status) {
170            this.status = status;
171        }
172    
173        public Date getLastModifiedTime() {
174            return lastModifiedTime;
175        }
176    
177        public void setLastModifiedTime(Date lastModTime) {
178            this.lastModifiedTime = lastModTime;
179        }
180    
181        public Date getCreatedTime() {
182            return createdTime;
183        }
184    
185        public void setCreatedTime(Date createdTime) {
186            this.createdTime = createdTime;
187        }
188    
189        public Date getStartTime() {
190            return startTime;
191        }
192    
193        public void setStartTime(Date startTime) {
194            this.startTime = startTime;
195        }
196    
197        public Date getEndTime() {
198            return endTime;
199        }
200    
201        public void setEndTime(Date endTime) {
202            this.endTime = endTime;
203        }
204    
205        public String getUser() {
206            return user;
207        }
208    
209        public void setUser(String user) {
210            this.user = user;
211        }
212    
213        public String getGroup() {
214            return group;
215        }
216    
217        @Override
218        public String getAcl() {
219            return getGroup();
220        }
221    
222        public void setGroup(String group) {
223            this.group = group;
224        }
225    
226        public int getRun() {
227            return run;
228        }
229    
230        public void setRun(int run) {
231            this.run = run;
232        }
233    
234        /**
235         * Return the workflow job console URL.
236         *
237         * @return the workflow job console URL.
238         */
239        public String getConsoleUrl() {
240            return consoleUrl;
241        }
242    
243        /**
244         * Return the corresponding Action ID, if any.
245         *
246         * @return the coordinator Action Id.
247         */
248        public String getParentId() {
249            return parentId;
250        }
251    
252        /**
253         * Set coordinator action id
254         *
255         * @param parentId : coordinator action id
256         */
257        public void setParentId(String parentId) {
258            this.parentId = parentId;
259        }
260    
261        /**
262         * Set the workflow job console URL.
263         *
264         * @param consoleUrl the workflow job console URL.
265         */
266        public void setConsoleUrl(String consoleUrl) {
267            this.consoleUrl = consoleUrl;
268        }
269    
270        @SuppressWarnings("unchecked")
271        public List<WorkflowAction> getActions() {
272            return (List) actions;
273        }
274    
275        public void setActions(List<? extends JsonWorkflowAction> nodes) {
276            this.actions = (nodes != null) ? nodes : new ArrayList<JsonWorkflowAction>();
277        }
278    
279        @Override
280        public String toString() {
281            return MessageFormat.format("Workflow id[{0}] status[{1}]", getId(), getStatus());
282        }
283    
284        /**
285         * Convert a workflows list into a JSONArray.
286         *
287         * @param workflows workflows list.
288         * @return the corresponding JSON array.
289         */
290        @SuppressWarnings("unchecked")
291        public static JSONArray toJSONArray(List<? extends JsonWorkflowJob> workflows) {
292            JSONArray array = new JSONArray();
293            if (workflows != null) {
294                for (JsonWorkflowJob node : workflows) {
295                    array.add(node.toJSONObject());
296                }
297            }
298            return array;
299        }
300    
301    }