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