BrightSide Workbench Full Report + Source Code
DossierFunctions.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.script;
20 
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.turro.string.Strings;
25 import org.turro.dossier.db.DossierPU;
26 import org.turro.dossier.dossier.ParticipantSet;
27 import org.turro.dossier.entity.Category;
28 import org.turro.dossier.entity.Dossier;
29 import org.turro.dossier.entity.Issue;
30 import org.turro.dossier.entity.Participant;
31 import org.turro.dossier.entity.ParticipantRole;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.plugin.contacts.IContact;
34 import org.turro.reflection.Beans;
35 import org.turro.reflection.Reflections;
36 
42 public class DossierFunctions implements IScriptingFunction {
43 
44  @Override
45  public void addFunctions(Script script) {
46  script.addVariable("dfunc", new DossierFunctions());
47  }
48 
49  public boolean anyOfCategories(Object entity, String categories) {
50  List<String> catList = Strings.csvToList(categories);
51  if(entity instanceof Issue) {
52  return catList.contains(String.valueOf(((Issue) entity).getDossier().getCategory().getId()));
53  } else if(entity instanceof Dossier) {
54  return catList.contains(String.valueOf(((Dossier) entity).getCategory().getId()));
55  } else if(entity instanceof Category) {
56  return ((Category) entity).getParent() != null && catList.contains(String.valueOf(((Category) entity).getParent().getId()));
57  }
58  return false;
59  }
60 
61  public boolean anyOfPhases(Object entity, String indexes) {
62  List<String> phaseList = Strings.csvToList(indexes);
63  if(entity instanceof Issue) {
64  return phaseList.contains(Integer.toString(((Issue) entity).getDossier().getProject().getPhase()));
65  } else if(entity instanceof Dossier) {
66  return phaseList.contains(Integer.toString(((Dossier) entity).getProject().getPhase()));
67  }
68  return false;
69  }
70 
71  public boolean anyOfPhaseTypes(Object entity, String types) {
72  List<String> typeList = Strings.csvToList(types);
73  if(entity instanceof Issue) {
74  return ((Issue) entity).getDossier().getProject().getPhaseDefinition().anyStringMatch(typeList);
75  } else if(entity instanceof Dossier) {
76  return ((Dossier) entity).getProject().getPhaseDefinition().anyStringMatch(typeList);
77  }
78  return false;
79  }
80 
81  public boolean anyOfRoles(Object dpl, Object user, String roles) {
82  List<String> roleList = Strings.csvToList(roles);
83  return roleList.stream().anyMatch(r -> ((ParticipantSet) dpl).isRole((IContact) user, ParticipantRole.valueOf(r)));
84  }
85 
86  public boolean anyOfExtRoles(Object dpl, Object user, String roles) {
87  List<String> roleList = Strings.csvToList(roles);
88  return roleList.stream().anyMatch(r -> {
89  Method method = Reflections.of(ParticipantSet.class).findMethod(Reflections.toBeanWay("is", r), IContact.class);
90  return (Boolean) Beans.of(dpl).silentInvoke(method, (IContact) user);
91  });
92  }
93 
94  public boolean anyOfDiscriminators(Object dpl, Object user, String discriminators) {
95  List<String> discriminatorList = Strings.csvToList(discriminators);
96  return discriminatorList.stream().anyMatch(d -> ((ParticipantSet) dpl).isParticipantByDiscriminator((IContact) user, d));
97  }
98 
99  public boolean anyOfRoles(Object user, String roles) {
100  return !getParticipationsFrom((IContact) user, roles, null).isEmpty() ||
101  !getCategoryParticipationsFrom((IContact) user, roles, null).isEmpty();
102  }
103 
104  public boolean anyOfCategoryRoles(Object user, String roles) {
105  return !getCategoryParticipationsFrom((IContact) user, roles, null).isEmpty();
106  }
107 
108  public boolean anyOfExtRoles(Object user, String roles) {
109  return !getParticipationsFrom((IContact) user, null, roles).isEmpty() ||
110  !getCategoryParticipationsFrom((IContact) user, null, roles).isEmpty();
111  }
112 
113  public boolean anyParticipation(Object user) {
114  return !getParticipationsFrom((IContact) user, null, null).isEmpty() ||
115  !getCategoryParticipationsFrom((IContact) user, null, null).isEmpty();
116  }
117 
118  public boolean anyCategoryParticipation(Object user) {
119  return !getCategoryParticipationsFrom((IContact) user, null, null).isEmpty();
120  }
121 
122  private List<Participant> getParticipationsFrom(IContact user, String roles, String extroles) {
123  WhereClause wc = new WhereClause();
124  wc.addClause("select p from Participant p");
125  wc.addClause("where p.idContact = :id");
126  wc.addNamedValue("id", user.getId());
127  if(!Strings.isBlank(roles)) {
128  wc.addIn("and", "p.role", Strings.csvToList(roles).stream().map(r -> ParticipantRole.valueOf(r)).collect(Collectors.toList()));
129  }
130  if(!Strings.isBlank(extroles)) {
131  Strings.csvToList(extroles).forEach(r -> {
132  wc.addClause("and p." + r + " = TRUE");
133  });
134  }
135  return new DossierPU().getResultList(wc);
136  }
137 
138  private List<Participant> getCategoryParticipationsFrom(IContact user, String roles, String extroles) {
139  WhereClause wc = new WhereClause();
140  wc.addClause("select p from CategoryParticipant p");
141  wc.addClause("where p.idContact = :id");
142  wc.addNamedValue("id", user.getId());
143  if(!Strings.isBlank(roles)) {
144  wc.addIn("and", "p.role", Strings.csvToList(roles).stream().map(r -> ParticipantRole.valueOf(r)).collect(Collectors.toList()));
145  }
146  if(!Strings.isBlank(extroles)) {
147  Strings.csvToList(extroles).forEach(r -> {
148  wc.addClause("and p." + r + " = TRUE");
149  });
150  }
151  return new DossierPU().getResultList(wc);
152  }
153 
154 }
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
boolean anyOfDiscriminators(Object dpl, Object user, String discriminators)
boolean anyOfCategoryRoles(Object user, String roles)
boolean anyOfExtRoles(Object user, String roles)
boolean anyOfPhaseTypes(Object entity, String types)
boolean anyOfRoles(Object user, String roles)
boolean anyOfRoles(Object dpl, Object user, String roles)
boolean anyOfCategories(Object entity, String categories)
boolean anyCategoryParticipation(Object user)
boolean anyOfPhases(Object entity, String indexes)
boolean anyOfExtRoles(Object dpl, Object user, String roles)