BrightSide Workbench Full Report + Source Code
StudentsEntities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.action.IEntityCtrl;
28 import org.turro.action.LinkType;
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.file.util.FileAttach;
35 import org.turro.i18n.I_;
36 import org.turro.jpa.Dao;
37 import org.turro.path.Path;
38 import org.turro.plugin.contacts.IContact;
39 import org.turro.polls.PollsCtrl;
40 import org.turro.students.db.StudentsPU;
41 import org.turro.students.entities.Challenge;
42 import org.turro.students.entities.PracticalWork;
43 import org.turro.students.entities.Response;
44 import org.turro.students.entities.TalentCategory;
45 import org.turro.students.menu.StudentsMenu;
46 import org.turro.www.commentit.CommentItCtrl;
47 import org.turro.www.describeit.DescribeItCtrl;
48 import org.turro.www.starit.StarItCtrl;
49 import org.turro.www.voteit.VoteItCtrl;
50 
55 @ElephantEntity
57 
58  public StudentsEntities() {
59  }
60 
61  public StudentsEntities(String entityPath) {
62  super(entityPath);
63  }
64 
65  public StudentsEntities(Object entity) {
66  super(entity);
67  }
68 
69  @Override
70  public Object getEntity() {
71  if(entity == null) {
72  if(getPath() != null) {
73  if(isValidLongEntity()) {
74  if(null != path.getRoot()) switch (path.getRoot()) {
75  case "challenge":
76  entity = getDao().find(Challenge.class, path.getNodeAs(1, Long.class));
77  break;
78  case "response":
79  entity = getDao().find(Response.class, path.getNodeAs(1, Long.class));
80  break;
81  case "practicalwork":
82  entity = getDao().find(PracticalWork.class, path.getNodeAs(1, Long.class));
83  break;
84  case "talent-category":
85  entity = getDao().find(TalentCategory.class, path.getNodeAs(1, Long.class));
86  break;
87  default:
88  break;
89  }
90  }
91  }
92  }
93  return entity;
94  }
95 
96  @Override
97  public String getName() {
98  if(getEntity() != null) {
99  if(entity instanceof Challenge) {
100  return ((Challenge) entity).getQuestion();
101  } else if(entity instanceof Response) {
102  return ((Response) entity).getChallenge().getQuestion();
103  } else if(entity instanceof PracticalWork) {
104  return ((PracticalWork) entity).getTitle();
105  } else if(entity instanceof TalentCategory) {
106  return ((TalentCategory) entity).getName();
107  }
108  }
109  return null;
110  }
111 
112  @Override
113  public String getLabel() {
114  if(getPath() != null) {
115  String root = path.getRoot();
116  if(null != root) switch (root) {
117  case "challenge":
118  return I_.get("Challenge");
119  case "response":
120  return I_.get("Answer");
121  case "practicalwork":
122  return I_.get("Practical work");
123  case "talent-category":
124  return I_.get("Talent category");
125  default:
126  break;
127  }
128  }
129  return null;
130  }
131 
132  @Override
133  public String getDescription() {
134  return null;
135  }
136 
137  @Override
138  public Object getLabelCtrl() {
139  if(getEntity() != null && !(entity instanceof TalentCategory)) {
140  return new LabelControl(getEntity());
141  }
142  return null;
143  }
144 
145  @Override
146  public String getImage() {
147  if(getPath() != null) {
148  String root = path.getRoot();
149  if(null != root) switch (root) {
150  case "challenge":
151  return "/_zul/images/challenge.png";
152  case "response":
153  return "/_zul/images/response.png";
154  case "practicalwork":
155  return "/_zul/images/operator.png";
156  case "talent-category":
157  return "/_zul/images/group.png";
158  default:
159  break;
160  }
161  }
162  return null;
163  }
164 
165  @Override
166  public String getHierarchicalPath() {
167  return getPath();
168  }
169 
170  @Override
172  if(getEntity() != null) {
173  if(entity instanceof Challenge) {
174  return ((Challenge) entity).getEntity();
175  } else if(entity instanceof Response) {
176  return Entities.getController(((Response) entity).getChallenge());
177  }
178  }
179  return null;
180  }
181 
182  @Override
183  public List<IElephantEntity> getChildren() {
184  if(getEntity() != null) {
185  if(entity instanceof Challenge) {
186  return new EntityListAdapter(((Challenge) entity).getResponses());
187  }
188  }
189  return Collections.EMPTY_LIST;
190  }
191 
192  @Override
193  public String getExtendedPath() {
195  }
196 
197  @Override
198  public File getFile() {
199  return null;
200  }
201 
202  @Override
203  public boolean showPath() {
204  if(getPath() != null) {
205  String root = path.getRoot();
206  }
207  return false;
208  }
209 
210  @Override
211  public boolean showEntity() {
212  if(getEntity() != null) {
213  if(entity instanceof Challenge) {
215  return true;
216  }
217  }
218  return false;
219  }
220 
221  @Override
222  public boolean canEdit(IContact contact) {
223  if(getEntity() != null) {
224  }
225  return false;
226  }
227 
228  @Override
229  public boolean canPublish(IContact contact) {
230  if(getEntity() != null) {
231  return true;
232  }
233  return false;
234  }
235 
236  @Override
237  public boolean canShowInContext(IContact contact, Object relatedEntity) {
238  return true;
239  }
240 
241  @Override
242  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
243  ArrayList<Object> list = new ArrayList<>();
244  list.addAll(searchChallenges(search, maxResults, kvm));
245  list.addAll(searchCategories(search, maxResults, kvm));
246  return list;
247  }
248 
249  @Override
250  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
251  ArrayList<Object> list = new ArrayList<>();
252  if(null != root) switch (root) {
253  case "challenge":
254  list.addAll(searchChallenges(search, maxResults, kvm));
255  break;
256  case "talent-category":
257  list.addAll(searchCategories(search, maxResults, kvm));
258  break;
259  default:
260  break;
261  }
262  return list;
263  }
264 
265  @Override
266  public Object configureCtrl(Object ctrl, IContact contact) {
267  if(ctrl instanceof VoteItCtrl) {
268  VoteItCtrl ictrl = (VoteItCtrl) ctrl;
269  ictrl.setEntityPath(getPath());
270  return ictrl;
271  } else if(ctrl instanceof StarItCtrl) {
272  StarItCtrl ictrl = (StarItCtrl) ctrl;
273  ictrl.setEntityPath(getPath());
274  return ictrl;
275  } else if(ctrl instanceof CommentItCtrl) {
276  CommentItCtrl ictrl = (CommentItCtrl) ctrl;
277  ictrl.setEntityPath(getPath());
278  return ictrl;
279  } else if(ctrl instanceof AttachCtrl) {
280  AttachCtrl ictrl = (AttachCtrl) ctrl;
281  ictrl.setEntityPath(getPath());
282  ictrl.setPublicOnly(true);
283  return ictrl;
284  } else if(ctrl instanceof FileAttach) {
285  FileAttach ictrl = (FileAttach) ctrl;
286  return ictrl;
287  } else if(ctrl instanceof DescribeItCtrl) {
288  DescribeItCtrl ictrl = (DescribeItCtrl) ctrl;
289  ictrl.setEntityPath(getPath());
290  return ictrl;
291  } else if(ctrl instanceof PollsCtrl) {
292  PollsCtrl ictrl = (PollsCtrl) ctrl;
293  ictrl.setEntityPath(getPath());
294  return ictrl;
295  } else if(ctrl instanceof IEntityCtrl) {
296  IEntityCtrl ictrl = (IEntityCtrl) ctrl;
297  ictrl.setEntityPath(getPath());
298  return ictrl;
299  }
300  return ctrl;
301  }
302 
303  @Override
304  public String getExtendedWebPath() {
305  return null;
306  }
307 
308  @Override
309  public String getUserContext() {
310  return "challenge".equals(getRoot()) ?
311  "/user/" + getLinkRoot() + "s" :
312  "/user/my" + getLinkRoot() + "s";
313  }
314 
315  @Override
316  protected Dao createDao() {
317  return new StudentsPU();
318  }
319 
320  @Override
321  protected boolean itsMine(Object entity) {
322  return (entity instanceof Challenge) || (entity instanceof Response) ||
323  (entity instanceof PracticalWork) || (entity instanceof TalentCategory);
324  }
325 
326  @Override
327  public Collection<String> getAllowedRoots() {
328  return Arrays.asList("challenge", "response", "practicalwork", "talent-category");
329  }
330 
331  @Override
333  return new StudentsEntities(entityPath);
334  }
335 
336  @Override
338  return new StudentsEntities(entity);
339  }
340 
341  @Override
342  protected Path createPath() {
343  return new Path(StudentsPU.getObjectPath(entity));
344  }
345 
346  private List<Object> searchChallenges(String search, int maxResults, KeyValueMap kvm) {
347  WhereClause wc = new WhereClause();
348  wc.addClause("select c from Challenge c");
349  wc.addClause("where c.question like :search");
350  wc.addClause("order by c.question");
351  wc.addNamedValue("search", "%" + search + "%");
352  return getDao().getResultList(wc, maxResults);
353  }
354 
355  private List<Object> searchCategories(String search, int maxResults, KeyValueMap kvm) {
356  WhereClause wc = new WhereClause();
357  wc.addClause("select c from TalentCategory c");
358  wc.addClause("where c.name like :search");
359  wc.addClause("order by c.name");
360  wc.addNamedValue("search", "%" + search + "%");
361  return getDao().getResultList(wc, maxResults);
362  }
363 
364  @Override
365  public String getEntityUrl(LinkType type, String contextPath) {
366  if("response".equals(getRoot())) {
367  return getParent().getEntityUrl(type, contextPath);
368  }
369  return super.getEntityUrl(type, contextPath);
370  }
371 
372 }
void setPublicOnly(boolean publicOnly)
Definition: AttachCtrl.java:77
void setEntityPath(String entityPath)
Definition: AttachCtrl.java:45
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
String getEntityUrl(LinkType type, String contextPath)
Object configureCtrl(Object ctrl, IContact contact)
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
IElephantEntity createInstance(String entityPath)
IElephantEntity createInstance(Object entity)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
boolean canShowInContext(IContact contact, Object relatedEntity)
List< IElephantEntity > getChildren()
boolean canPublish(IContact contact)
static String get(String msg)
Definition: I_.java:41
void setEntityPath(String entityPath)
Definition: PollsCtrl.java:42
static String getObjectPath(Object object)
Definition: StudentsPU.java:59
static String getObjectExtendedPath(Object object)
Definition: StudentsPU.java:72
static void showChallenge(Challenge challenge)
void setEntityPath(String entityPath)
void setEntityPath(String entityPath)
Definition: StarItCtrl.java:46
void setEntityPath(String entityPath)