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.json.simple.JSONArray;
019    import org.json.simple.JSONObject;
020    
021    import java.text.MessageFormat;
022    import java.util.Date;
023    import java.util.List;
024    
025    import javax.persistence.*;
026    
027    /**
028     * Json Bean that represents an Oozie workflow node.
029     */
030    @Entity
031    @Table(name = "WF_ACTIONS")
032    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
033    
034    public class JsonWorkflowAction implements WorkflowAction, JsonBean {
035        @Id
036        private String id;
037    
038        @Basic
039        @Column(name = "name")
040        private String name = null;
041    
042        @Basic
043        @Column(name = "type")
044        private String type = null;
045    
046        @Basic
047        @Column(name = "conf")
048        @Lob
049        private String conf = null;
050    
051        @Transient
052        private Status status = WorkflowAction.Status.PREP;
053    
054        @Basic
055        @Column(name = "retries")
056        private int retries;
057    
058        @Transient
059        private Date startTime;
060    
061        @Transient
062        private Date endTime;
063    
064        @Basic
065        @Column(name = "transition")
066        private String transition = null;
067    
068        @Column(name = "data")
069        @Lob
070        private String data = null;
071    
072        @Basic
073        @Column(name = "external_id")
074        private String externalId = null;
075    
076        @Basic
077        @Column(name = "external_status")
078        private String externalStatus = null;
079    
080        @Basic
081        @Column(name = "tracker_uri")
082        private String trackerUri = null;
083    
084        @Basic
085        @Column(name = "console_url")
086        private String consoleUrl = null;
087    
088        @Basic
089        @Column(name = "error_code")
090        private String errorCode = null;
091    
092        @Column(name = "error_message")
093        @Lob
094        private String errorMessage = null;
095    
096        public JsonWorkflowAction() {
097        }
098    
099        @SuppressWarnings("unchecked")
100        public JSONObject toJSONObject() {
101            JSONObject json = new JSONObject();
102            json.put(JsonTags.WORKFLOW_ACTION_ID, id);
103            json.put(JsonTags.WORKFLOW_ACTION_NAME, name);
104            json.put(JsonTags.WORKFLOW_ACTION_TYPE, type);
105            json.put(JsonTags.WORKFLOW_ACTION_CONF, conf);
106            json.put(JsonTags.WORKFLOW_ACTION_STATUS, status.toString());
107            json.put(JsonTags.WORKFLOW_ACTION_RETRIES, (long) retries);
108            json.put(JsonTags.WORKFLOW_ACTION_START_TIME, JsonUtils.formatDateRfc822(startTime));
109            json.put(JsonTags.WORKFLOW_ACTION_END_TIME, JsonUtils.formatDateRfc822(endTime));
110            json.put(JsonTags.WORKFLOW_ACTION_TRANSITION, transition);
111            json.put(JsonTags.WORKFLOW_ACTION_DATA, data);
112            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, externalId);
113            json.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, externalStatus);
114            json.put(JsonTags.WORKFLOW_ACTION_TRACKER_URI, trackerUri);
115            json.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, consoleUrl);
116            json.put(JsonTags.WORKFLOW_ACTION_ERROR_CODE, errorCode);
117            json.put(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, errorMessage);
118            json.put(JsonTags.TO_STRING, toString());
119            return json;
120        }
121    
122        public String getId() {
123            return id;
124        }
125    
126        public void setId(String id) {
127            this.id = id;
128        }
129    
130        public String getName() {
131            return name;
132        }
133    
134        public void setName(String name) {
135            this.name = name;
136        }
137    
138        public String getType() {
139            return type;
140        }
141    
142        public void setType(String type) {
143            this.type = type;
144        }
145    
146        public String getConf() {
147            return conf;
148        }
149    
150        public void setConf(String conf) {
151            this.conf = conf;
152        }
153    
154        public Status getStatus() {
155            return status;
156        }
157    
158        public void setStatus(Status status) {
159            this.status = status;
160        }
161    
162        public int getRetries() {
163            return retries;
164        }
165    
166        public void setRetries(int retries) {
167            this.retries = retries;
168        }
169    
170        public Date getStartTime() {
171            return startTime;
172        }
173    
174        public void setStartTime(Date startTime) {
175            this.startTime = startTime;
176        }
177    
178        public Date getEndTime() {
179            return endTime;
180        }
181    
182        public void setEndTime(Date endTime) {
183            this.endTime = endTime;
184        }
185    
186        public String getTransition() {
187            return transition;
188        }
189    
190        public void setTransition(String transition) {
191            this.transition = transition;
192        }
193    
194        public String getData() {
195            return data;
196        }
197    
198        public void setData(String data) {
199            this.data = data;
200        }
201    
202        public String getExternalId() {
203            return externalId;
204        }
205    
206        public void setExternalId(String externalId) {
207            this.externalId = externalId;
208        }
209    
210        public String getExternalStatus() {
211            return externalStatus;
212        }
213    
214        public void setExternalStatus(String externalStatus) {
215            this.externalStatus = externalStatus;
216        }
217    
218        public String getTrackerUri() {
219            return trackerUri;
220        }
221    
222        public void setTrackerUri(String trackerUri) {
223            this.trackerUri = trackerUri;
224        }
225    
226        public String getConsoleUrl() {
227            return consoleUrl;
228        }
229    
230        public void setConsoleUrl(String consoleUrl) {
231            this.consoleUrl = consoleUrl;
232        }
233    
234        public String getErrorCode() {
235            return errorCode;
236        }
237    
238        public String getErrorMessage() {
239            return errorMessage;
240        }
241    
242        public void setErrorInfo(String errorCode, String errorMessage) {
243            this.errorCode = errorCode;
244            this.errorMessage = errorMessage;
245        }
246    
247        @Override
248        public String toString() {
249            return MessageFormat.format("Action name[{0}] status[{1}]", getName(), getStatus());
250        }
251    
252        /**
253         * Convert a nodes list into a JSONArray.
254         *
255         * @param nodes nodes list.
256         * @return the corresponding JSON array.
257         */
258        @SuppressWarnings("unchecked")
259        public static JSONArray toJSONArray(List<? extends JsonWorkflowAction> nodes) {
260            JSONArray array = new JSONArray();
261            for (JsonWorkflowAction node : nodes) {
262                array.add(node.toJSONObject());
263            }
264            return array;
265        }
266    
267    }