BrightSide Workbench Full Report + Source Code
AllianceEntities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.alliance.db.AlliancePU;
28 import org.turro.alliance.db.entities.AxCenter;
29 import org.turro.alliance.db.entities.AxContact;
30 import org.turro.alliance.db.entities.AxProject;
31 import org.turro.alliance.db.entities.AxProjectGrant;
32 import org.turro.alliance.db.entities.AxStudent;
33 import org.turro.alliance.db.entities.ProcedenceId;
34 import org.turro.annotation.ElephantEntity;
35 import org.turro.collections.KeyValueMap;
36 import org.turro.command.LabelControl;
37 import org.turro.i18n.I_;
38 import org.turro.jpa.Dao;
39 import org.turro.path.Path;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.sql.SqlClause;
42 
47 @ElephantEntity
49 
50  public AllianceEntities() {
51  }
52 
53  public AllianceEntities(String entityPath) {
54  super(entityPath);
55  }
56 
57  public AllianceEntities(Object entity) {
58  super(entity);
59  }
60 
61  @Override
62  public Object getEntity() {
63  if(entity == null) {
64  if(getPath() != null) {
65  if(path.getSize() == 2) {
66  if(null != path.getRoot()) switch (path.getRoot()) {
67  case "axproject":
68  entity = getDao().find(AxProject.class, ProcedenceId.from(path.getNode(1)));
69  break;
70  case "axcontact":
71  entity = getDao().find(AxContact.class, ProcedenceId.from(path.getNode(1)));
72  break;
73  case "axproject-grant":
74  entity = getDao().find(AxProjectGrant.class, ProcedenceId.from(path.getNode(1)));
75  break;
76  case "axstudent":
77  entity = getDao().find(AxStudent.class, ProcedenceId.from(path.getNode(1)));
78  break;
79  case "axcenter":
80  entity = getDao().find(AxCenter.class, ProcedenceId.from(path.getNode(1)));
81  break;
82  default:
83  break;
84  }
85  }
86  }
87  }
88  return entity;
89  }
90 
91  @Override
92  public String getName() {
93  if(getEntity() != null) {
94  if(entity instanceof AxProject) {
95  return ((AxProject) entity).getFriendlyName();
96  } else if(entity instanceof AxContact) {
97  return ((AxContact) entity).getName();
98  } else if(entity instanceof AxProjectGrant) {
99  return ((AxProjectGrant) entity).getName();
100  } else if(entity instanceof AxStudent) {
101  return ((AxStudent) entity).getName();
102  } else if(entity instanceof AxCenter) {
103  return ((AxCenter) entity).getName();
104  }
105  }
106  return null;
107  }
108 
109  @Override
110  public String getLabel() {
111  if(getPath() != null) {
112  String root = path.getRoot();
113  if(null != root) switch (root) {
114  case "axproject":
115  return I_.get("Project");
116  case "axcontact":
117  return I_.get("Contact");
118  case "axproject-grant":
119  return I_.get("Project grant");
120  case "axstudent":
121  return I_.get("Student");
122  case "axcenter":
123  return I_.get("Learning center");
124  default:
125  break;
126  }
127  }
128  return null;
129  }
130 
131  @Override
132  public String getDescription() {
133  return null;
134  }
135 
136  @Override
137  public Object getLabelCtrl() {
138  if(getEntity() != null) {
139  return new LabelControl(getEntity());
140  }
141  return null;
142  }
143 
144  @Override
145  public String getImage() {
146  if(getPath() != null) {
147  String root = path.getRoot();
148  if(null != root) switch (root) {
149  case "axproject":
150  return "/_zul/images/project.png";
151  case "axcontact":
152  return "/_zul/images/contact.png";
153  case "axproject-grant":
154  return "/_zul/images/handshelp.png";
155  case "axstudent":
156  return "/_zul/images/student.png";
157  case "axcenter":
158  return "/_zul/images/university.png";
159  default:
160  break;
161  }
162  }
163  return null;
164  }
165 
166  @Override
167  public String getHierarchicalPath() {
168  if(getEntity() != null) {
169  }
170  return getPath();
171  }
172 
173  @Override
175  if(getEntity() != null) {
176  if(entity instanceof AxProject) {
177  return null;
178  } else if(entity instanceof AxContact) {
179  return null;
180  } else if(entity instanceof AxProjectGrant) {
181  return null;
182  } else if(entity instanceof AxStudent) {
183  return null;
184  } else if(entity instanceof AxCenter) {
185  return null;
186  }
187  }
188  return null;
189  }
190 
191  @Override
192  public List<IElephantEntity> getChildren() {
193  if(getEntity() != null) {
194  if(entity instanceof AxProject) {
195  } else if(entity instanceof AxContact) {
196  } else if(entity instanceof AxProjectGrant) {
197  } else if(entity instanceof AxStudent) {
198  } else if(entity instanceof AxCenter) {
199  }
200  }
201  return Collections.EMPTY_LIST;
202  }
203 
204  @Override
205  public String getExtendedPath() {
206  return null;
207  }
208 
209  @Override
210  public File getFile() {
211  return null;
212  }
213 
214  @Override
215  public boolean showPath() {
216  return false;
217  }
218 
219  @Override
220  public boolean showEntity() {
221  return false;
222  }
223 
224  @Override
225  public boolean canEdit(IContact contact) {
226  return false;
227  }
228 
229  @Override
230  public boolean canPublish(IContact contact) {
231  if(getEntity() != null) {
232  }
233  return false;
234  }
235 
236  @Override
237  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
238  ArrayList<Object> list = new ArrayList<>();
239  list.addAll(searchProjects(search, maxResults, kvm));
240  list.addAll(searchContacts(search, maxResults, kvm));
241  list.addAll(searchProjectGrants(search, maxResults, kvm));
242  list.addAll(searchStudents(search, maxResults, kvm));
243  list.addAll(searchCenters(search, maxResults, kvm));
244  return list;
245  }
246 
247  @Override
248  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
249  ArrayList<Object> list = new ArrayList<>();
250  if(null != root) switch (root) {
251  case "axproject":
252  list.addAll(searchProjects(search, maxResults, kvm));
253  break;
254  case "axcontact":
255  list.addAll(searchContacts(search, maxResults, kvm));
256  break;
257  case "axproject-grant":
258  list.addAll(searchProjectGrants(search, maxResults, kvm));
259  break;
260  case "axstudent":
261  list.addAll(searchStudents(search, maxResults, kvm));
262  break;
263  case "axcenter":
264  list.addAll(searchCenters(search, maxResults, kvm));
265  break;
266  default:
267  break;
268  }
269  return list;
270  }
271 
272  @Override
273  public Object configureCtrl(Object ctrl, IContact contact) {
274  return ctrl;
275  }
276 
277  @Override
278  public boolean canShowInContext(IContact contact, Object relatedEntity) {
279  return true;
280  }
281 
282  @Override
283  public String getExtendedWebPath() {
284  if(getEntity() != null) {
285  }
286  return null;
287  }
288 
289  @Override
290  public String getUserContext() {
291  if(getPath() != null) {
292  String root = path.getRoot();
293  if(null != root) switch (root) {
294  case "axproject":
295  return "/user/alliance/projects";
296  default:
297  break;
298  }
299  }
300  return null;
301  }
302 
303  @Override
304  protected Dao createDao() {
305  return new AlliancePU();
306  }
307 
308  @Override
309  protected boolean itsMine(Object entity) {
310  return (entity instanceof AxProject) || (entity instanceof AxContact) ||
311  (entity instanceof AxProjectGrant) || (entity instanceof AxStudent) ||
312  (entity instanceof AxCenter);
313  }
314 
315  @Override
316  public Collection<String> getAllowedRoots() {
317  return Arrays.asList("axproject", "axcontact", "axproject-grant", "axstudent", "axcenter");
318  }
319 
320  @Override
322  return new AllianceEntities(entityPath);
323  }
324 
325  @Override
327  return new AllianceEntities(entity);
328  }
329 
330  @Override
331  protected Path createPath() {
332  return new Path(AlliancePU.getObjectPath(entity));
333  }
334 
335  private List<AxProject> searchProjects(String search, int maxResults, KeyValueMap kvm) {
336  return SqlClause.select("p").from("AxProject p")
337  .where().search(search, "p.name", "p.code")
338  .orderBy("p.name")
339  .max(maxResults)
340  .dao(getDao())
341  .resultList(AxProject.class);
342  }
343 
344  private List<AxContact> searchContacts(String search, int maxResults, KeyValueMap kvm) {
345  return SqlClause.select("p").from("AxContact p")
346  .where().search(search, "p.name", "p.company")
347  .orderBy("p.name")
348  .max(maxResults)
349  .dao(getDao())
350  .resultList(AxContact.class);
351  }
352 
353  private List<AxProjectGrant> searchProjectGrants(String search, int maxResults, KeyValueMap kvm) {
354  return SqlClause.select("p").from("AxProjectGrant p")
355  .where().search(search, "p.title")
356  .orderBy("p.title")
357  .max(maxResults)
358  .dao(getDao())
359  .resultList(AxProjectGrant.class);
360  }
361 
362  private List<AxStudent> searchStudents(String search, int maxResults, KeyValueMap kvm) {
363  return SqlClause.select("p").from("AxStudent p")
364  .where().search(search, "p.name", "p.email")
365  .orderBy("p.name")
366  .max(maxResults)
367  .dao(getDao())
368  .resultList(AxStudent.class);
369  }
370 
371  private List<AxCenter> searchCenters(String search, int maxResults, KeyValueMap kvm) {
372  return SqlClause.select("p").from("AxCenter p")
373  .where().search(search, "p.name", "p.email")
374  .orderBy("p.name")
375  .max(maxResults)
376  .dao(getDao())
377  .resultList(AxCenter.class);
378  }
379 
380 }
static String getObjectPath(Object object)
Definition: AlliancePU.java:60
static ProcedenceId from(String id)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
List< IElephantEntity > getChildren()
boolean canPublish(IContact contact)
boolean canShowInContext(IContact contact, Object relatedEntity)
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
IElephantEntity createInstance(Object entity)
Object configureCtrl(Object ctrl, IContact contact)
IElephantEntity createInstance(String entityPath)
static String get(String msg)
Definition: I_.java:41