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.ArrayList;
022import java.util.Date;
023import java.util.List;
024
025import javax.persistence.Basic;
026import javax.persistence.Column;
027import javax.persistence.DiscriminatorColumn;
028import javax.persistence.DiscriminatorType;
029import javax.persistence.Entity;
030import javax.persistence.Id;
031import javax.persistence.Lob;
032import javax.persistence.Table;
033import javax.persistence.Transient;
034
035import org.apache.oozie.client.BundleJob;
036import org.apache.oozie.client.CoordinatorJob;
037import org.apache.oozie.client.Job;
038import org.json.simple.JSONArray;
039import org.json.simple.JSONObject;
040
041@Entity
042@Table(name = "BUNDLE_JOBS")
043@DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
044public class JsonBundleJob implements BundleJob, JsonBean {
045    @Id
046    private String id;
047
048    @Basic
049    @Column(name = "app_path")
050    private String appPath = null;
051
052    @Basic
053    @Column(name = "app_name")
054    private String appName = null;
055
056    @Basic
057    @Column(name = "external_id")
058    private String externalId = null;
059
060    @Column(name = "conf")
061    @Lob
062    private String conf = null;
063
064    @Transient
065    private Status status = Job.Status.PREP;
066
067    @Transient
068    private Date kickoffTime;
069
070    @Transient
071    private Date startTime;
072
073    @Transient
074    private Date endTime;
075
076    @Transient
077    private Date pauseTime;
078
079    @Transient
080    private Date createdTime;
081
082    @Transient
083    private Timeunit timeUnit = BundleJob.Timeunit.MINUTE;
084
085    @Basic
086    @Column(name = "time_out")
087    private int timeOut = 0;
088
089    @Basic
090    @Column(name = "user_name")
091    private String user = null;
092
093    @Basic
094    @Column(name = "group_name")
095    private String group = null;
096
097    @Transient
098    private String consoleUrl;
099
100    @Transient
101    private int pending = 0;
102
103    @Transient
104    private List<? extends JsonCoordinatorJob> coordJobs;
105
106    public JsonBundleJob() {
107        coordJobs = new ArrayList<JsonCoordinatorJob>();
108    }
109
110    /**
111     * Get the value from the json object.
112     *
113     * @param json
114    public JsonBundleJob(JSONObject json) {
115        appPath = (String) json.get(JsonTags.BUNDLE_JOB_PATH);
116        appName = (String) json.get(JsonTags.BUNDLE_JOB_NAME);
117        id = (String) json.get(JsonTags.BUNDLE_JOB_ID);
118        externalId = (String) json.get(JsonTags.BUNDLE_JOB_EXTERNAL_ID);
119        conf = (String) json.get(JsonTags.BUNDLE_JOB_CONF);
120        status = Status.valueOf((String) json.get(JsonTags.BUNDLE_JOB_STATUS));
121        kickoffTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.BUNDLE_JOB_KICKOFF_TIME));
122        startTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.BUNDLE_JOB_START_TIME));
123        endTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.BUNDLE_JOB_END_TIME));
124        pauseTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.BUNDLE_JOB_PAUSE_TIME));
125        createdTime = JsonUtils.parseDateRfc822((String) json.get(JsonTags.BUNDLE_JOB_CREATED_TIME));
126        timeUnit = Timeunit.valueOf((String) json.get(JsonTags.BUNDLE_JOB_TIMEUNIT));
127        timeOut = (int) JsonUtils.getLongValue(json, JsonTags.BUNDLE_JOB_TIMEOUT);
128        user = (String) json.get(JsonTags.BUNDLE_JOB_USER);
129        group = (String) json.get(JsonTags.BUNDLE_JOB_GROUP);
130        consoleUrl = (String) json.get(JsonTags.BUNDLE_JOB_CONSOLE_URL);
131        coordJobs = JsonCoordinatorJob.fromJSONArray((JSONArray) json.get(JsonTags.BUNDLE_COORDINATOR_JOBS));
132    }
133*/
134
135    /* (non-Javadoc)
136     * @see org.apache.oozie.client.rest.JsonBean#toJSONObject()
137     */
138    @Override
139    @SuppressWarnings("unchecked")
140    public JSONObject toJSONObject() {
141        return toJSONObject("GMT");
142    }
143    
144    /* (non-Javadoc)
145     * @see org.apache.oozie.client.rest.JsonBean#toJSONObject(String timeZoneId)
146     */
147    @Override
148    @SuppressWarnings("unchecked")
149    public JSONObject toJSONObject(String timeZoneId) {
150        JSONObject json = new JSONObject();
151        json.put(JsonTags.BUNDLE_JOB_PATH, appPath);
152        json.put(JsonTags.BUNDLE_JOB_NAME, appName);
153        json.put(JsonTags.BUNDLE_JOB_ID, id);
154        json.put(JsonTags.BUNDLE_JOB_EXTERNAL_ID, externalId);
155        json.put(JsonTags.BUNDLE_JOB_CONF, conf);
156        json.put(JsonTags.BUNDLE_JOB_STATUS, getStatus().toString());
157        json.put(JsonTags.BUNDLE_JOB_TIMEUNIT, getTimeUnit().toString());
158        json.put(JsonTags.BUNDLE_JOB_TIMEOUT, timeOut);
159        json.put(JsonTags.BUNDLE_JOB_KICKOFF_TIME, JsonUtils.formatDateRfc822(getKickoffTime(), timeZoneId));
160        json.put(JsonTags.BUNDLE_JOB_START_TIME, JsonUtils.formatDateRfc822(getStartTime(), timeZoneId));
161        json.put(JsonTags.BUNDLE_JOB_END_TIME, JsonUtils.formatDateRfc822(getEndTime(), timeZoneId));
162        json.put(JsonTags.BUNDLE_JOB_PAUSE_TIME, JsonUtils.formatDateRfc822(getPauseTime(), timeZoneId));
163        json.put(JsonTags.BUNDLE_JOB_CREATED_TIME, JsonUtils.formatDateRfc822(getCreatedTime(), timeZoneId));
164        json.put(JsonTags.BUNDLE_JOB_USER, getUser());
165        json.put(JsonTags.BUNDLE_JOB_GROUP, getGroup());
166        json.put(JsonTags.BUNDLE_JOB_ACL, getAcl());
167        json.put(JsonTags.BUNDLE_JOB_CONSOLE_URL, getConsoleUrl());
168        json.put(JsonTags.BUNDLE_COORDINATOR_JOBS, JsonCoordinatorJob.toJSONArray(coordJobs, timeZoneId));
169        json.put(JsonTags.TO_STRING, toString());
170
171        return json;
172    }
173
174    /* (non-Javadoc)
175     * @see org.apache.oozie.client.Job#getAppName()
176     */
177    @Override
178    public String getAppName() {
179        return appName;
180    }
181
182    /* (non-Javadoc)
183     * @see org.apache.oozie.client.Job#getAppPath()
184     */
185    @Override
186    public String getAppPath() {
187        return appPath;
188    }
189
190    /* (non-Javadoc)
191     * @see org.apache.oozie.client.Job#getConf()
192     */
193    @Override
194    public String getConf() {
195        return conf;
196    }
197
198    /* (non-Javadoc)
199     * @see org.apache.oozie.client.Job#getConsoleUrl()
200     */
201    @Override
202    public String getConsoleUrl() {
203        return consoleUrl;
204    }
205
206    /* (non-Javadoc)
207     * @see org.apache.oozie.client.BundleJob#getCoordinators()
208     */
209    @Override
210    @SuppressWarnings("unchecked")
211    public List<CoordinatorJob> getCoordinators() {
212        return (List) coordJobs;
213    }
214
215    /* (non-Javadoc)
216     * @see org.apache.oozie.client.Job#getEndTime()
217     */
218    @Override
219    public Date getEndTime() {
220        return endTime;
221    }
222
223    /* (non-Javadoc)
224     * @see org.apache.oozie.client.Job#getGroup()
225     */
226    @Override
227    @Deprecated
228    public String getGroup() {
229        return group;
230    }
231
232    @Override
233    public String getAcl() {
234        return getGroup();
235    }
236
237    /* (non-Javadoc)
238     * @see org.apache.oozie.client.Job#getId()
239     */
240    @Override
241    public String getId() {
242        return id;
243    }
244
245    /* (non-Javadoc)
246     * @see org.apache.oozie.client.Job#getKickoffTime()
247     */
248    @Override
249    public Date getKickoffTime() {
250        return kickoffTime;
251    }
252
253    /* (non-Javadoc)
254     * @see org.apache.oozie.client.Job#getStatus()
255     */
256    @Override
257    public Status getStatus() {
258        return status;
259    }
260
261    /* (non-Javadoc)
262     * @see org.apache.oozie.client.BundleJob#getTimeUnit()
263     */
264    @Override
265    public Timeunit getTimeUnit() {
266        return timeUnit;
267    }
268
269    /* (non-Javadoc)
270     * @see org.apache.oozie.client.BundleJob#getTimeout()
271     */
272    @Override
273    public int getTimeout() {
274        return timeOut;
275    }
276
277    /* (non-Javadoc)
278     * @see org.apache.oozie.client.Job#getUser()
279     */
280    @Override
281    public String getUser() {
282        return user;
283    }
284
285    /**
286     * Set id
287     *
288     * @param id the id to set
289     */
290    public void setId(String id) {
291        this.id = id;
292    }
293
294    /**
295     * Set bundlePath
296     *
297     * @param bundlePath the bundlePath to set
298     */
299    public void setAppPath(String bundlePath) {
300        this.appPath = bundlePath;
301    }
302
303    /**
304     * Set bundleName
305     *
306     * @param bundleName the bundleName to set
307     */
308    public void setAppName(String bundleName) {
309        this.appName = bundleName;
310    }
311
312    /**
313     * Return externalId
314     *
315     * @return externalId
316     */
317    public String getExternalId() {
318        return this.externalId;
319    }
320
321    /**
322     * Set externalId
323     *
324     * @param externalId the externalId to set
325     */
326    public void setExternalId(String externalId) {
327        this.externalId = externalId;
328    }
329
330    /**
331     * Set conf
332     *
333     * @param conf the conf to set
334     */
335    public void setConf(String conf) {
336        this.conf = conf;
337    }
338
339    /**
340     * Set status
341     *
342     * @param status the status to set
343     */
344    @Override
345    public void setStatus(Status status) {
346        this.status = status;
347    }
348
349    /**
350     * Set kickoffTime
351     *
352     * @param kickoffTime the kickoffTime to set
353     */
354    public void setKickoffTime(Date kickoffTime) {
355        this.kickoffTime = kickoffTime;
356    }
357
358    /**
359     * Set startTime
360     *
361     * @param kickoffTime the kickoffTime to set
362     */
363    public void setStartTime(Date startTime) {
364        this.startTime = startTime;
365    }
366
367    /**
368     * Set endTime
369     *
370     * @param endTime the endTime to set
371     */
372    public void setEndTime(Date endTime) {
373        this.endTime = endTime;
374    }
375
376    /**
377     * Get pauseTime
378     *
379     * @return pauseTime
380     */
381    public Date getPauseTime() {
382        return pauseTime;
383    }
384
385    /**
386     * Set pauseTime
387     *
388     * @param pauseTime the pauseTime to set
389     */
390    public void setPauseTime(Date pauseTime) {
391        this.pauseTime = pauseTime;
392    }
393
394    /**
395     * Get createdTime
396     *
397     * @return createdTime
398     */
399    public Date getCreatedTime() {
400        return createdTime;
401    }
402
403    /**
404     * Set createdTime
405     *
406     * @param createdTime the createdTime to set
407     */
408    public void setCreatedTime(Date createdTime) {
409        this.createdTime = createdTime;
410    }
411
412    /**
413     * Set timeUnit
414     *
415     * @param timeUnit the timeUnit to set
416     */
417    public void setTimeUnit(Timeunit timeUnit) {
418        this.timeUnit = timeUnit;
419    }
420
421    /**
422     * Set timeOut
423     *
424     * @param timeOut the timeOut to set
425     */
426    public void setTimeOut(int timeOut) {
427        this.timeOut = timeOut;
428    }
429
430    /**
431     * Set user
432     *
433     * @param user the user to set
434     */
435    public void setUser(String user) {
436        this.user = user;
437    }
438
439    /**
440     * Set group
441     *
442     * @param group the group to set
443     */
444    public void setGroup(String group) {
445        this.group = group;
446    }
447
448    /**
449     * Set consoleUrl
450     *
451     * @param consoleUrl the consoleUrl to set
452     */
453    public void setConsoleUrl(String consoleUrl) {
454        this.consoleUrl = consoleUrl;
455    }
456
457    /**
458     * Set coordJobs
459     *
460     * @param coordJobs the coordJobs to set
461     */
462    public void setCoordJobs(List<? extends JsonCoordinatorJob> coordJobs) {
463        this.coordJobs = (coordJobs != null) ? coordJobs : new ArrayList<JsonCoordinatorJob>();
464    }
465
466    /**
467     * Convert a Bundle job list into a JSONArray.
468     *
469     * @param application list.
470     * @param timeZoneId time zone to use for dates in the JSON array.
471     * @return the corresponding JSON array.
472     */
473    @SuppressWarnings("unchecked")
474    public static JSONArray toJSONArray(List<? extends JsonBundleJob> applications, String timeZoneId) {
475        JSONArray array = new JSONArray();
476        if (applications != null) {
477            for (JsonBundleJob application : applications) {
478                array.add(application.toJSONObject(timeZoneId));
479            }
480        }
481        return array;
482    }
483
484    /* (non-Javadoc)
485     * @see org.apache.oozie.client.Job#getStartTime()
486     */
487    @Override
488    public Date getStartTime() {
489        return startTime;
490    }
491
492    /**
493     * Set pending to true
494     *
495     * @param pending set pending to true
496     */
497    public void setPending() {
498        this.pending = 1;
499    }
500
501    /**
502     * Set pending to false
503     *
504     * @param pending set pending to false
505     */
506    public void resetPending() {
507        this.pending = 0;
508    }
509
510    @Override
511    public String toString() {
512        return MessageFormat.format("Bundle id[{0}] status[{1}]", getId(), getStatus());
513    }
514}