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