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.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  
34  import java.util.ArrayList;
35  import java.util.Date;
36  import java.util.List;
37  import java.util.Optional;
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    * Default constructor
134    */
135   public EventCommentDto() {
136   }
137 
138   public static EventCommentDto from(EventComment comment) {
139     EventCommentDto dto = new EventCommentDto();
140 
141     if (comment.getId().isPresent()) {
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    * Returns the id of this entity
162    *
163    * @return the id as long
164    */
165   public long getId() {
166     return id;
167   }
168 
169   /**
170    * Returns the event identifier
171    *
172    * @return the event identifier
173    */
174   public String getEventId() {
175     return eventId;
176   }
177 
178   /**
179    * Sets the event identifier
180    *
181    * @param eventId
182    *          the event identifier
183    */
184   public void setEventId(String eventId) {
185     this.eventId = eventId;
186   }
187 
188   /**
189    * Returns the organization
190    *
191    * @return the organization
192    */
193   public String getOrganization() {
194     return organization;
195   }
196 
197   /**
198    * Sets the organization
199    *
200    * @param organization
201    *          the organization
202    */
203   public void setOrganization(String organization) {
204     this.organization = organization;
205   }
206 
207   /**
208    * Sets the text
209    *
210    * @param text
211    *          the text
212    */
213   public void setText(String text) {
214     this.text = text;
215   }
216 
217   /**
218    * Returns the text
219    *
220    * @return the text
221    */
222   public String getText() {
223     return text;
224   }
225 
226   /**
227    * Sets the creation date
228    *
229    * @param creationDate
230    *          the creation date
231    */
232   public void setCreationDate(Date creationDate) {
233     this.creationDate = creationDate;
234   }
235 
236   /**
237    * Returns the creation date
238    *
239    * @return the creation date
240    */
241   public Date getCreationDate() {
242     return creationDate;
243   }
244 
245   /**
246    * Sets the modification date
247    *
248    * @param modificationDate
249    *          the modification date
250    */
251   public void setModificationDate(Date modificationDate) {
252     this.modificationDate = modificationDate;
253   }
254 
255   /**
256    * Returns the modification date
257    *
258    * @return the modification date
259    */
260   public Date getModificationDate() {
261     return modificationDate;
262   }
263 
264   /**
265    * Sets the author
266    *
267    * @param author
268    *          the author
269    */
270   public void setAuthor(String author) {
271     this.author = author;
272   }
273 
274   /**
275    * Returns the author
276    *
277    * @return the author
278    */
279   public String getAuthor() {
280     return author;
281   }
282 
283   /**
284    * Sets the reason
285    *
286    * @param reason
287    *          the reason
288    */
289   public void setReason(String reason) {
290     this.reason = reason;
291   }
292 
293   /**
294    * Returns the reason
295    *
296    * @return the reason
297    */
298   public String getReason() {
299     return reason;
300   }
301 
302   /**
303    * Sets whether the status is resolved
304    *
305    * @param resolvedStatus
306    *          whether the status is resolved
307    */
308   public void setResolvedStatus(boolean resolvedStatus) {
309     this.resolvedStatus = resolvedStatus;
310   }
311 
312   /**
313    * Returns whether the status is resolved
314    *
315    * @return whether the status is resolved
316    */
317   public boolean isResolvedStatus() {
318     return resolvedStatus;
319   }
320 
321   /**
322    * Sets a comment replies list.
323    *
324    * @param replies
325    *          the replies list
326    */
327   public void setReplies(List<EventCommentReplyDto> replies) {
328     this.replies = notNull(replies, "replies");
329   }
330 
331   /**
332    * Returns the replies list
333    *
334    * @return the replies list
335    */
336   public List<EventCommentReplyDto> getReplies() {
337     return replies;
338   }
339 
340   /**
341    * Add a reply to the comment
342    *
343    * @param reply
344    *          the reply to add to this comment
345    *
346    * @return true if this collection changed as a result of the call
347    */
348   public boolean addReply(EventCommentReplyDto reply) {
349     notNull(reply, "reply").setEventComment(this);
350     return replies.add(reply);
351   }
352 
353   /**
354    * Remove a reply from the comment
355    *
356    * @param reply
357    *          the reply to remove from this comment
358    *
359    * @return true if this collection changed as a result of the call
360    */
361   public boolean removeReply(EventCommentReplyDto reply) {
362     return replies.remove(notNull(reply, "reply"));
363   }
364 
365   /**
366    * Returns the business object of this comment
367    *
368    * @return the business object model of this comment
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(Optional.ofNullable(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 }