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.serviceregistry.impl;
23  
24  import static org.opencastproject.db.Queries.namedQuery;
25  import static org.opencastproject.util.JsonVal.asString;
26  import static org.opencastproject.util.Jsons.obj;
27  import static org.opencastproject.util.Jsons.p;
28  import static org.opencastproject.util.data.Tuple.tuple;
29  
30  import org.opencastproject.job.api.Incident.Severity;
31  import org.opencastproject.util.JsonObj;
32  import org.opencastproject.util.JsonVal;
33  import org.opencastproject.util.Jsons;
34  import org.opencastproject.util.Jsons.Prop;
35  import org.opencastproject.util.data.Tuple;
36  
37  import org.apache.commons.lang3.tuple.Pair;
38  
39  import java.util.ArrayList;
40  import java.util.Date;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Map.Entry;
45  import java.util.function.Function;
46  
47  import javax.persistence.Access;
48  import javax.persistence.AccessType;
49  import javax.persistence.Column;
50  import javax.persistence.Entity;
51  import javax.persistence.EntityManager;
52  import javax.persistence.GeneratedValue;
53  import javax.persistence.Id;
54  import javax.persistence.Index;
55  import javax.persistence.Lob;
56  import javax.persistence.NamedQueries;
57  import javax.persistence.NamedQuery;
58  import javax.persistence.Table;
59  import javax.persistence.Temporal;
60  import javax.persistence.TemporalType;
61  
62  @Entity(name = "Incident")
63  @Access(AccessType.FIELD)
64  @Table(name = "oc_incident", indexes = {
65      @Index(name = "IX_oc_incident_jobid", columnList = ("jobid")),
66      @Index(name = "IX_oc_incident_severity", columnList = ("severity")) })
67  @NamedQueries({@NamedQuery(name = "Incident.findByJobId",
68                             query = "select a from Incident a where a.jobId = :jobId")})
69  public class IncidentDto {
70    @Id
71    @GeneratedValue
72    @Column(name = "id")
73    private Long id;
74  
75    @Column(name = "jobid")
76    private Long jobId;
77  
78    @Column(name = "timestamp")
79    @Temporal(TemporalType.TIMESTAMP)
80    private Date timestamp;
81  
82    @Column(name = "code")
83    private String code;
84  
85    @Column(name = "severity")
86    private Integer severity;
87  
88    @Lob
89    @Column(name = "parameters", length = 65535)
90    private String parameters;
91  
92    @Lob
93    @Column(name = "details", length = 65535)
94    private String details;
95  
96    /** Constructor method. */
97    public static IncidentDto mk(
98            Long jobId,
99            Date date,
100           String code,
101           Severity severity,
102           Map<String, String> parameters,
103           List<Tuple<String, String>> details) {
104     IncidentDto dto = new IncidentDto();
105     dto.jobId = jobId;
106     dto.timestamp = date;
107     dto.code = code;
108     dto.severity = severity.ordinal();
109 
110     List<Prop> props = new ArrayList<Jsons.Prop>();
111     for (Entry<String, String> entry : parameters.entrySet()) {
112       props.add(p(entry.getKey(), entry.getValue()));
113     }
114     dto.parameters = obj(props.toArray(new Prop[props.size()])).toJson();
115 
116     props = new ArrayList<Jsons.Prop>();
117     for (Tuple<String, String> t : details) {
118       props.add(p(t.getA(), t.getB()));
119     }
120     dto.details = obj(props.toArray(new Prop[props.size()])).toJson();
121     return dto;
122   }
123 
124   public Long getId() {
125     return id;
126   }
127 
128   public long getJobId() {
129     return jobId;
130   }
131 
132   /** @see org.opencastproject.job.api.Incident#getTimestamp() */
133   public Date getTimestamp() {
134     return timestamp;
135   }
136 
137   /** @see org.opencastproject.job.api.Incident#getSeverity() */
138   public Severity getSeverity() {
139     return Severity.values()[severity];
140   }
141 
142   /** @see org.opencastproject.job.api.Incident#getCode() */
143   public String getCode() {
144     return code;
145   }
146 
147   /** @see org.opencastproject.job.api.Incident#getDetails() */
148   public List<Tuple<String, String>> getTechnicalInformation() {
149     final List<Tuple<String, String>> list = new ArrayList<Tuple<String, String>>();
150     JsonObj messageJson = JsonObj.jsonObj(details);
151     for (Object k : messageJson.keySet()) {
152       String title = JsonVal.asJsonVal.apply(k).as(asString);
153       String content = messageJson.val(title).as(asString);
154       list.add(tuple(title, content));
155     }
156     return list;
157   }
158 
159   /** @see org.opencastproject.job.api.Incident#getDescriptionParameters() */
160   public Map<String, String> getParameters() {
161     Map<String, String> param = new HashMap<String, String>();
162     JsonObj paramJson = JsonObj.jsonObj(parameters);
163     for (Object k : paramJson.keySet()) {
164       String key = JsonVal.asJsonVal.apply(k).as(asString);
165       String value = paramJson.val(key).as(asString);
166       param.put(key, value);
167     }
168     return param;
169   }
170 
171   public static Function<EntityManager, List<IncidentDto>> findByJobIdQuery(long jobId) {
172     return namedQuery.findAll("Incident.findByJobId", IncidentDto.class, Pair.of("jobId", jobId));
173   }
174 }