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  
34  import org.apache.commons.lang3.StringUtils;
35  
36  import java.util.Date;
37  import java.util.Optional;
38  
39  /**
40   * Business object for comment replies.
41   */
42  public final class EventCommentReply {
43  
44    /** comment reply identifier */
45    private Optional<Long> id;
46  
47    /** The comment reply text */
48    private String text;
49  
50    /** The creation date */
51    private Date creationDate;
52  
53    /** The modification date */
54    private Date modificationDate;
55  
56    /** The comment reply author */
57    private User author;
58  
59    /**
60     * Creates a comment reply
61     *
62     * @param id
63     *          the optional reply identifier
64     * @param text
65     *          the text
66     * @param author
67     *          the author of the comment reply
68     * @throws IllegalArgumentException
69     *           if some of the parameters aren't set
70     */
71    public static EventCommentReply create(Optional<Long> id, String text, User author) {
72      Date creationDate = new Date();
73      return create(id, text, author, creationDate, creationDate);
74    }
75  
76    /**
77     * Creates a comment reply
78     *
79     * @param id
80     *          the optional reply identifier
81     * @param text
82     *          the text
83     * @param author
84     *          the author of the comment reply
85     * @param creationDate
86     *          the creation date
87     * @param modificationDate
88     *          the modification date
89     * @throws IllegalArgumentException
90     *           if some of the parameters aren't set
91     */
92    public static EventCommentReply create(
93        Optional<Long> id,
94        String text,
95        User author,
96        Date creationDate,
97        Date modificationDate
98    ) {
99      return new EventCommentReply(id, text, author, creationDate, modificationDate);
100   }
101 
102   private EventCommentReply(Optional<Long> id, String text, User author, Date creationDate, Date modificationDate) {
103     this.id = notNull(id, "id");
104     this.text = notEmpty(text, "text");
105     this.author = notNull(author, "author");
106     this.creationDate = notNull(creationDate, "creationDate");
107     this.modificationDate = notNull(modificationDate, "modificationDate");
108   }
109 
110   /**
111    * Returns the reply id
112    *
113    * @return the reply id
114    */
115   public Optional<Long> getId() {
116     return id;
117   }
118 
119   /**
120    * Returns the reply text
121    *
122    * @return the reply text
123    */
124   public String getText() {
125     return text;
126   }
127 
128   /**
129    * Returns the reply creation date
130    *
131    * @return the reply creation date
132    */
133   public Date getCreationDate() {
134     return creationDate;
135   }
136 
137   /**
138    * Returns the reply modification date
139    *
140    * @return the reply modification date
141    */
142   public Date getModificationDate() {
143     return modificationDate;
144   }
145 
146   /**
147    * Returns the reply author
148    *
149    * @return the reply author
150    */
151   public User getAuthor() {
152     return author;
153   }
154 
155   @Override
156   public boolean equals(Object o) {
157     if (this == o) {
158       return true;
159     }
160     if (o == null || getClass() != o.getClass()) {
161       return false;
162     }
163     EventCommentReply reply = (EventCommentReply) o;
164     return text.equals(reply.getText()) && creationDate.equals(reply.getCreationDate())
165             && modificationDate.equals(reply.getModificationDate()) && author.equals(reply.getAuthor());
166   }
167 
168   @Override
169   public int hashCode() {
170     return EqualsUtil.hash(text, creationDate, modificationDate, author);
171   }
172 
173   @Override
174   public String toString() {
175     return "Comment reply:" + id + "|" + StringUtils.abbreviate(text, 25);
176   }
177 
178   public Obj toJson() {
179     Obj authorObj = Jsons.obj(Jsons.p("name", author.getName()), Jsons.p("username", author.getUsername()),
180             Jsons.p("email", author.getEmail()));
181 
182     Val idValue = Jsons.ZERO_VAL;
183     if (id.isPresent()) {
184       idValue = Jsons.v(id.get());
185     }
186 
187     return Jsons.obj(Jsons.p("id", idValue), Jsons.p("text", text), Jsons.p("author", authorObj),
188             Jsons.p("creationDate", DateTimeSupport.toUTC(creationDate.getTime())),
189             Jsons.p("modificationDate", DateTimeSupport.toUTC(modificationDate.getTime())));
190   }
191 }