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 org.opencastproject.event.comment.EventCommentReply;
25  import org.opencastproject.security.api.OrganizationDirectoryService;
26  import org.opencastproject.security.api.User;
27  import org.opencastproject.security.api.UserDirectoryService;
28  import org.opencastproject.security.impl.jpa.JpaOrganization;
29  import org.opencastproject.security.impl.jpa.JpaUser;
30  import org.opencastproject.util.data.Option;
31  
32  import java.util.Date;
33  
34  import javax.persistence.Access;
35  import javax.persistence.AccessType;
36  import javax.persistence.Column;
37  import javax.persistence.Entity;
38  import javax.persistence.GeneratedValue;
39  import javax.persistence.Id;
40  import javax.persistence.JoinColumn;
41  import javax.persistence.Lob;
42  import javax.persistence.ManyToOne;
43  import javax.persistence.NamedQueries;
44  import javax.persistence.NamedQuery;
45  import javax.persistence.Table;
46  import javax.persistence.Temporal;
47  import javax.persistence.TemporalType;
48  
49  /**
50   * Entity object for the comment replies.
51   */
52  @Entity(name = "EventCommentReply")
53  @Access(AccessType.FIELD)
54  @Table(name = "oc_event_comment_reply")
55  @NamedQueries({ @NamedQuery(name = "EventCommentReply.findAll", query = "SELECT c FROM EventCommentReply c"),
56          @NamedQuery(name = "EventCommentReply.clear", query = "DELETE FROM EventCommentReply") })
57  public class EventCommentReplyDto {
58  
59    @Id
60    @GeneratedValue
61    @Column(name = "id")
62    private long id;
63  
64    @ManyToOne(targetEntity = EventCommentDto.class)
65    @JoinColumn(name = "event_comment_id", referencedColumnName = "id", nullable = false)
66    private EventCommentDto eventComment;
67  
68    @Lob
69    @Column(name = "text", nullable = false)
70    private String text;
71  
72    @Column(name = "creation_date", nullable = false)
73    @Temporal(TemporalType.TIMESTAMP)
74    private Date creationDate;
75  
76    @Column(name = "modification_date", nullable = false)
77    @Temporal(TemporalType.TIMESTAMP)
78    private Date modificationDate;
79  
80    @Column(name = "author", nullable = false)
81    private String author;
82  
83    /**
84     * Default constructor
85     */
86    public EventCommentReplyDto() {
87    }
88  
89    public static EventCommentReplyDto from(EventCommentReply reply) {
90      EventCommentReplyDto dto = new EventCommentReplyDto();
91      if (reply.getId().isSome()) {
92        dto.id = reply.getId().get().longValue();
93      }
94      dto.text = reply.getText();
95      dto.creationDate = reply.getCreationDate();
96      dto.modificationDate = reply.getModificationDate();
97      dto.author = reply.getAuthor().getUsername();
98  
99      return dto;
100   }
101 
102   /**
103    * Returns the id of this entity
104    *
105    * @return the id as long
106    */
107   public long getId() {
108     return id;
109   }
110 
111   EventCommentDto getEventComment() {
112     return eventComment;
113   }
114 
115   void setEventComment(EventCommentDto eventComment) {
116     this.eventComment = eventComment;
117   }
118 
119   /**
120    * Sets the text
121    *
122    * @param text
123    *          the text
124    */
125   public void setText(String text) {
126     this.text = text;
127   }
128 
129   /**
130    * Returns the text
131    *
132    * @return the text
133    */
134   public String getText() {
135     return text;
136   }
137 
138   /**
139    * Sets the creation date
140    *
141    * @param creationDate
142    *          the creation date
143    */
144   public void setCreationDate(Date creationDate) {
145     this.creationDate = creationDate;
146   }
147 
148   /**
149    * Returns the creation date
150    *
151    * @return the creation date
152    */
153   public Date getCreationDate() {
154     return creationDate;
155   }
156 
157   /**
158    * Sets the modification date
159    *
160    * @param modificationDate
161    *          the modification date
162    */
163   public void setModificationDate(Date modificationDate) {
164     this.modificationDate = modificationDate;
165   }
166 
167   /**
168    * Returns the modification date
169    *
170    * @return the modification date
171    */
172   public Date getModificationDate() {
173     return modificationDate;
174   }
175 
176   /**
177    * Sets the author
178    *
179    * @param author
180    *          the author
181    */
182   public void setAuthor(String author) {
183     this.author = author;
184   }
185 
186   /**
187    * Returns the author
188    *
189    * @return the author
190    */
191   public String getAuthor() {
192     return author;
193   }
194 
195   /**
196    * Returns the business object of this comment reply
197    *
198    * @return the business object model of this comment reply
199    */
200   public EventCommentReply toCommentReply(UserDirectoryService userDirectoryService,
201       OrganizationDirectoryService organizationDirectoryService,
202       String organization) {
203     User user = userDirectoryService.loadUser(author);
204     if (user == null) {
205       JpaOrganization org = null;
206       try {
207         org = (JpaOrganization) organizationDirectoryService.getOrganization(organization);
208       } catch (Exception ignore) { }
209       user = new JpaUser(author, null, org, author, "ghost@localhost", "ghost", false);
210     }
211     return EventCommentReply.create(Option.option(id), text, user, creationDate, modificationDate);
212   }
213 
214 }