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