BrightSide Workbench Full Report + Source Code
ContactEntities.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.Date;
27 import java.util.List;
28 import org.turro.string.Strings;
29 import org.turro.action.IElephantSendable;
30 import org.turro.annotation.ElephantEntity;
31 import org.turro.collections.KeyValueMap;
32 import org.turro.contacts.CommentIt;
33 import org.turro.contacts.Contact;
34 import org.turro.contacts.ContactService;
35 import org.turro.contacts.ContactType;
36 import org.turro.contacts.Convocation;
37 import org.turro.contacts.GroupIt;
38 import org.turro.contacts.StarIt;
39 import org.turro.contacts.VoteIt;
40 import org.turro.contacts.db.ContactsPU;
41 import org.turro.contacts.relation.FuzzyRelationTypes;
42 import org.turro.contacts.service.ServiceSendable;
43 import org.turro.contacts.zul.label.ContactInfo;
44 import org.turro.contacts.zul.menu.ContactMenu;
45 import org.turro.elephant.db.WhereClause;
46 import static org.turro.entities.EntityRole.CHALLENGER;
47 import static org.turro.entities.EntityRole.DOCUMENTATION;
48 import org.turro.i18n.I_;
49 import org.turro.jpa.Dao;
50 import org.turro.path.Path;
51 import org.turro.plugin.contacts.IContact;
52 import org.turro.security.Permissions;
53 import org.turro.zul.convocation.ConvocationSendable;
54 
59 @ElephantEntity
60 public class ContactEntities extends DefaultElephantEntity {
61 
62  public ContactEntities() {
63  }
64 
65  public ContactEntities(String entityPath) {
66  super(entityPath);
67  }
68 
69  public ContactEntities(Object entity) {
70  super(entity);
71  }
72 
73  @Override
74  public Object getEntity() {
75  if(entity == null) {
76  if(getPath() != null) {
77  if(path.getSize() == 2) {
78  if(null != path.getRoot()) switch (path.getRoot()) {
79  case "contact":
80  entity = getDao().find(Contact.class, path.getNode(1));
81  break;
82  case "convocation":
83  entity = getDao().find(Convocation.class, path.getNode(1));
84  break;
85  case "groupit":
86  entity = getDao().find(GroupIt.class, path.getNode(1));
87  break;
88  case "service":
89  entity = getDao().find(ContactService.class, path.getNode(1));
90  break;
91  default:
92  break;
93  }
94  }
95  }
96  }
97  return entity;
98  }
99 
100  @Override
101  public String getName() {
102  if(getEntity() != null) {
103  if(entity instanceof Contact) {
104  return ((Contact) entity).getFullName();
105  } else if(entity instanceof Convocation) {
106  return ((Convocation) entity).getName();
107  } else if(entity instanceof GroupIt) {
108  return ((GroupIt) entity).getName();
109  } else if(entity instanceof ContactService) {
110  return ((ContactService) entity).getTitle();
111  }
112  }
113  return null;
114  }
115 
116  @Override
117  public String getLabel() {
118  if(getPath() != null) {
119  String root = path.getRoot();
120  if(null != root) switch (root) {
121  case "contact":
122  return I_.get("Contact");
123  case "groupit":
124  return I_.get("Group");
125  case "convocation":
126  return I_.get("Convocation");
127  case "service":
128  return I_.get("Service");
129  case "log":
130  if(path.getSize() > 1) switch(path.getNode(1)) {
131  case "in":
132  return I_.get("Login");
133  case "out":
134  return I_.get("Logout");
135  case "failed":
136  return I_.get("Login failed");
137  case "profile":
138  return I_.get("Profile");
139  case "webadmin":
140  return I_.get("Web site administration");
141  case "impersonate":
142  return I_.get("Impersonate");
143  case "backtoself":
144  return I_.get("Back to self");
145  }
146  default:
147  break;
148  }
149  }
150  return null;
151  }
152 
153  @Override
154  public String getDescription() {
155  if(getEntity() != null) {
156  if(entity instanceof Contact) {
157  return ((Contact) entity).getGrouping();
158  } else if(entity instanceof ContactService) {
159  return ((ContactService) entity).getText();
160  }
161  }
162  return null;
163  }
164 
165  @Override
166  public Object getLabelCtrl() {
167  if(getEntity() != null) {
168  if(entity instanceof Contact) {
169  return new ContactInfo((Contact) entity);
170  }
171  }
172  return null;
173  }
174 
175  @Override
176  public String getImage() {
177  if(getPath() != null) {
178  String root = path.getRoot();
179  if(null != root) switch (root) {
180  case "contact":
181  return "/_zul/images/contact.png";
182  case "convocation":
183  return "/_zul/images/contacts.png";
184  case "groupit":
185  return "/_zul/images/group.png";
186  case "service":
187  return "/_zul/images/service.png";
188  default:
189  break;
190  }
191  }
192  return null;
193  }
194 
195  @Override
196  public String getHierarchicalPath() {
197  if(getEntity() != null) {
198  if(entity instanceof VoteIt) {
199  IElephantEntity iee = ((VoteIt) entity).getOwnerEntity();
200  String hp = iee.getHierarchicalPath();
201  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
202  } else if(entity instanceof StarIt) {
203  IElephantEntity iee = ((StarIt) entity).getOwnerEntity();
204  String hp = iee.getHierarchicalPath();
205  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
206  } else if(entity instanceof CommentIt) {
207  IElephantEntity iee = ((CommentIt) entity).getOwnerEntity();
208  String hp = iee.getHierarchicalPath();
209  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
210  }
211  }
212  return getPath();
213  }
214 
215  @Override
217  if(getEntity() != null) {
218  if(entity instanceof Contact) {
219  return Entities.getController(((Contact) entity).getBusiness());
220  } else if(entity instanceof ContactService) {
221  return Entities.getController(((ContactService) entity).getContact());
222  }
223  }
224  return null;
225  }
226 
227  @Override
228  public List<IElephantEntity> getChildren() {
229  if(getEntity() != null) {
230  if(entity instanceof Contact) {
231  return new EntityListAdapter(((Contact) entity).getWorkers());
232  }
233  }
234  return Collections.EMPTY_LIST;
235  }
236 
237  @Override
238  public String getExtendedPath() {
239  return "";
240  }
241 
242  @Override
243  public File getFile() {
244  return null;
245  }
246 
247  @Override
248  public boolean showPath() {
249  return false;
250  }
251 
252  @Override
253  public boolean showEntity() {
254  if(getEntity() != null) {
255  if(entity instanceof Contact) {
256  ContactMenu.showContact(((Contact) entity).getId());
257  return true;
258  } else if(entity instanceof Convocation) {
260  return true;
261  } else if(entity instanceof ContactService) {
263  return true;
264  }
265  }
266  return false;
267  }
268 
269  @Override
270  public boolean canEdit(IContact contact) {
271  if(getEntity() != null) {
272  if(contact.getSyndications().contains("contacts_admin")) {
273  return true;
274  } else if(entity instanceof Contact) {
275  return contact.getPermissions().isInRole("contact:edit");
276  } else if(entity instanceof Convocation) {
277  return contact.getPermissions().isInRole("convocation:edit");
278  } else if(entity instanceof ContactService) {
279  return ((ContactService) entity).getContact().isInBusiness(contact);
280  }
281  }
282  return false;
283  }
284 
285  @Override
286  public boolean canPublish(IContact contact) {
287  if(getEntity() != null) {
288  if(contact.getSyndications().contains("contacts_admin")) {
289  return true;
290  } else if(entity instanceof Contact) {
291  Contact con = (Contact) getEntity();
292  return !con.getSyndications().isEmpty();
293  } else if(entity instanceof Convocation) {
295  return con.isAssistant(contact);
296  }
297  }
298  return false;
299  }
300 
301  @Override
302  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
303  ArrayList<Object> list = new ArrayList<>();
304  list.addAll(searchContacts(search, maxResults, kvm));
305  list.addAll(searchConvocations(search, maxResults, kvm));
306  list.addAll(searchGroups(search, maxResults, kvm));
307  list.addAll(searchServices(search, maxResults, kvm));
308  return list;
309  }
310 
311  @Override
312  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
313  ArrayList<Object> list = new ArrayList<>();
314  if(null != root) switch (root) {
315  case "contact":
316  list.addAll(searchContacts(search, maxResults, kvm));
317  break;
318  case "convocation":
319  list.addAll(searchConvocations(search, maxResults, kvm));
320  break;
321  case "groupit":
322  list.addAll(searchGroups(search, maxResults, kvm));
323  break;
324  case "service":
325  list.addAll(searchServices(search, maxResults, kvm));
326  break;
327  default:
328  break;
329  }
330  return list;
331  }
332 
333  @Override
335  if(getPath() != null) {
336  String root = path.getRoot();
337  if(null != root) switch (root) {
338  case "convocation":
339  return new ConvocationSendable(getPath());
340  case "service":
341  return new ServiceSendable(getPath());
342  default:
343  break;
344  }
345  }
346  return null;
347  }
348 
349  @Override
350  public Object configureCtrl(Object ctrl, IContact contact) {
351  return ctrl;
352  }
353 
354  @Override
355  public boolean canShowInContext(IContact contact, Object relatedEntity) {
356  return true;
357  }
358 
359  @Override
360  public String getExtendedWebPath() {
361  return null;
362  }
363 
364  @Override
365  public String getUserContext() {
366  return "/user/" + getLinkRoot() + "s";
367  }
368 
369  @Override
370  protected Dao createDao() {
371  return new ContactsPU();
372  }
373 
374  @Override
375  protected boolean itsMine(String rootPath) {
376  return super.itsMine(rootPath) || "log".equals(rootPath);
377  }
378 
379  @Override
380  protected boolean itsMine(Object entity) {
381  return (entity instanceof Contact) || (entity instanceof Convocation) ||
382  (entity instanceof GroupIt) || (entity instanceof ContactService);
383  }
384 
385  @Override
386  public Collection<String> getAllowedRoots() {
387  return Arrays.asList("contact", "convocation", "groupit", "service");
388  }
389 
390  @Override
392  return new ContactEntities(entityPath);
393  }
394 
395  @Override
397  return new ContactEntities(entity);
398  }
399 
400  @Override
401  protected Path createPath() {
402  return new Path(ContactsPU.getObjectPath(entity));
403  }
404 
405  @Override
406  public Object getObject(Path entityPath) {
407  if("comment-it".equals(entityPath.getRoot())) {
408  return getDao().find(CommentIt.class, entityPath.getNode(1));
409  } else if("star-it".equals(entityPath.getRoot())) {
410  return getDao().find(StarIt.class, entityPath.getNode(1));
411  } else if("vote-it".equals(entityPath.getRoot())) {
412  return getDao().find(VoteIt.class, entityPath.getNode(1));
413  }
414  return null;
415  }
416 
417  @Override
418  public IElephantEntity getMain(Object entity) {
419  if(entity instanceof CommentIt) {
420  return ((CommentIt) entity).getOwnerEntity();
421  } else if(entity instanceof StarIt) {
422  return ((StarIt) entity).getOwnerEntity();
423  } else if(entity instanceof VoteIt) {
424  return ((VoteIt) entity).getOwnerEntity();
425  }
426  return null;
427  }
428 
429  private List<Object> searchContacts(String search, int maxResults, KeyValueMap kvm) {
430  WhereClause wc = new WhereClause();
431  wc.addClause("select c from Contact c");
432  wc.addClause("where (c.name like :search");
433 
434  // Trade and fiscal names
435  wc.addClause("or exists (");
436  wc.addClause("select cn from Connector as cn");
437  wc.addClause("where (cn.description = 'TradeName' or cn.description = 'FiscalName')");
438  wc.addClause("and cn.value like :search");
439  wc.addClause("and cn.contact = c");
440  wc.addClause("))");
441 
442  if(kvm != null) {
443  if(kvm.containsKey("role")) {
444  List<String> syndicationNames = List.copyOf(Permissions.getSecurityGroupIdsBy(kvm.get("role")));
445  if(syndicationNames != null && !syndicationNames.isEmpty()) {
446  wc.addClause("and exists (");
447  wc.addClause("select syndication from Syndication as syndication");
448  wc.addClause("where syndication.name in (:syndicationNames)");
449  wc.addNamedValue("syndicationNames", syndicationNames);
450  wc.addClause("and syndication.contact = c");
451  wc.addClause(")");
452  }
453  }
454  if(kvm.containsKey("syndication")) {
455  wc.addClause("and exists (");
456  wc.addClause("select syndication from Syndication as syndication");
457  wc.addClause("where syndication.name in (:syndicationNames)");
458  wc.addNamedValue("syndicationNames", kvm.get("syndication"));
459  wc.addClause("and syndication.contact = c");
460  wc.addClause(")");
461  }
462  if(kvm.containsKey("type")) {
463  if(ContactType.getStudent().equals(kvm.get("type"))) {
464  wc.addClause("and type = :type");
465  wc.addNamedValue("type", ContactType.CONTACT_USER);
466  wc.addClause("and exists (");
467  wc.addClause("select relation from BusinessRelation as relation");
468  FuzzyRelationTypes.isStudent(wc, "where", "relation");
469  wc.addInRange("and", "relation.startDate", "relation.endDate", "rdate", new Date());
470  wc.addClause("and relation.contact = c");
471  wc.addClause(")");
472  } else if(ContactType.getWorker().equals(kvm.get("type"))) {
473  wc.addClause("and type = :type");
474  wc.addNamedValue("type", ContactType.CONTACT_USER);
475  wc.addClause("and exists (");
476  wc.addClause("select relation from BusinessRelation as relation");
477  FuzzyRelationTypes.isWorker(wc, "where", "relation");
478  wc.addInRange("and", "relation.startDate", "relation.endDate", "rdate", new Date());
479  wc.addClause("and relation.contact = c");
480  wc.addClause(")");
481  } else if(ContactType.getProfessional().equals(kvm.get("type"))) {
482  wc.addClause("and type = :type");
483  wc.addNamedValue("type", ContactType.CONTACT_USER);
484  wc.addClause("and exists (");
485  wc.addClause("select relation from BusinessRelation as relation");
486  FuzzyRelationTypes.isProfessional(wc, "where", "relation");
487  wc.addInRange("and", "relation.startDate", "relation.endDate", "rdate", new Date());
488  wc.addClause("and relation.contact = c");
489  wc.addClause(")");
490  } else if(ContactType.getEducator().equals(kvm.get("type"))) {
491  wc.addClause("and type = :type");
492  wc.addNamedValue("type", ContactType.CONTACT_USER);
493  wc.addClause("and exists (");
494  wc.addClause("select relation from BusinessRelation as relation");
495  FuzzyRelationTypes.isEducator(wc, "where", "relation");
496  wc.addInRange("and", "relation.startDate", "relation.endDate", "rdate", new Date());
497  wc.addClause("and relation.contact = c");
498  wc.addClause(")");
499  } else if(ContactType.getJuridical().equals(kvm.get("type"))) {
500  wc.addClause("and (type = :type1 or type = :type2)");
501  wc.addNamedValue("type1", ContactType.CONTACT_COMPANY);
502  wc.addNamedValue("type2", ContactType.CONTACT_LEARNINGCENTER);
503  } else if(ContactType.getCompany().equals(kvm.get("type"))) {
504  wc.addClause("and type = :type");
505  wc.addNamedValue("type", ContactType.CONTACT_COMPANY);
506  } else if(ContactType.getCenter().equals(kvm.get("type"))) {
507  wc.addClause("and type = :type");
508  wc.addNamedValue("type", ContactType.CONTACT_LEARNINGCENTER);
509  } else {
510  wc.addClause("and type = :type");
511  wc.addNamedValue("type", ContactType.valueOf(kvm.get("type")));
512  }
513  }
514  }
515  wc.addClause("order by c.name");
516  wc.addNamedValue("search", "%" + search + "%");
517  return getDao().getResultList(wc, maxResults);
518  }
519 
520  private List<Object> searchConvocations(String search, int maxResults, KeyValueMap kvm) {
521  WhereClause wc = new WhereClause();
522  wc.addClause("select c from Convocation c");
523  wc.addClause("where c.name like :search");
524  wc.addClause("order by c.name");
525  wc.addNamedValue("search", "%" + search + "%");
526  return getDao().getResultList(wc, maxResults);
527  }
528 
529  private List<Object> searchGroups(String search, int maxResults, KeyValueMap kvm) {
530  WhereClause wc = new WhereClause();
531  wc.addClause("select g from GroupIt g");
532  wc.addClause("where g.name like :search");
533  wc.addClause("order by g.name");
534  wc.addNamedValue("search", "%" + search + "%");
535  return getDao().getResultList(wc, maxResults);
536  }
537 
538  private List<Object> searchServices(String search, int maxResults, KeyValueMap kvm) {
539  WhereClause wc = new WhereClause();
540  wc.addClause("select s from ContactService s");
541  wc.addClause("where s.title like :search");
542  wc.addClause("order by s.title");
543  wc.addNamedValue("search", "%" + search + "%");
544  return getDao().getResultList(wc, maxResults);
545  }
546 
547 }
Set< Syndication > getSyndications()
Definition: Contact.java:403
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static void showConvocation(String id)
static void showContact(Contact contact)
void addInRange(String operator, String startField, String endField, String attribute, Date date)
void addNamedValue(String name, Object value)
IElephantEntity getMain(Object entity)
Object configureCtrl(Object ctrl, IContact contact)
boolean canShowInContext(IContact contact, Object relatedEntity)
boolean canEdit(IContact contact)
boolean canPublish(IContact contact)
IElephantEntity createInstance(String entityPath)
List< IElephantEntity > getChildren()
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
Collection< String > getAllowedRoots()
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
boolean itsMine(String rootPath)
IElephantEntity createInstance(Object entity)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
static Set< String > getSecurityGroupIdsBy(String role)