BrightSide Workbench Full Report + Source Code
AccountReport.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.report;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.financials.db.FinancialsPU;
24 import org.turro.jpa.Dao;
25 
30 public class AccountReport {
31 
32  private String name;
33  private List<AccountReportItem> children;
34  private List<AccountReportFormula> formulas;
35  private boolean showTotal;
36  private Dao dao;
37  private List<String> accounts;
38  private PeriodSet existingPeriods;
39 
40  public AccountReport(String name) {
41  this.name = name;
42  children = new ArrayList<AccountReportItem>();
43  formulas = new ArrayList<AccountReportFormula>();
44  }
45 
46  public AccountReportItem addItem(String label) {
47  return addItem(new AccountReportItem(label));
48  }
49 
51  item.setReport(this);
52  item.setParent(null);
53  children.add(item);
54  return item;
55  }
56 
57  public AccountReportFormula addFormula(String label) {
58  return addFormula(new AccountReportFormula(label));
59  }
60 
62  formula.setReport(this);
63  formulas.add(formula);
64  return formula;
65  }
66 
67  public List<AccountReportItem> getChildren() {
68  return children;
69  }
70 
71  public List<AccountReportFormula> getFormulas() {
72  return formulas;
73  }
74 
75  public String getName() {
76  return name;
77  }
78 
79  public void setName(String name) {
80  this.name = name;
81  }
82 
83  public boolean isShowTotal() {
84  return showTotal;
85  }
86 
87  public void setShowTotal(boolean showTotal) {
88  this.showTotal = showTotal;
89  }
90 
92  return existingPeriods;
93  }
94 
95  public AccountReportItem getItem(String label) {
96  if(!Strings.isBlank(label)) {
97  for(AccountReportItem ari : children) {
98  if(label.equals(ari.getLabel())) {
99  return ari;
100  }
101  AccountReportItem ari2 = ari.getItem(label);
102  if(ari2 != null) {
103  return ari2;
104  }
105  }
106  }
107  return null;
108  }
109 
110  public AccountReportFormula getFormula(String label) {
111  if(!Strings.isBlank(label)) {
112  for(AccountReportFormula arf : formulas) {
113  if(label.equals(arf.getLabel())) {
114  return arf;
115  }
116  }
117  }
118  return null;
119  }
120 
121  public Dao getDao() {
122  if(dao == null) {
123  dao = new FinancialsPU();
124  }
125  return dao;
126  }
127 
128  public List<String> getAccounts() {
129  if(accounts == null) {
130  accounts = getDao().getResultList("select a.id from Account as a");
131  }
132  return accounts;
133  }
134 
135  public void loadData() {
136  existingPeriods = new PeriodSet();
137  for(AccountReportItem ari : children) {
138  ari.loadData(existingPeriods);
139  }
140  existingPeriods.fillGapsTillNow(null);
141  for(AccountReportItem ari : children) {
142  ari.regularizeMode(existingPeriods);
143  }
144  }
145 
147  AccountReportItem total = new AccountReportItem(name);
148  total.setMode(null);
149  total.setParent(null);
150  total.setReport(this);
151  total.setTotalItem(true);
152  for(AccountReportPeriod period : existingPeriods) {
153  AccountReportPeriod arp = new AccountReportPeriod(period.getYear(), period.getMonth());
154  arp.setItem(total);
155  for(AccountReportItem item : children) {
156  arp.setValue(arp.getValue() + item.getAccumulatedFor(period));
157  }
158  total.getPeriods().add(arp);
159  }
160  return total;
161  }
162 
163 }
AccountReportItem getItem(String label)
AccountReportFormula getFormula(String label)
AccountReportFormula addFormula(String label)
AccountReportFormula addFormula(AccountReportFormula formula)
List< AccountReportItem > getChildren()
List< AccountReportFormula > getFormulas()
AccountReportItem addItem(AccountReportItem item)
AccountReportItem addItem(String label)
void fillGapsTillNow(AccountReportItem item)
Definition: PeriodSet.java:45