BrightSide Workbench Full Report + Source Code
DefaultElephantEntity.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.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Objects;
25 import org.turro.string.Strings;
26 import org.turro.action.IElephantSendable;
27 import org.turro.action.IEntityCtrl;
28 import org.turro.action.Interceptors;
29 import org.turro.action.LinkType;
30 import org.turro.action.Plugins;
31 import org.turro.actor.Actors;
32 import org.turro.auth.Authentication;
33 import org.turro.collections.KeyValueMap;
34 import org.turro.elephant.context.Application;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.context.IConstructor;
37 import org.turro.elephant.direct.AbstractDirectContentCtrl;
38 import org.turro.elephant.security.IUser;
39 import org.turro.file.Folder;
40 import org.turro.jpa.Dao;
41 import org.turro.jpa.entity.EntityWebUrls;
42 import org.turro.marker.MarkerHelper;
43 import org.turro.participation.IParticipationCtrl;
44 import org.turro.participation.ParticipationReason;
45 import org.turro.path.Path;
46 import org.turro.plugin.contacts.IContact;
47 import org.turro.script.Script;
48 import org.turro.script.Scripting;
49 import org.turro.security.ConceptPermission;
50 
55 public abstract class DefaultElephantEntity implements IElephantEntity {
56 
57  protected String entityPath;
58  protected Path path;
59  protected Object entity;
60 
62  }
63 
65  this.entityPath = entityPath;
66  this.path = new Path(entityPath);
67  }
68 
69  public DefaultElephantEntity(Object entity) {
70  this.entity = entity;
71  }
72 
73  @Override
75  String root = new Path(entityPath).getRoot();
76  if(itsMine(root)) {
77  return createInstance(entityPath);
78  }
79  return null;
80  }
81 
82  @Override
84  if(itsMine(entity)) {
85  return createInstance(entity);
86  }
87  return null;
88  }
89 
90  @Override
91  public boolean isEmpty() {
92  return getEntity() == null;
93  }
94 
95  @Override
96  public String getStringId() {
97  getPath();
98  return path != null && (path.getSize() > 1) ? path.getNode(1) : EMPTY_ID;
99  }
100 
101  @Override
102  public String getNameOrLabel() {
103  return Strings.isBlank(getName(), getLabel());
104  }
105 
106  @Override
107  public String getRoot() {
108  getPath();
109  return path != null ? path.getRoot(): null;
110  }
111 
112  @Override
113  public String getPath() {
114  if(path == null) {
115  path = createPath();
116  }
117  return path != null ? path.getPath() : null;
118  }
119 
120  @Override
121  public String getTreeLabel() {
122  if(path != null && path.isRoot()) {
123  return getLabel();
124  } else if(path.getSize() == 2) {
125  return getName();
126  }
127  return null;
128  }
129 
130  @Override
131  public Collection getEntitites(String search, int maxResults) {
132  return getEntitites(search, maxResults, new KeyValueMap(new HashMap()));
133  }
134 
135  @Override
136  public Collection getEntitites(String root, String search, int maxResults) {
137  return getEntitites(root, search, maxResults, new KeyValueMap(new HashMap()));
138  }
139 
140  @Override
142  return null;
143  }
144 
145  @Override
146  public Collection<Object> getRelatedByRole(EntityRole role, IContact contact) {
147  return Collections.EMPTY_LIST;
148  }
149 
150  @Override
151  public boolean hasRelatedRole(EntityRole role, IContact contact) {
152  return false;
153  }
154 
155  @Override
156  public String getLinkRoot() {
157  getPath();
158  return path != null ? path.getRoot() : null;
159  }
160 
161  @Override
162  public String getEntityUrl() {
163  return getEntityUrl(LinkType.WEB, null);
164  }
165 
166  @Override
167  public String getMemberUrl() {
168  return getEntityUrl(LinkType.WEB_INTERNAL, null);
169  }
170 
171  @Override
172  public String getAppUrl() {
173  IUser user = Application.getUser();
174  if(user instanceof IContact) {
175  IContact contact = (IContact) user;
176  if(contact.isWebapp()) {
177  getPath();
178  return "/app/frame?" + Interceptors.parameters(getLinkRoot(), path.getNode(1)).encoded();
179  } else {
180  return getEntityUrl(LinkType.WEB_INTERNAL, null);
181  }
182  }
183  return null;
184  }
185 
186  @Override
187  public String getEntityUrl(LinkType type, String contextPath) {
188  if(LinkType.INTERNAL.equals(type)) {
189  return getAppUrl();
190  } else {
191  String ewu = EntityWebUrls.getUrlFromEntity(getEntity());
192  if(ewu != null) {
193  return getReadAllUrl(type, contextPath) + ewu;
194  } else {
195  return getReadAllUrl(type, contextPath) + "?" + MarkerHelper.setObfuscatedPars("item=" + getStringId());
196  }
197  }
198  }
199 
200  @Override
201  public String getReadAllUrl(LinkType type, String contextPath) {
202  if(LinkType.WEB.equals(type)) {
203  if(Strings.isBlank(contextPath)) {
204  contextPath = ElephantContext.getEntityWebContext("/" + getLinkRoot());
205  }
206  if(Strings.isBlank(contextPath)) {
207  contextPath = getUserContext();
208  }
209  } else if(LinkType.WEB_INTERNAL.equals(type)) {
210  if(Strings.isBlank(contextPath)) {
211  contextPath = getUserContext();
212  }
213  if(Strings.isBlank(contextPath)) {
214  contextPath = ElephantContext.getEntityWebContext("/" + getLinkRoot());
215  }
216  }
217  return Strings.isBlank(contextPath, "/");
218  }
219 
220  protected boolean itsMine(String rootPath) {
221  return getAllowedRoots().contains(rootPath);
222  }
223 
224  protected abstract Dao createDao();
225  protected abstract boolean itsMine(Object entity);
226  protected abstract IElephantEntity createInstance(String entityPath);
227  protected abstract IElephantEntity createInstance(Object entity);
228  protected abstract Path createPath();
229 
230  /* Helpers */
231 
232  protected boolean isValidLongEntity() {
233  getPath();
234  return path.getSize() == 2 && path.getNode(1).matches("[0-9]+");
235  }
236 
237  /* Dao */
238 
239  private Dao _dao;
240 
241  protected Dao getDao() {
242  if(_dao == null) {
243  _dao = createDao();
244  }
245  return _dao;
246  }
247 
248  /* Controls */
249 
250  @Override
251  public Object getControl(String name, IContact contact) {
252  Object control = Controls.getControl(name);
253  return configureCtrl(control, contact);
254  }
255 
256  @Override
257  public Object getControl(String name, IConstructor constructor, IContact contact) {
258  Object control = Controls.getControl(name, constructor);
259  return configureCtrl(control, contact);
260  }
261 
262  @Override
263  public Object getControl(String name, String entityPath, IContact contact) {
264  Object control = Controls.getControl(name, entityPath);
265  return configureCtrl(control, contact);
266  }
267 
268  /* Participation */
269 
270  @Override
271  public Object getParticipationControl(IConstructor constructor, ParticipationReason reason) {
272  return getParticipationControl(constructor, Authentication.getIContact(), reason);
273  }
274 
275  @Override
276  public Object getParticipationControl(IConstructor constructor, IContact contact, ParticipationReason reason) {
277  IParticipationCtrl ctrl = Plugins.loadImplementation(IParticipationCtrl.class, "participation-ctrl");
278  ctrl = (IParticipationCtrl) configureCtrl(ctrl, contact);
279  ((IEntityCtrl) ctrl).setConstructor(constructor);
280  ((IEntityCtrl) ctrl).setEntityPath(getPath());
281  ctrl.setReason(reason);
282  ((IEntityCtrl) ctrl).setTemplate(reason.toString().replaceAll("_", "-").toLowerCase());
283  return ctrl;
284  }
285 
286  @Override
287  public String parseSpamCtrl(IConstructor constructor) {
289  return ctrl != null ? ctrl.parse(constructor) : "";
290  }
291 
292  @Override
293  public String parseFollowCtrl(IConstructor constructor) {
295  return ctrl != null ? ctrl.parse(constructor) : "";
296  }
297 
298  /* Children helpers */
299 
300  @Override
302  EntitySet set = new EntitySet();
303  set.add(this);
304  set.addAll(getChildren());
305  for(IElephantEntity iee : getChildren()) {
306  set.addAll(iee.getAllChildren());
307  }
308  return set;
309  }
310 
311  /* Actors */
312 
313  @Override
314  public Script getActorScript() {
315  return Scripting.instance();
316  }
317 
318  @Override
319  public boolean isActor(String actor) {
320  return Actors.isActorFor(this, actor);
321  }
322 
323  @Override
324  public boolean isActor(IContact contact, String actor) {
325  return Actors.isActorFor(contact, this, actor);
326  }
327 
328  @Override
330  return null;
331  }
332 
333  /* Deliverables */
334 
335  @Override
336  public Folder getDeliverables() {
337  return StandardFolder.internal()
338  .real()
339  .entityPath(getPath())
340  .folder("deliverables")
341  .asFolder();
342  }
343 
344  /* Simple Objects */
345 
346  @Override
347  public Object getObject(Path entityPath) {
348  return null;
349  }
350 
351  @Override
352  public IElephantEntity getMain(Object entity) {
353  return null;
354  }
355 
356  /* equals & hashCode */
357 
358  @Override
359  public int hashCode() {
360  int hash = 7;
361  hash = 97 * hash + Objects.hashCode(this.path);
362  return hash;
363  }
364 
365  @Override
366  public boolean equals(Object obj) {
367  if (this == obj) {
368  return true;
369  }
370  if (obj == null) {
371  return false;
372  }
373  if (getClass() != obj.getClass()) {
374  return false;
375  }
376  final DefaultElephantEntity other = (DefaultElephantEntity) obj;
377  if (!Objects.equals(this.getPath(), other.getPath())) {
378  return false;
379  }
380  return true;
381  }
382 
383 }
static Parameters parameters(String root)
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static boolean isActorFor(Object entity, String actor)
Definition: Actors.java:53
static String getEntityWebContext(String path)
static Object getControl(String name)
Definition: Controls.java:37
Object getParticipationControl(IConstructor constructor, IContact contact, ParticipationReason reason)
Collection getEntitites(String search, int maxResults)
String getReadAllUrl(LinkType type, String contextPath)
IElephantEntity getController(Object entity)
Object getControl(String name, IConstructor constructor, IContact contact)
Collection getEntitites(String root, String search, int maxResults)
Object getControl(String name, IContact contact)
boolean isActor(IContact contact, String actor)
IElephantEntity getController(String entityPath)
Object getControl(String name, String entityPath, IContact contact)
abstract boolean itsMine(Object entity)
String parseFollowCtrl(IConstructor constructor)
String parseSpamCtrl(IConstructor constructor)
boolean hasRelatedRole(EntityRole role, IContact contact)
abstract IElephantEntity createInstance(String entityPath)
Collection< Object > getRelatedByRole(EntityRole role, IContact contact)
Object getParticipationControl(IConstructor constructor, ParticipationReason reason)
String getEntityUrl(LinkType type, String contextPath)
abstract IElephantEntity createInstance(Object entity)
ConceptPermission getConceptPermission(String name)
static Folder internal(String entityPath)
static String getUrlFromEntity(Object entity)
static String setObfuscatedPars(String parameters)
static Script instance()
Definition: Scripting.java:92
Collection< String > getAllowedRoots()
List< IElephantEntity > getChildren()
Object configureCtrl(Object ctrl, IContact contact)
void setReason(ParticipationReason reason)