View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.job.api;
23  
24  import static org.opencastproject.util.data.Collections.nullToNil;
25  
26  import org.opencastproject.job.api.Incident.Severity;
27  import org.opencastproject.util.data.Tuple;
28  import org.opencastproject.util.jaxb.UtcTimestampAdapter;
29  
30  import java.util.Date;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Map.Entry;
34  import java.util.stream.Collectors;
35  
36  import javax.xml.bind.annotation.XmlAccessType;
37  import javax.xml.bind.annotation.XmlAccessorType;
38  import javax.xml.bind.annotation.XmlAttribute;
39  import javax.xml.bind.annotation.XmlElement;
40  import javax.xml.bind.annotation.XmlElementWrapper;
41  import javax.xml.bind.annotation.XmlRootElement;
42  import javax.xml.bind.annotation.XmlType;
43  import javax.xml.bind.annotation.XmlValue;
44  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
45  
46  /** 1:1 serialization of a {@link Incident}. */
47  @XmlAccessorType(XmlAccessType.NONE)
48  @XmlType(name = "incident", namespace = "http://job.opencastproject.org")
49  @XmlRootElement(name = "incident", namespace = "http://job.opencastproject.org")
50  public final class JaxbIncident {
51    @XmlElement(name = "id")
52    private long id;
53  
54    @XmlElement(name = "jobId")
55    private long jobId;
56  
57    @XmlElement(name = "serviceType")
58    private String serviceType;
59  
60    @XmlElement(name = "processingHost")
61    private String processingHost;
62  
63    @XmlElement(name = "timestamp")
64    @XmlJavaTypeAdapter(UtcTimestampAdapter.class)
65    private Date timestamp;
66  
67    @XmlElement(name = "severity")
68    private Severity severity;
69  
70    @XmlElement(name = "code")
71    private String code;
72  
73    @XmlElementWrapper(name = "descriptionParameters")
74    @XmlElement(name = "param")
75    private List<Param> descriptionParameters;
76  
77    @XmlElementWrapper(name = "details")
78    @XmlElement(name = "detail")
79    private List<JaxbIncidentDetail> details;
80  
81    /** Constructor for JAXB */
82    public JaxbIncident() {
83    }
84  
85    public JaxbIncident(Incident incident) {
86      this.id = incident.getId();
87      this.jobId = incident.getJobId();
88      this.serviceType = incident.getServiceType();
89      this.processingHost = incident.getProcessingHost();
90      this.timestamp = new Date(incident.getTimestamp().getTime());
91      this.severity = incident.getSeverity();
92      this.code = incident.getCode();
93      this.descriptionParameters = incident.getDescriptionParameters().entrySet().stream()
94          .map(Param::mk)
95          .collect(Collectors.toList());
96      this.details = incident.getDetails().stream()
97          .map(JaxbIncidentDetail::new)
98          .collect(Collectors.toList());
99    }
100 
101   public Incident toIncident() {
102     List<Tuple<String, String>> mappedDetails = nullToNil(details).stream()
103         .map(JaxbIncidentDetail::toDetail)
104         .collect(Collectors.toList());
105 
106     Map<String, String> paramMap = nullToNil(descriptionParameters).stream()
107         .collect(Collectors.toMap(
108             Param::getName,
109             Param::getValue
110         ));
111 
112     return new IncidentImpl(
113         id,
114         jobId,
115         serviceType,
116         processingHost,
117         timestamp,
118         severity,
119         code,
120         mappedDetails,
121         paramMap
122     );
123   }
124 
125   /**
126    * An description parameter. To read about why this class is necessary, see http://java.net/jira/browse/JAXB-223
127    */
128   @XmlAccessorType(XmlAccessType.FIELD)
129   @XmlType(name = "param", namespace = "http://job.opencastproject.org")
130   public static final class Param {
131     @XmlAttribute(name = "name")
132     private String name;
133 
134     @XmlValue
135     private String value;
136 
137     public static Param mk(Entry<String, String> entry) {
138       final Param dto = new Param();
139       dto.name = entry.getKey();
140       dto.value = entry.getValue();
141       return dto;
142     }
143 
144     public String getName() {
145       return name;
146     }
147 
148     public String getValue() {
149       return value;
150     }
151   }
152 }