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        @Basic
092        @Column(name = "external_id")
093        private String externalId = null;
094    
095        @Basic
096        @Column(name = "external_status")
097        private String externalStatus = null;
098    
099        @Basic
100        @Column(name = "tracker_uri")
101        private String trackerUri = null;
102    
103        @Basic
104        @Column(name = "console_url")
105        private String consoleUrl = null;
106    
107        @Basic
108        @Column(name = "error_code")
109        private String errorCode = null;
110    
111        @Column(name = "error_message")
112        @Lob
113        private String errorMessage = null;
114    
115        public JsonWorkflowAction() {
116        }
117    
118        @SuppressWarnings("unchecked")
119        public JSONObject toJSONObject() {
120            JSONObject json = new JSONObject();
121            json.put(JsonTags.WORKFLOW_ACTION_ID, id);
122            json.put(JsonTags.WORKFLOW_ACTION_NAME, name);
123            json.put(JsonTags.WORKFLOW_ACTION_AUTH, cred);
124            json.put(JsonTags.WORKFLOW_ACTION_TYPE, type);
125            json.put(JsonTags.WORKFLOW_ACTION_CONF, conf);
126            json.put(JsonTags.WORKFLOW_ACTION_STATUS, status.toString());
127            json.put(JsonTags.WORKFLOW_ACTION_RETRIES, (long) retries);
128            json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(startTime));
129            json.put(JsonTags.WORKFLOW_ACTION_END_TIME, JsonUtils.formatDateRfc822(endTime));
130            json.put(JsonTags.WORKFLOW_ACTION_TRANSITION, transition);
131            json.put(JsonTags.WORKFLOW_ACTION_DATA, data);
132            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, externalId);
133            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, externalStatus);
134            json.put(JsonTags.WORKFLOW_ACTION_TRACKER_URI, trackerUri);
135            json.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, consoleUrl);
136            json.put(JsonTags.WORKFLOW_ACTION_ERROR_CODE, errorCode);
137            json.put(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, errorMessage);
138            json.put(JsonTags.TO_STRING, toString());
139            return json;
140        }
141    
142        public String getId() {
143            return id;
144        }
145    
146        public void setId(String id) {
147            this.id = id;
148        }
149    
150        public String getName() {
151            return name;
152        }
153    
154        public void setName(String name) {
155            this.name = name;
156        }
157    
158        public String getCred() {
159            return cred;
160        }
161    
162        public void setCred(String cred) {
163            this.cred = cred;
164        }
165    
166        public String getType() {
167            return type;
168        }
169    
170        public void setType(String type) {
171            this.type = type;
172        }
173    
174        public String getConf() {
175            return conf;
176        }
177    
178        public void setConf(String conf) {
179            this.conf = conf;
180        }
181    
182        public Status getStatus() {
183            return status;
184        }
185    
186        public void setStatus(Status status) {
187            this.status = status;
188        }
189    
190        public int getRetries() {
191            return retries;
192        }
193    
194        public void setRetries(int retries) {
195            this.retries = retries;
196        }
197        
198        public int getUserRetryCount() {
199            return userRetryCount;
200        }
201    
202        public void setUserRetryCount(int retryCount) {
203            this.userRetryCount = retryCount;
204        }
205        
206        public void incrmentUserRetryCount() {
207            this.userRetryCount++;
208        }
209        
210        public int getUserRetryMax() {
211            return userRetryMax;
212        }
213    
214        public void setUserRetryMax(int retryMax) {
215            this.userRetryMax = retryMax;
216        }
217        
218        public int getUserRetryInterval() {
219            return userRetryInterval;
220        }
221    
222        public void setUserRetryInterval(int retryInterval) {
223            this.userRetryInterval = retryInterval;
224        }
225    
226        public Date getStartTime() {
227            return startTime;
228        }
229    
230        public void setStartTime(Date startTime) {
231            this.startTime = startTime;
232        }
233    
234        public Date getEndTime() {
235            return endTime;
236        }
237    
238        public void setEndTime(Date endTime) {
239            this.endTime = endTime;
240        }
241    
242        public String getTransition() {
243            return transition;
244        }
245    
246        public void setTransition(String transition) {
247            this.transition = transition;
248        }
249    
250        public String getData() {
251            return data;
252        }
253    
254        public void setData(String data) {
255            this.data = data;
256        }
257    
258        public String getExternalId() {
259            return externalId;
260        }
261    
262        public void setExternalId(String externalId) {
263            this.externalId = externalId;
264        }
265    
266        public String getExternalStatus() {
267            return externalStatus;
268        }
269    
270        public void setExternalStatus(String externalStatus) {
271            this.externalStatus = externalStatus;
272        }
273    
274        public String getTrackerUri() {
275            return trackerUri;
276        }
277    
278        public void setTrackerUri(String trackerUri) {
279            this.trackerUri = trackerUri;
280        }
281    
282        public String getConsoleUrl() {
283            return consoleUrl;
284        }
285    
286        public void setConsoleUrl(String consoleUrl) {
287            this.consoleUrl = consoleUrl;
288        }
289    
290        public String getErrorCode() {
291            return errorCode;
292        }
293    
294        public String getErrorMessage() {
295            return errorMessage;
296        }
297    
298        public void setErrorInfo(String errorCode, String errorMessage) {
299            this.errorCode = errorCode;
300            this.errorMessage = errorMessage;
301        }
302    
303        @Override
304        public String toString() {
305            return MessageFormat.format("Action name[{0}] status[{1}]", getName(), getStatus());
306        }
307    
308        /**
309         * Convert a nodes list into a JSONArray.
310         *
311         * @param nodes nodes list.
312         * @return the corresponding JSON array.
313         */
314        @SuppressWarnings("unchecked")
315        public static JSONArray toJSONArray(List<? extends JsonWorkflowAction> nodes) {
316            JSONArray array = new JSONArray();
317            for (JsonWorkflowAction node : nodes) {
318                array.add(node.toJSONObject());
319            }
320            return array;
321        }
322    
323    }