BrightSide Workbench Full Report + Source Code
PortfolioStatusGrid.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.portfolio;
19 
20 import org.turro.elephant.util.DecimalFormats;
21 import org.turro.financials.entity.Contract;
22 import org.turro.financials.entity.MajorAccount;
23 import org.turro.financials.model.contract.PortfolioStatus;
24 import org.turro.financials.model.contract.PortfolioStatusSet;
25 import org.turro.i18n.I_;
26 import org.turro.zkoss.grid.GroupExtended;
27 import org.turro.zkoss.grid.PagingGrid;
28 import org.zkoss.zul.Column;
29 import org.zkoss.zul.Columns;
30 import org.zkoss.zul.Group;
31 import org.zkoss.zul.Groupfoot;
32 import org.zkoss.zul.Label;
33 import org.zkoss.zul.Row;
34 import org.zkoss.zul.Rows;
35 
40 public class PortfolioStatusGrid extends PagingGrid {
41 
43  addColumns();
44  addRows();
45  }
46 
47  private void addColumns() {
48  Columns cols = getColumns(true);
49 
51 
52  Column col = new Column(I_.get("Contract"), null, "80%");
53  cols.appendChild(col);
54 
55  col = new Column(I_.get("Balance"), null, "150px");
56  col.setAlign("right");
57  cols.appendChild(col);
58 
59  col = new Column(I_.get("Debit/Credit effects"), null, "150px");
60  col.setAlign("right");
61  cols.appendChild(col);
62  }
63 
64  private Double balance, effects;
65  private int groupsCount;
66 
67  private void addRows() {
68  Contract.checkNames();
69 
70  PortfolioStatusSet pss = new PortfolioStatusSet();
71  pss.loadAll();
72  pss.clearEmpties();
73 
74  Rows rows = getRows(true);
75 
76  balance = effects = 0.0;
77  groupsCount = 0;
78 
79  for(PortfolioStatus ps : pss) {
80  checkGrouping(ps);
81  Row row = new Row();
82  row.appendChild(new PortfolioStatusDetailRow(ps));
83  row.appendChild(new Label(ps.getContract().getName()));
84  row.appendChild(new Label(DecimalFormats.format(ps.getBalance())));
85  row.appendChild(new Label(DecimalFormats.format(ps.getEffects())));
86  rows.appendChild(row);
87  balance += ps.getBalance() == null ? 0 : ps.getBalance();
88  effects += ps.getEffects() == null ? 0 : ps.getEffects();
89  }
90  addGroupFoot();
91 
92  setRowCount(pss.size() + (groupsCount * 2));
93  }
94 
95  private String lastMajor;
96 
97  private void checkGrouping(PortfolioStatus ps) {
98  if(!ps.getMajor().equals(lastMajor)) {
99  addGroupFoot();
100  Group g = new GroupExtended(ps.getMajorString());
101  g.setOpen(false);
102  getRows().appendChild(g);
103  lastMajor = ps.getMajor();
104  groupsCount++;
105  }
106  }
107 
108  private void addGroupFoot() {
109  if(lastMajor != null) {
110  Groupfoot gf = new Groupfoot();
111  gf.appendChild(new Label());
112  gf.appendChild(new Label(MajorAccount.getMajor(lastMajor).getDescription()));
113  gf.appendChild(new Label(DecimalFormats.format(balance)));
114  gf.appendChild(new Label(DecimalFormats.format(effects)));
115  getRows().appendChild(gf);
116  balance = effects = 0.0;
117  }
118  }
119 
120 }
static String get(String msg)
Definition: I_.java:41
Columns getColumns(boolean create)
Rows getRows(boolean create)