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