View Javadoc

1   /*
2    * Copyright 2011 Vincent Behar
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rundeck.api.parser;
17  
18  import java.util.Date;
19  import org.apache.commons.lang.StringUtils;
20  import org.dom4j.Node;
21  import org.rundeck.api.domain.RundeckEvent;
22  import org.rundeck.api.domain.RundeckEvent.EventStatus;
23  import org.rundeck.api.domain.RundeckEvent.NodeSummary;
24  
25  /**
26   * Parser for a single {@link RundeckEvent}
27   * 
28   * @author Vincent Behar
29   */
30  public class EventParser implements XmlNodeParser<RundeckEvent> {
31  
32      private String xpath;
33  
34      public EventParser() {
35          super();
36      }
37  
38      /**
39       * @param xpath of the event element if it is not the root node
40       */
41      public EventParser(String xpath) {
42          super();
43          this.xpath = xpath;
44      }
45  
46      @Override
47      public RundeckEvent parseXmlNode(Node node) {
48          Node eventNode = xpath != null ? node.selectSingleNode(xpath) : node;
49  
50          RundeckEvent event = new RundeckEvent();
51  
52          event.setTitle(StringUtils.trimToNull(eventNode.valueOf("title")));
53          try {
54              event.setStatus(EventStatus.valueOf(StringUtils.upperCase(eventNode.valueOf("status"))));
55          } catch (IllegalArgumentException e) {
56              event.setStatus(null);
57          }
58          event.setSummary(StringUtils.trimToNull(eventNode.valueOf("summary")));
59  
60          NodeSummary nodeSummary = new NodeSummary();
61          nodeSummary.setSucceeded(Integer.valueOf(eventNode.valueOf("node-summary/@succeeded")));
62          nodeSummary.setFailed(Integer.valueOf(eventNode.valueOf("node-summary/@failed")));
63          nodeSummary.setTotal(Integer.valueOf(eventNode.valueOf("node-summary/@total")));
64          event.setNodeSummary(nodeSummary);
65  
66          event.setUser(StringUtils.trimToNull(eventNode.valueOf("user")));
67          event.setProject(StringUtils.trimToNull(eventNode.valueOf("project")));
68          String startedAt = StringUtils.trimToNull(eventNode.valueOf("@starttime"));
69          if (startedAt != null) {
70              event.setStartedAt(new Date(Long.valueOf(startedAt)));
71          }
72          String endedAt = StringUtils.trimToNull(eventNode.valueOf("@endtime"));
73          if (endedAt != null) {
74              event.setEndedAt(new Date(Long.valueOf(endedAt)));
75          }
76          event.setAbortedBy(StringUtils.trimToNull(eventNode.valueOf("abortedby")));
77          try {
78              event.setExecutionId(Long.valueOf(eventNode.valueOf("execution/@id")));
79          } catch (NumberFormatException e) {
80              event.setExecutionId(null);
81          }
82          event.setJobId(StringUtils.trimToNull(eventNode.valueOf("job/@id")));
83  
84          return event;
85      }
86  
87  }