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