BrightSide Workbench Full Report + Source Code
PublicationEntities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.entities;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import org.turro.string.Strings;
28 import org.turro.action.IElephantSendable;
29 import org.turro.action.IEntityCtrl;
30 import org.turro.action.queue.NotificationCategory;
31 import org.turro.action.queue.Notifications;
32 import org.turro.annotation.ElephantEntity;
33 import org.turro.attach.www.AttachCtrl;
34 import org.turro.collections.KeyValueMap;
35 import org.turro.command.LabelControl;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.db.WhereClause;
38 import org.turro.file.util.FileAttach;
39 import org.turro.i18n.I_;
40 import org.turro.jpa.Dao;
41 import org.turro.path.Path;
42 import org.turro.plugin.contacts.IContact;
43 import org.turro.polls.PollsCtrl;
44 import org.turro.publication.db.PublicationPU;
45 import org.turro.publication.entity.Newsletter;
46 import org.turro.publication.entity.Publication;
47 import org.turro.publication.entity.PublicationCategory;
48 import org.turro.publication.zul.menu.PublicationMenu;
49 import org.turro.www.commentit.CommentItCtrl;
50 import org.turro.www.starit.StarItCtrl;
51 
56 @ElephantEntity
58 
60  }
61 
63  super(entityPath);
64  }
65 
66  public PublicationEntities(Object entity) {
67  super(entity);
68  }
69 
70  @Override
71  public Object getEntity() {
72  if(entity == null) {
73  if(getPath() != null) {
74  if(isValidLongEntity()) {
75  if(null != path.getRoot()) switch (path.getRoot()) {
76  case "publication":
77  entity = getDao().find(Publication.class, path.getNodeAs(1, Long.class));
78  break;
79  case "pub-category":
80  entity = getDao().find(PublicationCategory.class, path.getNodeAs(1, Long.class));
81  break;
82  case "newsletter":
83  entity = getDao().find(Newsletter.class, path.getNodeAs(1, Long.class));
84  break;
85  case "mail-category":
86  entity = Notifications.getCategory(path.getPath());
87  break;
88  default:
89  break;
90  }
91  }
92  }
93  }
94  return entity;
95  }
96 
97  @Override
98  public String getName() {
99  if(getEntity() != null) {
100  if(entity instanceof Publication) {
101  return ((Publication) entity).getTitle();
102  } else if(entity instanceof PublicationCategory) {
103  return ((PublicationCategory) entity).getName();
104  } else if(entity instanceof Newsletter) {
105  return ((Newsletter) entity).getTitle();
106  } else if(entity instanceof NotificationCategory) {
107  return ((NotificationCategory) entity).getCategory();
108  }
109  }
110  return null;
111  }
112 
113  @Override
114  public String getLabel() {
115  if(getPath() != null) {
116  String root = path.getRoot();
117  if(null != root) switch (root) {
118  case "publication":
119  return I_.get("Publication");
120  case "pub-category":
121  return I_.get("Category");
122  case "newsletter":
123  return I_.get("Newsletter");
124  case "mail-category":
125  return I_.get("Category");
126  default:
127  break;
128  }
129  }
130  return null;
131  }
132 
133  @Override
134  public String getDescription() {
135  if(getEntity() != null) {
136  if(entity instanceof PublicationCategory) {
137  return ((PublicationCategory) entity).getDescription();
138  } else if(entity instanceof NotificationCategory) {
139  return ((NotificationCategory) entity).getDescription();
140  }
141  }
142  return null;
143  }
144 
145  @Override
146  public Object getLabelCtrl() {
147  if(getEntity() != null) {
148  if(entity instanceof Publication) {
149  return new LabelControl(entity);
150  } else if(entity instanceof PublicationCategory) {
151  return new LabelControl(entity);
152  } else if(entity instanceof Newsletter) {
153  return new LabelControl(entity);
154  } else if(entity instanceof NotificationCategory) {
155  return new LabelControl(entity);
156  }
157  }
158  return null;
159  }
160 
161  @Override
162  public String getImage() {
163  if(getPath() != null) {
164  String root = path.getRoot();
165  if(null != root) switch (root) {
166  case "publication":
167  return "/_zul/images/publication.png";
168  case "pub-category":
169  return "/_zul/images/group.png";
170  case "newsletter":
171  return "/_zul/images/newsletter.png";
172  case "mail-category":
173  return "/_zul/images/group.png";
174  default:
175  break;
176  }
177  }
178  return null;
179  }
180 
181  @Override
182  public String getHierarchicalPath() {
183  if(getEntity() != null) {
184  if(entity instanceof Publication) {
186  ((Publication) entity).getPublicationCategory()) +
188  } else if(entity instanceof Newsletter) {
189  return "/mail-category/" + ((Newsletter) entity).getIdCategory() +
191  }
192  }
193  return getPath();
194  }
195 
196  @Override
198  if(getEntity() != null) {
199  if(entity instanceof Publication) {
200  return Entities.getController(((Publication) entity).getPublicationCategory());
201  }
202  }
203  return null;
204  }
205 
206  @Override
207  public List<IElephantEntity> getChildren() {
208  if(getEntity() != null) {
209  if(entity instanceof PublicationCategory) {
210  return new EntityListAdapter(((PublicationCategory) entity).getPublications());
211  }
212  }
213  return Collections.EMPTY_LIST;
214  }
215 
216  @Override
217  public String getExtendedPath() {
218  return "";
219  }
220 
221  @Override
222  public File getFile() {
223  return null;
224  }
225 
226  @Override
227  public boolean showPath() {
228  return false;
229  }
230 
231  @Override
232  public boolean showEntity() {
233  if(getEntity() != null) {
234  if(entity instanceof Publication) {
236  return true;
237  } else if(entity instanceof Newsletter) {
239  return true;
240  }
241  }
242  return false;
243  }
244 
245  @Override
246  public boolean canEdit(IContact contact) {
247  if(getEntity() != null) {
248  if(entity instanceof Publication) {
249  return contact.getPermissions().isInRole("publication:edit") && (contact.getPermissions().isInRole("publication:all") ||
250  ((Publication) entity).getIdContact().equals(contact.getId()));
251  } else if(entity instanceof Newsletter) {
252  return contact.getPermissions().isInRole("publication:edit");
253  }
254  }
255  return false;
256  }
257 
258  @Override
259  public boolean canPublish(IContact contact) {
260  if(getEntity() != null) {
261  if(entity instanceof Publication) {
262  return true;
263  } else if(entity instanceof Newsletter) {
264  return true;
265  }
266  }
267  return false;
268  }
269 
270  @Override
271  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
272  ArrayList<Object> list = new ArrayList<>();
273  list.addAll(searchPublications(search, maxResults, kvm));
274  list.addAll(searchCategories(search, maxResults, kvm));
275  list.addAll(searchNewsletters(search, maxResults, kvm));
276  return list;
277  }
278 
279  @Override
280  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
281  ArrayList<Object> list = new ArrayList<>();
282  if(null != root) switch (root) {
283  case "publication":
284  list.addAll(searchPublications(search, maxResults, kvm));
285  break;
286  case "pub-category":
287  list.addAll(searchCategories(search, maxResults, kvm));
288  break;
289  case "newsletter":
290  list.addAll(searchNewsletters(search, maxResults, kvm));
291  break;
292  default:
293  break;
294  }
295  return list;
296  }
297 
298  @Override
300  if(getPath() != null) {
301  String root = path.getRoot();
302  if(null != root) switch (root) {
303  case "publication":
304  return new PublicationSendable(getPath());
305  case "newsletter":
306  return new NewsletterSendable(getPath());
307  default:
308  break;
309  }
310  }
311  return null;
312  }
313 
314  @Override
315  public Object configureCtrl(Object ctrl, IContact contact) {
316  if(ctrl instanceof StarItCtrl) {
317  StarItCtrl ictrl = (StarItCtrl) ctrl;
318  ictrl.setEntityPath(getPath());
319  return ictrl;
320  } else if(ctrl instanceof CommentItCtrl) {
321  CommentItCtrl ictrl = (CommentItCtrl) ctrl;
322  ictrl.setEntityPath(getPath());
323  return ictrl;
324  } else if(ctrl instanceof AttachCtrl) {
325  AttachCtrl ictrl = (AttachCtrl) ctrl;
326  ictrl.setEntityPath(getPath());
327  ictrl.setPublicOnly(true);
328  return ictrl;
329  } else if(ctrl instanceof FileAttach) {
330  FileAttach ictrl = (FileAttach) ctrl;
331  return ictrl;
332  } else if(ctrl instanceof PollsCtrl) {
333  PollsCtrl ictrl = (PollsCtrl) ctrl;
334  ictrl.setEntityPath(getPath());
335  return ictrl;
336  } else if(ctrl instanceof IEntityCtrl) {
337  IEntityCtrl ictrl = (IEntityCtrl) ctrl;
338  ictrl.setEntityPath(getPath());
339  return ictrl;
340  }
341  return ctrl;
342  }
343 
344  @Override
345  public boolean canShowInContext(IContact contact, Object relatedEntity) {
346  return true;
347  }
348 
349  @Override
350  public String getExtendedWebPath() {
351  if(getEntity() != null) {
352  if(entity instanceof Publication) {
353  Publication publication = (Publication) entity;
354  return "/" + publication.getId() +
355  "/" + Strings.unpunctuate(publication.getTitle()).toLowerCase();
356  } else if(entity instanceof Newsletter) {
357  Newsletter newsletter = (Newsletter) entity;
358  return "/" + newsletter.getId() +
359  "/" + Strings.unpunctuate(newsletter.getTitle()).toLowerCase();
360  }
361  }
362  return null;
363  }
364 
365  @Override
366  public String getUserContext() {
367  getPath();
368  if(path.getRoot().equals("publication")) {
369  return ElephantContext.getEntityWebContext("/publication");
370  } else if(path.getRoot().equals("newsletter")) {
371  return "/user/newsletter";
372  }
373  return null;
374  }
375 
376  @Override
377  protected Dao createDao() {
378  return new PublicationPU();
379  }
380 
381  @Override
382  protected boolean itsMine(Object entity) {
383  return (entity instanceof Publication) || (entity instanceof PublicationCategory) ||
384  (entity instanceof Newsletter) || (entity instanceof NotificationCategory);
385  }
386 
387  @Override
388  public Collection<String> getAllowedRoots() {
389  return Arrays.asList("publication", "pub-category", "newsletter", "mail-category");
390  }
391 
392  @Override
394  return new PublicationEntities(entityPath);
395  }
396 
397  @Override
399  return new PublicationEntities(entity);
400  }
401 
402  @Override
403  protected Path createPath() {
404  return new Path(PublicationPU.getObjectPath(entity));
405  }
406 
407  private List<Object> searchPublications(String search, int maxResults, KeyValueMap kvm) {
408  WhereClause wc = new WhereClause();
409  wc.addClause("select c from Publication c");
410  wc.addClause("where c.title like :search");
411  wc.addClause("order by c.title");
412  wc.addNamedValue("search", "%" + search + "%");
413  return getDao().getResultList(wc, maxResults);
414  }
415 
416  private List<Object> searchCategories(String search, int maxResults, KeyValueMap kvm) {
417  WhereClause wc = new WhereClause();
418  wc.addClause("select c from PublicationCategory c");
419  wc.addClause("where c.name like :search");
420  wc.addClause("order by c.name");
421  wc.addNamedValue("search", "%" + search + "%");
422  return getDao().getResultList(wc, maxResults);
423  }
424 
425  private List<Object> searchNewsletters(String search, int maxResults, KeyValueMap kvm) {
426  WhereClause wc = new WhereClause();
427  wc.addClause("select c from Newsletter c");
428  wc.addClause("where c.title like :search");
429  wc.addClause("order by c.title");
430  wc.addNamedValue("search", "%" + search + "%");
431  return getDao().getResultList(wc, maxResults);
432  }
433 
434 }
static NotificationCategory getCategory(String id)
void setPublicOnly(boolean publicOnly)
Definition: AttachCtrl.java:77
void setEntityPath(String entityPath)
Definition: AttachCtrl.java:45
static String getEntityWebContext(String path)
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
Object configureCtrl(Object ctrl, IContact contact)
boolean canShowInContext(IContact contact, Object relatedEntity)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
IElephantEntity createInstance(String entityPath)
IElephantEntity createInstance(Object entity)
static String get(String msg)
Definition: I_.java:41
void setEntityPath(String entityPath)
Definition: PollsCtrl.java:42
static String getObjectPath(Object object)
void setEntityPath(String entityPath)
void setEntityPath(String entityPath)
Definition: StarItCtrl.java:46
void setEntityPath(String entityPath)