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