BrightSide Workbench Full Report + Source Code
FinancialsEntities.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.IEntityCtrl;
29 import org.turro.annotation.ElephantEntity;
30 import org.turro.attach.www.AttachCtrl;
31 import org.turro.collections.KeyValueMap;
32 import org.turro.command.LabelControl;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.elephant.util.DateFormats;
35 import org.turro.file.util.FileAttach;
36 import org.turro.financials.command.DocumentLabel;
37 import org.turro.financials.db.FinancialsPU;
38 import org.turro.financials.entity.Account;
39 import org.turro.financials.entity.BatchOf;
40 import org.turro.financials.entity.Company;
41 import org.turro.financials.entity.Contract;
42 import org.turro.financials.entity.ContractInterventionType;
43 import org.turro.financials.entity.ContractParticipant;
44 import org.turro.financials.entity.Department;
45 import org.turro.financials.entity.Document;
46 import org.turro.financials.entity.FixedAsset;
47 import org.turro.financials.entity.Headquarters;
48 import org.turro.financials.entity.MajorAccount;
49 import org.turro.financials.entity.Product;
50 import org.turro.financials.entity.Register;
51 import org.turro.financials.entity.Service;
52 import org.turro.financials.menu.FinancialsMenu;
53 import org.turro.i18n.I_;
54 import org.turro.jpa.Dao;
55 import org.turro.path.Path;
56 import org.turro.plugin.contacts.IContact;
57 import org.turro.www.commentit.CommentItCtrl;
58 import org.turro.www.describeit.DescribeItCtrl;
59 
66 
67  public FinancialsEntities() {
68  }
69 
70  public FinancialsEntities(String entityPath) {
71  super(entityPath);
72  }
73 
74  public FinancialsEntities(Object entity) {
75  super(entity);
76  }
77 
78  @Override
79  public Object getEntity() {
80  if(entity == null) {
81  if(getPath() != null) {
82  if(isValidLongEntity()) {
83  if(null != path.getRoot()) switch (path.getRoot()) {
84  case "contract":
85  entity = getDao().find(Contract.class, path.getNodeAs(1, Long.class));
86  break;
87  case "document":
88  entity = getDao().find(Document.class, path.getNodeAs(1, Long.class));
89  break;
90  case "bacthof":
91  entity = getDao().find(BatchOf.class, path.getNodeAs(1, Long.class));
92  break;
93  case "product":
94  entity = getDao().find(Product.class, path.getNodeAs(1, Long.class));
95  break;
96  case "fixed-asset":
97  entity = getDao().find(FixedAsset.class, path.getNodeAs(1, Long.class));
98  break;
99  case "company":
100  entity = getDao().find(Company.class, path.getNodeAs(1, Long.class));
101  break;
102  case "headquarters":
103  entity = getDao().find(Headquarters.class, path.getNodeAs(1, Long.class));
104  break;
105  case "department":
106  entity = getDao().find(Department.class, path.getNodeAs(1, Long.class));
107  break;
108  case "service":
109  entity = getDao().find(Service.class, path.getNodeAs(1, Long.class));
110  break;
111  case "register":
112  entity = getDao().find(Register.class, path.getNodeAs(1, Long.class));
113  break;
114  case "account":
115  entity = getDao().find(Account.class, path.getNode(1));
116  break;
117  case "major-account":
118  entity = getDao().find(MajorAccount.class, path.getNodeAs(1, Long.class));
119  break;
120  default:
121  break;
122  }
123  }
124  }
125  }
126  return entity;
127  }
128 
129  @Override
130  public String getName() {
131  if(getEntity() != null) {
132  if(entity instanceof Contract) {
133  return ((Contract) entity).getId() + " - " +((Contract) entity).getFullDescription();
134  } else if(entity instanceof Document) {
135  return ((Document) entity).getDocDescription();
136  } else if(entity instanceof BatchOf) {
137  return ((BatchOf) entity).getBatch().getDocDescription();
138  } else if(entity instanceof Product) {
139  return ((Product) entity).getProductString();
140  } else if(entity instanceof FixedAsset) {
141  return ((FixedAsset) entity).getId() + " - " + ((FixedAsset) entity).getName();
142  } else if(entity instanceof Company) {
143  return ((Company) entity).getName();
144  } else if(entity instanceof Headquarters) {
145  return ((Headquarters) entity).getName();
146  } else if(entity instanceof Department) {
147  return ((Department) entity).getFullName();
148  } else if(entity instanceof Service) {
149  return ((Service) entity).getFullName();
150  } else if(entity instanceof Register) {
151  return ((Register) entity).getIdRegister() + " " + DateFormats.format(((Register) entity).getRegisterDate(), true);
152  } else if(entity instanceof Account) {
153  return ((Account) entity).getId() + " - " + ((Account) entity).getDescription();
154  } else if(entity instanceof MajorAccount) {
155  return ((MajorAccount) entity).getAccount()+ " - " + ((MajorAccount) entity).getDescription();
156  }
157  }
158  return null;
159  }
160 
161  @Override
162  public String getLabel() {
163  if(getPath() != null) {
164  String root = path.getRoot();
165  if(null != root) switch (root) {
166  case "contract":
167  return I_.get("Contract");
168  case "document":
169  return I_.get("Document");
170  case "batchof":
171  return I_.get("Batch of");
172  case "product":
173  return I_.get("Product");
174  case "fixed-asset":
175  return I_.get("Fixed asset");
176  case "company":
177  return I_.get("Company");
178  case "headquarters":
179  return I_.get("Headquarters");
180  case "department":
181  return I_.get("Department");
182  case "service":
183  return I_.get("Service");
184  case "register":
185  return I_.get("Register");
186  case "account":
187  return I_.get("Account");
188  case "major-account":
189  return I_.get("Major account");
190  default:
191  break;
192  }
193  }
194  return null;
195  }
196 
197  @Override
198  public String getDescription() {
199  if(getEntity() != null) {
200  if(entity instanceof Contract) {
201  return ((Contract) entity).getContractDefinition().getDescription();
202  }
203  }
204  return null;
205  }
206 
207  @Override
208  public Object getLabelCtrl() {
209  if(getEntity() != null) {
210  if(entity instanceof Document) {
211  return new DocumentLabel(entity);
212  } else if(entity instanceof BatchOf) {
213  return new DocumentLabel(entity);
214  } else {
215  return new LabelControl(entity);
216  }
217  }
218  return null;
219  }
220 
221  @Override
222  public String getImage() {
223  if(getPath() != null) {
224  String root = path.getRoot();
225  if(null != root) switch (root) {
226  case "contract":
227  return "/_zul/images/contract.png";
228  case "document":
229  return "/_zul/images/document.png";
230  case "batchof":
231  return "/_zul/images/batchof.png";
232  case "product":
233  return "/_zul/images/product.png";
234  case "fixed-asset":
235  return "/_zul/images/asset.png";
236  case "company":
237  return "/_zul/images/b_company.png";
238  case "headquarters":
239  return "/_zul/images/b_headquarters.png";
240  case "department":
241  return "/_zul/images/b_department.png";
242  case "service":
243  return "/_zul/images/b_service.png";
244  case "register":
245  return "/_zul/images/register.png";
246  default:
247  break;
248  }
249  }
250  return null;
251  }
252 
253  @Override
254  public String getHierarchicalPath() {
255  if(getEntity() != null) {
256  if(entity instanceof Document) {
257  Contract contract = ((Document) entity).getContract();
258  return FinancialsPU.getObjectPath(contract) +
260  }
261  }
262  return getPath();
263  }
264 
265  @Override
267  if(getEntity() != null) {
268  if(entity instanceof Contract) {
269  return Entities.getController("/contact/" + ((Contract) entity).getContractor());
270  } else if(entity instanceof Document) {
271  return Entities.getController(((Document) entity).getContract());
272  }
273  }
274  return null;
275  }
276 
277  @Override
278  public List<IElephantEntity> getChildren() {
279  if(getEntity() != null) {
280  if(entity instanceof Contract) {
281  Contract contract = (Contract) entity;
282  return new EntityListAdapter(contract.getDocuments());
283  }
284  }
285  return Collections.EMPTY_LIST;
286  }
287 
288  @Override
289  public String getExtendedPath() {
291  }
292 
293  @Override
294  public File getFile() {
295  return null;
296  }
297 
298  @Override
299  public boolean showPath() {
300  return false;
301  }
302 
303  @Override
304  public boolean showEntity() {
305  if(getEntity() != null) {
306  if(entity instanceof Contract) {
308  return true;
309  } else if(entity instanceof Document) {
311  return true;
312  } else if(entity instanceof BatchOf) {
314  return true;
315  } else if(entity instanceof Product) {
317  return true;
318  } else if(entity instanceof FixedAsset) {
320  return true;
321  } else if(entity instanceof Register) {
323  return true;
324  } else if(entity instanceof Company) {
326  return true;
327  }
328  }
329  return false;
330  }
331 
332  @Override
333  public boolean canEdit(IContact contact) {
334  if(getEntity() != null) {
335  if(entity instanceof Contract) {
336  return contact.getPermissions().isInRole("finan-contract:edit");
337  } else if(entity instanceof Document) {
338  return contact.getPermissions().isInRole("finan-document:edit");
339  } else if(entity instanceof BatchOf) {
340  return contact.getPermissions().isInRole("finan-document:edit");
341  } else if(entity instanceof Product) {
342  return contact.getPermissions().isInRole("finan-product:edit");
343  } else if(entity instanceof FixedAsset) {
344  return contact.getPermissions().isInRole("finan-fixed-asset:edit");
345  } else if(entity instanceof Register) {
346  return contact.getPermissions().isInRole("finan-register:edit");
347  } else if(entity instanceof Company) {
348  return contact.getPermissions().isInRole("finan-model:edit");
349  }
350  }
351  return false;
352  }
353 
354  @Override
355  public boolean canPublish(IContact contact) {
356  if(getEntity() != null) {
357  if(entity instanceof Contract) {
358  Contract ctc = (Contract) getEntity();
359  return ctc.getIParticipants().contains(contact);
360  } else if(entity instanceof Document) {
361  Document doc = (Document) getEntity();
362  return doc.getContract().getIParticipants().contains(contact);
363  } else if(entity instanceof BatchOf) {
364  BatchOf doc = (BatchOf) getEntity();
365  return doc.getBatch().getContract().getIParticipants().contains(contact);
366  } else if(entity instanceof Product) {
367  Product prod = (Product) getEntity();
368  return prod.isPublishable();
369  }
370  }
371  return false;
372  }
373 
374  @Override
375  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
376  ArrayList<Object> list = new ArrayList<>();
377  list.addAll(searchContracts(search, maxResults, kvm));
378  list.addAll(searchProducts(search, maxResults, kvm));
379  list.addAll(searchFixedAssets(search, maxResults, kvm));
380  return list;
381  }
382 
383  @Override
384  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
385  ArrayList<Object> list = new ArrayList<>();
386  if(null != root) switch (root) {
387  case "contract":
388  list.addAll(searchContracts(search, maxResults, kvm));
389  break;
390  case "product":
391  list.addAll(searchProducts(search, maxResults, kvm));
392  break;
393  case "fixed-asset":
394  list.addAll(searchFixedAssets(search, maxResults, kvm));
395  break;
396  default:
397  break;
398  }
399  return list;
400  }
401 
402  @Override
403  public Object configureCtrl(Object ctrl, IContact contact) {
404  if(ctrl instanceof CommentItCtrl) {
405  CommentItCtrl ictrl = (CommentItCtrl) ctrl;
406  ictrl.setEntityPath(getPath());
407  return ictrl;
408  } else if(ctrl instanceof AttachCtrl) {
409  AttachCtrl ictrl = (AttachCtrl) ctrl;
410  ictrl.setEntityPath(getPath());
411  if(getEntity() instanceof Contract) {
412  Contract ctc = (Contract) getEntity();
413  ictrl.setPublicOnly(true);
414  if(ctc.getIParticipants().contains(contact)) {
415  ictrl.setPublicOnly(false);
416  }
417  }
418  if(getEntity() instanceof Document) {
419  Contract ctc = (Contract) ((Document) getEntity()).getContract();
420  ictrl.setPublicOnly(true);
421  if(ctc.getIParticipants().contains(contact)) {
422  ictrl.setPublicOnly(false);
423  }
424  }
425  return ictrl;
426  } else if(ctrl instanceof FileAttach) {
427  FileAttach ictrl = (FileAttach) ctrl;
428  return ictrl;
429  } else if(ctrl instanceof DescribeItCtrl) {
430  DescribeItCtrl ictrl = (DescribeItCtrl) ctrl;
431  ictrl.setEntityPath(getPath());
432  return ictrl;
433  } else if(ctrl instanceof IEntityCtrl) {
434  IEntityCtrl ictrl = (IEntityCtrl) ctrl;
435  ictrl.setEntityPath(getPath());
436  return ictrl;
437  }
438  return ctrl;
439  }
440 
441  @Override
442  public boolean canShowInContext(IContact contact, Object relatedEntity) {
443  return true;
444  }
445 
446  @Override
447  public Collection<Object> getRelatedByRole(EntityRole role, IContact contact) {
448  switch(role) {
449  case SELF_AS_COMPANY_OWNER:
450  return getRelatedEntities(role, contact);
451  }
452  return Collections.EMPTY_LIST;
453  }
454 
455  @Override
456  public boolean hasRelatedRole(EntityRole role, IContact contact) {
457  switch(role) {
458  case SELF_AS_COMPANY_OWNER:
459  if(getEntity() instanceof Contract) {
460  if(((Contract) getEntity()).getContractor().equals(contact.getId())) {
461  return true;
462  }
463  for(ContractParticipant cp : ((Contract) getEntity()).getParticipants()) {
464  if(cp.isSignature() && cp.getIdContact().equals(contact.getId())) {
465  return true;
466  }
467  }
468  }
469  break;
470  }
471  return false;
472  }
473 
474  @Override
475  public String getExtendedWebPath() {
476  if(getEntity() != null) {
477  if(entity instanceof Contract) {
478  Contract contract = (Contract) entity;
479  return "/" + contract.getId() +
480  "/" + Strings.unpunctuate(contract.getName()).toLowerCase();
481  }
482  }
483  return null;
484  }
485 
486  @Override
487  public String getUserContext() {
488  return "/user/" + getLinkRoot() + "s";
489  }
490 
491  @Override
492  protected Dao createDao() {
493  return new FinancialsPU();
494  }
495 
496  @Override
497  protected boolean itsMine(String rootPath) {
498  return super.itsMine(rootPath) ||
499  // Only for label purposes
500  "account".equals(rootPath) || "major-account".equals(rootPath);
501  }
502 
503  @Override
504  protected boolean itsMine(Object entity) {
505  return (entity instanceof Contract) || (entity instanceof Document) || (entity instanceof BatchOf) ||
506  (entity instanceof Product) || (entity instanceof FixedAsset) || (entity instanceof Company) ||
507  (entity instanceof Headquarters) || (entity instanceof Department) || (entity instanceof Service) ||
508  (entity instanceof Register) ||
509  // Only for label purposes
510  (entity instanceof Account) || (entity instanceof MajorAccount);
511  }
512 
513  @Override
514  public Collection<String> getAllowedRoots() {
515  return Arrays.asList("contract", "document", "batchof", "product", "fixed-asset",
516  "company", "headquarters", "department", "service", "register");
517  }
518 
519  @Override
521  return new FinancialsEntities(entityPath);
522  }
523 
524  @Override
526  return new FinancialsEntities(entity);
527  }
528 
529  @Override
530  protected Path createPath() {
531  return new Path(FinancialsPU.getObjectPath(entity));
532  }
533 
534  private List<Object> searchContracts(String search, int maxResults, KeyValueMap kvm) {
535  WhereClause wc = new WhereClause();
536  wc.addClause("select c from Contract c");
537  wc.addClause("where c.name like :search");
538  wc.addClause("order by c.name");
539  wc.addNamedValue("search", "%" + search + "%");
540  return getDao().getResultList(wc, maxResults);
541  }
542 
543  private List<Object> searchProducts(String search, int maxResults, KeyValueMap kvm) {
544  WhereClause wc = new WhereClause();
545  wc.addClause("select c from Product c");
546  wc.addClause("where c.description like :search");
547  wc.addClause("order by c.description");
548  wc.addNamedValue("search", "%" + search + "%");
549  return getDao().getResultList(wc, maxResults);
550  }
551 
552  private List<Object> searchFixedAssets(String search, int maxResults, KeyValueMap kvm) {
553  WhereClause wc = new WhereClause();
554  wc.addClause("select c from FixedAsset c");
555  wc.addClause("where c.name like :search");
556  wc.addClause("order by c.name");
557  wc.addNamedValue("search", "%" + search + "%");
558  return getDao().getResultList(wc, maxResults);
559  }
560 
561  private Collection<Object> getRelatedEntities(EntityRole role, IContact contact) {
562  switch(role) {
563  case SELF_AS_COMPANY_OWNER: // contract related
564  String idContact = contact.getId();
565  WhereClause wc = new WhereClause();
566  wc.addClause("select distinct ctc from Contract ctc");
567  wc.addClause("left join ctc.participants par");
568  wc.addClause("where 1=1");
569  wc.addClause("and (ctc.contractor = :contractor");
570  wc.addNamedValue("contractor", idContact);
571  for(ContractInterventionType cit : ContractInterventionType.values()) {
572  if(cit.isSignature()) {
573  wc.addClause("or (par.interventionType = :cit" + cit.toString());
574  wc.addNamedValue("cit" + cit.toString(), cit);
575  wc.addClause("and par.idContact = :con" + cit.toString() + ")");
576  wc.addNamedValue("con" + cit.toString(), idContact);
577  }
578  }
579  wc.addClause(")");
580  return getDao().getResultList(wc);
581  }
582  return Collections.EMPTY_LIST;
583  }
584 
585 }
void setPublicOnly(boolean publicOnly)
Definition: AttachCtrl.java:77
void setEntityPath(String entityPath)
Definition: AttachCtrl.java:45
void addNamedValue(String name, Object value)
static final String format(Date d, boolean dateOnly)
static IElephantEntity getController(String path)
Definition: Entities.java:78
boolean canShowInContext(IContact contact, Object relatedEntity)
IElephantEntity createInstance(Object entity)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
IElephantEntity createInstance(String entityPath)
boolean hasRelatedRole(EntityRole role, IContact contact)
Collection< Object > getRelatedByRole(EntityRole role, IContact contact)
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
Object configureCtrl(Object ctrl, IContact contact)
static String getObjectPath(Object object)
static String getObjectExtendedPath(Object object)
Collection< IContact > getIParticipants()
Definition: Contract.java:462
static String get(String msg)
Definition: I_.java:41
void setEntityPath(String entityPath)
void setEntityPath(String entityPath)