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