1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.event.comment.persistence;
23
24 import static org.opencastproject.util.RequireUtil.notNull;
25
26 import org.opencastproject.event.comment.EventComment;
27 import org.opencastproject.event.comment.EventCommentReply;
28 import org.opencastproject.security.api.OrganizationDirectoryService;
29 import org.opencastproject.security.api.User;
30 import org.opencastproject.security.api.UserDirectoryService;
31 import org.opencastproject.security.impl.jpa.JpaOrganization;
32 import org.opencastproject.security.impl.jpa.JpaUser;
33 import org.opencastproject.util.data.Option;
34
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.List;
38
39 import javax.persistence.CascadeType;
40 import javax.persistence.Column;
41 import javax.persistence.Entity;
42 import javax.persistence.FetchType;
43 import javax.persistence.GeneratedValue;
44 import javax.persistence.Id;
45 import javax.persistence.Index;
46 import javax.persistence.Lob;
47 import javax.persistence.NamedQueries;
48 import javax.persistence.NamedQuery;
49 import javax.persistence.OneToMany;
50 import javax.persistence.Table;
51 import javax.persistence.Temporal;
52 import javax.persistence.TemporalType;
53
54 @Entity(name = "EventComment")
55 @Table(name = "oc_event_comment", indexes = {
56 @Index(name = "IX_oc_event_comment_event", columnList = "event, organization")
57 })
58 @NamedQueries({
59 @NamedQuery(
60 name = "EventComment.countAll",
61 query = "SELECT COUNT(e) FROM EventComment e"
62 ),
63 @NamedQuery(
64 name = "EventComment.findAll",
65 query = "SELECT e FROM EventComment e"
66 ),
67 @NamedQuery(
68 name = "EventComment.findReasons",
69 query = "SELECT e.reason FROM EventComment e WHERE e.organization = :org GROUP BY e.reason"
70 ),
71 @NamedQuery(
72 name = "EventComment.findByEvent",
73 query = "SELECT e FROM EventComment e "
74 + "WHERE e.eventId = :eventId AND e.organization = :org ORDER BY e.creationDate"
75 ),
76 @NamedQuery(
77 name = "EventComment.findByCommentId",
78 query = "SELECT e FROM EventComment e WHERE e.id = :commentId"
79 ),
80 @NamedQuery(
81 name = "EventComment.findAllWIthOrg",
82 query = "SELECT e.organization, e.eventId FROM EventComment e ORDER BY e.organization ASC"
83 ),
84 @NamedQuery(
85 name = "EventComment.clear",
86 query = "DELETE FROM EventComment e WHERE e.organization = :org"
87 ),
88 })
89 public class EventCommentDto {
90
91 @Id
92 @GeneratedValue
93 @Column(name = "id")
94 private long id;
95
96 @Column(name = "organization", length = 128, nullable = false)
97 private String organization;
98
99 @Column(name = "event", length = 128, nullable = false)
100 private String eventId;
101
102 @Lob
103 @Column(name = "text", nullable = false)
104 private String text;
105
106 @Column(name = "creation_date", nullable = false)
107 @Temporal(TemporalType.TIMESTAMP)
108 private Date creationDate;
109
110 @Column(name = "modification_date", nullable = false)
111 @Temporal(TemporalType.TIMESTAMP)
112 private Date modificationDate;
113
114 @Column(name = "author", nullable = false)
115 private String author;
116
117 @Column(name = "reason")
118 private String reason;
119
120 @Column(name = "resolved_status", nullable = false)
121 private boolean resolvedStatus = false;
122
123 @OneToMany(
124 targetEntity = EventCommentReplyDto.class,
125 fetch = FetchType.LAZY,
126 cascade = { CascadeType.REMOVE },
127 mappedBy = "eventComment",
128 orphanRemoval = true
129 )
130 private List<EventCommentReplyDto> replies = new ArrayList<EventCommentReplyDto>();
131
132
133
134
135 public EventCommentDto() {
136 }
137
138 public static EventCommentDto from(EventComment comment) {
139 EventCommentDto dto = new EventCommentDto();
140
141 if (comment.getId().isSome()) {
142 dto.id = comment.getId().get().longValue();
143 }
144 dto.organization = comment.getOrganization();
145 dto.eventId = comment.getEventId();
146 dto.text = comment.getText();
147 dto.creationDate = comment.getCreationDate();
148 dto.modificationDate = comment.getModificationDate();
149 dto.author = comment.getAuthor().getUsername();
150 dto.reason = comment.getReason();
151 dto.resolvedStatus = comment.isResolvedStatus();
152
153 for (final EventCommentReply reply : comment.getReplies()) {
154 dto.addReply(EventCommentReplyDto.from(reply));
155 }
156
157 return dto;
158 }
159
160
161
162
163
164
165 public long getId() {
166 return id;
167 }
168
169
170
171
172
173
174 public String getEventId() {
175 return eventId;
176 }
177
178
179
180
181
182
183
184 public void setEventId(String eventId) {
185 this.eventId = eventId;
186 }
187
188
189
190
191
192
193 public String getOrganization() {
194 return organization;
195 }
196
197
198
199
200
201
202
203 public void setOrganization(String organization) {
204 this.organization = organization;
205 }
206
207
208
209
210
211
212
213 public void setText(String text) {
214 this.text = text;
215 }
216
217
218
219
220
221
222 public String getText() {
223 return text;
224 }
225
226
227
228
229
230
231
232 public void setCreationDate(Date creationDate) {
233 this.creationDate = creationDate;
234 }
235
236
237
238
239
240
241 public Date getCreationDate() {
242 return creationDate;
243 }
244
245
246
247
248
249
250
251 public void setModificationDate(Date modificationDate) {
252 this.modificationDate = modificationDate;
253 }
254
255
256
257
258
259
260 public Date getModificationDate() {
261 return modificationDate;
262 }
263
264
265
266
267
268
269
270 public void setAuthor(String author) {
271 this.author = author;
272 }
273
274
275
276
277
278
279 public String getAuthor() {
280 return author;
281 }
282
283
284
285
286
287
288
289 public void setReason(String reason) {
290 this.reason = reason;
291 }
292
293
294
295
296
297
298 public String getReason() {
299 return reason;
300 }
301
302
303
304
305
306
307
308 public void setResolvedStatus(boolean resolvedStatus) {
309 this.resolvedStatus = resolvedStatus;
310 }
311
312
313
314
315
316
317 public boolean isResolvedStatus() {
318 return resolvedStatus;
319 }
320
321
322
323
324
325
326
327 public void setReplies(List<EventCommentReplyDto> replies) {
328 this.replies = notNull(replies, "replies");
329 }
330
331
332
333
334
335
336 public List<EventCommentReplyDto> getReplies() {
337 return replies;
338 }
339
340
341
342
343
344
345
346
347
348 public boolean addReply(EventCommentReplyDto reply) {
349 notNull(reply, "reply").setEventComment(this);
350 return replies.add(reply);
351 }
352
353
354
355
356
357
358
359
360
361 public boolean removeReply(EventCommentReplyDto reply) {
362 return replies.remove(notNull(reply, "reply"));
363 }
364
365
366
367
368
369
370 public EventComment toComment(UserDirectoryService userDirectoryService,
371 OrganizationDirectoryService organizationDirectoryService) {
372 User user = userDirectoryService.loadUser(author);
373 if (user == null) {
374 JpaOrganization org = null;
375 try {
376 org = (JpaOrganization) organizationDirectoryService.getOrganization(organization);
377 } catch (Exception ignore) { }
378 user = new JpaUser(author, null, org, author, "ghost@localhost", "ghost", false);
379 }
380 EventComment comment = EventComment.create(Option.option(id), eventId, organization, text, user, reason,
381 resolvedStatus, creationDate, modificationDate);
382 for (EventCommentReplyDto reply : replies) {
383 comment.addReply(reply.toCommentReply(userDirectoryService, organizationDirectoryService, organization));
384 }
385 return comment;
386 }
387 }