001 /**
002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License. See accompanying LICENSE file.
014 */
015 package org.apache.oozie;
016
017 import java.io.DataInput;
018 import java.io.DataOutput;
019 import java.io.IOException;
020 import java.sql.Timestamp;
021 import java.text.MessageFormat;
022 import java.util.ArrayList;
023 import java.util.Date;
024 import java.util.List;
025
026 import javax.persistence.Basic;
027 import javax.persistence.Column;
028 import javax.persistence.Entity;
029 import javax.persistence.NamedQueries;
030 import javax.persistence.NamedQuery;
031
032 import org.apache.hadoop.io.Writable;
033 import org.apache.oozie.client.SLAEvent;
034 import org.apache.oozie.client.rest.JsonSLAEvent;
035 import org.apache.oozie.util.DateUtils;
036 import org.apache.oozie.util.XLog;
037 import org.jdom.Element;
038 import org.json.simple.JSONArray;
039 import org.json.simple.JSONObject;
040
041 @Entity
042 @NamedQueries({
043
044 @NamedQuery(name = "GET_SLA_EVENT_NEWER_SEQ_LIMITED", query = "select OBJECT(w) from SLAEventBean w where w.event_id > :id order by w.event_id")})
045 public class SLAEventBean extends JsonSLAEvent implements Writable {
046
047 @Basic
048 @Column(name = "job_status")
049 private String jobStatusStr = null;
050
051 @Basic
052 @Column(name = "app_type")
053 private String appTypeStr = null;
054
055 @Basic
056 @Column(name = "expected_start")
057 private java.sql.Timestamp expectedStartTS = null;
058
059 @Basic
060 @Column(name = "expected_end")
061 private java.sql.Timestamp expectedEndTS = null;
062
063 @Basic
064 @Column(name = "status_timestamp")
065 private java.sql.Timestamp statusTimestampTS = null;
066
067 @Basic
068 @Column(name = "event_type")
069 private String eventType = null;
070
071 public SLAEventBean() {
072
073 }
074
075 public String getJobStatusStr() {
076 return jobStatusStr;
077 }
078
079 public void setJobStatusStr(String jobStatusStr) {
080 this.jobStatusStr = jobStatusStr;
081 }
082
083 public Status getJobStatus() {
084 return Status.valueOf(this.jobStatusStr);
085 }
086
087 public void setJobStatus(Status jobStatus) {
088 super.setJobStatus(jobStatus);
089 this.jobStatusStr = jobStatus.toString();
090 }
091
092 public String getAppTypeStr() {
093 return appTypeStr;
094 }
095
096 public void setAppTypeStr(String appTypeStr) {
097 this.appTypeStr = appTypeStr;
098 }
099
100 public SlaAppType getAppType() {
101 return SlaAppType.valueOf(appTypeStr);
102 }
103
104 public void setAppType(SlaAppType appType) {
105 super.setAppType(appType);
106 this.appTypeStr = appType.toString();
107 }
108
109 public java.sql.Timestamp getExpectedStartTS() {
110 return expectedStartTS;
111 }
112
113 public Date getExpectedStart() {
114 return DateUtils.toDate(expectedStartTS);
115 }
116
117 public void setExpectedStart(Date expectedStart) {
118 super.setExpectedStart(expectedStart);
119 this.expectedStartTS = DateUtils.convertDateToTimestamp(expectedStart);
120 }
121
122 public java.sql.Timestamp getExpectedEndTS() {
123 return expectedEndTS;
124 }
125
126 public Date getExpectedEnd() {
127 return DateUtils.toDate(expectedEndTS);
128 }
129
130 public void setExpectedEnd(Date expectedEnd) {
131 super.setExpectedEnd(expectedEnd);
132 this.expectedEndTS = DateUtils.convertDateToTimestamp(expectedEnd);
133 }
134
135 public java.sql.Timestamp getStatusTimestampTS() {
136 return statusTimestampTS;
137 }
138
139 public Date getStatusTimestamp() {
140 return DateUtils.toDate(statusTimestampTS);
141 }
142
143 public void setStatusTimestamp(Date statusTimestamp) {
144 super.setStatusTimestamp(statusTimestamp);
145 this.statusTimestampTS = DateUtils.convertDateToTimestamp(statusTimestamp);
146 }
147
148 public String getEventType() {
149 return eventType;
150 }
151
152 public void setEventType(String eventType) {
153 this.eventType = eventType;
154 }
155
156 @Override
157 public void readFields(DataInput arg0) throws IOException {
158 // TODO Auto-generated method stub
159
160 }
161
162 @Override
163 public void write(DataOutput arg0) throws IOException {
164 // TODO Auto-generated method stub
165
166 }
167
168 public String toString() {
169 return MessageFormat.format("Event id[{0}] status[{1}]", getEvent_id(),
170 getJobStatus());
171 }
172
173 /**
174 * Convert a SLAEvent list into a JSONArray.
175 *
176 * @param SLAEVent list.
177 * @return the corresponding JSON array.
178 */
179 @SuppressWarnings("unchecked")
180 public static JSONArray toJSONArray(List<? extends SLAEventBean> events) {
181 JSONArray array = new JSONArray();
182 if (events != null) {
183 for (JsonSLAEvent node : events) {
184 array.add(node.toJSONObject());
185 }
186 }
187 return array;
188 }
189
190 /**
191 * Convert a JSONArray into a SLAEvent list.
192 *
193 * @param array JSON array.
194 * @return the corresponding SLA event list.
195 */
196 @SuppressWarnings("unchecked")
197 public static List<SLAEvent> fromJSONArray(JSONArray array) {
198 List<SLAEvent> list = new ArrayList<SLAEvent>();
199 for (Object obj : array) {
200 list.add(new JsonSLAEvent((JSONObject) obj));
201 }
202 return list;
203 }
204
205 /* public String toXml2() {
206 String ret = "";
207 if (getJobStatus() == Status.CREATED) {
208 ret = getRegistrationEventXml();
209 }
210 else {
211 ret = getStatusEventXml();
212 }
213 return createATag("event", ret);
214 }
215
216 private String getStatusEventXml() {
217 StringBuilder statXml = new StringBuilder();
218 statXml
219 .append(createATag("sequence-id", String.valueOf(getEvent_id())));
220 statXml.append("<status>");
221 statXml.append(createATag("sla-id", getSlaId()));
222 statXml.append(createATag("status-timestamp",
223 getDateString(getStatusTimestamp())));
224 statXml.append(createATag("job-status", getJobStatus().toString()));
225 statXml.append("</status>");
226 return statXml.toString();
227 }
228
229 private String getRegistrationEventXml() {
230 StringBuilder regXml = new StringBuilder();
231 regXml.append(createATag("sequence-id", String.valueOf(getEvent_id())));
232 regXml.append("<registration>");
233 regXml.append(createATag("sla-id", String.valueOf(getSlaId())));
234 regXml.append(createATag("app-type", getAppType().toString()));
235 regXml.append(createATag("app-name", getAppName()));
236 regXml.append(createATag("user", getUser()));
237 regXml.append(createATag("group", getGroupName()));
238 regXml.append(createATag("parent-sla-id", String
239 .valueOf(getParentSlaId())));
240 regXml.append(createATag("expected-start",
241 getDateString(getExpectedStart())));
242 regXml.append(createATag("expected-end",
243 getDateString(getExpectedEnd())));
244 regXml.append(createATag("status-timestamp",
245 getDateString(getStatusTimestamp())));
246 regXml.append(createATag("job-status", getJobStatus().toString()));
247
248 regXml.append(createATag("alert-contact", getAlertContact()));
249 regXml.append(createATag("dev-contact", getDevContact()));
250 regXml.append(createATag("qa-contact", getQaContact()));
251 regXml.append(createATag("se-contact", getSeContact()));
252 regXml.append(createATag("notification-msg", getNotificationMsg()));
253 regXml.append(createATag("alert-percentage", getAlertPercentage()));
254 regXml.append(createATag("alert-frequency", getAlertFrequency()));
255 regXml.append(createATag("upstream-apps", getUpstreamApps()));
256 regXml.append("</registration>");
257 return regXml.toString();
258 }
259 private String createATag(String tag, String content) {
260 if (content == null) {
261 content = "";
262 }
263 return "<" + tag + ">" + content + "</" + tag + ">";
264 }
265 */
266 public Element toXml() {
267 Element retElem = null;
268 if (getJobStatus() == Status.CREATED) {
269 retElem = getRegistrationEvent("event");
270 }
271 else {
272 retElem = getStatusEvent("event");
273 }
274 return retElem;
275 }
276
277 private Element getRegistrationEvent(String tag) {
278 Element eReg = new Element(tag);
279 eReg.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id())));
280 Element e = new Element("registration");
281 e.addContent(createATagElement("sla-id", getSlaId()));
282 //e.addContent(createATagElement("sla-id", String.valueOf(getSlaId())));
283 e.addContent(createATagElement("app-type", getAppType().toString()));
284 e.addContent(createATagElement("app-name", getAppName()));
285 e.addContent(createATagElement("user", getUser()));
286 e.addContent(createATagElement("group", getGroupName()));
287 e.addContent(createATagElement("parent-sla-id", String
288 .valueOf(getParentSlaId())));
289 e.addContent(createATagElement("expected-start",
290 getDateString(getExpectedStart())));
291 e.addContent(createATagElement("expected-end",
292 getDateString(getExpectedEnd())));
293 e.addContent(createATagElement("status-timestamp",
294 getDateString(getStatusTimestamp())));
295 e.addContent(createATagElement("notification-msg", getNotificationMsg()));
296
297 e.addContent(createATagElement("alert-contact", getAlertContact()));
298 e.addContent(createATagElement("dev-contact", getDevContact()));
299 e.addContent(createATagElement("qa-contact", getQaContact()));
300 e.addContent(createATagElement("se-contact", getSeContact()));
301
302 e.addContent(createATagElement("alert-percentage", getAlertPercentage()));
303 e.addContent(createATagElement("alert-frequency", getAlertFrequency()));
304
305 e.addContent(createATagElement("upstream-apps", getUpstreamApps()));
306 e.addContent(createATagElement("job-status", getJobStatus().toString()));
307 e.addContent(createATagElement("job-data", getJobData()));
308 eReg.addContent(e);
309 return eReg;
310 }
311
312 private Element getStatusEvent(String tag) {
313 Element eStat = new Element(tag);
314 eStat.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id())));
315 Element e = new Element("status");
316 e.addContent(createATagElement("sla-id", getSlaId()));
317 e.addContent(createATagElement("status-timestamp",
318 getDateString(getStatusTimestamp())));
319 e.addContent(createATagElement("job-status", getJobStatus().toString()));
320 e.addContent(createATagElement("job-data", getJobData()));
321 eStat.addContent(e);
322 return eStat;
323 }
324
325 private Element createATagElement(String tag, String content) {
326 if (content == null) {
327 content = "";
328 }
329 Element e = new Element(tag);
330 e.addContent(content);
331 return e;
332 }
333
334 private Element createATagElement(String tag, Element content) {
335 Element e = new Element(tag);
336 e.addContent(content);
337 return e;
338 }
339
340 private String getDateString(Date d) {
341 try {
342 return DateUtils.formatDateUTC(d);
343 }
344 catch (Exception e) {
345 // TODO Auto-generated catch block
346 e.printStackTrace();
347 XLog.getLog(getClass()).error("Date formatting error " + d, e);
348 throw new RuntimeException("Date formatting error " + d + e);
349 }
350 }
351
352 }