BrightSide Workbench Full Report + Source Code
CrmEntities.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.annotation.ElephantEntity;
28 import org.turro.collections.KeyValueMap;
29 import org.turro.command.LabelControl;
30 import org.turro.crm.db.CrmPU;
31 import org.turro.crm.entity.Campaign;
32 import org.turro.crm.entity.Customer;
33 import org.turro.crm.entity.SaleAction;
34 import org.turro.crm.entity.SaleProspect;
35 import org.turro.crm.entity.Vendor;
36 import org.turro.crm.entity.VendorProspect;
37 import org.turro.crm.zul.menu.CrmMenu;
38 import org.turro.elephant.db.WhereClause;
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 
48 @ElephantEntity
49 public class CrmEntities extends DefaultElephantEntity {
50 
51  public CrmEntities() {
52  }
53 
54  public CrmEntities(String entityPath) {
55  super(entityPath);
56  }
57 
58  public CrmEntities(Object entity) {
59  super(entity);
60  }
61 
62  @Override
63  public Object getEntity() {
64  if(entity == null) {
65  if(getPath() != null) {
66  if(isValidLongEntity()) {
67  if(null != path.getRoot()) switch (path.getRoot()) {
68  case "vendor":
69  entity = getDao().find(Vendor.class, path.getNodeAs(1, Long.class));
70  break;
71  case "customer":
72  entity = getDao().find(Customer.class, path.getNodeAs(1, Long.class));
73  break;
74  case "campaign":
75  entity = getDao().find(Campaign.class, path.getNodeAs(1, Long.class));
76  break;
77  case "sale-prospect":
78  entity = getDao().find(SaleProspect.class, path.getNodeAs(1, Long.class));
79  break;
80  case "vendor-prospect":
81  entity = getDao().find(VendorProspect.class, path.getNodeAs(1, Long.class));
82  break;
83  case "sale-action":
84  entity = getDao().find(SaleAction.class, path.getNodeAs(1, Long.class));
85  break;
86  default:
87  break;
88  }
89  }
90  }
91  }
92  return entity;
93  }
94 
95  @Override
96  public String getName() {
97  if(getEntity() != null) {
98  if(entity instanceof Vendor) {
99  return ((Vendor) entity).getName();
100  } else if(entity instanceof Customer) {
101  return ((Customer) entity).getName();
102  } else if(entity instanceof Campaign) {
103  return ((Campaign) entity).getDescription();
104  } else if(entity instanceof SaleProspect) {
105  return ((SaleProspect) entity).getFullDescription();
106  } else if(entity instanceof SaleAction) {
107  return ((SaleAction) entity).getComment();
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 "vendor":
119  return I_.get("Vendor");
120  case "customer":
121  return I_.get("Customer");
122  case "campaign":
123  return I_.get("Campaign");
124  case "sale-prospect":
125  return I_.get("Sale prospect");
126  case "vendor-prospect":
127  return I_.get("Vendor prospect");
128  case "sale-action":
129  return I_.get("Sale action");
130  default:
131  break;
132  }
133  }
134  return null;
135  }
136 
137  @Override
138  public String getDescription() {
139  return null;
140  }
141 
142  @Override
143  public Object getLabelCtrl() {
144  if(getEntity() != null) {
145  return new LabelControl(entity);
146  }
147  return null;
148  }
149 
150  @Override
151  public String getImage() {
152  if(getPath() != null) {
153  String root = path.getRoot();
154  if(null != root) switch (root) {
155  case "vendor":
156  return "/_zul/images/vendor.png";
157  case "customer":
158  return "/_zul/images/customer.png";
159  case "campaign":
160  return "/_zul/images/campaign.png";
161  case "sale-prospect":
162  return "/_zul/images/sale.png";
163  case "sale-action":
164  return "/_zul/images/appointment.png";
165  default:
166  break;
167  }
168  }
169  return null;
170  }
171 
172  @Override
173  public String getHierarchicalPath() {
174  return getPath();
175  }
176 
177  @Override
179  return null;
180  }
181 
182  @Override
183  public List<IElephantEntity> getChildren() {
184  return Collections.EMPTY_LIST;
185  }
186 
187  @Override
188  public String getExtendedPath() {
189  return "";
190  }
191 
192  @Override
193  public File getFile() {
194  return null;
195  }
196 
197  @Override
198  public boolean showPath() {
199  return false;
200  }
201 
202  @Override
203  public boolean showEntity() {
204  if(getEntity() != null) {
205  if(entity instanceof Vendor) {
206  CrmMenu.showVendor(((Vendor) entity).getId());
207  return true;
208  } else if(entity instanceof Customer) {
209  CrmMenu.showCustomer(((Customer) entity).getId());
210  return true;
211  } else if(entity instanceof Campaign) {
212  CrmMenu.showCampaign(((Campaign) entity).getId());
213  return true;
214  } else if(entity instanceof SaleProspect) {
216  return true;
217  } else if(entity instanceof SaleAction) {
221  return true;
222  }
223  }
224  return false;
225  }
226 
227  @Override
228  public boolean canEdit(IContact contact) {
229  if(getEntity() != null) {
230  if(entity instanceof Vendor) {
231  return contact.getPermissions().isInRole("vendor:edit");
232  } else if(entity instanceof Customer) {
233  return contact.getPermissions().isInRole("customer:edit");
234  } else if(entity instanceof Campaign) {
235  return contact.getPermissions().isInRole("campaign:edit");
236  } else if(entity instanceof SaleProspect) {
237  return contact.getPermissions().isInRole("sale-prospect:edit");
238  } else if(entity instanceof SaleAction) {
239  return contact.getPermissions().isInRole("sale-action:edit");
240  }
241  }
242  return false;
243  }
244 
245  @Override
246  public boolean canPublish(IContact contact) {
247  if(getEntity() != null) {
248  if(entity instanceof Vendor) {
249  return contact.getPermissions().isInRole("vendor:list");
250  } else if(entity instanceof Customer) {
251  return contact.getPermissions().isInRole("customer:list");
252  } else if(entity instanceof Campaign) {
253  return contact.getPermissions().isInRole("campaign:list");
254  } else if(entity instanceof SaleProspect) {
255  return contact.getPermissions().isInRole("sale-prospect:list");
256  } else if(entity instanceof SaleAction) {
257  return contact.getPermissions().isInRole("sale-action:list");
258  }
259  }
260  return false;
261  }
262 
263  @Override
264  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
265  ArrayList<Object> list = new ArrayList<>();
266  list.addAll(searchVendors(search, maxResults, kvm));
267  list.addAll(searchCustomers(search, maxResults, kvm));
268  return list;
269  }
270 
271  @Override
272  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
273  ArrayList<Object> list = new ArrayList<>();
274  if(null != root) switch (root) {
275  case "vendor":
276  list.addAll(searchVendors(search, maxResults, kvm));
277  break;
278  case "customer":
279  list.addAll(searchCustomers(search, maxResults, kvm));
280  break;
281  default:
282  break;
283  }
284  return list;
285  }
286 
287  @Override
288  public Object configureCtrl(Object ctrl, IContact contact) {
289  return ctrl;
290  }
291 
292  @Override
293  public boolean canShowInContext(IContact contact, Object relatedEntity) {
294  return true;
295  }
296 
297  @Override
298  public String getExtendedWebPath() {
299  return null;
300  }
301 
302  @Override
303  public String getUserContext() {
304  return null;
305  }
306 
307  @Override
308  protected Dao createDao() {
309  return new CrmPU();
310  }
311 
312  @Override
313  protected boolean itsMine(String rootPath) {
314  return getAllowedRoots().contains(rootPath);
315  }
316 
317  @Override
318  protected boolean itsMine(Object entity) {
319  return (entity instanceof Vendor) || (entity instanceof Customer) ||
320  (entity instanceof Campaign) || (entity instanceof SaleProspect) ||
321  (entity instanceof VendorProspect) || (entity instanceof SaleAction);
322  }
323 
324  @Override
325  public Collection<String> getAllowedRoots() {
326  return Arrays.asList("vendor", "customer", "campaign", "sale-prospect",
327  "vendor-prospect", "sale-action");
328  }
329 
330  @Override
332  return new CrmEntities(entityPath);
333  }
334 
335  @Override
337  return new CrmEntities(entity);
338  }
339 
340  @Override
341  protected Path createPath() {
342  return new Path(CrmPU.getObjectPath(entity));
343  }
344 
345  private List<Object> searchVendors(String search, int maxResults, KeyValueMap kvm) {
346  WhereClause wc = new WhereClause();
347  wc.addClause("select c from Vendor c");
348  wc.addClause("where c.name like :search");
349  wc.addClause("order by c.name");
350  wc.addNamedValue("search", "%" + search + "%");
351  return getDao().getResultList(wc, maxResults);
352  }
353 
354  private List<Object> searchCustomers(String search, int maxResults, KeyValueMap kvm) {
355  WhereClause wc = new WhereClause();
356  wc.addClause("select c from Customer c");
357  wc.addClause("where c.name like :search");
358  wc.addClause("order by c.name");
359  wc.addNamedValue("search", "%" + search + "%");
360  return getDao().getResultList(wc, maxResults);
361  }
362 
363 }
static String getObjectPath(Object object)
Definition: CrmPU.java:43
VendorProspect getVendorProspect()
static void showSaleProspect(Long id)
Definition: CrmMenu.java:65
static void showSaleActions(Vendor vendor, SaleProspect saleProspect)
Definition: CrmMenu.java:69
static void showVendor(long id)
Definition: CrmMenu.java:57
static void showCampaign(Long id)
Definition: CrmMenu.java:61
static void showCustomer(long id)
Definition: CrmMenu.java:53
void addNamedValue(String name, Object value)
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
boolean canShowInContext(IContact contact, Object relatedEntity)
IElephantEntity createInstance(String entityPath)
boolean itsMine(String rootPath)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
boolean itsMine(Object entity)
boolean canPublish(IContact contact)
IElephantEntity createInstance(Object entity)
List< IElephantEntity > getChildren()
Object configureCtrl(Object ctrl, IContact contact)
CrmEntities(String entityPath)
Collection< String > getAllowedRoots()
boolean canEdit(IContact contact)
static String get(String msg)
Definition: I_.java:41