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    package org.apache.oozie.client.rest;
016    
017    import org.apache.oozie.client.CoordinatorJob;
018    import org.apache.oozie.client.CoordinatorAction;
019    import org.json.simple.JSONArray;
020    import org.json.simple.JSONObject;
021    
022    import java.text.MessageFormat;
023    import java.util.Date;
024    import java.util.List;
025    import java.util.ArrayList;
026    
027    import javax.persistence.*;
028    
029    @Entity
030    @Table(name = "COORD_JOBS")
031    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
032    public class JsonCoordinatorJob implements CoordinatorJob, JsonBean {
033    
034        @Id
035        private String id;
036    
037        @Basic
038        @Column(name = "app_path")
039        private String appPath = null;
040    
041        @Basic
042        @Column(name = "app_name")
043        private String appName = null;
044    
045        @Basic
046        @Column(name = "external_id")
047        private String externalId = null;
048    
049        @Column(name = "conf")
050        @Lob
051        private String conf = null;
052    
053        @Transient
054        private Status status = CoordinatorJob.Status.PREP;
055    
056        @Transient
057        private Execution executionOrder = CoordinatorJob.Execution.LIFO;
058    
059        @Transient
060        private Date startTime;
061    
062        @Transient
063        private Date endTime;
064    
065        @Transient
066        private Date pauseTime;
067    
068        @Basic
069        @Column(name = "frequency")
070        private int frequency = 0;
071    
072        @Basic
073        @Column(name = "time_zone")
074        private String timeZone = null;
075    
076        @Basic
077        @Column(name = "concurrency")
078        private int concurrency = 0;
079    
080    
081        @Transient
082        private Timeunit timeUnit = CoordinatorJob.Timeunit.MINUTE;
083    
084        @Basic
085        @Column(name = "time_out")
086        private int timeOut = 0;
087    
088        @Transient
089        private Date lastAction;
090    
091        @Basic
092        @Column(name = "last_action_number")
093        private int lastActionNumber;
094    
095        @Transient
096        private Date nextMaterializedTime;
097    
098        @Basic
099        @Column(name = "user_name")
100        private String user = null;
101    
102        @Basic
103        @Column(name = "group_name")
104        private String group = null;
105    
106        @Basic
107        @Column(name = "bundle_id")
108        private String bundleId = null;
109    
110        @Transient
111        private String consoleUrl;
112    
113        @Transient
114        private List<? extends JsonCoordinatorAction> actions;
115    
116        public JsonCoordinatorJob() {
117            actions = new ArrayList<JsonCoordinatorAction>();
118        }
119    
120        @SuppressWarnings("unchecked")
121        public JSONObject toJSONObject() {
122            JSONObject json = new JSONObject();
123            json.put(JsonTags.COORDINATOR_JOB_PATH, appPath);
124            json.put(JsonTags.COORDINATOR_JOB_NAME, appName);
125            json.put(JsonTags.COORDINATOR_JOB_ID, id);
126            json.put(JsonTags.COORDINATOR_JOB_EXTERNAL_ID, externalId);
127            json.put(JsonTags.COORDINATOR_JOB_CONF, conf);
128            json.put(JsonTags.COORDINATOR_JOB_STATUS, status.toString());
129            json.put(JsonTags.COORDINATOR_JOB_EXECUTIONPOLICY, executionOrder.toString());
130            json.put(JsonTags.COORDINATOR_JOB_FREQUENCY, frequency);
131            json.put(JsonTags.COORDINATOR_JOB_TIMEUNIT, timeUnit.toString());
132            json.put(JsonTags.COORDINATOR_JOB_TIMEZONE, timeZone);
133            json.put(JsonTags.COORDINATOR_JOB_CONCURRENCY, concurrency);
134            json.put(JsonTags.COORDINATOR_JOB_TIMEOUT, timeOut);
135            json.put(JsonTags.COORDINATOR_JOB_LAST_ACTION_TIME, JsonUtils.formatDateRfc822(lastAction));
136            json.put(JsonTags.COORDINATOR_JOB_NEXT_MATERIALIZED_TIME, JsonUtils.formatDateRfc822(nextMaterializedTime));
137            json.put(JsonTags.COORDINATOR_JOB_START_TIME, JsonUtils.formatDateRfc822(startTime));
138            json.put(JsonTags.COORDINATOR_JOB_END_TIME, JsonUtils.formatDateRfc822(endTime));
139            json.put(JsonTags.COORDINATOR_JOB_PAUSE_TIME, JsonUtils.formatDateRfc822(pauseTime));
140            json.put(JsonTags.COORDINATOR_JOB_USER, user);
141            json.put(JsonTags.COORDINATOR_JOB_GROUP, group);
142            json.put(JsonTags.COORDINATOR_JOB_CONSOLE_URL, consoleUrl);
143            json.put(JsonTags.COORDINATOR_ACTIONS, JsonCoordinatorAction.toJSONArray(actions));
144            json.put(JsonTags.TO_STRING,toString());
145    
146            return json;
147        }
148    
149        public String getAppPath() {
150            return appPath;
151        }
152    
153        public void setAppPath(String appPath) {
154            this.appPath = appPath;
155        }
156    
157        public String getAppName() {
158            return appName;
159        }
160    
161        public void setAppName(String appName) {
162            this.appName = appName;
163        }
164    
165        public String getId() {
166            return id;
167        }
168    
169        public void setId(String id) {
170            this.id = id;
171        }
172    
173        public void setExternalId(String externalId) {
174            this.externalId = externalId;
175        }
176    
177        public String getExternalId() {
178            return externalId;
179        }
180    
181        public String getConf() {
182            return conf;
183        }
184    
185        public void setConf(String conf) {
186            this.conf = conf;
187        }
188    
189        public Status getStatus() {
190            return status;
191        }
192    
193        public void setStatus(Status status) {
194            this.status = status;
195        }
196    
197        public void setFrequency(int frequency) {
198            this.frequency = frequency;
199        }
200    
201        public int getFrequency() {
202            return frequency;
203        }
204    
205        public void setTimeUnit(Timeunit timeUnit) {
206            this.timeUnit = timeUnit;
207        }
208    
209        public Timeunit getTimeUnit() {
210            return timeUnit;
211        }
212    
213        public void setTimeZone(String timeZone) {
214            this.timeZone = timeZone;
215        }
216    
217        public String getTimeZone() {
218            return timeZone;
219        }
220    
221        public void setConcurrency(int concurrency) {
222            this.concurrency = concurrency;
223        }
224    
225        public int getConcurrency() {
226            return concurrency;
227        }
228    
229        public void setExecutionOrder(Execution order) {
230            this.executionOrder = order;
231        }
232    
233        public Execution getExecutionOrder() {
234            return executionOrder;
235        }
236    
237        public void setTimeout(int timeOut) {
238            this.timeOut = timeOut;
239        }
240    
241        public int getTimeout() {
242            return timeOut;
243        }
244    
245        public void setLastActionTime(Date lastAction) {
246            this.lastAction = lastAction;
247        }
248    
249        public Date getLastActionTime() {
250            return lastAction;
251        }
252    
253        public Date getNextMaterializedTime() {
254            return nextMaterializedTime;
255        }
256    
257        public void setNextMaterializedTime(Date nextMaterializedTime) {
258            this.nextMaterializedTime = nextMaterializedTime;
259        }
260    
261        public Date getStartTime() {
262            return startTime;
263        }
264    
265        public void setStartTime(Date startTime) {
266            this.startTime = startTime;
267        }
268    
269        public Date getEndTime() {
270            return endTime;
271        }
272    
273        public void setEndTime(Date endTime) {
274            this.endTime = endTime;
275        }
276    
277        public Date getPauseTime() {
278            return pauseTime;
279        }
280    
281        public void setPauseTime(Date pauseTime) {
282            this.pauseTime = pauseTime;
283        }
284    
285        public String getUser() {
286            return user;
287        }
288    
289        public void setUser(String user) {
290            this.user = user;
291        }
292    
293        public String getGroup() {
294            return group;
295        }
296    
297        public void setGroup(String group) {
298            this.group = group;
299        }
300    
301        public String getBundleId() {
302            return bundleId;
303        }
304    
305        public void setBundleId(String bundleId) {
306            this.bundleId = bundleId;
307        }
308    
309        /**
310         * Return the coordinate application console URL.
311         *
312         * @return the coordinate application console URL.
313         */
314        public String getConsoleUrl() {
315            return consoleUrl;
316        }
317    
318        /**
319         * Set the coordinate application console URL.
320         *
321         * @param consoleUrl the coordinate application console URL.
322         */
323        public void setConsoleUrl(String consoleUrl) {
324            this.consoleUrl = consoleUrl;
325        }
326    
327        public String toString() {
328            return MessageFormat.format("Coornidator application id[{0}] status[{1}]", getId(), getStatus());
329        }
330    
331        public void setActions(List<? extends JsonCoordinatorAction> nodes) {
332            this.actions = (nodes != null) ? nodes : new ArrayList<JsonCoordinatorAction>();
333        }
334    
335        @SuppressWarnings("unchecked")
336        public List<CoordinatorAction> getActions() {
337            return (List) actions;
338        }
339    
340        /**
341         * Convert a coordinator application list into a JSONArray.
342         *
343         * @param applications list.
344         * @return the corresponding JSON array.
345         */
346        @SuppressWarnings("unchecked")
347        public static JSONArray toJSONArray(List<? extends JsonCoordinatorJob> applications) {
348            JSONArray array = new JSONArray();
349            if (applications != null) {
350                for (JsonCoordinatorJob application : applications) {
351                    array.add(application.toJSONObject());
352                }
353            }
354            return array;
355        }
356    
357        public int getLastActionNumber() {
358            return lastActionNumber;
359        }
360    
361        public void setLastActionNumber(int lastActionNumber) {
362            this.lastActionNumber = lastActionNumber;
363        }
364    }