BrightSide Workbench Full Report + Source Code
Business.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.Date;
22 import java.util.List;
23 import java.util.TreeSet;
24 import java.util.stream.Collectors;
25 import org.amic.util.date.Dates;
26 import org.turro.contacts.BusinessRelation;
27 import org.turro.contacts.Contact;
28 import org.turro.contacts.ContactType;
29 import org.turro.contacts.db.ContactsPU;
30 import org.turro.contacts.util.ContactList;
31 import org.turro.jpa.Dao;
32 import org.turro.plugin.contacts.ContactRelations;
33 import org.turro.plugin.contacts.IContactRelation;
34 
39 public class Business extends TreeSet<BusinessRelation> {
40 
41  private final Contact contact;
42  private final Date now;
43 
44  public Business(Contact contact) {
45  this.contact = contact;
46  this.now = new Date();
47  load();
48  }
49 
50  public Contact getBusiness() {
51  return getBusiness(now);
52  }
53 
54  public Contact getBusiness(Date date) {
55  for(BusinessRelation br : this) {
56  if(br.isPreferential() && Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
57  return br.getBusiness();
58  }
59  }
60  for(IContactRelation cr : ContactRelations.getBusiness(contact.getId(), ContactRelations.MODE_OUTER)) {
61  if(cr.getStrong() && cr.asWorker()) {
62  return getDao().find(Contact.class, cr.getRelatedId());
63  }
64  }
65  for(BusinessRelation br : this) {
66  if(Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
67  return br.getBusiness();
68  }
69  }
70  return null;
71  }
72 
74  return getBusinessRelation(now);
75  }
76 
78  for(BusinessRelation br : this) {
79  if(br.isPreferential() && Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
80  return br;
81  }
82  }
83  for(BusinessRelation br : this) {
84  if(Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
85  return br;
86  }
87  }
88  return null;
89  }
90 
91  public List<Contact> getCompaniesList() {
92  return getBusinessList().stream()
93  .filter(c -> ContactType.CONTACT_COMPANY.equals(c.getType()))
94  .collect(Collectors.toList());
95  }
96 
97  public List<Contact> getCentersList() {
98  return getBusinessList().stream()
99  .filter(c -> ContactType.CONTACT_LEARNINGCENTER.equals(c.getType()))
100  .collect(Collectors.toList());
101  }
102 
103  public List<Contact> getBusinessList() {
104  return getBusinessList(now);
105  }
106 
107  public List<Contact> getBusinessList(Date date) {
108  ContactList cs = new ContactList();
109  for(BusinessRelation br : this) {
110  if(Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
111  cs.add(br.getBusiness());
112  }
113  }
114  return cs;
115  }
116 
117  public List<Contact> getOutdatedList() {
118  return getOutdatedList(now);
119  }
120 
121  public List<Contact> getOutdatedList(Date date) {
122  ContactList cs = new ContactList();
123  for(BusinessRelation br : this) {
124  if(!Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
125  cs.add(br.getBusiness());
126  }
127  }
128  return cs;
129  }
130 
131  public List<Contact> getCoworkers() {
132  return getCoworkers(now);
133  }
134 
135  public List<Contact> getCoworkers(Date date) {
136  ContactList cs = new ContactList();
137  for(BusinessRelation br : this) {
138  Workers workers = new Workers(br.getBusiness());
139  cs.addAll(workers.getWorkers(date));
140  }
141  return cs;
142  }
143 
144  private void load() {
145  addAll(contact.getBusinessRelations());
146  }
147 
148  /* Dao helpers */
149 
150  private Dao _dao;
151 
152  private Dao getDao() {
153  if(_dao == null) {
154  _dao = new ContactsPU();
155  }
156  return _dao;
157  }
158 
159 }
Set< BusinessRelation > getBusinessRelations()
Definition: Contact.java:379
BusinessRelation getBusinessRelation(Date date)
Definition: Business.java:77
List< Contact > getCompaniesList()
Definition: Business.java:91
List< Contact > getOutdatedList(Date date)
Definition: Business.java:121
BusinessRelation getBusinessRelation()
Definition: Business.java:73
List< Contact > getBusinessList(Date date)
Definition: Business.java:107
List< Contact > getCoworkers(Date date)
Definition: Business.java:135
static List< IContactRelation > getBusiness(IContact contact, int mode)