BrightSide Workbench Full Report + Source Code
ContactRelationSolver.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.contacts.relation;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.turro.contacts.BusinessRelation;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.plugin.contacts.ElephantRelationSolver;
27 import org.turro.plugin.contacts.IContactRelation;
28 import org.turro.plugin.contacts.IContactRelationSolver;
29 
34 @ElephantRelationSolver
36 
37  @Override
38  public boolean isDirect() {
39  return true;
40  }
41 
42  @Override
43  public List<IContactRelation> getRelations(String idContact) {
44  List<IContactRelation> list = new ArrayList<>();
45  list.addAll(getBusiness(idContact));
46  list.addAll(getWorkers(idContact));
47  return list;
48  }
49 
50  @Override
51  public List<IContactRelation> getBusiness(String idContact) {
52  List<IContactRelation> list = new ArrayList<>();
53  for(BusinessRelation br : getBusinessRelations(idContact)) {
54  if(!br.isEmpty()) list.add(new ContactRelationAdapter(true, br));
55  }
56  return list;
57  }
58 
59  @Override
60  public List<IContactRelation> getWorkers(String idContact) {
61  List<IContactRelation> list = new ArrayList<>();
62  for(BusinessRelation br : getWorkerRelations(idContact)) {
63  if(!br.isEmpty()) list.add(new ContactRelationAdapter(false, br));
64  }
65  return list;
66  }
67 
68  private Iterable<BusinessRelation> getBusinessRelations(String idContact) {
69  return getContact(idContact).getBusinessRelations();
70  }
71 
72  private Iterable<BusinessRelation> getWorkerRelations(String idContact) {
73  return getContact(idContact).getWorkerRelations();
74  }
75 
76  /* Contact */
77 
78  private Contact contact;
79 
80  private Contact getContact(String idContact) {
81  if(contact == null || !idContact.equals(contact.getId())) {
82  contact = new ContactsPU().find(Contact.class, idContact);
83  }
84  return contact;
85  }
86 
87 }
Set< BusinessRelation > getBusinessRelations()
Definition: Contact.java:379
Set< BusinessRelation > getWorkerRelations()
Definition: Contact.java:391
List< IContactRelation > getWorkers(String idContact)
List< IContactRelation > getBusiness(String idContact)
List< IContactRelation > getRelations(String idContact)