BrightSide Workbench Full Report + Source Code
ParticipationInfo.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.assistant;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.function.Consumer;
26 import org.turro.string.Strings;
27 import org.turro.auth.Authentication;
28 import org.turro.elephant.db.ElephantPU;
29 import org.turro.elephant.db.WhereClause;
30 import org.turro.elephant.entities.db.EntityParticipation;
31 import org.turro.elephant.entities.db.EntityParticipationPK;
32 import org.turro.entities.Entities;
33 import org.turro.entities.IElephantEntity;
34 import org.turro.i18n.I_;
35 import org.turro.jpa.Dao;
36 import org.turro.participation.IEntityParticipation;
37 import org.turro.participation.ParticipationReason;
38 import org.turro.plugin.contacts.ContactList;
39 import org.turro.plugin.contacts.IContact;
40 import org.turro.sql.SqlClause;
41 
46 public class ParticipationInfo {
47 
48  private final String entityPath;
49  private String participatorPath;
50  private ParticipationReason reason;
51  private Long count;
52  private Boolean checked;
53 
54  public ParticipationInfo(Object entity, ParticipationReason reason) {
55  this(entity, Authentication.getIContact(), reason);
56  }
57 
58  public ParticipationInfo(Object entity, IContact contact, ParticipationReason reason) {
59  entityPath = Entities.getController(entity).getPath();
60  if(!Strings.isBlank(entityPath)) {
61  if(contact != null && contact.isValid()) {
62  participatorPath = "/contact/" + contact.getId();
63  }
64  }
65  this.reason = reason;
66  }
67 
68  public ParticipationInfo(String entityPath, ParticipationReason reason) {
69  this(entityPath, (String) null, reason);
70  }
71 
72  public ParticipationInfo(String entityPath, String participatorPath, ParticipationReason reason) {
73  this.entityPath = entityPath;
74  this.participatorPath = participatorPath;
75  this.reason = reason;
76  }
77 
78  public long getCount() {
79  if(count == null) {
80  WhereClause wc = new WhereClause();
81  wc.addClause("select count(ep) from EntityParticipation ep");
82  wc.addClause("where ep.entityPath = :entityPath");
83  wc.addNamedValue("entityPath", entityPath);
84  if(reason != null) {
85  wc.addClause("and ep.reason = :reason");
86  wc.addNamedValue("reason", reason);
87  }
88  count = (long) getDao().getSingleResult(wc);
89  }
90  return count;
91  }
92 
93  public List<IEntityParticipation> getParticipations() {
94  return getParticipations(null);
95  }
96 
97  public List<IEntityParticipation> getParticipations(Date from) {
98  WhereClause wc = new WhereClause();
99  wc.addClause("select ep from EntityParticipation ep");
100  wc.addClause("where ep.entityPath = :entityPath");
101  wc.addNamedValue("entityPath", entityPath);
102  if(reason != null) {
103  wc.addClause("and ep.reason = :reason");
104  wc.addNamedValue("reason", reason);
105  }
106  if(from != null) {
107  wc.addClause("and ep.participationDate >= :from");
108  wc.addNamedValue("from", from);
109  }
110  return getDao().getResultList(wc);
111  }
112 
113  public List<IEntityParticipation> getCrossParticipations() {
114  return getCrossParticipations(null);
115  }
116 
117  public List<IEntityParticipation> getCrossParticipations(Date from) {
118  WhereClause wc = new WhereClause();
119  wc.addClause("select ep from EntityParticipation ep");
120  wc.addClause("where ep.participatorPath = :entityPath");
121  wc.addNamedValue("entityPath", entityPath);
122  if(reason != null) {
123  wc.addClause("and ep.reason = :reason");
124  wc.addNamedValue("reason", reason);
125  }
126  if(from != null) {
127  wc.addClause("and ep.participationDate >= :from");
128  wc.addNamedValue("from", from);
129  }
130  return getDao().getResultList(wc);
131  }
132 
133  public List<IEntityParticipation> getAnyParticipations(Date from) {
134  WhereClause wc = new WhereClause();
135  wc.addClause("select ep from EntityParticipation ep");
136  wc.addClause("where 1=1");
137  if(reason != null) {
138  wc.addClause("and ep.reason = :reason");
139  wc.addNamedValue("reason", reason);
140  }
141  if(from != null) {
142  wc.addClause("and ep.participationDate >= :from");
143  wc.addNamedValue("from", from);
144  }
145  return getDao().getResultList(wc);
146  }
147 
148  public void getAnyParticipations(Date from, Consumer<IEntityParticipation> action) {
149  WhereClause wc = new WhereClause();
150  wc.addClause("select ep from EntityParticipation ep");
151  wc.addClause("where 1=1");
152  if(reason != null) {
153  wc.addClause("and ep.reason = :reason");
154  wc.addNamedValue("reason", reason);
155  }
156  if(from != null) {
157  wc.addClause("and ep.participationDate >= :from");
158  wc.addNamedValue("from", from);
159  }
160  getDao().forEach(IEntityParticipation.class, wc, action);
161  }
162 
163  public boolean isChecked() {
164  if(checked == null) {
165  checked = false;
166  if(!isEmpty()) {
168  key.setEntityPath(entityPath);
169  key.setParticipatorPath(participatorPath);
170  key.setReason(reason);
171  EntityParticipation ep = getDao().find(EntityParticipation.class, key);
172  checked = ep != null;
173  }
174  }
175  return checked;
176  }
177 
178  public boolean isUser() {
179  return !Strings.isBlank(participatorPath);
180  }
181 
182  public boolean isEmpty() {
183  return Strings.isBlank(entityPath) || Strings.isBlank(participatorPath) || reason == null;
184  }
185 
186  public boolean toggle() {
187  boolean result = false;
188  if(!isEmpty()) {
190  key.setEntityPath(entityPath);
191  key.setParticipatorPath(participatorPath);
192  key.setReason(reason);
193  EntityParticipation ep = getDao().find(EntityParticipation.class, key);
194  if(ep != null) {
195  getDao().deleteObject(ep);
196  result = false;
197  } else {
198  ep = new EntityParticipation();
199  ep.setEntityPath(entityPath);
200  ep.setParticipatorPath(participatorPath);
201  ep.setReason(reason);
202  ep.setParticipationDate(new Date());
203  getDao().saveObject(ep);
204  result = true;
205  }
206  reset();
207  }
208  return result;
209  }
210 
211  public void check() {
212  if(isEmpty()) return;
214  key.setEntityPath(entityPath);
215  key.setParticipatorPath(participatorPath);
216  key.setReason(reason);
217  EntityParticipation ep = getDao().find(EntityParticipation.class, key);
218  if(ep == null) {
219  ep = new EntityParticipation();
220  ep.setEntityPath(entityPath);
221  ep.setParticipatorPath(participatorPath);
222  ep.setReason(reason);
223  ep.setParticipationDate(new Date());
224  getDao().saveObject(ep);
225  }
226  reset();
227  }
228 
229  public void uncheck() {
230  if(isEmpty()) return;
232  key.setEntityPath(entityPath);
233  key.setParticipatorPath(participatorPath);
234  key.setReason(reason);
235  EntityParticipation ep = getDao().find(EntityParticipation.class, key);
236  if(ep != null) {
237  getDao().deleteObject(ep);
238  }
239  reset();
240  }
241 
242  /* Entity helpers */
243 
245  return Entities.getController(participatorPath);
246  }
247 
249  return Entities.getController(entityPath);
250  }
251 
252  public String getReasonStr() {
253  return I_.byKey(reason.toString());
254  }
255 
256  /* IContact helpers */
257 
259  ContactList list = new ContactList();
261  list.add(ep.getContact());
262  }
263  return list;
264  }
265 
266  private void reset() {
267  count = null;
268  checked = null;
269  }
270 
271  /* Helpers */
272 
273  public static long getParticipationCount(String root, IContact contact, ParticipationReason reason) {
274  WhereClause wc = new WhereClause();
275  wc.addClause("select count(ep) from EntityParticipation ep");
276  wc.addClause("where ep.entityPath like concat('/', :rootPath, '/%')");
277  wc.addNamedValue("rootPath", root);
278  wc.addClause("and ep.participatorPath = concat('/contact/', :participator)");
279  wc.addNamedValue("participator", contact.getId());
280  wc.addClause("and ep.reason = :reason");
281  wc.addNamedValue("reason", reason);
282  return (long) new ElephantPU().getSingleResult(wc);
283  }
284 
285  public static void cleanup(String entityPath) {
286  SqlClause.delete("EntityParticipation")
287  .where().equal("entityPath", entityPath)
288  .or().equal("participatorPath", entityPath)
289  .dao(new ElephantPU())
290  .execute();
291  }
292 
293  /* Utils */
294 
295  public static Set<String> getAllPaths(String root) {
296  return new HashSet<>(SqlClause.select("distinct ep.entityPath").from("EntityParticipation ep")
297  .where().startsWith("ep.entityPath", "/" + root + "/")
298  .dao(new ElephantPU())
299  .resultList(String.class));
300  }
301 
302  /* Dao */
303 
304  private Dao _dao;
305 
306  private Dao getDao() {
307  if(_dao == null) {
308  _dao = new ElephantPU();
309  }
310  return _dao;
311  }
312 
313 }
List< IEntityParticipation > getParticipations(Date from)
ParticipationInfo(String entityPath, ParticipationReason reason)
List< IEntityParticipation > getAnyParticipations(Date from)
List< IEntityParticipation > getParticipations()
static long getParticipationCount(String root, IContact contact, ParticipationReason reason)
ParticipationInfo(Object entity, ParticipationReason reason)
List< IEntityParticipation > getCrossParticipations(Date from)
void getAnyParticipations(Date from, Consumer< IEntityParticipation > action)
ParticipationInfo(Object entity, IContact contact, ParticipationReason reason)
ParticipationInfo(String entityPath, String participatorPath, ParticipationReason reason)
List< IEntityParticipation > getCrossParticipations()
static void cleanup(String entityPath)
static Set< String > getAllPaths(String root)
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String byKey(String key)
Definition: I_.java:83
void deleteObject(Object obj)
Definition: Dao.java:162
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380