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  
20  import org.apache.commons.lang.StringUtils;
21  import org.dom4j.Node;
22  import org.rundeck.api.domain.RundeckEvent;
23  import org.rundeck.api.domain.RundeckEvent.EventStatus;
24  import org.rundeck.api.domain.RundeckEvent.NodeSummary;
25  
26  /**
27   * Parser for a single {@link RundeckEvent}
28   *
29   * @author Vincent Behar
30   */
31  public class EventParser extends BaseXpathParser<RundeckEvent> {
32  
33  
34      public EventParser(final String xpath) {
35          super(xpath);
36      }
37  
38      public EventParser() {
39          super();
40      }
41  
42      @Override
43      public RundeckEvent parse(Node eventNode) {
44  
45          RundeckEvent event = new RundeckEvent();
46  
47          event.setTitle(StringUtils.trimToNull(eventNode.valueOf("title")));
48          try {
49              event.setStatus(EventStatus.valueOf(StringUtils.upperCase(eventNode.valueOf("status"))));
50          } catch (IllegalArgumentException e) {
51              event.setStatus(null);
52          }
53          event.setSummary(StringUtils.trimToNull(eventNode.valueOf("summary")));
54  
55          NodeSummary nodeSummary = new NodeSummary();
56          nodeSummary.setSucceeded(Integer.valueOf(eventNode.valueOf("node-summary/@succeeded")));
57          nodeSummary.setFailed(Integer.valueOf(eventNode.valueOf("node-summary/@failed")));
58          nodeSummary.setTotal(Integer.valueOf(eventNode.valueOf("node-summary/@total")));
59          event.setNodeSummary(nodeSummary);
60  
61          event.setUser(StringUtils.trimToNull(eventNode.valueOf("user")));
62          event.setProject(StringUtils.trimToNull(eventNode.valueOf("project")));
63          String startedAt = StringUtils.trimToNull(eventNode.valueOf("@starttime"));
64          if (startedAt != null) {
65              event.setStartedAt(new Date(Long.valueOf(startedAt)));
66          }
67          String endedAt = StringUtils.trimToNull(eventNode.valueOf("@endtime"));
68          if (endedAt != null) {
69              event.setEndedAt(new Date(Long.valueOf(endedAt)));
70          }
71          event.setAbortedBy(StringUtils.trimToNull(eventNode.valueOf("abortedby")));
72          try {
73              event.setExecutionId(Long.valueOf(eventNode.valueOf("execution/@id")));
74          } catch (NumberFormatException e) {
75              event.setExecutionId(null);
76          }
77          event.setJobId(StringUtils.trimToNull(eventNode.valueOf("job/@id")));
78  
79          return event;
80      }
81  
82  }