BrightSide Workbench Full Report + Source Code
elephant-dossier/src/main/java/org/turro/dossier/dossier/ParticipantSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.dossier.dossier;
19 
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import java.util.function.Predicate;
25 import org.turro.action.Contacts;
26 import org.turro.string.Strings;
27 import org.turro.auth.Authentication;
28 import org.turro.dossier.entity.CategoryParticipant;
29 import org.turro.dossier.entity.IDossierParticipant;
30 import org.turro.dossier.entity.IssueParticipant;
31 import org.turro.dossier.entity.IssueParticipantRole;
32 import org.turro.dossier.entity.Participant;
33 import org.turro.dossier.entity.ParticipantRole;
34 import org.turro.plugin.contacts.ContactList;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.visual.RoleUtil;
37 
42 public class ParticipantSet<P> extends TreeSet<P> {
43 
44  public ParticipantSet(Collection<? extends P> collection) {
45  super(new ParticipantComparator());
46  addAllExceptEmpties(collection);
47  }
48 
49  public ParticipantSet() {
50  super(new ParticipantComparator());
51  }
52 
53  @Override
54  public boolean addAll(Collection<? extends P> collection) {
55  addAllExceptEmpties(collection);
56  return true;
57  }
58 
59  public boolean isParticipant() {
60  return isParticipant(getContact(null));
61  }
62 
63  public boolean isParticipant(IContact contact) {
64  return getActiveParticipation(contact) != null;
65  }
66 
67  public boolean isIssueParticipant(IContact contact) {
68  return getActiveParticipation(contact) instanceof IssueParticipant;
69  }
70 
71  public boolean isIssueActiveParticipant(IContact contact) {
72  Object obj = getActiveParticipation(contact);
73  if(obj instanceof IssueParticipant) {
75  return !IssueParticipantRole.ISSUE_ASSISTANT.equals(ip.getRole());
76  }
77  return false;
78  }
79 
80  public boolean isDossierParticipant(IContact contact) {
81  return getActiveParticipation(contact) instanceof Participant;
82  }
83 
84  public boolean isCategoryParticipant(IContact contact) {
85  return getActiveParticipation(contact) instanceof CategoryParticipant;
86  }
87 
88  public boolean isReporter(IContact contact) {
89  return participatesAs(contact, IssueParticipantRole.ISSUE_REPORTER) != null;
90  }
91 
92  public boolean isResponsible(IContact contact) {
93  return participatesAs(contact, IssueParticipantRole.ISSUE_RESPONSIBLE) != null;
94  }
95 
96  public boolean isQA(IContact contact) {
97  return participatesAs(contact, IssueParticipantRole.ISSUE_QA) != null;
98  }
99 
100  public boolean isAssistant(IContact contact) {
101  return participatesAs(contact, IssueParticipantRole.ISSUE_ASSISTANT) != null;
102  }
103 
104  public boolean isOwner(IContact contact) {
105  return participatesAs(contact, ParticipantRole.PARTICIPANT_OWNER) != null;
106  }
107 
108  public boolean isSubject(IContact contact) {
109  return participatesAs(contact, ParticipantRole.PARTICIPANT_SUBJECT) != null;
110  }
111 
112  public boolean isShowAllAttachments(IContact contact) {
113  return getIShowAllAttachments().contains(getContact(contact));
114  }
115 
116  public boolean isShowAllIssues(IContact contact) {
117  return getIShowAllIssues().contains(getContact(contact));
118  }
119 
120  public boolean isShowParticipants(IContact contact) {
121  return getIShowParticipants().contains(getContact(contact));
122  }
123 
124  public boolean isReceiveAllEmails(IContact contact) {
125  return getIReceiveAllEmails().contains(getContact(contact));
126  }
127 
128  public boolean isBindingVote(IContact contact) {
129  return getIBidingVote().contains(getContact(contact));
130  }
131 
132  public boolean isDriver() {
133  return isDriver(null);
134  }
135 
136  public boolean isDriver(IContact contact) {
137  return getIDrivers().contains(getContact(contact));
138  }
139 
140  public boolean isOfferer() {
141  return isOfferer(null);
142  }
143 
144  public boolean isOfferer(IContact contact) {
145  return getIOfferers().contains(getContact(contact));
146  }
147 
148  public boolean isBeneficiary() {
149  return isBeneficiary(null);
150  }
151 
152  public boolean isBeneficiary(IContact contact) {
153  return getIBeneficiaries().contains(getContact(contact));
154  }
155 
156  public boolean isCoordinator() {
157  return isCoordinator(null);
158  }
159 
160  public boolean isCoordinator(IContact contact) {
161  return getICoordinators().contains(getContact(contact));
162  }
163 
164  public boolean isSupport() {
165  return isSupport(null);
166  }
167 
168  public boolean isSupport(IContact contact) {
169  return getISupport().contains(getContact(contact));
170  }
171 
172  public boolean isFunding() {
173  return isFunding(null);
174  }
175 
176  public boolean isFunding(IContact contact) {
177  return getIFunding().contains(getContact(contact));
178  }
179 
180  public boolean isResearch() {
181  return isResearch(null);
182  }
183 
184  public boolean isResearch(IContact contact) {
185  return getIResearch().contains(getContact(contact));
186  }
187 
188  public boolean isConsortium() {
189  return isConsortium(null);
190  }
191 
192  public boolean isConsortium(IContact contact) {
193  return getIConsortium().contains(getContact(contact));
194  }
195 
196  public boolean isAdmin() {
197  return isAdmin(null);
198  }
199 
200  public boolean isAdmin(IContact contact) {
201  return getIAdmins().contains(getContact(contact));
202  }
203 
204  public Object participatesAs(IContact contact, Object participation) {
205  for(Object obj : this) {
206  if(obj instanceof IDossierParticipant) {
208  if(dp.getRole().equals(participation) && dp.getIdContact().equals(getContact(contact).getId())) {
209  return dp;
210  }
211  } else if(obj instanceof IssueParticipant) {
213  if(ip.getRole().equals(participation) && ip.getIdContact().equals(getContact(contact).getId())) {
214  return ip;
215  }
216  }
217  }
218  return null;
219  }
220 
221  public Object getActiveParticipation(IContact contact) {
222  for(Object obj : this) {
223  if(obj instanceof IDossierParticipant) {
225  if(!dp.getRole().equals(ParticipantRole.PARTICIPANT_SUBJECT) && dp.getIdContact().equals(getContact(contact).getId())) {
226  return dp;
227  }
228  } else if(obj instanceof IssueParticipant) {
230  if(ip.getIdContact().equals(getContact(contact).getId())) {
231  return ip;
232  }
233  }
234  }
235  return null;
236  }
237 
238  public Object getParticipation(IContact contact) {
239  for(Object obj : this) {
240  if(obj instanceof IDossierParticipant) {
242  if(dp.getIdContact().equals(getContact(contact).getId())) {
243  return dp;
244  }
245  } else if(obj instanceof IssueParticipant) {
247  if(ip.getIdContact().equals(getContact(contact).getId())) {
248  return ip;
249  }
250  }
251  }
252  return null;
253  }
254 
256  for(Object obj : this) {
257  if(obj instanceof IDossierParticipant) {
259  if(!dp.getRole().equals(ParticipantRole.PARTICIPANT_SUBJECT) && dp.getIdContact().equals(getContact(contact).getId())) {
260  return dp;
261  }
262  }
263  }
264  return null;
265  }
266 
267  public boolean canShowAllAttachments(IContact contact) {
268  if(isIssueParticipant(contact)) {
269  return true;
270  } else {
272  if(dp != null) {
273  return dp.isShowAllAttachments();
274  }
275  }
276  return false;
277  }
278 
279  public boolean canShowIssue(IContact contact) {
280  if(isIssueParticipant(contact)) {
281  return true;
282  } else {
284  if(dp != null) {
285  return dp.isShowAllIssues();
286  }
287  }
288  return false;
289  }
290 
291  public boolean canShowParticipants(IContact contact) {
293  return isIssueActiveParticipant(contact) || (dp != null && dp.isShowParticipants());
294  }
295 
296  /* Checks only in dossiers and categories */
297 
298  public String getDiscriminator(IContact contact) {
300  return dp != null ? dp.getDiscriminator() : null;
301  }
302 
303  public boolean isParticipantByDiscriminator(IContact contact, String discriminator) {
304  for(Object obj : this) {
305  if(obj instanceof IDossierParticipant) {
307  if(dp != null && !Strings.isBlank(discriminator) && discriminator.equalsIgnoreCase(dp.getDiscriminator())
308  && dp.getIdContact().equals(contact.getId())) {
309  return true;
310  }
311  }
312  }
313  return false;
314  }
315 
316  public Collection<String> getDiscriminators() {
317  Set<String> discriminators = new TreeSet<>();
318  for(Object obj : this) {
319  if(obj instanceof IDossierParticipant) {
321  discriminators.add(dp.getDiscriminator() == null ? "" : dp.getDiscriminator());
322  }
323 
324  }
325  return discriminators;
326  }
327 
328  public Collection<IDossierParticipant> getParticipantsByDiscriminator(String discriminator) {
329  Set<IDossierParticipant> participants = new ParticipantSet<>();
330  for(Object obj : this) {
331  if(obj instanceof IDossierParticipant) {
333  if((Strings.isBlank(discriminator) && Strings.isBlank(dp.getDiscriminator())) ||
334  discriminator.equals(dp.getDiscriminator())) {
335  participants.add(dp);
336  }
337  }
338  }
339  return participants;
340  }
341 
342  public boolean isVoteParticipant(IContact contact) {
344  return dp != null && dp.isBindingVote();
345  }
346 
347  public Collection<IContact> getVoteIParticipants() {
348  ContactList list = new ContactList();
349  for(Object obj : this) {
350  if(obj instanceof IDossierParticipant) {
352  if(dp.isBindingVote()) {
353  list.add(dp.getIContact());
354  }
355  }
356  }
357  return list;
358  }
359 
360  public Collection<IssueParticipant> getReporters() {
361  Set<IssueParticipant> participants = new ParticipantSet<>();
362  for(Object obj : this) {
363  if(obj instanceof IssueParticipant) {
365  if(dp.getRole().equals(IssueParticipantRole.ISSUE_REPORTER)) {
366  participants.add(dp);
367  }
368  }
369  }
370  return participants;
371  }
372 
373  public Collection<IssueParticipant> getResponsibles() {
374  Set<IssueParticipant> participants = new ParticipantSet<>();
375  for(Object obj : this) {
376  if(obj instanceof IssueParticipant) {
379  participants.add(dp);
380  }
381  }
382  }
383  return participants;
384  }
385 
386  public HashSet<String> getParticipationStrings(IContact contact) {
387  HashSet<String> set = new HashSet<>();
388  if(Contacts.isValid(contact)) {
389  for(Object obj : this) {
390  if(obj instanceof IDossierParticipant) {
392  if(dp.getIdContact().equals(contact.getId())) {
393  set.addAll(RoleUtil.getStringRoles(dp));
394  }
395  } else if(obj instanceof IssueParticipant) {
397  if(ip.getIdContact().equals(contact.getId())) {
398  set.addAll(RoleUtil.getStringRoles(ip));
399  }
400  }
401  }
402  }
403  return set;
404  }
405 
406  /* Participation helpers */
407 
408  public Collection<IDossierParticipant> getOwners() {
409  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) ->
411  }
412 
413  public Collection<IDossierParticipant> getAssistants() {
414  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) ->
416  }
417 
418  public Collection<IDossierParticipant> getSubjects() {
419  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) ->
421  }
422 
423  public Collection<IDossierParticipant> getShowAllAttachments() {
424  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isShowAllAttachments());
425  }
426 
427  public Collection<IDossierParticipant> getShowAllIssues() {
428  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isShowAllIssues());
429  }
430 
431  public Collection<IDossierParticipant> getShowParticipants() {
432  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isShowParticipants());
433  }
434 
435  public Collection<IDossierParticipant> getReceiveAllEmails() {
436  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isReceiveAllEmails());
437  }
438 
439  public Collection<IDossierParticipant> getBidingVote() {
440  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isBindingVote());
441  }
442 
443  public Collection<IDossierParticipant> getDrivers() {
444  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isDriver());
445  }
446 
447  public Collection<IDossierParticipant> getOfferers() {
448  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isOfferer());
449  }
450 
451  public Collection<IDossierParticipant> getBeneficiaries() {
452  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isBeneficiary());
453  }
454 
455  public Collection<IDossierParticipant> getCoordinators() {
456  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isCoordinator());
457  }
458 
459  public Collection<IDossierParticipant> getSupport() {
460  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isSupport());
461  }
462 
463  public Collection<IDossierParticipant> getFunding() {
464  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isFunding());
465  }
466 
467  public Collection<IDossierParticipant> getResearch() {
468  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isResearch());
469  }
470 
471  public Collection<IDossierParticipant> getConsortium() {
472  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isConsortium());
473  }
474 
475  public Collection<IDossierParticipant> getAdmins() {
476  return getByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isAdmin());
477  }
478 
479  public Collection<IDossierParticipant> getByFunction(Predicate whatIs) {
480  Set<IDossierParticipant> set = new ParticipantSet<>();
481  this.stream().filter((dp) -> ((dp instanceof IDossierParticipant) && whatIs.test(dp))).forEachOrdered((dp) -> {
482  set.add((IDossierParticipant) dp);
483  });
484  return set;
485  }
486 
487  /* IContact helpers */
488 
489  public boolean isRole(IContact contact, ParticipantRole role) {
490  for(Object obj : this) {
491  if(obj instanceof IDossierParticipant) {
493  if(dp.getIdContact().equals(contact.getId()) && dp.getRole().equals(role)) {
494  return true;
495  }
496  }
497  }
498  return false;
499  }
500 
501  public Collection<IContact> getIParticipants() {
502  ContactList list = new ContactList();
503  for(Object obj : this) {
504  if(obj instanceof IDossierParticipant) {
506  list.add(dp.getIContact());
507  } else if(obj instanceof IssueParticipant) {
509  list.add(ip.getIContact());
510  }
511  }
512  return list;
513  }
514 
515  public Collection<IContact> getIShowAllAttachments() {
516  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isShowAllAttachments());
517  }
518 
519  public Collection<IContact> getIShowAllIssues() {
520  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isShowAllIssues());
521  }
522 
523  public Collection<IContact> getIShowParticipants() {
524  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isShowParticipants());
525  }
526 
527  public Collection<IContact> getIReceiveAllEmails() {
528  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isReceiveAllEmails());
529  }
530 
531  public Collection<IContact> getIBidingVote() {
532  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isBindingVote());
533  }
534 
535  public Collection<IContact> getIDrivers() {
536  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isDriver());
537  }
538 
539  public Collection<IContact> getIOfferers() {
540  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isOfferer());
541  }
542 
543  public Collection<IContact> getIBeneficiaries() {
544  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isBeneficiary());
545  }
546 
547  public Collection<IContact> getICoordinators() {
548  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isCoordinator());
549  }
550 
551  public Collection<IContact> getISupport() {
552  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isSupport());
553  }
554 
555  public Collection<IContact> getIFunding() {
556  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isFunding());
557  }
558 
559  public Collection<IContact> getIResearch() {
560  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isResearch());
561  }
562 
563  public Collection<IContact> getIConsortium() {
564  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isConsortium());
565  }
566 
567  public Collection<IContact> getIAdmins() {
568  return getIContactsByFunction((Predicate<IDossierParticipant>) (IDossierParticipant t) -> t.isAdmin());
569  }
570 
571  public Collection<IContact> getIContactsByFunction(Predicate whatIs) {
572  ContactList list = new ContactList();
573  this.stream().filter((obj) -> (obj instanceof IDossierParticipant))
574  .map((obj) -> (IDossierParticipant) obj).filter((p) -> (whatIs.test(p))).forEachOrdered((p) -> {
575  list.add(p.getIContact());
576  });
577  return list;
578  }
579 
580  /* Empties */
581 
582  private void addAllExceptEmpties(Collection<? extends P> collection) {
583  for(P obj: collection) {
584  if(!isEmpty(obj)) add(obj);
585  }
586  }
587 
588  private boolean isEmpty(Object obj) {
589  if(obj instanceof Participant) {
590  return ((Participant) obj).isEmpty();
591  } else if(obj instanceof CategoryParticipant) {
592  return ((CategoryParticipant) obj).isEmpty();
593  } else if(obj instanceof IssueParticipant) {
594  return ((IssueParticipant) obj).isEmpty();
595  }
596  return true;
597  }
598 
599  /* Authenticated */
600 
601  private IContact _contact;
602 
603  private IContact getContact(IContact contact) {
604  if(contact != null) {
605  _contact = contact;
606  }
607  if(_contact == null) {
608  _contact = Authentication.getIContact();
609  }
610  return _contact;
611  }
612 
613 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static HashSet< String > getStringRoles(IDossierParticipant dp)
Definition: RoleUtil.java:35