BrightSide Workbench Full Report + Source Code
BrightSide/contacts/src/main/java/org/turro/contacts/relation/Relations.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.contacts.relation;
20 
21 import java.util.Collections;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.turro.contacts.BusinessRelation;
25 import org.turro.contacts.Contact;
26 import org.turro.plugin.contacts.AbstractRelations;
27 import org.turro.plugin.contacts.ContactRelations;
28 import org.turro.plugin.contacts.IContact;
29 import org.turro.plugin.contacts.IContactRelation;
30 import org.turro.plugin.contacts.IRelations;
31 
36 public class Relations extends AbstractRelations implements IRelations {
37 
38  private final Contact baseContact;
39 
40  /* Common */
41 
42  public static boolean inStaff(IContact company, IContact worker) {
43  return Relations.staff(company).inStaff(worker);
44  }
45 
46  /* As worker */
47 
48  @Override
49  public Set<IContactRelation> getCoworkerRelations() {
50  if(asWorker) {
51  return new TreeSet(getCurrent().stream()
52  .flatMap(r -> Relations.staff(r.getRelatedIContact()).getWorkerRelations().stream())
53  .toList());
54  }
55  return Collections.EMPTY_SET;
56  }
57 
58  /* Populate */
59 
60  @Override
61  protected Relations load() {
62  if(isEmpty()) {
63  if(baseContact != null) {
64  for(BusinessRelation br : asWorker ? baseContact.getBusinessRelations() : baseContact.getWorkerRelations()) {
65  add(new ContactRelationAdapter(asWorker, br));
66  }
67  }
68  if(outer) {
70  if(cr.getStrong() && asWorker == cr.asWorker()) {
71  add(cr);
72  }
73  }
74  }
75  }
76  return this;
77  }
78 
79  /* Factory */
80 
82  return new Relations(contact, true);
83  }
84 
85  public static Relations staff(IContact contact) {
86  return new Relations(contact, false);
87  }
88 
89  private Relations(IContact contact, boolean asWorker) {
90  super(contact, asWorker);
91  this.baseContact = (Contact) contact.getContact();
92  }
93 
94 }
Set< BusinessRelation > getBusinessRelations()
Definition: Contact.java:379
Set< BusinessRelation > getWorkerRelations()
Definition: Contact.java:391
static List< IContactRelation > getRelations(IContact contact, int mode)