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