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