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;
23  
24  import static org.opencastproject.util.RequireUtil.notEmpty;
25  import static org.opencastproject.util.RequireUtil.notNull;
26  
27  import org.opencastproject.security.api.User;
28  import org.opencastproject.util.DateTimeSupport;
29  import org.opencastproject.util.EqualsUtil;
30  import org.opencastproject.util.Jsons;
31  import org.opencastproject.util.Jsons.Obj;
32  import org.opencastproject.util.Jsons.Val;
33  import org.opencastproject.util.data.Option;
34  
35  import org.apache.commons.lang3.StringUtils;
36  
37  import java.util.ArrayList;
38  import java.util.Date;
39  import java.util.List;
40  
41  /**
42   * Business object for comments.
43   */
44  public final class EventComment {
45  
46    /** The key for the reason that the video needs cutting */
47    public static final String REASON_NEEDS_CUTTING = "EVENTS.EVENTS.DETAILS.COMMENTS.REASONS.CUTTING";
48  
49    /** The comment identifier */
50    private Option<Long> id;
51  
52    /** The event identifier */
53    private String eventId;
54  
55    /** The organization */
56    private String organization;
57  
58    /** The comment text */
59    private String text;
60  
61    /** The creation date */
62    private Date creationDate;
63  
64    /** The modification date */
65    private Date modificationDate;
66  
67    /** The comment author */
68    private User author;
69  
70    /** The comment reason */
71    private String reason;
72  
73    /** The comment resolve status */
74    private boolean resolvedStatus = false;
75  
76    /** The comment replies */
77    private List<EventCommentReply> replies = new ArrayList<EventCommentReply>();
78  
79    /**
80     * Creates a simple comment
81     *
82     * @param id
83     *          the optional identifier
84     * @param text
85     *          the text
86     * @param author
87     *          the author of the comment
88     * @throws IllegalArgumentException
89     *           if id, text or author is not set
90     */
91    public static EventComment create(Option<Long> id, String eventId, String organization, String text, User author) {
92      return create(id, eventId, organization, text, author, null, false);
93    }
94  
95    /**
96     * Creates a complex comment
97     *
98     * @param id
99     *          the optional identifier
100    * @param text
101    *          the text
102    * @param author
103    *          the author of the comment
104    * @param reason
105    *          the comment reason
106    * @param resolvedStatus
107    *          whether the comment is resolved
108    * @throws IllegalArgumentException
109    *           if id, text, or author is not set
110    */
111   public static EventComment create(
112       Option<Long> id,
113       String eventId,
114       String organization,
115       String text,
116       User author,
117       String reason,
118       boolean resolvedStatus
119   ) {
120     Date creationDate = new Date();
121     return create(
122         id, eventId, organization, text, author, reason, resolvedStatus,
123         creationDate, creationDate, new ArrayList<EventCommentReply>()
124     );
125   }
126 
127   /**
128    * Creates a complex comment
129    *
130    * @param id
131    *          the optional identifier
132    * @param text
133    *          the text
134    * @param author
135    *          the author of the comment
136    * @param reason
137    *          the comment reason
138    * @param resolvedStatus
139    *          whether the comment is resolved
140    * @param resolvedStatus
141    *          whether the comment is resolved
142    * @param creationDate
143    *          the creation date
144    * @param modificationDate
145    *          the modification date
146    * @throws IllegalArgumentException
147    *           if id, text, author, creation date or modification date is not set
148    */
149   public static EventComment create(
150       Option<Long> id,
151       String eventId,
152       String organization,
153       String text,
154       User author,
155       String reason,
156       boolean resolvedStatus,
157       Date creationDate,
158       Date modificationDate
159   ) {
160     return new EventComment(
161         id, eventId, organization, text, author, reason, resolvedStatus,
162         creationDate, modificationDate, new ArrayList<EventCommentReply>());
163   }
164 
165   /**
166    * Creates a complex comment
167    *
168    * @param id
169    *          the optional identifier
170    * @param text
171    *          the text
172    * @param author
173    *          the author of the comment
174    * @param reason
175    *          the comment reason
176    * @param resolvedStatus
177    *          whether the comment is resolved
178    * @param resolvedStatus
179    *          whether the comment is resolved
180    * @param creationDate
181    *          the creation date
182    * @param modificationDate
183    *          the modification date
184    * @param replies
185    *          the replies
186    * @throws IllegalArgumentException
187    *           if id, text, author, creation date, modification date or replies is not set
188    */
189   public static EventComment create(
190       Option<Long> id,
191       String eventId,
192       String organization,
193       String text,
194       User author,
195       String reason,
196       boolean resolvedStatus,
197       Date creationDate,
198       Date modificationDate,
199       List<EventCommentReply> replies
200   ) {
201     return new EventComment(id, eventId, organization, text, author, reason,
202         resolvedStatus, creationDate, modificationDate, replies);
203   }
204 
205   private EventComment(
206       Option<Long> id,
207       String eventId,
208       String organization,
209       String text,
210       User author,
211       String reason,
212       boolean resolvedStatus,
213       Date creationDate,
214       Date modificationDate,
215       List<EventCommentReply> replies
216   ) {
217     this.id = notNull(id, "id");
218     this.eventId = notEmpty(eventId, "eventId");
219     this.organization = notEmpty(organization, "organization");
220     this.text = notEmpty(text, "text");
221     this.author = notNull(author, "author");
222     this.reason = reason;
223     this.resolvedStatus = resolvedStatus;
224     this.creationDate = notNull(creationDate, "creationDate");
225     this.modificationDate = notNull(modificationDate, "modificationDate");
226     this.replies = notNull(replies, "replies");
227   }
228 
229   /**
230    * Returns the comment id
231    *
232    * @return the id
233    */
234   public Option<Long> getId() {
235     return id;
236   }
237 
238   public String getEventId() {
239     return eventId;
240   }
241 
242   public void setEventId(String eventId) {
243     this.eventId = eventId;
244   }
245 
246   public String getOrganization() {
247     return organization;
248   }
249 
250   public void setOrganization(String organization) {
251     this.organization = organization;
252   }
253 
254   /**
255    * Returns the text
256    *
257    * @return the text
258    */
259   public String getText() {
260     return text;
261   }
262 
263   /**
264    * Returns the creation date
265    *
266    * @return the creation date
267    */
268   public Date getCreationDate() {
269     return creationDate;
270   }
271 
272   /**
273    * Returns the modification date
274    *
275    * @return the modification date
276    */
277   public Date getModificationDate() {
278     return modificationDate;
279   }
280 
281   /**
282    * Returns the author
283    *
284    * @return the author
285    */
286   public User getAuthor() {
287     return author;
288   }
289 
290   /**
291    * Returns the reason
292    *
293    * @return the reason
294    */
295   public String getReason() {
296     return reason;
297   }
298 
299   /**
300    * Returns whether the status is resolved
301    *
302    * @return whether the status is resolved
303    */
304   public boolean isResolvedStatus() {
305     return resolvedStatus;
306   }
307 
308   /**
309    * Returns the comment replies
310    *
311    * @return the comment replies
312    */
313   public List<EventCommentReply> getReplies() {
314     return replies;
315   }
316 
317   /**
318    * Add a reply to the comment
319    *
320    * @param reply
321    *          the reply to add to this comment
322    *
323    * @return true if this collection changed as a result of the call
324    */
325   public boolean addReply(EventCommentReply reply) {
326     return replies.add(notNull(reply, "reply"));
327   }
328 
329   /**
330    * Remove a reply from the comment
331    *
332    * @param reply
333    *          the reply to remove from this comment
334    *
335    * @return true if this collection changed as a result of the call
336    */
337   public boolean removeReply(EventCommentReply reply) {
338     return replies.remove(notNull(reply, "reply"));
339   }
340 
341   @Override
342   public boolean equals(Object o) {
343     if (this == o) {
344       return true;
345     }
346     if (o == null || getClass() != o.getClass()) {
347       return false;
348     }
349     EventComment comment = (EventComment) o;
350 
351     return text.equals(comment.getText()) && creationDate.equals(comment.getCreationDate())
352         && modificationDate.equals(comment.getModificationDate()) && author.equals(comment.getAuthor())
353         && (reason == null ? comment.getReason() == null : reason.equals(comment.getReason()))
354         && resolvedStatus == comment.isResolvedStatus();
355   }
356 
357   @Override
358   public int hashCode() {
359     return EqualsUtil.hash(text, creationDate, modificationDate, author, reason, resolvedStatus);
360   }
361 
362   @Override
363   public String toString() {
364     return "Comment:" + id + "|" + StringUtils.abbreviate(text, 25);
365   }
366 
367   public Obj toJson() {
368     Obj authorObj = Jsons.obj(Jsons.p("name", author.getName()), Jsons.p("username", author.getUsername()),
369             Jsons.p("email", author.getEmail()));
370 
371     List<Val> replyArr = new ArrayList<Val>();
372     for (EventCommentReply reply : replies) {
373       replyArr.add(reply.toJson());
374     }
375 
376     Val idValue = Jsons.ZERO_VAL;
377     if (id.isSome()) {
378       idValue = Jsons.v(id.get());
379     }
380 
381     return Jsons.obj(
382         Jsons.p("id", idValue),
383         Jsons.p("text", text),
384         Jsons.p("creationDate", DateTimeSupport.toUTC(creationDate.getTime())),
385         Jsons.p("modificationDate", DateTimeSupport.toUTC(modificationDate.getTime())),
386         Jsons.p("author", authorObj), Jsons.p("reason", reason), Jsons.p("resolvedStatus", resolvedStatus),
387         Jsons.p("replies", Jsons.arr(replyArr))
388     );
389   }
390 
391 }