BrightSide Workbench Full Report + Source Code
ContractDefinition.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.ArrayList;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import javax.persistence.*;
25 import org.turro.string.Strings;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.entity.IDaoEntity;
29 import org.turro.util.PhraseBuilder;
30 
35 @Entity
36 public class ContractDefinition implements java.io.Serializable, IDaoEntity {
37  @Id
38  @GeneratedValue(strategy=GenerationType.IDENTITY)
39  @Column(name="IDENTIFIER")
40  private long id;
41 
42  @Column(name="CTCDEF_NAME")
43  private String name;
44 
45  private boolean cash, stock, useContractorCode;
46 
47  //private String contactPreference, operatingPreference, cashPreference, taxPreference, retentionPreference;
48  private String asContact, asCash;
49 
50  @OneToMany(mappedBy = "contractDefinition", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
51  @OrderBy("name")
52  private Set<ContractPreference> contractPreferences = new TreeSet<ContractPreference>();
53 
54  @OneToMany(mappedBy = "contractDefinition", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
55  @OrderBy("documentDefinition")
56  private Set<RelatedDocument> relatedDocuments = new TreeSet<RelatedDocument>();
57 
58  @OneToMany(mappedBy = "contractDefinition", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
59  @OrderBy("ancestor")
60  private Set<DocumentWorkflow> documentWorkflows = new TreeSet<DocumentWorkflow>();
61 
62  public String getAsCash() {
63  return asCash;
64  }
65 
66  public void setAsCash(String asCash) {
67  this.asCash = asCash;
68  }
69 
70  public String getAsContact() {
71  return asContact;
72  }
73 
74  public void setAsContact(String asContact) {
75  this.asContact = asContact;
76  }
77 
78  public boolean isCash() {
79  return cash;
80  }
81 
82  public void setCash(boolean cash) {
83  this.cash = cash;
84  }
85 
86 // public String getCashPreference() {
87 // return cashPreference;
88 // }
89 //
90 // public void setCashPreference(String cashPreference) {
91 // this.cashPreference = cashPreference;
92 // }
93 //
94 // public String getContactPreference() {
95 // return contactPreference;
96 // }
97 //
98 // public void setContactPreference(String contactPreference) {
99 // this.contactPreference = contactPreference;
100 // }
101 
102  public Set<ContractPreference> getContractPreferences() {
103  Dao dao = new FinancialsPU();
104  if(id > 0 && dao.isNotLoaded(contractPreferences)) {
105  contractPreferences = dao.lazyLoader(ContractDefinition.class, this, "contractPreferences").contractPreferences;
106  }
107  return contractPreferences;
108  }
109 
110  public void setContractPreferences(Set<ContractPreference> contractPreferences) {
111  this.contractPreferences = contractPreferences;
112  }
113 
114  public Set<DocumentWorkflow> getDocumentWorkflows() {
115  Dao dao = new FinancialsPU();
116  if(id > 0 && dao.isNotLoaded(documentWorkflows)) {
117  documentWorkflows = dao.lazyLoader(ContractDefinition.class, this, "documentWorkflows").documentWorkflows;
118  }
119  return documentWorkflows;
120  }
121 
122  public void setDocumentWorkflows(Set<DocumentWorkflow> documentWorkflows) {
123  this.documentWorkflows = documentWorkflows;
124  }
125 
126  public long getId() {
127  return id;
128  }
129 
130  public void setId(long id) {
131  this.id = id;
132  }
133 
134  public String getName() {
135  return name;
136  }
137 
138  public void setName(String name) {
139  this.name = name;
140  }
141 
142 // public String getOperatingPreference() {
143 // return operatingPreference;
144 // }
145 //
146 // public void setOperatingPreference(String operatingPreference) {
147 // this.operatingPreference = operatingPreference;
148 // }
149 
150  public Set<RelatedDocument> getRelatedDocuments() {
151  Dao dao = new FinancialsPU();
152  if(id > 0 && dao.isNotLoaded(relatedDocuments)) {
153  relatedDocuments = dao.lazyLoader(ContractDefinition.class, this, "relatedDocuments").relatedDocuments;
154  }
155  return relatedDocuments;
156  }
157 
158  public void setRelatedDocuments(Set<RelatedDocument> relatedDocuments) {
159  this.relatedDocuments = relatedDocuments;
160  }
161 
162 // public String getRetentionPreference() {
163 // return retentionPreference;
164 // }
165 //
166 // public void setRetentionPreference(String retentionPreference) {
167 // this.retentionPreference = retentionPreference;
168 // }
169 
170  public boolean isStock() {
171  return stock;
172  }
173 
174  public void setStock(boolean stock) {
175  this.stock = stock;
176  }
177 
178 // public String getTaxPreference() {
179 // return taxPreference;
180 // }
181 //
182 // public void setTaxPreference(String taxPreference) {
183 // this.taxPreference = taxPreference;
184 // }
185 
186  public boolean isUseContractorCode() {
187  return useContractorCode;
188  }
189 
190  public void setUseContractorCode(boolean useContractorCode) {
191  this.useContractorCode = useContractorCode;
192  }
193 
194  /* IDaoEntity */
195 
196  @Override
197  public Object entityId() {
198  return id;
199  }
200 
201  @Override
202  public boolean isEmpty() {
203  return Strings.isBlank(name);
204  }
205 
206  /* Helpers */
207 
209  ContractPreference result = null;
211  if(cp.getName().contentEquals("#")) {
212  result = cp;
213  break;
214  }
215  if(result == null) {
216  result = cp;
217  }
218  }
219  return result;
220  }
221 
222  public String getDescription() {
223  PhraseBuilder pb = new PhraseBuilder(name);
224  pb.addWord("[");
225 // if(contactPreference != null) {
226 // pb.addPendingSeparator(",");
227 // pb.addWord("c:" + contactPreference);
228 // }
229 // if(operatingPreference != null) {
230 // pb.addPendingSeparator(",");
231 // pb.addWord("e:" + operatingPreference);
232 // }
233 // if(cashPreference != null) {
234 // pb.addPendingSeparator(",");
235 // pb.addWord("d:" + cashPreference);
236 // }
237 // if(taxPreference != null) {
238 // pb.addPendingSeparator(",");
239 // pb.addWord("t:" + taxPreference);
240 // }
241 // if(retentionPreference != null) {
242 // pb.addPendingSeparator(",");
243 // pb.addWord("r:" + retentionPreference);
244 // }
245  if(asContact != null) {
246  pb.addPendingSeparator(",");
247  pb.addWord("c:" + asContact);
248  }
249  if(asCash != null) {
250  pb.addPendingSeparator(",");
251  pb.addWord("d:" + asCash);
252  }
253  pb.addWord("]");
254  return pb.toString();
255  }
256 
257  private transient ContractDefinitionNotes notes;
258 
260  if(notes == null) {
261  Dao dao = new FinancialsPU();
263  "select cdn from ContractDefinitionNotes cdn " +
264  "where cdn.contractDefinition = ?",
265  new Object[] { this });
266  }
267  return notes;
268  }
269 
270  public List<DocumentDefinition> getWorkflow() {
271  ArrayList<DocumentDefinition> v = new ArrayList<DocumentDefinition>();
272  for(DocumentWorkflow dw : getDocumentWorkflows()) {
273  int pos1 = getPosition(v, dw.getAncestor());
274  int pos2 = getPosition(v, dw.getDescendant());
275  if(pos1 == -1) {
276  v.add(0, dw.getAncestor());
277  }
278  if(pos2 == -1) {
279  if(pos1 + 1 >= v.size()) {
280  v.add(dw.getDescendant());
281  } else {
282  v.add(pos1 + 1, dw.getDescendant());
283  }
284  }
285  }
286  for(DocumentWorkflow dw : getDocumentWorkflows()) {
287  int pos1 = getPosition(v, dw.getAncestor());
288  int pos2 = getPosition(v, dw.getDescendant());
289  if(pos2 < pos1) {
290  v.set(pos1, dw.getDescendant());
291  v.set(pos2, dw.getAncestor());
292  }
293  }
294  return v;
295  }
296 
297  private int getPosition(List<DocumentDefinition> list, DocumentDefinition documentDefinition) {
298  int count = -1;
299  for(DocumentDefinition dd : list) {
300  count++;
301  if(dd.getId() == documentDefinition.getId()) {
302  return count;
303  }
304  }
305  return -1;
306  }
307 
308 }
void setUseContractorCode(boolean useContractorCode)
void setDocumentWorkflows(Set< DocumentWorkflow > documentWorkflows)
void setRelatedDocuments(Set< RelatedDocument > relatedDocuments)
void setContractPreferences(Set< ContractPreference > contractPreferences)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
boolean isNotLoaded(Object o, String attribute)
Definition: Dao.java:216