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;
020
021import java.io.DataInput;
022import java.io.DataOutput;
023import java.io.IOException;
024import java.text.MessageFormat;
025import java.util.Date;
026
027import javax.persistence.Basic;
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.NamedQueries;
031import javax.persistence.NamedQuery;
032
033import org.apache.hadoop.io.Writable;
034import org.apache.oozie.client.rest.JsonSLAEvent;
035import org.apache.oozie.util.DateUtils;
036import org.apache.oozie.util.XLog;
037import org.jdom.Element;
038
039@Deprecated
040@Entity
041@NamedQueries({
042        @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"),
043        @NamedQuery(name = "GET_SLA_EVENTS", query = "select OBJECT(w) from SLAEventBean w") })
044
045public 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(), getJobStatus());
181    }
182
183    public Element toXml() {
184        Element retElem = null;
185        if (getJobStatus() == Status.CREATED) {
186            retElem = getRegistrationEvent("event");
187        }
188        else {
189            retElem = getStatusEvent("event");
190        }
191        return retElem;
192    }
193
194    private Element getRegistrationEvent(String tag) {
195        Element eReg = new Element(tag);
196        eReg.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id())));
197        Element e = new Element("registration");
198        e.addContent(createATagElement("sla-id", getSlaId()));
199        e.addContent(createATagElement("app-type", getAppType().toString()));
200        e.addContent(createATagElement("app-name", getAppName()));
201        e.addContent(createATagElement("user", getUser()));
202        e.addContent(createATagElement("group", getGroupName()));
203        e.addContent(createATagElement("parent-sla-id", String
204                .valueOf(getParentSlaId())));
205        e.addContent(createATagElement("expected-start",
206                                       getDateString(getExpectedStart())));
207        e.addContent(createATagElement("expected-end",
208                                       getDateString(getExpectedEnd())));
209        e.addContent(createATagElement("status-timestamp",
210                                       getDateString(getStatusTimestamp())));
211        e.addContent(createATagElement("notification-msg", getNotificationMsg()));
212
213        e.addContent(createATagElement("alert-contact", getAlertContact()));
214        e.addContent(createATagElement("dev-contact", getDevContact()));
215        e.addContent(createATagElement("qa-contact", getQaContact()));
216        e.addContent(createATagElement("se-contact", getSeContact()));
217
218        e.addContent(createATagElement("alert-percentage", getAlertPercentage()));
219        e.addContent(createATagElement("alert-frequency", getAlertFrequency()));
220
221        e.addContent(createATagElement("upstream-apps", getUpstreamApps()));
222        e.addContent(createATagElement("job-status", getJobStatus().toString()));
223        e.addContent(createATagElement("job-data", getJobData()));
224        eReg.addContent(e);
225        return eReg;
226    }
227
228    private Element getStatusEvent(String tag) {
229        Element eStat = new Element(tag);
230        eStat.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id())));
231        Element e = new Element("status");
232        e.addContent(createATagElement("sla-id", getSlaId()));
233        e.addContent(createATagElement("status-timestamp", getDateString(getStatusTimestamp())));
234        e.addContent(createATagElement("job-status", getJobStatus().toString()));
235        e.addContent(createATagElement("job-data", getJobData()));
236        e.addContent(createATagElement("user", getUser()));
237        e.addContent(createATagElement("group", getGroupName()));
238        e.addContent(createATagElement("app-name", getAppName()));
239        eStat.addContent(e);
240        return eStat;
241    }
242
243    private Element createATagElement(String tag, String content) {
244        if (content == null) {
245            content = "";
246        }
247        Element e = new Element(tag);
248        e.addContent(content);
249        return e;
250    }
251
252    private String getDateString(Date d) {
253        try {
254            return DateUtils.formatDateOozieTZ(d);
255        }
256        catch (Exception e) {
257            e.printStackTrace();
258            XLog.getLog(getClass()).error("Date formatting error " + d, e);
259            throw new RuntimeException("Date formatting error " + d + e);
260        }
261    }
262
263}