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