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