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