BrightSide Workbench Full Report + Source Code
ElephantEntities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.text.DateFormat;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import org.amic.util.date.DateFormats;
29 import org.turro.action.LinkType;
30 import org.turro.string.Strings;
31 import org.turro.annotation.ElephantEntity;
32 import org.turro.collections.KeyValueMap;
33 import org.turro.elephant.db.ElephantPU;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.elephant.entities.db.EntityParticipation;
36 import org.turro.elephant.entities.db.EntityParticipationPK;
37 import org.turro.elephant.entities.db.Poll;
38 import org.turro.elephant.entities.db.Post;
39 import org.turro.elephant.entities.db.Sendable;
40 import org.turro.elephant.entities.db.Topic;
41 import org.turro.i18n.I_;
42 import org.turro.jpa.Dao;
43 import org.turro.path.Path;
44 import org.turro.plugin.contacts.IContact;
45 
50 @ElephantEntity
52 
53  public ElephantEntities() {
54  }
55 
56  public ElephantEntities(String entityPath) {
57  super(entityPath);
58  }
59 
60  public ElephantEntities(Object entity) {
61  super(entity);
62  }
63 
64  @Override
65  public Object getEntity() {
66  if(entity == null) {
67  if(getPath() != null) {
68  if(path.getSize() == 2) {
69  if(null != path.getRoot()) switch (path.getRoot()) {
70  case "sendable":
71  if(isValidLongEntity()) {
72  entity = getDao().find(Sendable.class, path.getFrom(1));
73  }
74  break;
75  case "poll":
76  if(isValidLongEntity()) {
77  entity = getDao().find(Poll.class, path.getNodeAs(1, Long.class));
78  }
79  break;
80  case "topic":
81  if(isValidLongEntity()) {
82  entity = getDao().find(Topic.class, path.getNodeAs(1, Long.class));
83  }
84  break;
85  case "post":
86  if(isValidLongEntity()) {
87  entity = getDao().find(Post.class, path.getNodeAs(1, Long.class));
88  }
89  break;
90  default:
91  break;
92  }
93  }
94  }
95  }
96  return entity;
97  }
98 
99  @Override
100  public String getName() {
101  if(getEntity() != null) {
102  if(entity instanceof Sendable) {
103  return DateFormats.format(
104  ((Sendable) entity).getSchedule(),
105  DateFormat.SHORT, DateFormat.SHORT,
106  I_.api().used());
107  } else if(entity instanceof Poll) {
108  return ((Poll) entity).getTitle();
109  } else if(entity instanceof Topic) {
110  return ((Topic) entity).getText();
111  } else if(entity instanceof Post) {
112  return ((Post) entity).getText();
113  }
114  }
115  return null;
116  }
117 
118  @Override
119  public String getLabel() {
120  if(getPath() != null) {
121  String root = path.getRoot();
122  if(null != root) switch (root) {
123  case "sendable":
124  return I_.get("Sendable");
125  case "poll":
126  return I_.get("Poll");
127  case "topic":
128  return I_.get("Topic");
129  case "post":
130  return I_.get("Post");
131  default:
132  break;
133  }
134  }
135  return null;
136  }
137 
138  @Override
139  public String getDescription() {
140  return null;
141  }
142 
143  @Override
144  public Object getLabelCtrl() {
145  return null;
146  }
147 
148  @Override
149  public String getImage() {
150  if(getPath() != null) {
151  String root = path.getRoot();
152  if(null != root) switch (root) {
153  case "sendable":
154  return "/_zul/images/mail_queue.png";
155  case "poll":
156  return "/_zul/images/poll.png";
157  case "topic":
158  return "/_zul/images/topic.png";
159  case "post":
160  return "/_zul/images/topic.png";
161  default:
162  break;
163  }
164  }
165  return null;
166  }
167 
168  @Override
169  public String getHierarchicalPath() {
170  if(getEntity() != null) {
171  if(entity instanceof Poll) {
172  IElephantEntity iee = ((Poll) entity).getEntity();
173  String hp = iee.getHierarchicalPath();
174  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
175  } else if(entity instanceof Topic) {
177  String hp = iee.getHierarchicalPath();
178  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
179  } else if(entity instanceof Post) {
180  IElephantEntity iee = ((Post) entity).getTopic().getEntity();
181  String hp = iee.getHierarchicalPath();
182  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
183  }
184  }
185  return getPath();
186  }
187 
188  @Override
190  if(getEntity() != null) {
191  if(entity instanceof Sendable) {
192  return (IElephantEntity) ((Sendable) entity).getEntity();
193  } else if(entity instanceof Poll) {
194  return ((Poll) entity).getEntity();
195  } else if(entity instanceof Topic) {
196  return ((Topic) entity).getEntity();
197  } else if(entity instanceof Post) {
198  return Entities.getController(((Post) entity).getParentTopic());
199  }
200  }
201  return null;
202  }
203 
204  @Override
205  public List<IElephantEntity> getChildren() {
206  if(getEntity() != null) {
207  if(entity instanceof Topic) {
208  return new EntityListAdapter(((Topic) entity).getPosts());
209  } else if(entity instanceof Post) {
210  return new EntityListAdapter(((Post) entity).getPosts());
211  }
212  }
213  return Collections.EMPTY_LIST;
214  }
215 
216  @Override
217  public String getExtendedPath() {
218  return null;
219  }
220 
221  @Override
222  public File getFile() {
223  return null;
224  }
225 
226  @Override
227  public boolean showPath() {
228  return false;
229  }
230 
231  @Override
232  public boolean showEntity() {
233  return false;
234  }
235 
236  @Override
237  public boolean canEdit(IContact contact) {
238  return false;
239  }
240 
241  @Override
242  public boolean canPublish(IContact contact) {
243  if(getEntity() != null) {
244  if(entity instanceof Poll) {
245  return ((Poll) entity).isParticipant(contact);
246  }
247  }
248  return false;
249  }
250 
251  @Override
252  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
253  ArrayList<Object> list = new ArrayList<>();
254  list.addAll(searchPolls(search, maxResults, kvm));
255  return list;
256  }
257 
258  @Override
259  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
260  ArrayList<Object> list = new ArrayList<>();
261  if(null != root) switch (root) {
262  case "poll":
263  list.addAll(searchPolls(search, maxResults, kvm));
264  break;
265  default:
266  break;
267  }
268  return list;
269  }
270 
271  @Override
272  public Object configureCtrl(Object ctrl, IContact contact) {
273  return ctrl;
274  }
275 
276  @Override
277  public boolean canShowInContext(IContact contact, Object relatedEntity) {
278  return true;
279  }
280 
281  @Override
282  public String getExtendedWebPath() {
283  if(getEntity() != null) {
284  if(entity instanceof Poll) {
285  Poll poll = (Poll) entity;
286  return "/" + poll.getId() +
287  "/" + Strings.unpunctuate(poll.getTitle()).toLowerCase();
288  }
289  }
290  return null;
291  }
292 
293  @Override
294  public String getUserContext() {
295  getPath();
296  if(path.getRoot().equals("poll")) {
297  return "/user/mypolls";
298  }
299  if(path.getRoot().equals("topic")) {
300  return "/user/forums";
301  }
302  return null;
303  }
304 
305  @Override
306  protected Dao createDao() {
307  return new ElephantPU();
308  }
309 
310  @Override
311  protected boolean itsMine(Object entity) {
312  return (entity instanceof Sendable) || (entity instanceof Poll) ||
313  (entity instanceof Topic) || (entity instanceof Post);
314  }
315 
316  @Override
317  public Collection<String> getAllowedRoots() {
318  return Arrays.asList("sendable", "poll", "topic", "post");
319  }
320 
321  @Override
323  return new ElephantEntities(entityPath);
324  }
325 
326  @Override
328  return new ElephantEntities(entity);
329  }
330 
331  @Override
332  protected Path createPath() {
333  return new Path(ElephantPU.getObjectPath(entity));
334  }
335 
336  @Override
337  public Object getObject(Path entityPath) {
338  if("entity-participation".equals(entityPath.getRoot())) {
339  return getDao().find(EntityParticipation.class, EntityParticipationPK.from(entityPath.getComplex(1)));
340  }
341  return null;
342  }
343 
344  private List<Object> searchPolls(String search, int maxResults, KeyValueMap kvm) {
345  WhereClause wc = new WhereClause();
346  wc.addClause("select p from Poll p");
347  wc.addClause("where p.title like :search");
348  wc.addClause("order by p.title");
349  wc.addNamedValue("search", "%" + search + "%");
350  return getDao().getResultList(wc, maxResults);
351  }
352 
353  @Override
354  public String getEntityUrl(LinkType type, String contextPath) {
355  if("post".equals(getRoot())) {
356  return getParent().getEntityUrl(type, contextPath);
357  }
358  return super.getEntityUrl(type, contextPath);
359  }
360 
361 }
static String getObjectPath(Object object)
Definition: ElephantPU.java:63
void addNamedValue(String name, Object value)
static final String format(Date d, boolean dateOnly)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
IElephantEntity createInstance(Object entity)
boolean canShowInContext(IContact contact, Object relatedEntity)
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
Object configureCtrl(Object ctrl, IContact contact)
List< IElephantEntity > getChildren()
IElephantEntity createInstance(String entityPath)
boolean canPublish(IContact contact)
String getEntityUrl(LinkType type, String contextPath)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65