BrightSide Workbench Full Report + Source Code
PortfolioDisagreementSet.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.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.TreeMap;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.Document;
27 import org.turro.financials.entity.DocumentLine;
28 import org.turro.financials.entity.DocumentRelation;
29 import org.turro.financials.entity.DocumentWorkflow;
30 import org.turro.financials.model.business.CompanyWrapper;
31 import org.turro.jpa.Dao;
32 import org.turro.util.CompareUtil;
33 
38 public class PortfolioDisagreementSet extends TreeMap<Long, PortfolioDisagreement> {
39 
40  private Dao dao;
41 
43  fillSet();
44  checkBackwards();
45  }
46 
48  fillSet(contract);
49  checkBackwards();
50  }
51 
52  private void fillSet() {
53  Date closingDate = CompanyWrapper.getCompanyClosingDate();
54  dao = new FinancialsPU();
55  List<Object[]> l = dao.getResultList(
56  "select dr, dw from DocumentRelation dr, DocumentWorkflow dw " +
57  "where dr.ancestor.documentDefinition.id = dw.ancestor.id " +
58  "and dr.descendant.documentDefinition.id = dw.descendant.id " +
59  "and dr.ancestor.contract.contractDefinition.id = dw.contractDefinition.id " +
60  "and dr.ancestor.documentDate >= ?",
61  new Object[] { closingDate }
62  );
63  for(Object[] o : l) {
64  checkRelation((DocumentRelation) o[0], (DocumentWorkflow) o[1]);
65  }
66  }
67 
68  private void fillSet(Contract contract) {
69  Date closingDate = CompanyWrapper.getCompanyClosingDate();
70  dao = new FinancialsPU();
71  List<Object[]> l = dao.getResultList(
72  "select dr, dw from DocumentRelation dr, DocumentWorkflow dw " +
73  "where dr.ancestor.documentDefinition.id = dw.ancestor.id " +
74  "and dr.descendant.documentDefinition.id = dw.descendant.id " +
75  "and dr.ancestor.contract.contractDefinition.id = dw.contractDefinition.id " +
76  "and dr.ancestor.contract.id = ? " +
77  "and dr.ancestor.documentDate >= ?",
78  new Object[] { contract.getId(), closingDate }
79  );
80  for(Object[] o : l) {
81  checkRelation((DocumentRelation) o[0], (DocumentWorkflow) o[1]);
82  }
83  }
84 
85  private void checkRelation(DocumentRelation dr, DocumentWorkflow dw) {
86  double descendantAmount = 0.0;
87  if(CompareUtil.compare(dr.getAncestor().isDraft(), dr.getDescendant().isDraft()) != 0) {
88  return;
89  }
90  for(DocumentLine dl : dr.getDescendant().getDocumentLines()) {
91  if(dl.getLineType() != null && dl.getLineType().getId() == dw.getLineType().getId()) {
92  descendantAmount += dl.getAmount();
93  }
94  }
95  if(CompareUtil.compare(dr.getAncestor().getTotalAmount(), descendantAmount) != 0) {
96  PortfolioDisagreement pd = get(dr.getAncestor().getId());
97  if(pd != null) {
98  if(pd.fullfillsWith(descendantAmount)) {
99  remove(dr.getAncestor().getId());
100  } else {
101  pd.addAmount(descendantAmount);
102  }
103  } else {
104  put(dr.getAncestor().getId(), new PortfolioDisagreement(dr.getAncestor(), descendantAmount));
105  }
106  }
107  }
108 
109  private void checkBackwards() {
110  Iterator<PortfolioDisagreement> it = values().iterator();
111  while(it.hasNext()) {
112  if(checBackwardRelation(it.next())) {
113  it.remove();
114  }
115  }
116  }
117 
118  private boolean checBackwardRelation(PortfolioDisagreement pd) {
119  double siblingsAmount = 0.0;
120  for(DocumentRelation dr : pd.getDocument().getDescendants()) {
121  // OK, now backward: siblings
122  for(DocumentRelation drdoc : dr.getDescendant().getAncestors()) {
123  Document doc = drdoc.getAncestor();
124  siblingsAmount += doc.getTotalAmount();
125  }
126  }
127  return pd.fullfillsBackwardWith(siblingsAmount);
128  }
129 
130 }