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    
046        @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"),
047        @NamedQuery(name = "GET_SLA_EVENTS", query = "select OBJECT(w) from SLAEventBean w")})
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(),
184                                        getJobStatus());
185        }
186    
187        /**
188         * Convert a SLAEvent list into a JSONArray.
189         *
190         * @param SLAEVent list.
191         * @param timeZoneId time zone to use for dates in the JSON array.
192         * @return the corresponding JSON array.
193         */
194        @SuppressWarnings("unchecked")
195        public static JSONArray toJSONArray(List<? extends SLAEventBean> events, String timeZoneId) {
196            JSONArray array = new JSONArray();
197            if (events != null) {
198                for (JsonSLAEvent node : events) {
199                    array.add(node.toJSONObject(timeZoneId));
200                }
201            }
202            return array;
203        }
204    
205        /**
206         * Convert a JSONArray into a SLAEvent list.
207         *
208         * @param array JSON array.
209         * @return the corresponding SLA event list.
210         */
211        @SuppressWarnings("unchecked")
212        public static List<SLAEvent> fromJSONArray(JSONArray array) {
213            List<SLAEvent> list = new ArrayList<SLAEvent>();
214            for (Object obj : array) {
215                list.add(new JsonSLAEvent((JSONObject) obj));
216            }
217            return list;
218        }
219    
220        public Element toXml() {
221            Element retElem = null;
222            if (getJobStatus() == Status.CREATED) {
223                retElem = getRegistrationEvent("event");
224            }
225            else {
226                retElem = getStatusEvent("event");
227            }
228            return retElem;
229        }
230    
231        private Element getRegistrationEvent(String tag) {
232            Element eReg = new Element(tag);
233            eReg.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id())));
234            Element e = new Element("registration");
235            e.addContent(createATagElement("sla-id", getSlaId()));
236            //e.addContent(createATagElement("sla-id", String.valueOf(getSlaId())));
237            e.addContent(createATagElement("app-type", getAppType().toString()));
238            e.addContent(createATagElement("app-name", getAppName()));
239            e.addContent(createATagElement("user", getUser()));
240            e.addContent(createATagElement("group", getGroupName()));
241            e.addContent(createATagElement("parent-sla-id", String
242                    .valueOf(getParentSlaId())));
243            e.addContent(createATagElement("expected-start",
244                                           getDateString(getExpectedStart())));
245            e.addContent(createATagElement("expected-end",
246                                           getDateString(getExpectedEnd())));
247            e.addContent(createATagElement("status-timestamp",
248                                           getDateString(getStatusTimestamp())));
249            e.addContent(createATagElement("notification-msg", getNotificationMsg()));
250    
251            e.addContent(createATagElement("alert-contact", getAlertContact()));
252            e.addContent(createATagElement("dev-contact", getDevContact()));
253            e.addContent(createATagElement("qa-contact", getQaContact()));
254            e.addContent(createATagElement("se-contact", getSeContact()));
255    
256            e.addContent(createATagElement("alert-percentage", getAlertPercentage()));
257            e.addContent(createATagElement("alert-frequency", getAlertFrequency()));
258    
259            e.addContent(createATagElement("upstream-apps", getUpstreamApps()));
260            e.addContent(createATagElement("job-status", getJobStatus().toString()));
261            e.addContent(createATagElement("job-data", getJobData()));
262            eReg.addContent(e);
263            return eReg;
264        }
265    
266        private Element getStatusEvent(String tag) {
267            Element eStat = new Element(tag);
268            eStat.addContent(createATagElement("sequence-id", String.valueOf(getEvent_id())));
269            Element e = new Element("status");
270            e.addContent(createATagElement("sla-id", getSlaId()));
271            e.addContent(createATagElement("status-timestamp",
272                                           getDateString(getStatusTimestamp())));
273            e.addContent(createATagElement("job-status", getJobStatus().toString()));
274            e.addContent(createATagElement("job-data", getJobData()));
275            eStat.addContent(e);
276            return eStat;
277        }
278    
279        private Element createATagElement(String tag, String content) {
280            if (content == null) {
281                content = "";
282            }
283            Element e = new Element(tag);
284            e.addContent(content);
285            return e;
286        }
287    
288        private Element createATagElement(String tag, Element content) {
289            Element e = new Element(tag);
290            e.addContent(content);
291            return e;
292        }
293    
294        private String getDateString(Date d) {
295            try {
296                return DateUtils.formatDateOozieTZ(d);
297            }
298            catch (Exception e) {
299                e.printStackTrace();
300                XLog.getLog(getClass()).error("Date formatting error " + d, e);
301                throw new RuntimeException("Date formatting error " + d + e);
302            }
303        }
304    
305    }