BrightSide Workbench Full Report + Source Code
Topic.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.Table;
33 import javax.persistence.Temporal;
34 import org.turro.string.Strings;
35 import org.turro.action.Contacts;
36 import org.turro.assistant.ParticipationInfo;
37 import org.turro.auth.Authentication;
38 import org.turro.elephant.db.ElephantPU;
39 import org.turro.elephant.db.WhereClause;
40 import org.turro.entities.Entities;
41 import org.turro.entities.IElephantEntity;
42 import org.turro.entities.ITreeEntity;
43 import org.turro.entities.IUniquePath;
44 import org.turro.jpa.Dao;
45 import org.turro.participation.ParticipationReason;
46 import org.turro.plugin.contacts.IContact;
47 
52 @Entity
53 @Table(indexes={
54  @Index(name = "UniquePath", columnList = "uniquePath")
55 })
56 public class Topic implements java.io.Serializable, IUniquePath, ITreeEntity {
57 
58  @Id
59  @GeneratedValue(strategy=GenerationType.IDENTITY)
60  @Column(name="IDENTIFIER")
61  private Long id;
62 
63  private String entityPath;
64 
65  private String uniquePath;
66 
67  private String authorId;
68 
69  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
70  private java.util.Date creation;
71 
72  @Lob
73  @Column(length=4096)
74  private String text;
75 
76  public Long getId() {
77  return id;
78  }
79 
80  public void setId(Long id) {
81  this.id = id;
82  }
83 
84  public String getEntityPath() {
85  return entityPath;
86  }
87 
88  public void setEntityPath(String entityPath) {
89  this.entityPath = entityPath;
90  this.iee = null;
91  }
92 
93  @Override
94  public String getUniquePath() {
95  return uniquePath;
96  }
97 
98  public void setUniquePath(String uniquePath) {
99  this.uniquePath = uniquePath;
100  }
101 
102  public String getAuthorId() {
103  return authorId;
104  }
105 
106  public void setAuthorId(String authorId) {
107  this.authorId = authorId;
108  }
109 
110  public Date getCreation() {
111  return creation;
112  }
113 
114  public void setCreation(Date creation) {
115  this.creation = creation;
116  }
117 
118  public String getText() {
119  return text;
120  }
121 
122  public void setText(String text) {
123  this.text = text;
124  }
125 
126  /* Helpers */
127 
128  public boolean isEmpty() {
129  return Strings.isBlank(text) || Strings.isBlank(authorId) ||
130  Strings.isBlank(entityPath) || getEntity().getPath() == null;
131  }
132 
133  private transient IElephantEntity iee;
134 
136  if(iee == null) {
137  iee = Entities.getController(entityPath);
138  }
139  return iee;
140  }
141 
142  public List<Post> getPosts() {
143  return getPosts(new ElephantPU());
144  }
145 
146  public List<Post> getPosts(Dao dao) {
147  WhereClause wc = new WhereClause();
148  wc.addClause("select p from Post p");
149  wc.addClause("where p.topic = :topic");
150  wc.addNamedValue("topic", this);
151  wc.addClause("order by creation");
152  return dao.getResultList(wc);
153  }
154 
155  public boolean isSeen() {
156  return isSeen(Authentication.getIContact());
157  }
158 
159  public boolean isSeen(IContact contact) {
160  return new ParticipationInfo("/topic/" + getId(), "/contact/" + contact.getId(),
162  }
163 
164  public IContact getContact() {
165  return Contacts.getContactById(authorId);
166  }
167 
168  public boolean isParticipant(IContact contact) {
169  if(contact != null && contact.isValid() && contact.getId().equals(authorId)) {
170  return true;
171  } else {
172  for(Post post : getPosts()) {
173  if(post.isParticipant(contact)) {
174  return true;
175  }
176  }
177  }
178  return new ParticipationInfo("/topic/" + getId(), "/contact/" + contact.getId(),
180  }
181 
182  public long getPostCount() {
183  WhereClause wc = new WhereClause();
184  wc.addClause("select count(p) from Post p");
185  wc.addClause("where p.uniquePath like concat(:tp, '/%')");
186  wc.addNamedValue("tp", uniquePath);
187  return (Long) new ElephantPU().getSingleResultOrNull(wc);
188  }
189 
190  /* ITreeEntity */
191 
192  @Override
194  return null;
195  }
196 
197  @Override
198  public Collection<ITreeEntity> getEntityChildren() {
199  return new ArrayList<>(getPosts());
200  }
201 
202 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void addNamedValue(String name, Object value)
void setCreation(Date creation)
Definition: Topic.java:114
void setEntityPath(String entityPath)
Definition: Topic.java:88
Collection< ITreeEntity > getEntityChildren()
Definition: Topic.java:198
void setAuthorId(String authorId)
Definition: Topic.java:106
boolean isParticipant(IContact contact)
Definition: Topic.java:168
boolean isSeen(IContact contact)
Definition: Topic.java:159
List< Post > getPosts(Dao dao)
Definition: Topic.java:146
void setUniquePath(String uniquePath)
Definition: Topic.java:98
static IElephantEntity getController(String path)
Definition: Entities.java:78
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419