BrightSide Workbench Full Report + Source Code
Entities.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.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Set;
26 import org.turro.string.Strings;
27 import org.turro.annotation.ElephantEntity;
28 import org.turro.collections.KeyValueMap;
29 import org.turro.path.Path;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.reflection.Instances;
32 
37 public class Entities {
38 
39  final static private EmptyController empty = new EmptyController();
40 
41  /* Simple objects */
42 
43  public static Object getObject(String path) {
44  if(!Strings.isBlank(path)) {
45  Path entityPath = Path.pathFrom(path);
46  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
47  IElephantEntity ei = iElEntity.getController(path);
48  if(ei != null) {
49  return ei.getEntity();
50  }
51  Object obj = iElEntity.getObject(entityPath);
52  if(obj != null) {
53  return obj;
54  }
55  }
56  }
57  return null;
58  }
59 
60  public static IElephantEntity getMainEntity(Object entity) {
61  if(entity != null) {
62  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
63  IElephantEntity ei = iElEntity.getMain(entity);
64  if(ei != null) {
65  return ei;
66  }
67  }
68  }
69  return null;
70  }
71 
72  /* External Actions */
73 
74  public static IElephantEntity emptyController() {
75  return empty;
76  }
77 
78  public static IElephantEntity getController(String path) {
79  if(!Strings.isBlank(path)) {
80  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
81  IElephantEntity ei = iElEntity.getController(path);
82  if(ei != null) {
83  return ei;
84  }
85  }
86  }
87  return empty;
88  }
89 
90  public static IElephantEntity getController(Object entity) {
91  if(entity instanceof String) {
92  return getController((String) entity);
93  } else if(entity instanceof Path) {
94  return getController(((Path) entity).getPath());
95  } else if(entity != null) {
96  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
97  IElephantEntity ei = iElEntity.getController(entity);
98  if(ei != null) {
99  return ei;
100  }
101  }
102  }
103  return empty;
104  }
105 
106  public static Collection<Object> getEntities(String search, int maxResults) {
107  return getEntities(search, maxResults, null);
108  }
109 
110  public static Collection<Object> getEntities(String root, String search, int maxResults) {
111  return getEntities(root, search, maxResults, null);
112  }
113 
114  public static Collection<Object> getEntities(String search, int maxResults, KeyValueMap kvm) {
115  if(kvm == null) { kvm = new KeyValueMap(new HashMap()); }
116  ArrayList<Object> list = new ArrayList<>();
117  if(!Strings.isBlank(search)) {
118  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
119  list.addAll(iElEntity.getEntitites(search, maxResults, kvm));
120  }
121  }
122  return list;
123  }
124 
125  public static Collection<Object> getEntities(String root, String search, int maxResults, KeyValueMap kvm) {
126  if(kvm == null) { kvm = new KeyValueMap(new HashMap()); }
127  ArrayList<Object> list = new ArrayList<>();
128  if(!Strings.isBlank(search)) {
129  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
130  for(String entityRoot : Strings.csvToList(root)) {
131  list.addAll(iElEntity.getEntitites(entityRoot, search, maxResults, kvm));
132  }
133  }
134  }
135  return list;
136  }
137 
138  /* Roles */
139 
140  public static Object getSingleRelatedByRole(EntityRole role, IContact contact) {
141  Collection<Object> list = Entities.getRelatedByRole(role, contact);
142  if(!list.isEmpty()) {
143  return list.stream().findFirst().get();
144  }
145  return null;
146  }
147 
148  public static Collection<Object> getRelatedByRole(EntityRole role, IContact contact) {
149  ArrayList<Object> list = new ArrayList<>();
150  Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class).forEach((iElEntity) -> {
151  list.addAll(iElEntity.getRelatedByRole(role, contact));
152  });
153  return list;
154  }
155 
156  public static boolean hasRelatedRole(EntityRole role, IContact contact) {
157  return Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)
158  .stream().map((iElEntity) -> iElEntity.hasRelatedRole(role, contact)).anyMatch((has) -> (has));
159  }
160 
161  /* EntityPath utils */
162 
163  public static Set<String> getEntityPaths(Collection entitites) {
164  HashSet<String> set = new HashSet<>();
165  for(Object entity : entitites) {
166  String path = Entities.getController(entity).getPath();
167  if(!Strings.isBlank(path)) {
168  set.add(path);
169  }
170  }
171  return set;
172  }
173 
174  public static Set<String> getEntityPaths(Object[] entitites) {
175  HashSet<String> set = new HashSet<>();
176  for(Object entity : entitites) {
177  String path = Entities.getController(entity).getPath();
178  if(!Strings.isBlank(path)) {
179  set.add(path);
180  }
181  }
182  return set;
183  }
184 
185  public static Set<String> getEntityPaths(Object entity) {
186  HashSet<String> set = new HashSet<>();
187  String path = Entities.getController(entity).getPath();
188  if(!Strings.isBlank(path)) {
189  set.add(path);
190  }
191  return set;
192  }
193 
194  public static Set<String> getAllowedRoots() {
195  HashSet<String> set = new HashSet<>();
196  for(IElephantEntity iElEntity : Instances.cached().byAnnotation(ElephantEntity.class, IElephantEntity.class)) {
197  set.addAll(iElEntity.getAllowedRoots());
198  }
199  return set;
200  }
201 
202  /* Entities */
203 
204  private Entities() {
205  }
206 
207 }
static Set< String > getEntityPaths(Object entity)
Definition: Entities.java:185
static IElephantEntity getController(Object entity)
Definition: Entities.java:90
static IElephantEntity getController(String path)
Definition: Entities.java:78
static IElephantEntity emptyController()
Definition: Entities.java:74
static Object getSingleRelatedByRole(EntityRole role, IContact contact)
Definition: Entities.java:140
static Collection< Object > getEntities(String search, int maxResults)
Definition: Entities.java:106
static boolean hasRelatedRole(EntityRole role, IContact contact)
Definition: Entities.java:156
static Collection< Object > getEntities(String root, String search, int maxResults, KeyValueMap kvm)
Definition: Entities.java:125
static Collection< Object > getEntities(String search, int maxResults, KeyValueMap kvm)
Definition: Entities.java:114
static Set< String > getEntityPaths(Object[] entitites)
Definition: Entities.java:174
static Collection< Object > getRelatedByRole(EntityRole role, IContact contact)
Definition: Entities.java:148
static Set< String > getEntityPaths(Collection entitites)
Definition: Entities.java:163
static Set< String > getAllowedRoots()
Definition: Entities.java:194
static Object getObject(String path)
Definition: Entities.java:43
static Collection< Object > getEntities(String root, String search, int maxResults)
Definition: Entities.java:110
static IElephantEntity getMainEntity(Object entity)
Definition: Entities.java:60
IElephantEntity getController(String entityPath)
IElephantEntity getMain(Object entity)