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 int 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    
134        public JsonCoordinatorJob() {
135            actions = new ArrayList<JsonCoordinatorAction>();
136        }
137    
138        @SuppressWarnings("unchecked")
139        public JSONObject toJSONObject() {
140            return toJSONObject("GMT");
141        }
142        
143        @SuppressWarnings("unchecked")
144        public JSONObject toJSONObject(String timeZoneId) {
145            JSONObject json = new JSONObject();
146            json.put(JsonTags.COORDINATOR_JOB_PATH, getAppPath());
147            json.put(JsonTags.COORDINATOR_JOB_NAME, getAppName());
148            json.put(JsonTags.COORDINATOR_JOB_ID, getId());
149            json.put(JsonTags.COORDINATOR_JOB_EXTERNAL_ID, getExternalId());
150            json.put(JsonTags.COORDINATOR_JOB_CONF, getConf());
151            json.put(JsonTags.COORDINATOR_JOB_STATUS, getStatus().toString());
152            json.put(JsonTags.COORDINATOR_JOB_EXECUTIONPOLICY, getExecutionOrder().toString());
153            json.put(JsonTags.COORDINATOR_JOB_FREQUENCY, getFrequency());
154            json.put(JsonTags.COORDINATOR_JOB_TIMEUNIT, getTimeUnit().toString());
155            json.put(JsonTags.COORDINATOR_JOB_TIMEZONE, getTimeZone());
156            json.put(JsonTags.COORDINATOR_JOB_CONCURRENCY, getConcurrency());
157            json.put(JsonTags.COORDINATOR_JOB_TIMEOUT, getTimeout());
158            json.put(JsonTags.COORDINATOR_JOB_LAST_ACTION_TIME, JsonUtils.formatDateRfc822(getLastActionTime(), timeZoneId));
159            json.put(JsonTags.COORDINATOR_JOB_NEXT_MATERIALIZED_TIME, 
160                    JsonUtils.formatDateRfc822(getNextMaterializedTime(), timeZoneId));
161            json.put(JsonTags.COORDINATOR_JOB_START_TIME, JsonUtils.formatDateRfc822(getStartTime(), timeZoneId));
162            json.put(JsonTags.COORDINATOR_JOB_END_TIME, JsonUtils.formatDateRfc822(getEndTime(), timeZoneId));
163            json.put(JsonTags.COORDINATOR_JOB_PAUSE_TIME, JsonUtils.formatDateRfc822(getPauseTime(), timeZoneId));
164            json.put(JsonTags.COORDINATOR_JOB_USER, getUser());
165            json.put(JsonTags.COORDINATOR_JOB_GROUP, getGroup());
166            json.put(JsonTags.COORDINATOR_JOB_ACL, getAcl());
167            json.put(JsonTags.COORDINATOR_JOB_CONSOLE_URL, getConsoleUrl());
168            json.put(JsonTags.COORDINATOR_JOB_MAT_THROTTLING, getMatThrottling());
169            json.put(JsonTags.COORDINATOR_ACTIONS, JsonCoordinatorAction.toJSONArray(actions, timeZoneId));
170            json.put(JsonTags.TO_STRING,toString());
171    
172            return json;
173        }
174    
175        public String getAppPath() {
176            return appPath;
177        }
178    
179        public void setAppPath(String appPath) {
180            this.appPath = appPath;
181        }
182    
183        public String getAppName() {
184            return appName;
185        }
186    
187        public void setAppName(String appName) {
188            this.appName = appName;
189        }
190    
191        public String getId() {
192            return id;
193        }
194    
195        public void setId(String id) {
196            this.id = id;
197        }
198    
199        public void setExternalId(String externalId) {
200            this.externalId = externalId;
201        }
202    
203        public String getExternalId() {
204            return externalId;
205        }
206    
207        public String getConf() {
208            return conf;
209        }
210    
211        public void setConf(String conf) {
212            this.conf = conf;
213        }
214    
215        public Status getStatus() {
216            return status;
217        }
218    
219        public void setStatus(Status status) {
220            this.status = status;
221        }
222    
223        public void setFrequency(int frequency) {
224            this.frequency = frequency;
225        }
226    
227        public int getFrequency() {
228            return frequency;
229        }
230    
231        public void setTimeUnit(Timeunit timeUnit) {
232            this.timeUnit = timeUnit;
233        }
234    
235        public Timeunit getTimeUnit() {
236            return timeUnit;
237        }
238    
239        public void setTimeZone(String timeZone) {
240            this.timeZone = timeZone;
241        }
242    
243        public String getTimeZone() {
244            return timeZone;
245        }
246    
247        public void setConcurrency(int concurrency) {
248            this.concurrency = concurrency;
249        }
250    
251        public int getConcurrency() {
252            return concurrency;
253        }
254    
255        public int getMatThrottling() {
256            return matThrottling;
257        }
258    
259        public void setMatThrottling(int matThrottling) {
260            this.matThrottling = matThrottling;
261        }
262    
263        public void setExecutionOrder(Execution order) {
264            this.executionOrder = order;
265        }
266    
267        public Execution getExecutionOrder() {
268            return executionOrder;
269        }
270    
271        public void setTimeout(int timeOut) {
272            this.timeOut = timeOut;
273        }
274    
275        public int getTimeout() {
276            return timeOut;
277        }
278    
279        public void setLastActionTime(Date lastAction) {
280            this.lastAction = lastAction;
281        }
282    
283        public Date getLastActionTime() {
284            return lastAction;
285        }
286    
287        public Date getNextMaterializedTime() {
288            return nextMaterializedTime;
289        }
290    
291        public void setNextMaterializedTime(Date nextMaterializedTime) {
292            this.nextMaterializedTime = nextMaterializedTime;
293        }
294    
295        public Date getStartTime() {
296            return startTime;
297        }
298    
299        public void setStartTime(Date startTime) {
300            this.startTime = startTime;
301        }
302    
303        public Date getEndTime() {
304            return endTime;
305        }
306    
307        public void setEndTime(Date endTime) {
308            this.endTime = endTime;
309        }
310    
311        public Date getPauseTime() {
312            return pauseTime;
313        }
314    
315        public void setPauseTime(Date pauseTime) {
316            this.pauseTime = pauseTime;
317        }
318    
319        public String getUser() {
320            return user;
321        }
322    
323        public void setUser(String user) {
324            this.user = user;
325        }
326    
327        public String getGroup() {
328            return group;
329        }
330    
331        @Override
332        public String getAcl() {
333            return getGroup();
334        }
335    
336        public void setGroup(String group) {
337            this.group = group;
338        }
339    
340        public String getBundleId() {
341            return bundleId;
342        }
343    
344        public void setBundleId(String bundleId) {
345            this.bundleId = bundleId;
346        }
347    
348        /**
349         * Return the coordinate application console URL.
350         *
351         * @return the coordinate application console URL.
352         */
353        public String getConsoleUrl() {
354            return consoleUrl;
355        }
356    
357        /**
358         * Set the coordinate application console URL.
359         *
360         * @param consoleUrl the coordinate application console URL.
361         */
362        public void setConsoleUrl(String consoleUrl) {
363            this.consoleUrl = consoleUrl;
364        }
365    
366        @Override
367        public String toString() {
368            return MessageFormat.format("Coordinator application id[{0}] status[{1}]", getId(), getStatus());
369        }
370    
371        public void setActions(List<? extends JsonCoordinatorAction> nodes) {
372            this.actions = (nodes != null) ? nodes : new ArrayList<JsonCoordinatorAction>();
373        }
374    
375        @SuppressWarnings("unchecked")
376        public List<CoordinatorAction> getActions() {
377            return (List) actions;
378        }
379    
380        /**
381         * Convert a coordinator application list into a JSONArray.
382         *
383         * @param applications list.
384         * @param timeZoneId time zone to use for dates in the JSON array.
385         * @return the corresponding JSON array.
386         */
387        @SuppressWarnings("unchecked")
388        public static JSONArray toJSONArray(List<? extends JsonCoordinatorJob> applications, String timeZoneId) {
389            JSONArray array = new JSONArray();
390            if (applications != null) {
391                for (JsonCoordinatorJob application : applications) {
392                    array.add(application.toJSONObject(timeZoneId));
393                }
394            }
395            return array;
396        }
397    
398        public int getLastActionNumber() {
399            return lastActionNumber;
400        }
401    
402        public void setLastActionNumber(int lastActionNumber) {
403            this.lastActionNumber = lastActionNumber;
404        }
405    
406        /**
407         * Set pending to true
408         *
409         * @param pending set pending to true
410         */
411        public void setPending() {
412            this.pending = 1;
413        }
414    
415        /**
416         * Set pending to false
417         *
418         * @param pending set pending to false
419         */
420        public void resetPending() {
421            this.pending = 0;
422        }
423    
424    }