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.sla;
019    
020    import java.sql.Timestamp;
021    import java.util.ArrayList;
022    import java.util.Date;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Map.Entry;
027    
028    import javax.persistence.Basic;
029    import javax.persistence.Column;
030    import javax.persistence.Entity;
031    import javax.persistence.Id;
032    import javax.persistence.NamedQueries;
033    import javax.persistence.NamedQuery;
034    import javax.persistence.Table;
035    import javax.persistence.Transient;
036    
037    import org.apache.oozie.AppType;
038    import org.apache.oozie.client.event.Event.MessageType;
039    import org.apache.oozie.client.rest.JsonBean;
040    import org.apache.oozie.util.DateUtils;
041    import org.apache.openjpa.persistence.jdbc.Index;
042    import org.json.simple.JSONArray;
043    import org.json.simple.JSONObject;
044    
045    @Entity
046    @Table(name = "SLA_REGISTRATION")
047    @NamedQueries({
048    
049     @NamedQuery(name = "UPDATE_SLA_REG_ALL", query = "update SLARegistrationBean w set w.jobId = :jobId, w.nominalTimeTS = :nominalTime, w.expectedStartTS = :expectedStartTime, w.expectedEndTS = :expectedEndTime, w.expectedDuration = :expectedDuration, w.slaConfig = :slaConfig, w.notificationMsg = :notificationMsg, w.upstreamApps = :upstreamApps, w.appType = :appType, w.appName = :appName, w.user = :user, w.parentId = :parentId, w.jobData = :jobData where w.jobId = :jobId"),
050    
051     @NamedQuery(name = "GET_SLA_REG_ON_RESTART", query = "select w.notificationMsg, w.upstreamApps, w.slaConfig, w.jobData from SLARegistrationBean w where w.jobId = :id"),
052    
053     @NamedQuery(name = "GET_SLA_REG_ALL", query = "select OBJECT(w) from SLARegistrationBean w where w.jobId = :id") })
054    public class SLARegistrationBean implements JsonBean {
055    
056        @Id
057        @Basic
058        @Column(name = "job_id")
059        private String jobId;
060    
061        @Basic
062        @Column(name = "parent_id")
063        private String parentId = null;
064    
065        @Basic
066        @Column(name = "app_name")
067        private String appName = null;
068    
069        @Basic
070        @Column(name = "app_type")
071        private String appType = null;
072    
073        @Basic
074        @Column(name = "created_time")
075        private Timestamp createdTimeTS = null;
076    
077        @Basic
078        @Index
079        @Column(name = "nominal_time")
080        private Timestamp nominalTimeTS = null;
081    
082        @Basic
083        @Column(name = "expected_start")
084        private Timestamp expectedStartTS = null;
085    
086        @Basic
087        @Column(name = "expected_end")
088        private Timestamp expectedEndTS = null;
089    
090        @Basic
091        @Column(name = "expected_duration")
092        private long expectedDuration = -1;
093    
094        @Basic
095        @Column(name = "user_name")
096        private String user = null;
097    
098        @Basic
099        @Column(name = "upstream_apps")
100        private String upstreamApps = null;
101    
102        @Basic
103        @Column(name = "job_data")
104        private String jobData = null;
105    
106        @Basic
107        @Column(name = "sla_config")
108        private String slaConfig = null;
109    
110        @Basic
111        @Column(name = "notification_msg")
112        private String notificationMsg = null;
113    
114        @Transient
115        private Map<String, String> slaConfigMap;
116    
117        @Transient
118        private MessageType msgType;
119    
120        private final String ALERT_EVENTS = "alert_events";
121        private final String ALERT_CONTACT = "alert_contact";
122    
123        public SLARegistrationBean() {
124            slaConfigMap = new HashMap<String, String>();
125            msgType = MessageType.SLA;
126        }
127    
128        public SLARegistrationBean(JSONObject obj) {
129            // TODO use JSONObject
130            this();
131        }
132    
133        public String getId() {
134            return jobId;
135        }
136    
137        public void setId(String jobId) {
138            this.jobId = jobId;
139        }
140    
141        public String getParentId() {
142            return parentId;
143        }
144    
145        public void setParentId(String parentId) {
146            this.parentId = parentId;
147        }
148    
149        public String getAppName() {
150            return appName;
151        }
152    
153        public void setAppName(String appName) {
154            this.appName = appName;
155        }
156    
157        public AppType getAppType() {
158            return AppType.valueOf(appType);
159        }
160    
161        public void setAppType(AppType appType) {
162            this.appType = appType.toString();
163        }
164    
165        public Timestamp getCreatedTimestamp() {
166            return createdTimeTS;
167        }
168    
169        public void setCreatedTimestamp(Timestamp createdTime) {
170            this.createdTimeTS = createdTime;
171        }
172    
173        public Date getCreatedTime() {
174            return DateUtils.toDate(createdTimeTS);
175        }
176    
177        public void setCreatedTime(Date createdTime) {
178            this.createdTimeTS = DateUtils.convertDateToTimestamp(createdTime);
179        }
180    
181        public Date getNominalTime() {
182            return DateUtils.toDate(nominalTimeTS);
183        }
184    
185        public Timestamp getNominalTimestamp() {
186            return this.nominalTimeTS;
187        }
188    
189        public void setNominalTime(Date nominalTime) {
190            this.nominalTimeTS = DateUtils.convertDateToTimestamp(nominalTime);
191        }
192    
193        public Date getExpectedStart() {
194            return DateUtils.toDate(expectedStartTS);
195        }
196    
197        public Timestamp getExpectedStartTimestamp() {
198            return this.expectedStartTS;
199        }
200    
201        public void setExpectedStart(Date expectedStart) {
202            this.expectedStartTS = DateUtils.convertDateToTimestamp(expectedStart);
203        }
204    
205        public Date getExpectedEnd() {
206            return DateUtils.toDate(expectedEndTS);
207        }
208    
209        public Timestamp getExpectedEndTimestamp() {
210            return this.expectedEndTS;
211        }
212    
213        public void setExpectedEnd(Date expectedEnd) {
214            this.expectedEndTS = DateUtils.convertDateToTimestamp(expectedEnd);
215        }
216    
217        public long getExpectedDuration() {
218            return expectedDuration;
219        }
220    
221        public void setExpectedDuration(long expectedDuration) {
222            this.expectedDuration = expectedDuration;
223        }
224    
225        public String getUser() {
226            return user;
227        }
228    
229        public void setUser(String user) {
230            this.user = user;
231        }
232    
233        public String getUpstreamApps() {
234            return upstreamApps;
235        }
236    
237        public void setUpstreamApps(String upstreamApps) {
238            this.upstreamApps = upstreamApps;
239        }
240    
241        public String getJobData() {
242            return jobData;
243        }
244    
245        public void setJobData(String jobData) {
246            this.jobData = jobData;
247        }
248    
249        public String getSlaConfig() {
250            return slaConfig;
251        }
252    
253        public void setSlaConfig(String configStr) {
254            this.slaConfig = configStr;
255            slaConfigStringToMap();
256        }
257    
258        public String getNotificationMsg() {
259            return notificationMsg;
260        }
261    
262        public void setNotificationMsg(String notificationMsg) {
263            this.notificationMsg = notificationMsg;
264        }
265    
266        public String getAlertEvents() {
267            return slaConfigMap.get(ALERT_EVENTS);
268        }
269    
270        public void setAlertEvents(String alertEvents) {
271            slaConfigMap.put(ALERT_EVENTS, alertEvents);
272            slaConfig = slaConfigMapToString();
273        }
274    
275        public String getAlertContact() {
276            return slaConfigMap.get(ALERT_CONTACT);
277        }
278    
279        public void setAlertContact(String alertContact) {
280            slaConfigMap.put(ALERT_CONTACT, alertContact);
281            slaConfig = slaConfigMapToString();
282        }
283    
284        public Map<String, String> getSlaConfigMap() {
285            return slaConfigMap;
286        }
287    
288        private void slaConfigStringToMap() {
289            if (slaConfig != null) {
290                String[] splitString = slaConfig.split("},");
291                for (String config : splitString) {
292                    String[] pair = config.split("=");
293                    if (pair.length == 2) {
294                        slaConfigMap.put(pair[0].substring(1), pair[1]);
295                    }
296                }
297            }
298        }
299        
300        public String slaConfigMapToString() {
301            StringBuilder sb = new StringBuilder();
302            for (Entry<String, String> e : slaConfigMap.entrySet()) {
303                sb.append("{" + e.getKey() + "=" + e.getValue() + "},");
304            }
305            return sb.toString();
306        }
307    
308        @Override
309        public JSONObject toJSONObject() {
310            // TODO
311            return null;
312        }
313    
314        @Override
315        public JSONObject toJSONObject(String timeZoneId) {
316            // TODO
317            return null;
318        }
319    
320        /**
321         * Convert a SLARegistrationBean list into a JSONArray.
322         *
323         * @param events SLARegistrationBean list.
324         * @param timeZoneId time zone to use for dates in the JSON array.
325         * @return the corresponding JSON array.
326         */
327        @SuppressWarnings("unchecked")
328        public static JSONArray toJSONArray(List<? extends SLARegistrationBean> events, String timeZoneId) {
329            JSONArray array = new JSONArray();
330            if (events != null) {
331                for (SLARegistrationBean node : events) {
332                    array.add(node.toJSONObject(timeZoneId));
333                }
334            }
335            return array;
336        }
337    
338        /**
339         * Convert a JSONArray into a SLARegistrationBean list.
340         *
341         * @param array JSON array.
342         * @return the corresponding SLA SLARegistrationBean list.
343         */
344        public static List<SLARegistrationBean> fromJSONArray(JSONArray array) {
345            List<SLARegistrationBean> list = new ArrayList<SLARegistrationBean>();
346            for (Object obj : array) {
347                list.add(new SLARegistrationBean((JSONObject) obj));
348            }
349            return list;
350        }
351    
352        public MessageType getMsgType(){
353            return this.msgType;
354        }
355    
356        public void setMsgType(MessageType msgType){
357            this.msgType = msgType;
358        }
359    }