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            JSONObject json = new JSONObject();
119            json.put(JsonTags.COORDINATOR_ACTION_ID, id);
120            json.put(JsonTags.COORDINATOR_JOB_ID, jobId);
121            json.put(JsonTags.COORDINATOR_ACTION_TYPE, type);
122            json.put(JsonTags.COORDINATOR_ACTION_NUMBER, actionNumber);
123            json.put(JsonTags.COORDINATOR_ACTION_CREATED_CONF, createdConf);
124            json.put(JsonTags.COORDINATOR_ACTION_CREATED_TIME, JsonUtils
125                    .formatDateRfc822(createdTime));
126            json.put(JsonTags.COORDINATOR_ACTION_NOMINAL_TIME, JsonUtils
127                    .formatDateRfc822(nominalTime));
128            json.put(JsonTags.COORDINATOR_ACTION_EXTERNALID, externalId);
129            // json.put(JsonTags.COORDINATOR_ACTION_START_TIME, JsonUtils
130            // .formatDateRfc822(startTime));
131            json.put(JsonTags.COORDINATOR_ACTION_STATUS, status.toString());
132            json.put(JsonTags.COORDINATOR_ACTION_RUNTIME_CONF, runConf);
133            json.put(JsonTags.COORDINATOR_ACTION_LAST_MODIFIED_TIME, JsonUtils
134                    .formatDateRfc822(lastModifiedTime));
135            // json.put(JsonTags.COORDINATOR_ACTION_START_TIME, JsonUtils
136            // .formatDateRfc822(startTime));
137            // json.put(JsonTags.COORDINATOR_ACTION_END_TIME, JsonUtils
138            // .formatDateRfc822(endTime));
139            json.put(JsonTags.COORDINATOR_ACTION_MISSING_DEPS, missingDependencies);
140            json.put(JsonTags.COORDINATOR_ACTION_EXTERNAL_STATUS, externalStatus);
141            json.put(JsonTags.COORDINATOR_ACTION_TRACKER_URI, trackerUri);
142            json.put(JsonTags.COORDINATOR_ACTION_CONSOLE_URL, consoleUrl);
143            json.put(JsonTags.COORDINATOR_ACTION_ERROR_CODE, errorCode);
144            json.put(JsonTags.COORDINATOR_ACTION_ERROR_MESSAGE, errorMessage);
145            json.put(JsonTags.TO_STRING, toString());
146            return json;
147        }
148    
149        public String getId() {
150            return id;
151        }
152    
153        public void setId(String id) {
154            this.id = id;
155        }
156    
157        public String getJobId() {
158            return jobId;
159        }
160    
161        public void setJobId(String id) {
162            this.jobId = id;
163        }
164    
165        public String getType() {
166            return type;
167        }
168    
169        public void setType(String type) {
170            this.type = type;
171        }
172    
173        public String getExternalId() {
174            return externalId;
175        }
176    
177        public void setExternalId(String extId) {
178            this.externalId = extId;
179        }
180    
181    
182        public void setActionNumber(int actionNumber) {
183            this.actionNumber = actionNumber;
184        }
185    
186        public int getActionNumber() {
187            return actionNumber;
188        }
189    
190        public String getCreatedConf() {
191            return createdConf;
192        }
193    
194        public void setCreatedConf(String createdConf) {
195            this.createdConf = createdConf;
196        }
197    
198        public void setCreatedTime(Date createdTime) {
199            this.createdTime = createdTime;
200        }
201    
202        public Date getCreatedTime() {
203            return createdTime;
204        }
205    
206        public Status getStatus() {
207            return status;
208        }
209    
210        public void setStatus(Status status) {
211            this.status = status;
212        }
213    
214        public void setLastModifiedTime(Date lastModifiedTime) {
215            this.lastModifiedTime = lastModifiedTime;
216        }
217    
218        public Date getLastModifiedTime() {
219            return lastModifiedTime;
220        }
221    
222        public void setRunConf(String runConf) {
223            this.runConf = runConf;
224        }
225    
226        public String getRunConf() {
227            return runConf;
228        }
229    
230        public void setMissingDependencies(String missingDependencies) {
231            this.missingDependencies = missingDependencies;
232        }
233    
234        public String getMissingDependencies() {
235            return missingDependencies;
236        }
237    
238        public String getExternalStatus() {
239            return externalStatus;
240        }
241    
242        public void setExternalStatus(String externalStatus) {
243            this.externalStatus = externalStatus;
244        }
245    
246        public String getTrackerUri() {
247            return trackerUri;
248        }
249    
250        public void setTrackerUri(String trackerUri) {
251            this.trackerUri = trackerUri;
252        }
253    
254        public String getConsoleUrl() {
255            return consoleUrl;
256        }
257    
258        public void setConsoleUrl(String consoleUrl) {
259            this.consoleUrl = consoleUrl;
260        }
261    
262        public String getErrorCode() {
263            return errorCode;
264        }
265    
266        public String getErrorMessage() {
267            return errorMessage;
268        }
269    
270        public void setErrorInfo(String errorCode, String errorMessage) {
271            this.errorCode = errorCode;
272            this.errorMessage = errorMessage;
273        }
274    
275        public String getActionXml() {
276            return actionXml;
277        }
278    
279        public void setActionXml(String actionXml) {
280            this.actionXml = actionXml;
281        }
282    
283        @Override
284        public String toString() {
285            return MessageFormat.format("WorkflowAction name[{0}] status[{1}]",
286                                        getId(), getStatus());
287        }
288    
289        public Date getNominalTime() {
290            return nominalTime;
291        }
292    
293        public void setNominalTime(Date nominalTime) {
294            this.nominalTime = nominalTime;
295        }
296    
297        public int getTimeOut() {
298            return timeOut;
299        }
300    
301        public void setTimeOut(int timeOut) {
302            this.timeOut = timeOut;
303        }
304    
305    
306        public void setErrorCode(String errorCode) {
307            this.errorCode = errorCode;
308        }
309    
310        public void setErrorMessage(String errorMessage) {
311            this.errorMessage = errorMessage;
312        }
313    
314        /**
315         * Convert a nodes list into a JSONArray.
316         *
317         * @param actions nodes list.
318         * @return the corresponding JSON array.
319         */
320        @SuppressWarnings("unchecked")
321        public static JSONArray toJSONArray(List<? extends JsonCoordinatorAction> actions) {
322            JSONArray array = new JSONArray();
323            for (JsonCoordinatorAction action : actions) {
324                array.add(action.toJSONObject());
325            }
326            return array;
327        }
328    
329        /*
330         * (non-Javadoc)
331         *
332         * @see java.lang.Object#hashCode()
333         */
334        @Override
335        public int hashCode() {
336            final int prime = 31;
337            int result = 1;
338            result = prime * result + ((id == null) ? 0 : id.hashCode());
339            return result;
340        }
341    
342        /*
343         * (non-Javadoc)
344         *
345         * @see java.lang.Object#equals(java.lang.Object)
346         */
347        @Override
348        public boolean equals(Object obj) {
349            if (this == obj) {
350                return true;
351            }
352            if (obj == null) {
353                return false;
354            }
355            if (getClass() != obj.getClass()) {
356                return false;
357            }
358            JsonCoordinatorAction other = (JsonCoordinatorAction) obj;
359            if (id == null) {
360                if (other.id != null) {
361                    return false;
362                }
363            }
364            else if (!id.equals(other.id)) {
365                return false;
366            }
367            return true;
368        }
369    }