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  import static org.opencastproject.util.data.Monadics.mlist;
26  
27  import org.opencastproject.job.api.Incident.Severity;
28  import org.opencastproject.util.data.Function;
29  import org.opencastproject.util.data.Function2;
30  import org.opencastproject.util.jaxb.UtcTimestampAdapter;
31  
32  import java.util.Date;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Map.Entry;
37  
38  import javax.xml.bind.annotation.XmlAccessType;
39  import javax.xml.bind.annotation.XmlAccessorType;
40  import javax.xml.bind.annotation.XmlAttribute;
41  import javax.xml.bind.annotation.XmlElement;
42  import javax.xml.bind.annotation.XmlElementWrapper;
43  import javax.xml.bind.annotation.XmlRootElement;
44  import javax.xml.bind.annotation.XmlType;
45  import javax.xml.bind.annotation.XmlValue;
46  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
47  
48  /** 1:1 serialization of a {@link Incident}. */
49  @XmlAccessorType(XmlAccessType.NONE)
50  @XmlType(name = "incident", namespace = "http://job.opencastproject.org")
51  @XmlRootElement(name = "incident", namespace = "http://job.opencastproject.org")
52  public final class JaxbIncident {
53    @XmlElement(name = "id")
54    private long id;
55  
56    @XmlElement(name = "jobId")
57    private long jobId;
58  
59    @XmlElement(name = "serviceType")
60    private String serviceType;
61  
62    @XmlElement(name = "processingHost")
63    private String processingHost;
64  
65    @XmlElement(name = "timestamp")
66    @XmlJavaTypeAdapter(UtcTimestampAdapter.class)
67    private Date timestamp;
68  
69    @XmlElement(name = "severity")
70    private Severity severity;
71  
72    @XmlElement(name = "code")
73    private String code;
74  
75    @XmlElementWrapper(name = "descriptionParameters")
76    @XmlElement(name = "param")
77    private List<Param> descriptionParameters;
78  
79    @XmlElementWrapper(name = "details")
80    @XmlElement(name = "detail")
81    private List<JaxbIncidentDetail> details;
82  
83    /** Constructor for JAXB */
84    public JaxbIncident() {
85    }
86  
87    public JaxbIncident(Incident incident) {
88      this.id = incident.getId();
89      this.jobId = incident.getJobId();
90      this.serviceType = incident.getServiceType();
91      this.processingHost = incident.getProcessingHost();
92      this.timestamp = new Date(incident.getTimestamp().getTime());
93      this.severity = incident.getSeverity();
94      this.code = incident.getCode();
95      this.descriptionParameters = mlist(incident.getDescriptionParameters().entrySet()).map(Param.mkFn).value();
96      this.details = mlist(incident.getDetails()).map(JaxbIncidentDetail.mkFn).value();
97    }
98  
99    public static final Function<Incident, JaxbIncident> mkFn = new Function<Incident, JaxbIncident>() {
100     @Override
101     public JaxbIncident apply(Incident incident) {
102       return new JaxbIncident(incident);
103     }
104   };
105 
106   public Incident toIncident() {
107     return new IncidentImpl(id, jobId, serviceType, processingHost, timestamp, severity, code,
108             mlist(nullToNil(details)).map(JaxbIncidentDetail.toDetailFn).value(), mlist(
109                     nullToNil(descriptionParameters)).foldl(new HashMap<>(),
110                     new Function2<Map<String, String>, Param, Map<String, String>>() {
111                       @Override
112                       public Map<String, String> apply(Map<String, String> sum, Param param) {
113                         sum.put(param.getName(), param.getValue());
114                         return sum;
115                       }
116                     }));
117   }
118 
119   public static final Function<JaxbIncident, Incident> toIncidentFn = new Function<JaxbIncident, Incident>() {
120     @Override
121     public Incident apply(JaxbIncident dto) {
122       return dto.toIncident();
123     }
124   };
125 
126   /**
127    * An description parameter. To read about why this class is necessary, see http://java.net/jira/browse/JAXB-223
128    */
129   @XmlAccessorType(XmlAccessType.FIELD)
130   @XmlType(name = "param", namespace = "http://job.opencastproject.org")
131   public static final class Param {
132     @XmlAttribute(name = "name")
133     private String name;
134 
135     @XmlValue
136     private String value;
137 
138     public static Param mk(Entry<String, String> entry) {
139       final Param dto = new Param();
140       dto.name = entry.getKey();
141       dto.value = entry.getValue();
142       return dto;
143     }
144 
145     public String getName() {
146       return name;
147     }
148 
149     public String getValue() {
150       return value;
151     }
152 
153     public static final Function<Entry<String, String>, Param> mkFn = new Function<Entry<String, String>, Param>() {
154       @Override
155       public Param apply(Entry<String, String> entry) {
156         return mk(entry);
157       }
158     };
159   }
160 }