BrightSide Workbench Full Report + Source Code
Contract.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.entity;
19 
20 import java.util.*;
21 import javax.persistence.*;
22 import org.turro.string.Strings;
23 import org.turro.action.Contacts;
24 import org.turro.elephant.calendar.Periodicity;
25 import org.turro.elephant.db.IdUtils;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.financials.db.FinancialsPU;
28 import org.turro.jpa.Dao;
29 import org.turro.jpa.entity.IDaoEntity;
30 import org.turro.plugin.contacts.ContactList;
31 import org.turro.plugin.contacts.IContact;
32 import org.turro.reflection.MappingSet;
33 import org.turro.util.PhraseBuilder;
34 
39 @Entity
40 public class Contract implements java.io.Serializable, IDaoEntity {
41 
42  @Id
43  @Column(name="IDENTIFIER")
44  private long id;
45 
46  private String globalId;
47 
48  private String contractor;
49 
50  private String name;
51 
52  private boolean stock, cash, active, useContractorCode;
53 
54  private OperatingModifier operatingModifier = OperatingModifier.OPMOD_NORMAL;
55 
56  private String grouping;
57 
58  private double fee;
59 
60  private Periodicity periodicity = Periodicity.PUNCTUAL;
61 
62  @ManyToOne
63  private Department department;
64 
65  @ManyToOne
66  private Service service;
67 
68  @Column(name="START_DATE")
69  @Temporal(value = TemporalType.TIMESTAMP)
70  private Date startDate;
71 
72  @Column(name="END_DATE")
73  @Temporal(value = TemporalType.TIMESTAMP)
74  private Date endDate;
75 
76  @Lob
77  @Column(length=4096)
78  private String notes;
79 
80  private Currency currency;
81 
82  private boolean equivalenceSurcharge;
83 
84  @ManyToOne(fetch=FetchType.LAZY)
85  private ContractDefinition contractDefinition;
86 
87  @OneToMany(fetch = FetchType.EAGER)
88  private Set<ContractPreference> contractPreferences = new HashSet<ContractPreference>();
89 
90  @OneToMany(mappedBy = "contract", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
91  private Set<RelatedContractStore> relatedContractStores = new HashSet<RelatedContractStore>();
92 
93  @OneToMany(mappedBy = "contract", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
94  @OrderBy(value="name")
95  private Set<ContractParticipant> participants = new HashSet<ContractParticipant>();
96 
97  @OneToMany(mappedBy = "contract", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
98  private Set<ContractFlow> contractFlows = new HashSet<ContractFlow>();
99 
100  @OneToMany(mappedBy = "contract", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
101  private Set<Tax> taxes = new HashSet<Tax>();
102 
103  @OneToMany(mappedBy = "contract", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
104  private Set<Contracted> contracted = new HashSet<Contracted>();
105 
106  @OneToOne(cascade = CascadeType.ALL)
107  private ContractHandshake contractHandshake;
108 
109  public boolean isActive() {
110  return active;
111  }
112 
113  public void setActive(boolean active) {
114  this.active = active;
115  }
116 
117  public boolean isCash() {
118  return cash;
119  }
120 
121  public void setCash(boolean cash) {
122  this.cash = cash;
123  }
124 
126  Dao dao = new FinancialsPU();
127  if(id > 0 && dao.isNotLoaded(contractDefinition)) {
128  contractDefinition = dao.lazyLoader(Contract.class, this, "contractDefinition").contractDefinition;
129  }
130  return contractDefinition;
131  }
132 
133  public void setContractDefinition(ContractDefinition contractDefinition) {
134  this.contractDefinition = contractDefinition;
135  }
136 
137  public Set<ContractFlow> getContractFlows() {
138  return contractFlows;
139  }
140 
141  public void setContractFlows(Set<ContractFlow> contractFlows) {
142  this.contractFlows = contractFlows;
143  }
144 
145  public Set<ContractPreference> getContractPreferences() {
146  return contractPreferences;
147  }
148 
149  public void setContractPreferences(Set<ContractPreference> contractPreferences) {
150  this.contractPreferences = contractPreferences;
151  }
152 
153  public String getContractor() {
154  return contractor;
155  }
156 
157  public void setContractor(String contractor) {
158  this.contractor = contractor;
159  }
160 
161  public Currency getCurrency() {
162  if(currency == null) {
163  currency = Currency.getInstance("EUR");
164  }
165  return currency;
166  }
167 
168  public void setCurrency(Currency currency) {
169  this.currency = currency;
170  }
171 
173  return department;
174  }
175 
176  public void setDepartment(Department department) {
177  this.department = department;
178  }
179 
180  public Date getEndDate() {
181  return endDate;
182  }
183 
184  public void setEndDate(Date endDate) {
185  this.endDate = endDate;
186  }
187 
188  public boolean isEquivalenceSurcharge() {
189  return equivalenceSurcharge;
190  }
191 
192  public void setEquivalenceSurcharge(boolean equivalenceSurcharge) {
193  this.equivalenceSurcharge = equivalenceSurcharge;
194  }
195 
196  public String getGlobalId() {
197  return globalId;
198  }
199 
200  public void setGlobalId(String globalId) {
201  this.globalId = globalId;
202  }
203 
204  public long getId() {
205  return id;
206  }
207 
208  public void setId(long id) {
209  this.id = id;
210  }
211 
212  public String getName() {
213  return name;
214  }
215 
216  public void setName(String name) {
217  this.name = name;
218  }
219 
220  public String getNotes() {
221  return notes;
222  }
223 
224  public void setNotes(String notes) {
225  this.notes = notes;
226  }
227 
229  return operatingModifier;
230  }
231 
232  public void setOperatingModifier(OperatingModifier operatingModifier) {
233  this.operatingModifier = operatingModifier;
234  }
235 
236  public String getGrouping() {
237  return grouping;
238  }
239 
240  public void setGrouping(String grouping) {
241  this.grouping = grouping;
242  }
243 
244  public double getFee() {
245  return fee;
246  }
247 
248  public void setFee(double fee) {
249  this.fee = fee;
250  }
251 
253  return periodicity;
254  }
255 
256  public void setPeriodicity(Periodicity periodicity) {
257  this.periodicity = periodicity;
258  }
259 
260  public Set<ContractParticipant> getParticipants() {
261  return participants;
262  }
263 
264  public void setParticipants(Set<ContractParticipant> participants) {
265  this.participants = participants;
266  }
267 
268  public Set<RelatedContractStore> getRelatedContractStores() {
269  Dao dao = new FinancialsPU();
270  if(id > 0 && dao.isNotLoaded(relatedContractStores)) {
271  relatedContractStores = dao.lazyLoader(Contract.class, this, "relatedContractStores").relatedContractStores;
272  }
273  return relatedContractStores;
274  }
275 
276  public void setRelatedContractStores(Set<RelatedContractStore> relatedContractStores) {
277  this.relatedContractStores = relatedContractStores;
278  }
279 
280  public Service getService() {
281  return service;
282  }
283 
284  public void setService(Service service) {
285  this.service = service;
286  }
287 
288  public Date getStartDate() {
289  return startDate;
290  }
291 
292  public void setStartDate(Date startDate) {
293  this.startDate = startDate;
294  }
295 
296  public boolean isStock() {
297  return stock;
298  }
299 
300  public void setStock(boolean stock) {
301  this.stock = stock;
302  }
303 
304  public Set<Tax> getTaxes() {
305  return taxes;
306  }
307 
308  public void setTaxes(Set<Tax> taxes) {
309  this.taxes = taxes;
310  }
311 
312  public Set<Contracted> getContracted() {
313  return contracted;
314  }
315 
316  public void setContracted(Set<Contracted> contracted) {
317  this.contracted = contracted;
318  }
319 
320  public boolean isUseContractorCode() {
321  return useContractorCode;
322  }
323 
324  public void setUseContractorCode(boolean useContractorCode) {
325  this.useContractorCode = useContractorCode;
326  }
327 
329  return contractHandshake;
330  }
331 
332  public void setContractHandshake(ContractHandshake contractHandshake) {
333  this.contractHandshake = contractHandshake;
334  }
335 
336  /* IDaoEntity */
337 
338  @Override
339  public Object entityId() {
340  return id;
341  }
342 
343  @Override
344  public boolean isEmpty() {
345  return Strings.isBlank(contractor) || getContractDefinition() == null;
346  }
347 
348  @Override
349  public void prepareSave() {
350  if(!stock) {
351  department = null;
352  service = null;
353  }
354  IDaoEntity.super.prepareSave();
355  name = Strings.truncateAndWarn(getFullDescription(), 255);
356  if(operatingModifier == null) {
357  operatingModifier = OperatingModifier.OPMOD_NORMAL;
358  }
359  if(id == 0) {
360  id = IdUtils.getNewLongIdFromLong(new FinancialsPU(), "Contract", "id");
361  }
362  }
363 
364  @Override
365  public Collection<Collection> collections() {
366  return List.of(
367  getParticipants(),
370  getTaxes(),
371  getContracted());
372  }
373 
374  /* Helpers */
375 
377  if(!Strings.isBlank(contractor)) {
378  return Contacts.getContactById(contractor);
379  }
380  return null;
381  }
382 
383  public String getPartialDescription() {
384  StringBuilder sb = new StringBuilder();
385  if(!Strings.isBlank(globalId)) {
386  sb.append(globalId);
387  }
388  return sb.length() > 0 ? sb.toString() : getFullDescription();
389  }
390 
391  public String getFullDescription() {
392  StringBuilder sb = new StringBuilder();
393  if(department != null) {
394  sb.append(department.getFullName());
395  service = null;
396  } else if(service != null) {
397  sb.append(service.getFullName());
398  department = null;
399  } else {
400  IContact ic = getIContractor();
401  if(ic != null) {
402  sb.append(ic.getName());
403  }
404  }
405  if(getContractDefinition() != null) {
406  PhraseBuilder pb = new PhraseBuilder();
407  pb.addWord(contractDefinition.getName());
408  pb.addPendingSeparator(": ");
410  pb.addWord(cp.getName());
411  pb.addPendingSeparator(",");
412  }
413  if(!pb.isBlank()) {
414  pb.enclose("()");
415  sb.append(" " + pb.toString());
416  }
417  }
419  sb.append(" [" + getOperatingModifier().toString().substring(6, 12) + "]");
420  }
421  if(globalId != null) {
422  sb.append(" " + globalId);
423  }
424  return sb.toString();
425  }
426 
427  public Collection<ContractFlow> getFlowFor(Document document) {
428  ArrayList<ContractFlow> flows = new ArrayList<>();
429  if(document != null && document.getDocumentDefinition() != null) {
430  for(ContractFlow cf : contractFlows) {
431  if(cf.getDocumentDefinition().getId() == document.getDocumentDefinition().getId()) {
432  flows.add(cf);
433  }
434  }
435  }
436  return flows;
437  }
438 
439  public List<Document> getDocuments() {
440  WhereClause wc = new WhereClause();
441  wc.addClause("select d from Document d");
442  wc.addClause("where d.contract = :contract");
443  wc.addNamedValue("contract", this);
444  Dao dao = new FinancialsPU();
445  return dao.getResultList(Document.class, wc);
446  }
447 
448  public static void checkNames() {
449  Dao dao = new FinancialsPU();
450  List<Contract> list = dao.getResultList(
451  "select contract from Contract as contract");
452  for(Contract c : list) {
453  c.prepareSave();
454  dao.saveObject(c);
455  }
456  }
457 
458  /* Participants helper */
459 
460  private transient ContactList _participants, _docParticipants;
461 
462  public Collection<IContact> getIParticipants() {
463  if(_participants == null) {
464  _participants = new ContactList();
466  _participants.add(Contacts.getContactById(cp.getIdContact()));
467  }
468  _participants.add(getIContractor());
469  }
470  return _participants;
471  }
472 
473  public Collection<IContact> getIParticipantsEmail() {
474  if(_docParticipants == null) {
475  _docParticipants = new ContactList();
477  if(cp.getInterventionType().isSendDocuments()) {
478  _docParticipants.add(Contacts.getContactById(cp.getIdContact()));
479  }
480  }
481  _docParticipants.add(getIContractor());
482  }
483  return _docParticipants;
484  }
485 
486  /* Usual */
487 
488  public String getUsualPath(DocumentDefinition documentDefinition) {
489  if(documentDefinition != null) {
490  return "dc-" +
491  getId() + "-" +
492  documentDefinition.getId() + "-";
493  }
494  return "";
495  }
496 
497  /* XML Serializer */
498 
499  public MappingSet getSerializerMappings() {
500  MappingSet set = new MappingSet();
501  set.addMapping(Contract.class, 1,
502  new String[] { "id", "globalId", "contractor", "name" },
503  new String[] { "stock", "cash", "active", "useContractorCode", "operatingModifier",
504  "startDate", "endDate", "notes", "department", "service", "contractDefinition",
505  "contractPreferences", "relatedContractStores", "participants", "currency",
506  "taxes", "equivalenceSurcharge", "contractFlows", "contractHandshake",
507  "grouping", "fee", "periodicity", "contracted" });
508  set.addMapping(Department.class, 2,
509  new String[] { "id", "name" },
510  null);
511  set.addMapping(Service.class, 2,
512  new String[] { "id", "name" },
513  null);
514  set.addMapping(ContractDefinition.class, 2,
515  new String[] { "id", "name" },
516  null);
517  set.addMapping(ContractPreference.class, 2,
518  new String[] { "id", "name" },
519  null);
520  set.addMapping(RelatedContractStore.class, 2,
521  new String[] { "allowed" },
522  new String[] { "documentDefinition", "store" });
523  set.addMapping(DocumentDefinition.class, 3,
524  new String[] { "id", "name" },
525  null);
526  set.addMapping(Contract.class, 3,
527  new String[] { "id", "name" },
528  null);
529  set.addMapping(ContractParticipant.class, 2,
530  new String[] { "name", "interventionType" },
531  null);
532  set.addMapping(Currency.class, 2,
533  new String[] { "currencyCode" },
534  null);
535  set.addMapping(Tax.class, 2,
536  new String[] { "taxType", "tax", "equivalenceSurcharge" },
537  null);
538  set.addMapping(ContractFlow.class, 2,
539  new String[] { "description" },
540  new String[] { "documentDefinition", "contractExpiries" });
541  set.addMapping(ContractExpiry.class, 3,
542  new String[] { "concept", "daysToAdd", "dayOfWeek", "percentValue", "amountValue" },
543  new String[] { "documentDefinition", "lineType", "daysOfMonth", "excludedMonths" });
544  set.addMapping(DocumentDefinition.class, 4,
545  new String[] { "id", "name" },
546  null);
547  set.addMapping(LineType.class, 4,
548  new String[] { "name" },
549  null);
550  set.addMapping(Integer.class, 4,
551  new String[] { "this" },
552  null);
553  set.addMapping(ContractHandshake.class, 2,
554  new String[] { "remoteId", "remoteName", "remoteServer" },
555  null);
556  set.addMapping(Contracted.class, 2,
557  new String[] { "concept", "price", "quantity" },
558  new String[] { "product" });
559  set.addMapping(Product.class, 3,
560  new String[] { "id", "productCode" },
561  new String[] { "description" });
562  return set;
563  }
564 
565 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static long getNewLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:33
void addNamedValue(String name, Object value)
Collection< IContact > getIParticipants()
Definition: Contract.java:462
void setService(Service service)
Definition: Contract.java:284
void setTaxes(Set< Tax > taxes)
Definition: Contract.java:308
Set< ContractParticipant > getParticipants()
Definition: Contract.java:260
Collection< IContact > getIParticipantsEmail()
Definition: Contract.java:473
void setEquivalenceSurcharge(boolean equivalenceSurcharge)
Definition: Contract.java:192
ContractHandshake getContractHandshake()
Definition: Contract.java:328
void setOperatingModifier(OperatingModifier operatingModifier)
Definition: Contract.java:232
ContractDefinition getContractDefinition()
Definition: Contract.java:125
Collection< ContractFlow > getFlowFor(Document document)
Definition: Contract.java:427
void setActive(boolean active)
Definition: Contract.java:113
void setRelatedContractStores(Set< RelatedContractStore > relatedContractStores)
Definition: Contract.java:276
void setDepartment(Department department)
Definition: Contract.java:176
void setStartDate(Date startDate)
Definition: Contract.java:292
void setContractor(String contractor)
Definition: Contract.java:157
String getUsualPath(DocumentDefinition documentDefinition)
Definition: Contract.java:488
OperatingModifier getOperatingModifier()
Definition: Contract.java:228
void setContractHandshake(ContractHandshake contractHandshake)
Definition: Contract.java:332
void setGrouping(String grouping)
Definition: Contract.java:240
Set< ContractPreference > getContractPreferences()
Definition: Contract.java:145
Collection< Collection > collections()
Definition: Contract.java:365
void setContractPreferences(Set< ContractPreference > contractPreferences)
Definition: Contract.java:149
void setPeriodicity(Periodicity periodicity)
Definition: Contract.java:256
void setGlobalId(String globalId)
Definition: Contract.java:200
Set< RelatedContractStore > getRelatedContractStores()
Definition: Contract.java:268
void setCurrency(Currency currency)
Definition: Contract.java:168
void setContracted(Set< Contracted > contracted)
Definition: Contract.java:316
void setParticipants(Set< ContractParticipant > participants)
Definition: Contract.java:264
Set< ContractFlow > getContractFlows()
Definition: Contract.java:137
void setUseContractorCode(boolean useContractorCode)
Definition: Contract.java:324
void setContractDefinition(ContractDefinition contractDefinition)
Definition: Contract.java:133
void setContractFlows(Set< ContractFlow > contractFlows)
Definition: Contract.java:141
Set< Contracted > getContracted()
Definition: Contract.java:312
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
boolean isNotLoaded(Object o, String attribute)
Definition: Dao.java:216