BrightSide Workbench Full Report + Source Code
Post.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.entities.db;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.List;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.Index;
31 import javax.persistence.Lob;
32 import javax.persistence.ManyToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import org.turro.string.Strings;
36 import org.turro.action.Contacts;
37 import org.turro.assistant.ParticipationInfo;
38 import org.turro.auth.Authentication;
39 import org.turro.elephant.db.ElephantPU;
40 import org.turro.elephant.db.WhereClause;
41 import org.turro.entities.ITreeEntity;
42 import org.turro.entities.IUniquePath;
43 import org.turro.jpa.Dao;
44 import org.turro.participation.ParticipationReason;
45 import org.turro.plugin.contacts.IContact;
46 
51 @Entity
52 @Table(indexes={
53  @Index(name = "UniquePath", columnList = "uniquePath")
54 })
55 public class Post implements java.io.Serializable, IUniquePath, ITreeEntity {
56 
57  @Id
58  @GeneratedValue(strategy=GenerationType.IDENTITY)
59  @Column(name="IDENTIFIER")
60  private Long id;
61 
62  @ManyToOne
63  private Topic topic;
64 
65  @ManyToOne
66  private Post parent;
67 
68  private String uniquePath;
69 
70  private String authorId;
71 
72  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
73  private java.util.Date creation;
74 
75  @Lob
76  @Column(length=4096)
77  private String text;
78 
79  public Long getId() {
80  return id;
81  }
82 
83  public void setId(Long id) {
84  this.id = id;
85  }
86 
87  public Topic getTopic() {
88  return topic;
89  }
90 
91  public void setTopic(Topic topic) {
92  this.topic = topic;
93  }
94 
95  public Post getParent() {
96  return parent;
97  }
98 
99  public void setParent(Post parent) {
100  this.parent = parent;
101  }
102 
103  @Override
104  public String getUniquePath() {
105  return uniquePath;
106  }
107 
108  public void setUniquePath(String uniquePath) {
109  this.uniquePath = uniquePath;
110  }
111 
112  public String getAuthorId() {
113  return authorId;
114  }
115 
116  public void setAuthorId(String authorId) {
117  this.authorId = authorId;
118  }
119 
120  public Date getCreation() {
121  return creation;
122  }
123 
124  public void setCreation(Date creation) {
125  this.creation = creation;
126  }
127 
128  public String getText() {
129  return text;
130  }
131 
132  public void setText(String text) {
133  this.text = text;
134  }
135 
136  /* Helpers */
137 
138  public boolean isEmpty() {
139  return Strings.isBlank(text) || Strings.isBlank(authorId) ||
140  (parent == null && topic == null);
141  }
142 
144  if(parent == null) {
145  return topic;
146  } else {
147  return parent;
148  }
149  }
150 
152  Post post = this;
153  while(post != null && post.getTopic() == null) {
154  post = post.getParent();
155  }
156  return post != null ? post.getTopic() : null;
157  }
158 
159  public List<Post> getPosts() {
160  return getPosts(new ElephantPU());
161  }
162 
163  public List<Post> getPosts(Dao dao) {
164  WhereClause wc = new WhereClause();
165  wc.addClause("select p from Post p");
166  wc.addClause("where p.parent = :post");
167  wc.addNamedValue("post", this);
168  wc.addClause("order by creation");
169  return dao.getResultList(wc);
170  }
171 
172  public boolean isSeen() {
173  return isSeen(Authentication.getIContact());
174  }
175 
176  public boolean isSeen(IContact contact) {
177  return new ParticipationInfo("/post/" + getId(), "/contact/" + contact.getId(),
179  }
180 
181  public IContact getContact() {
182  return Contacts.getContactById(authorId);
183  }
184 
185  boolean isParticipant(IContact contact) {
186  if(contact != null && contact.isValid() && contact.getId().equals(authorId)) {
187  return true;
188  } else {
189  for(Post post : getPosts()) {
190  if(post.isParticipant(contact)) {
191  return true;
192  }
193  }
194  }
195  return false;
196  }
197 
198  /* ITreeEntity */
199 
200  @Override
202  return (ITreeEntity) getCurrentParent();
203  }
204 
205  @Override
206  public Collection<ITreeEntity> getEntityChildren() {
207  return new ArrayList<>(getPosts());
208  }
209 
210 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void addNamedValue(String name, Object value)
void setUniquePath(String uniquePath)
Definition: Post.java:108
void setAuthorId(String authorId)
Definition: Post.java:116
Collection< ITreeEntity > getEntityChildren()
Definition: Post.java:206
boolean isSeen(IContact contact)
Definition: Post.java:176
void setCreation(Date creation)
Definition: Post.java:124
void setParent(Post parent)
Definition: Post.java:99
List< Post > getPosts(Dao dao)
Definition: Post.java:163
void setTopic(Topic topic)
Definition: Post.java:91