BrightSide Workbench Full Report + Source Code
VisualElement.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.visual;
20 
21 import java.util.HashSet;
22 import java.util.Objects;
23 import org.turro.string.Strings;
24 import org.turro.i18n.I_;
25 import org.turro.plugin.contacts.IContact;
26 import org.turro.script.Script;
27 import org.turro.script.Scripting;
28 import org.turro.util.CompareUtil;
29 
34 public abstract class VisualElement<F extends VisualElements, V extends VisualElement, E> implements Comparable<VisualElement> {
35 
36  private String name, label, role, eval;
37  private int order;
38  private HashSet<String> syndications, groupings,
39  businessSyndications, businessGroupings;
40  private Boolean visitor, admin, worker, student, hhrr;
41  private F subElements;
42 
43  private final transient F factory;
44  private transient V parent;
45 
46  private transient Script script;
47 
48  public VisualElement(String name, F factory) {
49  this.name = name;
50  this.factory = factory;
51  }
52 
53  public V getParent() {
54  return parent;
55  }
56 
57  public void setParent(V parent) {
58  this.parent = parent;
59  }
60 
61  public boolean check(E entity, IContact contact) {
63  return isNullOrFalse(visitor) ? true : checkContact(contact); // No constraint
64  }
65  return checkConstraints(entity, contact);
66  }
67 
68  public String getName() {
69  return name;
70  }
71 
72  public String getLabel() {
73  return I_.get(label);
74  }
75 
76  public int getOrder() {
77  return order;
78  }
79 
80  public boolean allows(String name, E entity, IContact contact) {
81  VisualElement dve = getElement(name);
82  return dve == null || dve.check(entity, contact);
83  }
84 
85  public VisualElement getElement(String name) {
86  return getSubElements() == null ? null : getSubElements().getElement(name);
87  }
88 
89  public VisualElement getElement(String name, E entity, IContact contact) {
90  return getSubElements() == null ? null : getSubElements().getElement(name, entity, contact);
91  }
92 
93  public F getSubElements() {
94  return subElements;
95  }
96 
97  public V setName(String name) {
98  this.name = name;
99  return (V) this;
100  }
101 
102  public V setLabel(String label) {
103  this.label = label;
104  return (V) this;
105  }
106 
107  public V setOrder(int order) {
108  this.order = order;
109  return (V) this;
110  }
111 
112  public V setRole(String role) {
113  this.role = role;
114  return (V) this;
115  }
116 
117  public V setAdmin(Boolean admin) {
118  this.admin = admin;
119  return (V) this;
120  }
121 
122  public V setWorker(Boolean worker) {
123  this.worker = worker;
124  return (V) this;
125  }
126 
127  public V setStudent(Boolean student) {
128  this.student = student;
129  return (V) this;
130  }
131 
132  public V setHHRR(Boolean hhrr) {
133  this.hhrr = hhrr;
134  return (V) this;
135  }
136 
137  public V setVisitor(Boolean visitor) {
138  this.visitor = visitor;
139  return (V) this;
140  }
141 
142  public V addSyndication(String syndication) {
143  if(syndications == null) {
144  syndications = new HashSet<>();
145  }
146  syndications.add(syndication);
147  return (V) this;
148  }
149 
150  public V addGrouping(String grouping) {
151  if(groupings == null) {
152  groupings = new HashSet<>();
153  }
154  groupings.add(grouping);
155  return (V) this;
156  }
157 
158  public V addBusinessSyndication(String syndication) {
159  if(businessSyndications == null) {
160  businessSyndications = new HashSet<>();
161  }
162  businessSyndications.add(syndication);
163  return (V) this;
164  }
165 
166  public V addBusinessGrouping(String grouping) {
167  if(businessGroupings == null) {
168  businessGroupings = new HashSet<>();
169  }
170  businessGroupings.add(grouping);
171  return (V) this;
172  }
173 
174  protected V addSubElement(String name) {
175  if(subElements == null) {
176  subElements = (F) factory.createSubElements();
177  }
178  V v = (V) factory.createElement(name);
179  v.setParent(this);
180  return subElements.add(v) ? v : null;
181  }
182 
183  protected boolean checkConstraints(E entity, IContact contact) {
184  if(checkContact(contact)) {
185  if(!Strings.isBlank(role) && !contact.getPermissions().isInRole(role)) {
186  return false;
187  }
188  IContact business = contact.getBusiness();
189  business = (business != null && business.isValid()) ? business : null;
190  return checkAllMatch(entity, contact) &&
191  (checkEval(entity, contact) ||
192  checkSyndications(contact) ||
193  checkGroupings(contact) ||
194  checkBusinessSyndications(business) ||
195  checkBusinessGroupings(business) ||
196  checkRoles(contact) ||
197  checkAnyMatch(entity, contact));
198  }
199  return false;
200  }
201 
202  protected boolean emptyConstraints() {
203  return eval == null && role == null && syndications == null && groupings == null &&
204  businessSyndications == null && businessGroupings == null &&
205  admin == null && worker == null && student == null && hhrr == null;
206  }
207 
208  protected boolean checkEval(E entity, IContact contact) {
209  if(hasScript()) {
210  getScript().addVariable("user", contact);
211  return getScript().evalToBoolean(Scripting.realScript(eval));
212  }
213  return false;
214  }
215 
216  private boolean checkSyndications(IContact contact) {
217  if(syndications != null) {
218  for(String syndication : contact.getSyndications()) {
219  if(syndications.contains(syndication)) {
220  return true;
221  }
222  }
223  }
224  return false;
225  }
226 
227  private boolean checkGroupings(IContact contact) {
228  if(groupings != null) {
229  if(groupings.contains(contact.getGrouping())) {
230  return true;
231  }
232  }
233  return false;
234  }
235 
236  private boolean checkBusinessSyndications(IContact business) {
237  if(business != null && businessSyndications != null) {
238  for(String syndication : business.getSyndications()) {
239  if(businessSyndications.contains(syndication)) {
240  return true;
241  }
242  }
243  }
244  return false;
245  }
246 
247  private boolean checkBusinessGroupings(IContact business) {
248  if(business != null && businessGroupings != null) {
249  if(businessGroupings.contains(business.getGrouping())) {
250  return true;
251  }
252  }
253  return false;
254  }
255 
256  private boolean checkRoles(IContact contact) {
257  return (isNullOrFalse(admin) ? false : contact.isAdmin()) ||
258  (isNullOrFalse(worker) ? false : contact.isWorker()) ||
259  (isNullOrFalse(student) ? false : contact.isStudent()) ||
260  (isNullOrFalse(hhrr) ? false : contact.isHHRR());
261  }
262 
263  public boolean isVisitor(E entity, IContact contact) {
264  return !checkContact(contact) || !checkConstraints(entity, contact);
265  }
266 
267  protected boolean checkContact(IContact contact) {
268  return contact != null && contact.isValid();
269  }
270 
271  protected boolean isNullOrFalse(Boolean value) {
272  return value == null || value.equals(Boolean.FALSE);
273  }
274 
275  protected abstract boolean emptyEntityConstraints();
276  protected abstract boolean checkAllMatch(E entity, IContact contact);
277  protected abstract boolean checkAnyMatch(E entity, IContact contact);
278 
279  /* Scripting */
280 
281  protected boolean hasScript() {
282  return !Strings.isBlank(eval);
283  }
284 
285  protected Script getScript() {
286  if(script == null) {
287  script = Scripting.instance();
288  }
289  return script;
290  }
291 
292  /* Comparable */
293 
294  @Override
295  public int compareTo(VisualElement o) {
296  if(o == null) return 0;
297  int result = CompareUtil.compare(this.order, o.order);
298  if(result == 0) {
299  result = CompareUtil.compare(this.name, o.name);
300  }
301  return result;
302  }
303 
304  @Override
305  public int hashCode() {
306  int hash = 7;
307  hash = 97 * hash + Objects.hashCode(this.name);
308  hash = 97 * hash + this.order;
309  return hash;
310  }
311 
312  @Override
313  public boolean equals(Object obj) {
314  if (this == obj) {
315  return true;
316  }
317  if (obj == null) {
318  return false;
319  }
320  if (getClass() != obj.getClass()) {
321  return false;
322  }
324  if (this.order != other.order) {
325  return false;
326  }
327  if (!Objects.equals(this.name, other.name)) {
328  return false;
329  }
330  return true;
331  }
332 
333 }
static String get(String msg)
Definition: I_.java:41
static String realScript(String script)
Definition: Scripting.java:75
static Script instance()
Definition: Scripting.java:92
abstract boolean checkAllMatch(E entity, IContact contact)
VisualElement getElement(String name)
boolean allows(String name, E entity, IContact contact)
boolean check(E entity, IContact contact)
boolean checkContact(IContact contact)
boolean checkEval(E entity, IContact contact)
abstract boolean checkAnyMatch(E entity, IContact contact)
V addBusinessGrouping(String grouping)
V addSyndication(String syndication)
boolean checkConstraints(E entity, IContact contact)
V addBusinessSyndication(String syndication)
int compareTo(VisualElement o)
abstract boolean emptyEntityConstraints()
VisualElement(String name, F factory)
boolean isNullOrFalse(Boolean value)
VisualElement getElement(String name, E entity, IContact contact)
boolean isVisitor(E entity, IContact contact)