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; 019 020 import java.io.DataInput; 021 import java.io.DataOutput; 022 import java.io.IOException; 023 import java.text.MessageFormat; 024 import java.util.ArrayList; 025 import java.util.Date; 026 import java.util.List; 027 028 import javax.persistence.Basic; 029 import javax.persistence.Column; 030 import javax.persistence.Entity; 031 import javax.persistence.NamedQueries; 032 import javax.persistence.NamedQuery; 033 034 import org.apache.hadoop.io.Writable; 035 import org.apache.oozie.client.SLAEvent; 036 import org.apache.oozie.client.rest.JsonSLAEvent; 037 import org.apache.oozie.util.DateUtils; 038 import org.apache.oozie.util.XLog; 039 import org.jdom.Element; 040 import org.json.simple.JSONArray; 041 import org.json.simple.JSONObject; 042 043 @Entity 044 @NamedQueries({ 045 @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"), 046 @NamedQuery(name = "GET_SLA_EVENTS", query = "select OBJECT(w) from SLAEventBean w") }) 047 048 public class SLAEventBean extends JsonSLAEvent implements Writable { 049 050 @Basic 051 @Column(name = "job_status") 052 private String jobStatusStr = null; 053 054 @Basic 055 @Column(name = "app_type") 056 private String appTypeStr = null; 057 058 @Basic 059 @Column(name = "expected_start") 060 private java.sql.Timestamp expectedStartTS = null; 061 062 @Basic 063 @Column(name = "expected_end") 064 private java.sql.Timestamp expectedEndTS = null; 065 066 @Basic 067 @Column(name = "status_timestamp") 068 private java.sql.Timestamp statusTimestampTS = null; 069 070 @Basic 071 @Column(name = "event_type") 072 private String eventType = null; 073 074 public SLAEventBean() { 075 076 } 077 078 public String getJobStatusStr() { 079 return jobStatusStr; 080 } 081 082 public void setJobStatusStr(String jobStatusStr) { 083 this.jobStatusStr = jobStatusStr; 084 } 085 086 @Override 087 public Status getJobStatus() { 088 return Status.valueOf(this.jobStatusStr); 089 } 090 091 @Override 092 public void setJobStatus(Status jobStatus) { 093 super.setJobStatus(jobStatus); 094 this.jobStatusStr = jobStatus.toString(); 095 } 096 097 public String getAppTypeStr() { 098 return appTypeStr; 099 } 100 101 public void setAppTypeStr(String appTypeStr) { 102 this.appTypeStr = appTypeStr; 103 } 104 105 @Override 106 public SlaAppType getAppType() { 107 return SlaAppType.valueOf(appTypeStr); 108 } 109 110 @Override 111 public void setAppType(SlaAppType appType) { 112 super.setAppType(appType); 113 this.appTypeStr = appType.toString(); 114 } 115 116 public java.sql.Timestamp getExpectedStartTS() { 117 return expectedStartTS; 118 } 119 120 @Override 121 public Date getExpectedStart() { 122 return DateUtils.toDate(expectedStartTS); 123 } 124 125 @Override 126 public void setExpectedStart(Date expectedStart) { 127 super.setExpectedStart(expectedStart); 128 this.expectedStartTS = DateUtils.convertDateToTimestamp(expectedStart); 129 } 130 131 public java.sql.Timestamp getExpectedEndTS() { 132 return expectedEndTS; 133 } 134 135 @Override 136 public Date getExpectedEnd() { 137 return DateUtils.toDate(expectedEndTS); 138 } 139 140 @Override 141 public void setExpectedEnd(Date expectedEnd) { 142 super.setExpectedEnd(expectedEnd); 143 this.expectedEndTS = DateUtils.convertDateToTimestamp(expectedEnd); 144 } 145 146 public java.sql.Timestamp getStatusTimestampTS() { 147 return statusTimestampTS; 148 } 149 150 @Override 151 public Date getStatusTimestamp() { 152 return DateUtils.toDate(statusTimestampTS); 153 } 154 155 @Override 156 public void setStatusTimestamp(Date statusTimestamp) { 157 super.setStatusTimestamp(statusTimestamp); 158 this.statusTimestampTS = DateUtils.convertDateToTimestamp(statusTimestamp); 159 } 160 161 public String getEventType() { 162 return eventType; 163 } 164 165 public void setEventType(String eventType) { 166 this.eventType = eventType; 167 } 168 169 @Override 170 public void readFields(DataInput arg0) throws IOException { 171 // TODO Auto-generated method stub 172 173 } 174 175 @Override 176 public void write(DataOutput arg0) throws IOException { 177 // TODO Auto-generated method stub 178 179 } 180 181 @Override 182 public String toString() { 183 return MessageFormat.format("Event id[{0}] status[{1}]", getEvent_id(), getJobStatus()); 184 } 185 186 /** 187 * Convert a SLAEvent list into a JSONArray. 188 * 189 * @param SLAEVent list. 190 * @param timeZoneId time zone to use for dates in the JSON array. 191 * @return the corresponding JSON array. 192 */ 193 @SuppressWarnings("unchecked") 194 public static JSONArray toJSONArray(List<? extends SLAEventBean> events, String timeZoneId) { 195 JSONArray array = new JSONArray(); 196 if (events != null) { 197 for (JsonSLAEvent node : events) { 198 array.add(node.toJSONObject(timeZoneId)); 199 } 200 } 201 return array; 202 } 203 204 /** 205 * Convert a JSONArray into a SLAEvent list. 206 * 207 * @param array JSON array. 208 * @return the corresponding SLA event list. 209 */ 210 @SuppressWarnings("unchecked") 211 public static List<SLAEvent> fromJSONArray(JSONArray array) { 212 List<SLAEvent> list = new ArrayList<SLAEvent>(); 213 for (Object obj : array) { 214 list.add(new JsonSLAEvent((JSONObject) obj)); 215 } 216 return list; 217 } 218 219 public Element toXml() { 220 Element retElem = null; 221 if (getJobStatus() == Status.CREATED) { 222 retElem = getRegistrationEvent("event"); 223 } 224 else { 225 retElem = getStatusEvent("event"); 226 } 227 return retElem; 228 } 229 230 private Element getRegistrationEvent(String tag) { 231 Element eReg = new Element(tag); 232 eReg.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id()))); 233 Element e = new Element("registration"); 234 e.addContent(createATagElement("sla-id", getSlaId())); 235 e.addContent(createATagElement("app-type", getAppType().toString())); 236 e.addContent(createATagElement("app-name", getAppName())); 237 e.addContent(createATagElement("user", getUser())); 238 e.addContent(createATagElement("group", getGroupName())); 239 e.addContent(createATagElement("parent-sla-id", String 240 .valueOf(getParentSlaId()))); 241 e.addContent(createATagElement("expected-start", 242 getDateString(getExpectedStart()))); 243 e.addContent(createATagElement("expected-end", 244 getDateString(getExpectedEnd()))); 245 e.addContent(createATagElement("status-timestamp", 246 getDateString(getStatusTimestamp()))); 247 e.addContent(createATagElement("notification-msg", getNotificationMsg())); 248 249 e.addContent(createATagElement("alert-contact", getAlertContact())); 250 e.addContent(createATagElement("dev-contact", getDevContact())); 251 e.addContent(createATagElement("qa-contact", getQaContact())); 252 e.addContent(createATagElement("se-contact", getSeContact())); 253 254 e.addContent(createATagElement("alert-percentage", getAlertPercentage())); 255 e.addContent(createATagElement("alert-frequency", getAlertFrequency())); 256 257 e.addContent(createATagElement("upstream-apps", getUpstreamApps())); 258 e.addContent(createATagElement("job-status", getJobStatus().toString())); 259 e.addContent(createATagElement("job-data", getJobData())); 260 eReg.addContent(e); 261 return eReg; 262 } 263 264 private Element getStatusEvent(String tag) { 265 Element eStat = new Element(tag); 266 eStat.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id()))); 267 Element e = new Element("status"); 268 e.addContent(createATagElement("sla-id", getSlaId())); 269 e.addContent(createATagElement("status-timestamp", getDateString(getStatusTimestamp()))); 270 e.addContent(createATagElement("job-status", getJobStatus().toString())); 271 e.addContent(createATagElement("job-data", getJobData())); 272 e.addContent(createATagElement("user", getUser())); 273 e.addContent(createATagElement("group", getGroupName())); 274 eStat.addContent(e); 275 return eStat; 276 } 277 278 private Element createATagElement(String tag, String content) { 279 if (content == null) { 280 content = ""; 281 } 282 Element e = new Element(tag); 283 e.addContent(content); 284 return e; 285 } 286 287 private Element createATagElement(String tag, Element content) { 288 Element e = new Element(tag); 289 e.addContent(content); 290 return e; 291 } 292 293 private String getDateString(Date d) { 294 try { 295 return DateUtils.formatDateOozieTZ(d); 296 } 297 catch (Exception e) { 298 e.printStackTrace(); 299 XLog.getLog(getClass()).error("Date formatting error " + d, e); 300 throw new RuntimeException("Date formatting error " + d + e); 301 } 302 } 303 304 }