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.json.simple.JSONArray;
022    import org.json.simple.JSONObject;
023    
024    import java.text.MessageFormat;
025    import java.util.Date;
026    import java.util.List;
027    
028    import javax.persistence.*;
029    
030    /**
031     * Json Bean that represents an Oozie workflow node.
032     */
033    @Entity
034    @Table(name = "WF_ACTIONS")
035    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
036    
037    public class JsonWorkflowAction implements WorkflowAction, JsonBean {
038        @Id
039        private String id;
040    
041        @Basic
042        @Column(name = "name")
043        private String name = null;
044    
045        @Basic
046        @Column(name = "cred")
047        private String cred = null;
048    
049        @Basic
050        @Column(name = "type")
051        private String type = null;
052    
053        @Basic
054        @Column(name = "conf")
055        @Lob
056        private String conf = null;
057    
058        @Transient
059        private Status status = WorkflowAction.Status.PREP;
060    
061        @Basic
062        @Column(name = "retries")
063        private int retries;
064        
065        @Basic
066        @Column(name = "user_retry_count")
067        private int userRetryCount;
068        
069        @Basic
070        @Column(name = "user_retry_max")
071        private int userRetryMax;
072        
073        @Basic
074        @Column(name = "user_retry_interval")
075        private int userRetryInterval;
076    
077        @Transient
078        private Date startTime;
079    
080        @Transient
081        private Date endTime;
082    
083        @Basic
084        @Column(name = "transition")
085        private String transition = null;
086    
087        @Column(name = "data")
088        @Lob
089        private String data = null;
090    
091        @Column(name = "stats")
092        @Lob
093        private String stats = null;
094    
095        @Column(name = "external_child_ids")
096        @Lob
097        private String externalChildIDs = null;
098    
099        @Basic
100        @Column(name = "external_id")
101        private String externalId = null;
102    
103        @Basic
104        @Column(name = "external_status")
105        private String externalStatus = null;
106    
107        @Basic
108        @Column(name = "tracker_uri")
109        private String trackerUri = null;
110    
111        @Basic
112        @Column(name = "console_url")
113        private String consoleUrl = null;
114    
115        @Basic
116        @Column(name = "error_code")
117        private String errorCode = null;
118    
119        @Column(name = "error_message")
120        @Lob
121        private String errorMessage = null;
122    
123        public JsonWorkflowAction() {
124        }
125    
126        @SuppressWarnings("unchecked")
127        public JSONObject toJSONObject() {
128            return toJSONObject("GMT");
129        }
130        
131        @SuppressWarnings("unchecked")
132        public JSONObject toJSONObject(String timeZoneId) {
133            JSONObject json = new JSONObject();
134            json.put(JsonTags.WORKFLOW_ACTION_ID, id);
135            json.put(JsonTags.WORKFLOW_ACTION_NAME, name);
136            json.put(JsonTags.WORKFLOW_ACTION_AUTH, cred);
137            json.put(JsonTags.WORKFLOW_ACTION_TYPE, type);
138            json.put(JsonTags.WORKFLOW_ACTION_CONF, conf);
139            json.put(JsonTags.WORKFLOW_ACTION_STATUS, status.toString());
140            json.put(JsonTags.WORKFLOW_ACTION_RETRIES, (long) retries);
141            json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(startTime, timeZoneId));
142            json.put(JsonTags.WORKFLOW_ACTION_END_TIME, JsonUtils.formatDateRfc822(endTime, timeZoneId));
143            json.put(JsonTags.WORKFLOW_ACTION_TRANSITION, transition);
144            json.put(JsonTags.WORKFLOW_ACTION_DATA, data);
145            json.put(JsonTags.WORKFLOW_ACTION_STATS, stats);
146            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_CHILD_IDS, externalChildIDs);
147            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, externalId);
148            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, externalStatus);
149            json.put(JsonTags.WORKFLOW_ACTION_TRACKER_URI, trackerUri);
150            json.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, consoleUrl);
151            json.put(JsonTags.WORKFLOW_ACTION_ERROR_CODE, errorCode);
152            json.put(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, errorMessage);
153            json.put(JsonTags.TO_STRING, toString());
154            return json;
155        }
156    
157        public String getId() {
158            return id;
159        }
160    
161        public void setId(String id) {
162            this.id = id;
163        }
164    
165        public String getName() {
166            return name;
167        }
168    
169        public void setName(String name) {
170            this.name = name;
171        }
172    
173        public String getCred() {
174            return cred;
175        }
176    
177        public void setCred(String cred) {
178            this.cred = cred;
179        }
180    
181        public String getType() {
182            return type;
183        }
184    
185        public void setType(String type) {
186            this.type = type;
187        }
188    
189        public String getConf() {
190            return conf;
191        }
192    
193        public void setConf(String conf) {
194            this.conf = conf;
195        }
196    
197        public Status getStatus() {
198            return status;
199        }
200    
201        public void setStatus(Status status) {
202            this.status = status;
203        }
204    
205        public int getRetries() {
206            return retries;
207        }
208    
209        public void setRetries(int retries) {
210            this.retries = retries;
211        }
212        
213        public int getUserRetryCount() {
214            return userRetryCount;
215        }
216    
217        public void setUserRetryCount(int retryCount) {
218            this.userRetryCount = retryCount;
219        }
220        
221        public void incrmentUserRetryCount() {
222            this.userRetryCount++;
223        }
224        
225        public int getUserRetryMax() {
226            return userRetryMax;
227        }
228    
229        public void setUserRetryMax(int retryMax) {
230            this.userRetryMax = retryMax;
231        }
232        
233        public int getUserRetryInterval() {
234            return userRetryInterval;
235        }
236    
237        public void setUserRetryInterval(int retryInterval) {
238            this.userRetryInterval = retryInterval;
239        }
240    
241        public Date getStartTime() {
242            return startTime;
243        }
244    
245        public void setStartTime(Date startTime) {
246            this.startTime = startTime;
247        }
248    
249        public Date getEndTime() {
250            return endTime;
251        }
252    
253        public void setEndTime(Date endTime) {
254            this.endTime = endTime;
255        }
256    
257        public String getTransition() {
258            return transition;
259        }
260    
261        public void setTransition(String transition) {
262            this.transition = transition;
263        }
264    
265        public String getData() {
266            return data;
267        }
268    
269        public void setData(String data) {
270            this.data = data;
271        }
272    
273        public String getStats() {
274            return stats;
275        }
276    
277        public void setStats(String stats) {
278            this.stats = stats;
279        }
280    
281        public String getExternalChildIDs() {
282            return externalChildIDs;
283        }
284    
285        public void setExternalChildIDs(String externalChildIDs) {
286            this.externalChildIDs = externalChildIDs;
287        }
288    
289        public String getExternalId() {
290            return externalId;
291        }
292    
293        public void setExternalId(String externalId) {
294            this.externalId = externalId;
295        }
296    
297        public String getExternalStatus() {
298            return externalStatus;
299        }
300    
301        public void setExternalStatus(String externalStatus) {
302            this.externalStatus = externalStatus;
303        }
304    
305        public String getTrackerUri() {
306            return trackerUri;
307        }
308    
309        public void setTrackerUri(String trackerUri) {
310            this.trackerUri = trackerUri;
311        }
312    
313        public String getConsoleUrl() {
314            return consoleUrl;
315        }
316    
317        public void setConsoleUrl(String consoleUrl) {
318            this.consoleUrl = consoleUrl;
319        }
320    
321        public String getErrorCode() {
322            return errorCode;
323        }
324    
325        public String getErrorMessage() {
326            return errorMessage;
327        }
328    
329        public void setErrorInfo(String errorCode, String errorMessage) {
330            this.errorCode = errorCode;
331            this.errorMessage = errorMessage;
332        }
333    
334        @Override
335        public String toString() {
336            return MessageFormat.format("Action name[{0}] status[{1}]", getName(), getStatus());
337        }
338    
339        /**
340         * Convert a nodes list into a JSONArray.
341         *
342         * @param nodes nodes list.
343         * @param timeZoneId time zone to use for dates in the JSON array.
344         * @return the corresponding JSON array.
345         */
346        @SuppressWarnings("unchecked")
347        public static JSONArray toJSONArray(List<? extends JsonWorkflowAction> nodes, String timeZoneId) {
348            JSONArray array = new JSONArray();
349            for (JsonWorkflowAction node : nodes) {
350                array.add(node.toJSONObject(timeZoneId));
351            }
352            return array;
353        }
354    
355    }