BrightSide Workbench Full Report + Source Code
ContactRelations.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.plugin.contacts;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.turro.reflection.Instances;
24 import org.turro.util.CompareUtil;
25 
30 public class ContactRelations {
31 
32  public static final int MODE_ALL = 0, MODE_DIRECT = 1, MODE_OUTER = 2;
33 
34  public static List<IContactRelation> getBusiness(IContact contact, int mode) {
35  return getBusiness(contact.getId(), mode);
36  }
37 
38  public static List<IContactRelation> getBusiness(String idContact, int mode) {
39  List<IContactRelation> list = new ArrayList<>();
40  for(IContactRelationSolver process : Instances.cached().byAnnotation(ElephantRelationSolver.class, IContactRelationSolver.class)) {
41  if(checkMode(mode, process)) list.addAll(process.getBusiness(idContact));
42  }
43  return list;
44  }
45 
46  public static List<IContactRelation> getWorkers(IContact contact, int mode) {
47  return getWorkers(contact.getId(), mode);
48  }
49 
50  public static List<IContactRelation> getWorkers(String idContact, int mode) {
51  List<IContactRelation> list = new ArrayList<>();
52  for(IContactRelationSolver process : Instances.cached().byAnnotation(ElephantRelationSolver.class, IContactRelationSolver.class)) {
53  if(checkMode(mode, process)) list.addAll(process.getWorkers(idContact));
54  }
55  return list;
56  }
57 
58  public static List<IContactRelation> getRelations(IContact contact, int mode) {
59  return getRelations(contact.getId(), mode);
60  }
61 
62  public static List<IContactRelation> getRelations(String idContact, int mode) {
63  List<IContactRelation> list = new ArrayList<>();
64  for(IContactRelationSolver process : Instances.cached().byAnnotation(ElephantRelationSolver.class, IContactRelationSolver.class)) {
65  if(checkMode(mode, process)) list.addAll(process.getRelations(idContact));
66  }
67  return list;
68  }
69 
70  public static boolean checkBusiness(String businessId, String workerId) {
71  return CompareUtil.compare(businessId, workerId) != 0 &&
72  getWorkers(businessId, MODE_ALL).stream().noneMatch(b -> b.getRelatedId().equals(workerId));
73  }
74 
75  private static boolean checkMode(int mode, IContactRelationSolver process) {
76  if(mode == MODE_ALL) return true;
77  if(mode == MODE_DIRECT && process.isDirect()) return true;
78  if(mode == MODE_OUTER && !process.isDirect()) return true;
79  return false;
80  }
81 
82  private ContactRelations() {
83  }
84 
85 }
static List< IContactRelation > getBusiness(String idContact, int mode)
static List< IContactRelation > getRelations(String idContact, int mode)
static List< IContactRelation > getRelations(IContact contact, int mode)
static boolean checkBusiness(String businessId, String workerId)
static List< IContactRelation > getWorkers(IContact contact, int mode)
static List< IContactRelation > getWorkers(String idContact, int mode)
static List< IContactRelation > getBusiness(IContact contact, int mode)