BrightSide Workbench Full Report + Source Code
BusinessRelation.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;
20 
21 import java.util.Date;
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.GeneratedValue;
25 import javax.persistence.Id;
26 import javax.persistence.ManyToOne;
27 import javax.persistence.Temporal;
28 import org.turro.string.Strings;
29 import org.turro.contacts.organigram.RelationType;
30 import org.turro.contacts.relation.FuzzyRelationTypes;
31 import org.turro.contacts.relation.ValidationStatus;
32 import org.turro.elephant.util.Images;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.entity.IDaoEntity;
35 import org.turro.plugin.contacts.PositionDates;
36 import org.turro.util.CompareUtil;
37 import org.turro.util.PhraseBuilder;
38 
43 @Entity
44 @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
45 public class BusinessRelation implements java.io.Serializable, IDaoEntity, Comparable<BusinessRelation> {
46 
47  @Id
48  @GeneratedValue(generator = "hibernate-uuid")
49  @Column(name="IDENTIFIER")
50  private String id;
51 
52  private String description, relationType;
53 
54  private boolean preferential, validated;
55 
56  @ManyToOne
57  private Contact contact;
58 
59  @ManyToOne
60  private Contact business;
61 
62  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
63  private java.util.Date startDate;
64 
65  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
66  private java.util.Date endDate;
67 
68  public String getId() {
69  return id;
70  }
71 
72  public void setId(String id) {
73  this.id = id;
74  }
75 
76  public String getDescription() {
77  return description;
78  }
79 
80  public void setDescription(String description) {
81  this.description = description;
82  }
83 
84  public String getRelationType() {
85  return relationType;
86  }
87 
88  public void setRelationType(String relationType) {
89  this.relationType = relationType;
90  }
91 
92  public boolean isPreferential() {
93  return preferential;
94  }
95 
96  public void setPreferential(boolean preferential) {
97  this.preferential = preferential;
98  }
99 
100  public boolean isValidated() {
101  return validated;
102  }
103 
104  public void setValidated(boolean validated) {
105  this.validated = validated;
106  }
107 
108  public Contact getContact() {
109  return contact;
110  }
111 
112  public void setContact(Contact contact) {
113  this.contact = contact;
114  }
115 
116  public Contact getBusiness() {
117  return business;
118  }
119 
120  public void setBusiness(Contact business) {
121  this.business = business;
122  }
123 
124  public Date getStartDate() {
125  return startDate;
126  }
127 
128  public void setStartDate(Date startDate) {
129  this.startDate = startDate;
130  }
131 
132  public Date getEndDate() {
133  return endDate;
134  }
135 
136  public void setEndDate(Date endDate) {
137  this.endDate = endDate;
138  }
139 
140  /* IDaoEntity */
141 
142  @Override
143  public Object entityId() {
144  return id;
145  }
146 
147  @Override
148  public boolean isEmpty() {
149  return (Strings.isEmpty(description) && Strings.isEmpty(relationType)) ||
150  contact == null || business == null;
151  }
152 
153  /* Helpers */
154 
156  return RelationType.getFromString(relationType);
157  }
158 
159  public boolean isTyped() {
160  return !Strings.isBlank(relationType) && relationType.startsWith("$");
161  }
162 
163  public boolean isResponsible() {
164  return isTyped() && !RelationType.REL_STAFF.equals(getType());
165  }
166 
167  public boolean isStudent() {
168  return FuzzyRelationTypes.isStudent(this);
169  }
170 
171  public boolean isWorker() {
172  return FuzzyRelationTypes.isWorker(this);
173  }
174 
175  public boolean isHHRR() {
176  return FuzzyRelationTypes.isHHRR(this);
177  }
178 
180  return new ValidationStatus(this);
181  }
182 
183  /* Description helper */
184 
185  public String getFormattedDescription() {
186  PhraseBuilder pb = new PhraseBuilder();
187  if(!Strings.isBlank(relationType)) {
188  pb.addWord(I_.byKey(relationType.substring(1)));
189  }
190  pb.addWord(description);
191  if(!pb.isBlank()) {
192  return pb.toString();
193  } else {
194  return "";
195  }
196  }
197 
198  public String getDatesString() {
199  return PositionDates.format(startDate, endDate);
200  }
201 
202  public String getImage() {
203  if(!Strings.isBlank(relationType)) {
204  if(relationType.startsWith("$INT")) {
205  return Images.getImage("contract");
206  }
207  RelationType rt = RelationType.getFromString(relationType);
208  if(rt != null) {
209  return rt.getImage();
210  }
211  }
212  return null;
213  }
214 
215  /* Related */
216 
217  private transient String related;
218 
219  public String getRelated() {
220  return related;
221  }
222 
223  public void setRelated(String related) {
224  this.related = related;
225  }
226 
227  /* Comparable */
228 
229  @Override
230  public int compareTo(BusinessRelation o) {
231  int result = CompareUtil.compare(validated, o.validated);
232  if(result == 0) {
233  result = CompareUtil.compare(preferential, o.preferential);
234  }
235  if(result == 0) {
236  result = CompareUtil.compare(endDate, o.endDate);
237  }
238  if(result == 0) {
239  result = CompareUtil.compare(startDate, o.startDate);
240  }
241  if(result == 0) {
242  result = CompareUtil.compare(description, o.description);
243  }
244  if(result == 0) {
245  result = CompareUtil.compare(id, o.id);
246  }
247  return result;
248  }
249 
250 }
void setPreferential(boolean preferential)
void setDescription(String description)
void setRelationType(String relationType)
static boolean isStudent(BusinessRelation relation)
static boolean isHHRR(BusinessRelation relation)
static boolean isWorker(BusinessRelation relation)
static String getImage(String image)
Definition: Images.java:36
static String byKey(String key)
Definition: I_.java:83
static String format(Date start, Date end)
static RelationType getFromString(String relation)