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.CoordinatorAction;
036    import org.apache.oozie.client.CoordinatorJob;
037    import org.json.simple.JSONArray;
038    import org.json.simple.JSONObject;
039    
040    @Entity
041    @Table(name = "COORD_JOBS")
042    @DiscriminatorColumn(name = "bean_type", discriminatorType = DiscriminatorType.STRING)
043    public class JsonCoordinatorJob implements CoordinatorJob, JsonBean {
044    
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 = CoordinatorJob.Status.PREP;
066    
067        @Transient
068        private Execution executionOrder = CoordinatorJob.Execution.FIFO;
069    
070        @Transient
071        private Date startTime;
072    
073        @Transient
074        private Date endTime;
075    
076        @Transient
077        private Date pauseTime;
078    
079        @Basic
080        @Column(name = "frequency")
081        private String frequency = "0";
082    
083        @Basic
084        @Column(name = "time_zone")
085        private String timeZone = null;
086    
087        @Basic
088        @Column(name = "concurrency")
089        private int concurrency = 0;
090    
091        @Basic
092        @Column(name = "mat_throttling")
093        private int matThrottling = 0;
094    
095        @Transient
096        private Timeunit timeUnit = CoordinatorJob.Timeunit.MINUTE;
097    
098        @Basic
099        @Column(name = "time_out")
100        private int timeOut = 0;
101    
102        @Transient
103        private Date lastAction;
104    
105        @Basic
106        @Column(name = "last_action_number")
107        private int lastActionNumber;
108    
109        @Transient
110        private Date nextMaterializedTime;
111    
112        @Basic
113        @Column(name = "user_name")
114        private String user = null;
115    
116        @Basic
117        @Column(name = "group_name")
118        private String group = null;
119    
120        @Basic
121        @Column(name = "bundle_id")
122        private String bundleId = null;
123    
124        @Transient
125        private String consoleUrl;
126    
127        @Transient
128        private List<? extends JsonCoordinatorAction> actions;
129    
130        @Transient
131        private int pending = 0;
132    
133        @Transient
134        private int numActions = 0;
135    
136        public JsonCoordinatorJob() {
137            actions = new ArrayList<JsonCoordinatorAction>();
138        }
139    
140        @SuppressWarnings("unchecked")
141        public JSONObject toJSONObject() {
142            return toJSONObject("GMT");
143        }
144    
145        @SuppressWarnings("unchecked")
146        public JSONObject toJSONObject(String timeZoneId) {
147            JSONObject json = new JSONObject();
148            json.put(JsonTags.COORDINATOR_JOB_PATH, getAppPath());
149            json.put(JsonTags.COORDINATOR_JOB_NAME, getAppName());
150            json.put(JsonTags.COORDINATOR_JOB_ID, getId());
151            json.put(JsonTags.COORDINATOR_JOB_EXTERNAL_ID, getExternalId());
152            json.put(JsonTags.COORDINATOR_JOB_BUNDLE_ID, getBundleId());
153            json.put(JsonTags.COORDINATOR_JOB_CONF, getConf());
154            json.put(JsonTags.COORDINATOR_JOB_STATUS, getStatus().toString());
155            json.put(JsonTags.COORDINATOR_JOB_EXECUTIONPOLICY, getExecutionOrder().toString());
156            json.put(JsonTags.COORDINATOR_JOB_FREQUENCY, getFrequency());
157            json.put(JsonTags.COORDINATOR_JOB_TIMEUNIT, getTimeUnit().toString());
158            json.put(JsonTags.COORDINATOR_JOB_TIMEZONE, getTimeZone());
159            json.put(JsonTags.COORDINATOR_JOB_CONCURRENCY, getConcurrency());
160            json.put(JsonTags.COORDINATOR_JOB_TIMEOUT, getTimeout());
161            json.put(JsonTags.COORDINATOR_JOB_LAST_ACTION_TIME, JsonUtils.formatDateRfc822(getLastActionTime(), timeZoneId));
162            json.put(JsonTags.COORDINATOR_JOB_NEXT_MATERIALIZED_TIME,
163                    JsonUtils.formatDateRfc822(getNextMaterializedTime(), timeZoneId));
164            json.put(JsonTags.COORDINATOR_JOB_START_TIME, JsonUtils.formatDateRfc822(getStartTime(), timeZoneId));
165            json.put(JsonTags.COORDINATOR_JOB_END_TIME, JsonUtils.formatDateRfc822(getEndTime(), timeZoneId));
166            json.put(JsonTags.COORDINATOR_JOB_PAUSE_TIME, JsonUtils.formatDateRfc822(getPauseTime(), timeZoneId));
167            json.put(JsonTags.COORDINATOR_JOB_USER, getUser());
168            json.put(JsonTags.COORDINATOR_JOB_GROUP, getGroup());
169            json.put(JsonTags.COORDINATOR_JOB_ACL, getAcl());
170            json.put(JsonTags.COORDINATOR_JOB_CONSOLE_URL, getConsoleUrl());
171            json.put(JsonTags.COORDINATOR_JOB_MAT_THROTTLING, getMatThrottling());
172            json.put(JsonTags.COORDINATOR_ACTIONS, JsonCoordinatorAction.toJSONArray(actions, timeZoneId));
173            json.put(JsonTags.TO_STRING,toString());
174            json.put(JsonTags.COORDINATOR_JOB_NUM_ACTION, numActions);
175    
176            return json;
177        }
178    
179        public String getAppPath() {
180            return appPath;
181        }
182    
183        public void setAppPath(String appPath) {
184            this.appPath = appPath;
185        }
186    
187        public String getAppName() {
188            return appName;
189        }
190    
191        public void setAppName(String appName) {
192            this.appName = appName;
193        }
194    
195        public String getId() {
196            return id;
197        }
198    
199        public void setId(String id) {
200            this.id = id;
201        }
202    
203        public void setExternalId(String externalId) {
204            this.externalId = externalId;
205        }
206    
207        public String getExternalId() {
208            return externalId;
209        }
210    
211        public String getConf() {
212            return conf;
213        }
214    
215        public void setConf(String conf) {
216            this.conf = conf;
217        }
218    
219        public Status getStatus() {
220            return status;
221        }
222    
223        public void setStatus(Status status) {
224            this.status = status;
225        }
226    
227        public void setFrequency(String frequency) {
228            this.frequency = frequency;
229        }
230    
231        public String getFrequency() {
232            return frequency;
233        }
234    
235        public void setTimeUnit(Timeunit timeUnit) {
236            this.timeUnit = timeUnit;
237        }
238    
239        public Timeunit getTimeUnit() {
240            return timeUnit;
241        }
242    
243        public void setTimeZone(String timeZone) {
244            this.timeZone = timeZone;
245        }
246    
247        public String getTimeZone() {
248            return timeZone;
249        }
250    
251        public void setConcurrency(int concurrency) {
252            this.concurrency = concurrency;
253        }
254    
255        public int getConcurrency() {
256            return concurrency;
257        }
258    
259        public int getMatThrottling() {
260            return matThrottling;
261        }
262    
263        public void setMatThrottling(int matThrottling) {
264            this.matThrottling = matThrottling;
265        }
266    
267        public void setExecutionOrder(Execution order) {
268            this.executionOrder = order;
269        }
270    
271        public Execution getExecutionOrder() {
272            return executionOrder;
273        }
274    
275        public void setTimeout(int timeOut) {
276            this.timeOut = timeOut;
277        }
278    
279        public int getTimeout() {
280            return timeOut;
281        }
282    
283        public void setLastActionTime(Date lastAction) {
284            this.lastAction = lastAction;
285        }
286    
287        public Date getLastActionTime() {
288            return lastAction;
289        }
290    
291        public Date getNextMaterializedTime() {
292            return nextMaterializedTime;
293        }
294    
295        public void setNextMaterializedTime(Date nextMaterializedTime) {
296            this.nextMaterializedTime = nextMaterializedTime;
297        }
298    
299        public Date getStartTime() {
300            return startTime;
301        }
302    
303        public void setStartTime(Date startTime) {
304            this.startTime = startTime;
305        }
306    
307        public Date getEndTime() {
308            return endTime;
309        }
310    
311        public void setEndTime(Date endTime) {
312            this.endTime = endTime;
313        }
314    
315        public Date getPauseTime() {
316            return pauseTime;
317        }
318    
319        public void setPauseTime(Date pauseTime) {
320            this.pauseTime = pauseTime;
321        }
322    
323        public String getUser() {
324            return user;
325        }
326    
327        public void setUser(String user) {
328            this.user = user;
329        }
330    
331        public String getGroup() {
332            return group;
333        }
334    
335        @Override
336        public String getAcl() {
337            return getGroup();
338        }
339    
340        public void setGroup(String group) {
341            this.group = group;
342        }
343    
344        public String getBundleId() {
345            return bundleId;
346        }
347    
348        public void setBundleId(String bundleId) {
349            this.bundleId = bundleId;
350        }
351    
352        /**
353         * Return the coordinate application console URL.
354         *
355         * @return the coordinate application console URL.
356         */
357        public String getConsoleUrl() {
358            return consoleUrl;
359        }
360    
361        /**
362         * Set the coordinate application console URL.
363         *
364         * @param consoleUrl the coordinate application console URL.
365         */
366        public void setConsoleUrl(String consoleUrl) {
367            this.consoleUrl = consoleUrl;
368        }
369    
370        @Override
371        public String toString() {
372            return MessageFormat.format("Coordinator application id[{0}] status[{1}]", getId(), getStatus());
373        }
374    
375        public void setActions(List<? extends JsonCoordinatorAction> nodes) {
376            this.actions = (nodes != null) ? nodes : new ArrayList<JsonCoordinatorAction>();
377        }
378    
379        @SuppressWarnings("unchecked")
380        public List<CoordinatorAction> getActions() {
381            return (List) actions;
382        }
383    
384        /**
385         * Convert a coordinator application list into a JSONArray.
386         *
387         * @param applications list.
388         * @param timeZoneId time zone to use for dates in the JSON array.
389         * @return the corresponding JSON array.
390         */
391        @SuppressWarnings("unchecked")
392        public static JSONArray toJSONArray(List<? extends JsonCoordinatorJob> applications, String timeZoneId) {
393            JSONArray array = new JSONArray();
394            if (applications != null) {
395                for (JsonCoordinatorJob application : applications) {
396                    array.add(application.toJSONObject(timeZoneId));
397                }
398            }
399            return array;
400        }
401    
402        public int getLastActionNumber() {
403            return lastActionNumber;
404        }
405    
406        public void setLastActionNumber(int lastActionNumber) {
407            this.lastActionNumber = lastActionNumber;
408        }
409    
410        /**
411         * Set pending to true
412         */
413        public void setPending() {
414            this.pending = 1;
415        }
416    
417        /**
418         * Set pending to false
419         */
420        public void resetPending() {
421            this.pending = 0;
422        }
423    
424        public int getNumActions() {
425            return numActions;
426        }
427    
428        public void setNumActions(int numAction) {
429            this.numActions = numAction;
430        }
431    
432    }