BrightSide Workbench Full Report + Source Code
SingleLineDocumentWrapper.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.model.document;
19 
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.ContractPreference;
27 import org.turro.financials.entity.Document;
28 import org.turro.financials.entity.DocumentDefinition;
29 import org.turro.financials.entity.DocumentLine;
30 import org.turro.financials.entity.DocumentRelation;
31 import org.turro.financials.entity.LineType;
32 import org.turro.financials.entity.Product;
33 import org.turro.financials.entity.ProductByContractor;
34 import org.turro.jpa.entity.EntityCollections;
35 import org.turro.util.CompareUtil;
36 
41 public class SingleLineDocumentWrapper implements Comparable<SingleLineDocumentWrapper> {
42 
43  private DocumentRelation relation;
44  private DocumentLine line;
45  private boolean errorState = false;
46 
47  public SingleLineDocumentWrapper(Document source, DocumentDefinition expiryDefinition) {
48  this.relation = createRelation(source, expiryDefinition);
49  }
50 
52  this.relation = relation;
53  if(relation != null && relation.getDescendant() != null &&
54  relation.getDescendant().getDocumentLines() != null &&
55  relation.getDescendant().getDocumentLines().size() == 1) {
56  this.line = relation.getDescendant().getDocumentLines().iterator().next();
57  } else {
58  errorState = true;
59  }
60  }
61 
63  return relation;
64  }
65 
66  public String getConcept() {
67  return line.getConcept();
68  }
69 
70  public void setConcept(String concept) {
71  line.setConcept(concept);
72  }
73 
74  public Date getDocumentDate() {
75  return relation.getDescendant().getReceiptDate();
76  }
77 
78  public void setDocumentDate(Date documentDate) {
79  relation.getDescendant().setReceiptDate(documentDate);
80  relation.getDescendant().setDocumentDate(documentDate);
81  }
82 
83  public String getDocumentNumber() {
84  return relation.getDescendant().getDocumentNumber();
85  }
86 
87  public void setDocumentNumber(String documentNumber) {
88  relation.getDescendant().setDocumentNumber(documentNumber);
89  }
90 
92  return relation.getDescendant().getDocumentDefinition();
93  }
94 
95  public void setExpiryDefinition(DocumentDefinition expiryDefinition) {
96  relation.getDescendant().setDocumentDefinition(expiryDefinition);
97  }
98 
99  public LineType getLineType() {
100  if(line.getLineType().getContractPreference() == null) {
102  }
103  return line.getLineType();
104  }
105 
106  public void setLineType(LineType lineType) {
107  line.setLineType(lineType);
108  if(lineType != null) {
110  }
111  }
112 
114  return line.getContractPreference();
115  }
116 
117 
118  public double getPrice() {
119  return line.getPrice();
120  }
121 
122  public void setPrice(double price) {
123  line.setPrice(price);
124  }
125 
126  public Product getProduct() {
127  return line.getProduct();
128  }
129 
130  public void setProduct(Product product) {
131  line.setProduct(product);
132  }
133 
135  return line.getProductByContractor();
136  }
137 
138  public void setProductByContractor(ProductByContractor productByContractor) {
139  line.setProductByContractor(productByContractor);
140  }
141 
142  public double getQuantity() {
143  return line.getQuantity();
144  }
145 
146  public void setQuantity(double quantity) {
147  line.setQuantity(quantity);
148  }
149 
150  public Contract getStore() {
151  return line.getStore();
152  }
153 
154  public void setStore(Contract store) {
155  line.setStore(store);
156  }
157 
158  public double getTax() {
159  return line.getTax();
160  }
161 
162  public void setTax(double tax) {
163  line.setTax(tax);
164  }
165 
166  public double getTotal() {
167  return relation.getDescendant().getTotalAmount();
168  }
169 
170  public Document getTarget() {
171  return relation.getDescendant();
172  }
173 
174  public boolean isValid() {
175  return relation != null &&
176  relation.getDescendant() != null &&
177  line != null &&
178  !line.isEmpty();
179  }
180 
182  return line;
183  }
184 
185  public void removeRelation() {
186  Set<DocumentRelation> rels = relation.getAncestor().getAncestors();
187  EntityCollections.entities(rels).remove(relation);
188  rels = relation.getAncestor().getDescendants();
189  EntityCollections.entities(rels).remove(relation);
190  rels = relation.getDescendant().getAncestors();
191  EntityCollections.entities(rels).remove(relation);
192  rels = relation.getDescendant().getDescendants();
193  EntityCollections.entities(rels).remove(relation);
194  relation.setDescendant(null);
195  relation.setAncestor(null);
197  "delete from DocumentRelation where id = ?",
198  new Object[] { relation.getId() });
199  }
200 
201  private DocumentRelation createRelation(Document source, DocumentDefinition expiryDefinition) {
203  dr.setAncestor(source);
204  Document target = createDocument(source, expiryDefinition);
205  dr.setDescendant(target);
206  line.setDocument(target);
207  line.setLineType(target.getDefaultLineType());
208  source.getDescendants().add(dr);
209  return dr;
210  }
211 
212  private Document createDocument(Document source, DocumentDefinition expiryDefinition) {
213  Document document = new Document();
214  document.setContract(source.getContract());
215  document.setCurrency(source.getCurrency());
216  document.setDocumentDate(source.getReceiptDate());
217  document.setReceiptDate(source.getReceiptDate());
218  document.setDocumentDefinition(expiryDefinition);
219  document.setForcedView(source.getForcedView());
220  line = createLine();
221  document.getDocumentLines().add(line);
222  return document;
223  }
224 
225  private DocumentLine createLine() {
226  DocumentLine lin = new DocumentLine();
227  return lin;
228  }
229 
230  public static Collection<SingleLineDocumentWrapper> adapt(Document source, Collection<DocumentRelation> relations) {
231  TreeSet<SingleLineDocumentWrapper> set = new TreeSet<>();
232  for(DocumentRelation dr : relations) {
234  if(sldw.isValid()) {
235  set.add(sldw);
236  }
237  }
238  return set;
239  }
240 
241  @Override
243  if(o == null) {
244  return -1;
245  }
246  int result = CompareUtil.compare(getDocumentDate(), o.getDocumentDate());
247  if(result == 0 && relation.getDescendant() != null && o.getRelation().getDescendant() != null) {
248  result = CompareUtil.compare(relation.getDescendant().getId(), o.getRelation().getDescendant().getId());
249  }
250  if(result == 0) {
251  result = CompareUtil.compare(getConcept(), o.getConcept());
252  }
253  return result;
254  }
255 
256 }
void setContractPreference(ContractPreference contractPreference)
void setProductByContractor(ProductByContractor productByContractor)
ProductByContractor getProductByContractor()
void setForcedView(RegisterView forcedView)
Definition: Document.java:208
void setDocumentNumber(String documentNumber)
Definition: Document.java:192
void setDocumentDate(Date documentDate)
Definition: Document.java:163
Set< DocumentRelation > getAncestors()
Definition: Document.java:107
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
void setContract(Contract contract)
Definition: Document.java:123
void setCurrency(Currency currency)
Definition: Document.java:135
ContractPreference getDefaultContractPreference()
Definition: Document.java:429
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
void setDocumentDefinition(DocumentDefinition documentDefinition)
Definition: Document.java:175
void setReceiptDate(Date receiptDate)
Definition: Document.java:256
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
ContractPreference getContractPreference()
Definition: LineType.java:77
void setContractPreference(ContractPreference contractPreference)
Definition: LineType.java:81
static Collection< SingleLineDocumentWrapper > adapt(Document source, Collection< DocumentRelation > relations)
SingleLineDocumentWrapper(DocumentRelation relation, Document source)
void setProductByContractor(ProductByContractor productByContractor)
SingleLineDocumentWrapper(Document source, DocumentDefinition expiryDefinition)
int executeUpdate(String query)
Definition: Dao.java:463
static EntityCollections entities(Collection values)