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 java.text.MessageFormat;
018    import java.util.Date;
019    import java.util.List;
020    
021    import javax.persistence.Basic;
022    import javax.persistence.Column;
023    import javax.persistence.DiscriminatorColumn;
024    import javax.persistence.DiscriminatorType;
025    import javax.persistence.Entity;
026    import javax.persistence.Id;
027    import javax.persistence.Lob;
028    import javax.persistence.Table;
029    import javax.persistence.Transient;
030    
031    import org.apache.oozie.client.CoordinatorAction;
032    import org.json.simple.JSONArray;
033    import org.json.simple.JSONObject;
034    
035    @Entity
036    @Table(name = "COORD_ACTIONS")
037    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
038    public class JsonCoordinatorAction implements CoordinatorAction, JsonBean {
039    
040        @Id
041        private String id;
042    
043        @Transient
044        private String jobId;
045    
046        @Basic
047        @Column(name = "job_type")
048        private String type;
049    
050        @Transient
051        private Status status = CoordinatorAction.Status.WAITING;
052    
053        @Basic
054        @Column(name = "action_number")
055        private int actionNumber;
056    
057        @Transient
058        private Date createdTime;
059    
060        @Column(name = "created_conf")
061        @Lob
062        private String createdConf;
063    
064        @Transient
065        private String externalId;
066    
067        @Basic
068        @Column(name = "time_out")
069        private int timeOut = 0;
070    
071        @Transient
072        private Date lastModifiedTime;
073    
074        @Transient
075        private Date nominalTime;
076    
077        @Column(name = "run_conf")
078        @Lob
079        private String runConf;
080    
081        @Column(name = "action_xml")
082        @Lob
083        private String actionXml;
084    
085        @Column(name = "missing_dependencies")
086        @Lob
087        private String missingDependencies;
088    
089        @Basic
090        @Column(name = "external_status")
091        private String externalStatus;
092    
093        @Basic
094        @Column(name = "tracker_uri")
095        private String trackerUri;
096    
097        @Basic
098        @Column(name = "console_url")
099        private String consoleUrl;
100    
101        @Basic
102        @Column(name = "error_code")
103        private String errorCode;
104    
105        @Basic
106        @Column(name = "error_message")
107        private String errorMessage;
108    
109        public JsonCoordinatorAction() {
110    
111        }
112    
113        @SuppressWarnings("unchecked")
114        public JSONObject toJSONObject() {
115            JSONObject json = new JSONObject();
116            json.put(JsonTags.COORDINATOR_ACTION_ID, id);
117            json.put(JsonTags.COORDINATOR_JOB_ID, jobId);
118            json.put(JsonTags.COORDINATOR_ACTION_TYPE, type);
119            json.put(JsonTags.COORDINATOR_ACTION_NUMBER, actionNumber);
120            json.put(JsonTags.COORDINATOR_ACTION_CREATED_CONF, createdConf);
121            json.put(JsonTags.COORDINATOR_ACTION_CREATED_TIME, JsonUtils
122                    .formatDateRfc822(createdTime));
123            json.put(JsonTags.COORDINATOR_ACTION_NOMINAL_TIME, JsonUtils
124                    .formatDateRfc822(nominalTime));
125            json.put(JsonTags.COORDINATOR_ACTION_EXTERNALID, externalId);
126            // json.put(JsonTags.COORDINATOR_ACTION_START_TIME, JsonUtils
127            // .formatDateRfc822(startTime));
128            json.put(JsonTags.COORDINATOR_ACTION_STATUS, status.toString());
129            json.put(JsonTags.COORDINATOR_ACTION_RUNTIME_CONF, runConf);
130            json.put(JsonTags.COORDINATOR_ACTION_LAST_MODIFIED_TIME, JsonUtils
131                    .formatDateRfc822(lastModifiedTime));
132            // json.put(JsonTags.COORDINATOR_ACTION_START_TIME, JsonUtils
133            // .formatDateRfc822(startTime));
134            // json.put(JsonTags.COORDINATOR_ACTION_END_TIME, JsonUtils
135            // .formatDateRfc822(endTime));
136            json.put(JsonTags.COORDINATOR_ACTION_MISSING_DEPS, missingDependencies);
137            json.put(JsonTags.COORDINATOR_ACTION_EXTERNAL_STATUS, externalStatus);
138            json.put(JsonTags.COORDINATOR_ACTION_TRACKER_URI, trackerUri);
139            json.put(JsonTags.COORDINATOR_ACTION_CONSOLE_URL, consoleUrl);
140            json.put(JsonTags.COORDINATOR_ACTION_ERROR_CODE, errorCode);
141            json.put(JsonTags.COORDINATOR_ACTION_ERROR_MESSAGE, errorMessage);
142            json.put(JsonTags.TO_STRING, toString());
143            return json;
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 String getJobId() {
155            return jobId;
156        }
157    
158        public void setJobId(String id) {
159            this.jobId = id;
160        }
161    
162        public String getType() {
163            return type;
164        }
165    
166        public void setType(String type) {
167            this.type = type;
168        }
169    
170        public String getExternalId() {
171            return externalId;
172        }
173    
174        public void setExternalId(String extId) {
175            this.externalId = extId;
176        }
177    
178    
179        public void setActionNumber(int actionNumber) {
180            this.actionNumber = actionNumber;
181        }
182    
183        public int getActionNumber() {
184            return actionNumber;
185        }
186    
187        public String getCreatedConf() {
188            return createdConf;
189        }
190    
191        public void setCreatedConf(String createdConf) {
192            this.createdConf = createdConf;
193        }
194    
195        public void setCreatedTime(Date createdTime) {
196            this.createdTime = createdTime;
197        }
198    
199        public Date getCreatedTime() {
200            return createdTime;
201        }
202    
203        public Status getStatus() {
204            return status;
205        }
206    
207        public void setStatus(Status status) {
208            this.status = status;
209        }
210    
211        public void setLastModifiedTime(Date lastModifiedTime) {
212            this.lastModifiedTime = lastModifiedTime;
213        }
214    
215        public Date getLastModifiedTime() {
216            return lastModifiedTime;
217        }
218    
219        public void setRunConf(String runConf) {
220            this.runConf = runConf;
221        }
222    
223        public String getRunConf() {
224            return runConf;
225        }
226    
227        public void setMissingDependencies(String missingDependencies) {
228            this.missingDependencies = missingDependencies;
229        }
230    
231        public String getMissingDependencies() {
232            return missingDependencies;
233        }
234    
235        public String getExternalStatus() {
236            return externalStatus;
237        }
238    
239        public void setExternalStatus(String externalStatus) {
240            this.externalStatus = externalStatus;
241        }
242    
243        public String getTrackerUri() {
244            return trackerUri;
245        }
246    
247        public void setTrackerUri(String trackerUri) {
248            this.trackerUri = trackerUri;
249        }
250    
251        public String getConsoleUrl() {
252            return consoleUrl;
253        }
254    
255        public void setConsoleUrl(String consoleUrl) {
256            this.consoleUrl = consoleUrl;
257        }
258    
259        public String getErrorCode() {
260            return errorCode;
261        }
262    
263        public String getErrorMessage() {
264            return errorMessage;
265        }
266    
267        public void setErrorInfo(String errorCode, String errorMessage) {
268            this.errorCode = errorCode;
269            this.errorMessage = errorMessage;
270        }
271    
272        public String getActionXml() {
273            return actionXml;
274        }
275    
276        public void setActionXml(String actionXml) {
277            this.actionXml = actionXml;
278        }
279    
280        @Override
281        public String toString() {
282            return MessageFormat.format("WorkflowAction name[{0}] status[{1}]",
283                                        getId(), getStatus());
284        }
285    
286        public Date getNominalTime() {
287            return nominalTime;
288        }
289    
290        public void setNominalTime(Date nominalTime) {
291            this.nominalTime = nominalTime;
292        }
293    
294        public int getTimeOut() {
295            return timeOut;
296        }
297    
298        public void setTimeOut(int timeOut) {
299            this.timeOut = timeOut;
300        }
301    
302    
303        public void setErrorCode(String errorCode) {
304            this.errorCode = errorCode;
305        }
306    
307        public void setErrorMessage(String errorMessage) {
308            this.errorMessage = errorMessage;
309        }
310    
311        /**
312         * Convert a nodes list into a JSONArray.
313         *
314         * @param actions nodes list.
315         * @return the corresponding JSON array.
316         */
317        @SuppressWarnings("unchecked")
318        public static JSONArray toJSONArray(List<? extends JsonCoordinatorAction> actions) {
319            JSONArray array = new JSONArray();
320            for (JsonCoordinatorAction action : actions) {
321                array.add(action.toJSONObject());
322            }
323            return array;
324        }
325    
326        /*
327         * (non-Javadoc)
328         *
329         * @see java.lang.Object#hashCode()
330         */
331        @Override
332        public int hashCode() {
333            final int prime = 31;
334            int result = 1;
335            result = prime * result + ((id == null) ? 0 : id.hashCode());
336            return result;
337        }
338    
339        /*
340         * (non-Javadoc)
341         *
342         * @see java.lang.Object#equals(java.lang.Object)
343         */
344        @Override
345        public boolean equals(Object obj) {
346            if (this == obj) {
347                return true;
348            }
349            if (obj == null) {
350                return false;
351            }
352            if (getClass() != obj.getClass()) {
353                return false;
354            }
355            JsonCoordinatorAction other = (JsonCoordinatorAction) obj;
356            if (id == null) {
357                if (other.id != null) {
358                    return false;
359                }
360            }
361            else if (!id.equals(other.id)) {
362                return false;
363            }
364            return true;
365        }
366    }