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