BrightSide Workbench Full Report + Source Code
AbstractRelations.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.plugin.contacts;
20 
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28 import org.amic.util.date.Dates;
29 
34 public abstract class AbstractRelations extends TreeSet<IContactRelation> implements IRelations {
35 
36  protected final IContact contact;
37 
38  protected Date date;
39  protected boolean outer, asWorker;
40 
41  protected AbstractRelations(IContact contact, boolean asWorker) {
42  this.contact = contact;
43  this.date = new Date();
44  this.asWorker = asWorker;
45  }
46 
47  @Override
48  public IRelations date(Date date) {
49  if(date != null) this.date = date;
50  clear();
51  return this;
52  }
53 
54  @Override
55  public IRelations outer() {
56  outer = true;
57  clear();
58  return this;
59  }
60 
61  @Override
62  public IRelations company() {
63  asWorker = false;
64  clear();
65  return this;
66  }
67 
68  @Override
69  public IRelations staff() {
70  asWorker = true;
71  clear();
72  return this;
73  }
74 
75  /* As worker results */
76 
77  @Override
78  public IContact getCompany() {
80  return cr != null ? cr.getRelatedIContact() : null;
81  }
82 
83  @Override
84  public Optional<IContact> getOptCompany() {
85  return Optional.ofNullable(getCompany());
86  }
87 
88  @Override
90  if(asWorker) {
91  for(IContactRelation cr : getCurrent()) {
92  return cr;
93  }
94  }
95  return null;
96  }
97 
98  @Override
99  public Optional<IContactRelation> getOptCompanyRelation() {
100  return Optional.ofNullable(getCompanyRelation());
101  }
102 
103  @Override
104  public List<IContact> getCompanies() {
105  return getCompanyRelations().stream().map(r -> r.getRelatedIContact()).toList();
106  }
107 
108  @Override
109  public Set<IContactRelation> getCompanyRelations() {
110  if(asWorker) {
111  return getCurrent();
112  }
113  return Collections.EMPTY_SET;
114  }
115 
116  @Override
117  public List<IContact> getCoworkers() {
118  return getCoworkerRelations().stream().map(r -> r.getRelatedIContact()).toList();
119  }
120 
121  @Override
122  public abstract Set<IContactRelation> getCoworkerRelations();
123 
124  /* As company results */
125 
126  @Override
127  public List<IContact> getWorkers() {
128  return getWorkerRelations().stream().map(r -> r.getRelatedIContact()).toList();
129  }
130 
131  @Override
132  public Set<IContactRelation> getWorkerRelations() {
133  if(!asWorker) {
134  return getCurrent();
135  }
136  return Collections.EMPTY_SET;
137  }
138 
139  /* As learning center results */
140 
141  @Override
142  public List<IContact> getDocents() {
143  return getDocentRelations().stream().map(r -> r.getRelatedIContact()).toList();
144  }
145 
146  @Override
147  public List<IContactRelation> getDocentRelations() {
148  if(contact.isCenter()) {
149  return getWorkerRelations().stream().filter(r -> r.isWorker()).toList();
150  }
151  return Collections.EMPTY_LIST;
152  }
153 
154  @Override
155  public List<IContact> getStudents() {
156  return getStudentRelations().stream().map(r -> r.getRelatedIContact()).toList();
157  }
158 
159  @Override
160  public List<IContactRelation> getStudentRelations() {
161  if(contact.isCenter()) {
162  return getWorkerRelations().stream().filter(r -> !r.isWorker()).toList();
163  }
164  return Collections.EMPTY_LIST;
165  }
166 
167  /* Common */
168 
169  @Override
170  public boolean inStaff(IContact worker) {
171  return asWorker ?
172  getCoworkerRelations().stream().anyMatch(r -> r.getRelatedId().equals(worker.getId())) :
173  getWorkerRelations().stream().anyMatch(r -> r.getRelatedId().equals(worker.getId()));
174  }
175 
176  @Override
177  public Set<IContactRelation> getCurrent() {
178  Set<IContactRelation> set = new TreeSet<>();
179  for(IContactRelation cr : load()) {
180  if(Dates.inRange(cr.getStartDate(), cr.getEndDate(), date)) {
181  set.add(cr);
182  }
183  }
184  return set;
185  }
186 
187  @Override
188  public Set<IContactRelation> getOutdated() {
189  SortedSet<IContactRelation> set = new TreeSet<>();
190  for(IContactRelation cr : load()) {
191  if(!Dates.inRange(cr.getStartDate(), cr.getEndDate(), date)) {
192  set.add(cr);
193  }
194  }
195  return set;
196  }
197 
198  /* Populate */
199 
200  protected abstract AbstractRelations load();
201 
202 }
AbstractRelations(IContact contact, boolean asWorker)
abstract Set< IContactRelation > getCoworkerRelations()
Optional< IContactRelation > getOptCompanyRelation()
abstract AbstractRelations load()