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