BrightSide Workbench Full Report + Source Code
ReportComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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 
19 package org.turro.financials.report;
20 
21 import java.io.IOException;
22 import java.util.Date;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.amic.util.date.CheckDate;
26 import org.amic.util.file.FileUtil;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.zkoss.grid.NumericColumn;
29 import org.turro.zkoss.grid.PagingGrid;
30 import org.turro.zkoss.label.LabelExtended;
31 import org.zkoss.zk.ui.Component;
32 import org.zkoss.zk.ui.Executions;
33 import org.zkoss.zk.ui.select.SelectorComposer;
34 import org.zkoss.zk.ui.select.annotation.Listen;
35 import org.zkoss.zk.ui.select.annotation.Wire;
36 import org.zkoss.zul.Column;
37 import org.zkoss.zul.Datebox;
38 import org.zkoss.zul.Row;
39 import org.zkoss.zul.Textbox;
40 import org.zkoss.zul.Toolbarbutton;
41 
46 public class ReportComposer extends SelectorComposer<Component> {
47 
48  private ReportBody body;
49 
50  @Wire("#from") Datebox from;
51  @Wire("#to") Datebox to;
52  @Wire("#reportgrid") PagingGrid reportgrid;
53  @Wire("#execute") Toolbarbutton execute;
54  @Wire("#text") Textbox text;
55 
56  @Listen("onClick = #execute")
57  public void onExecute() {
58  if(body.getFile() != null) {
59  body = ReportBody.load(body.getFile());
60  }
61  updateGrid();
62  }
63 
64  @Listen("onClick = #save")
65  public void onSave() {
66  if(body.getFile() != null) {
67  try {
68  FileUtil.setContent(body.getFile(), text.getText(), ElephantContext.getEncoding());
69  } catch (IOException ex) {
70  Logger.getLogger(ReportComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
71  }
72  }
73  }
74 
75 
76  @Override
77  public void doAfterCompose(Component comp) throws Exception {
78  super.doAfterCompose(comp);
79  to.setValue(new Date());
80  from.setValue(new CheckDate(to.getValue()).addMonths(-1).getDate());
81  body = (ReportBody) Executions.getCurrent().getAttribute("body");
82  execute.setDisabled(body == null);
83  if(body.getFile() != null) {
84  text.setValue(FileUtil.getContent(body.getFile(), ElephantContext.getEncoding()));
85  }
86  }
87 
88  private void updateGrid() {
89  body.fill(from.getValue(), to.getValue());
90 
91  reportgrid.clearColumns();
92  reportgrid.clearRows();
93 
94  // Column labels
95  reportgrid.getColumns(true).appendChild(new Column(""));
96  for(ReportColumn column : body.getColumns()) {
97  reportgrid.getColumns(true).appendChild(new NumericColumn(column.getName()));
98  }
99 
100  if(body.hasValues()) {
101  // Row labels and values
102  for(ReportRow row : body.getRows()) {
103  Row grow = new Row();
104  grow.appendChild(new LabelExtended().setString(row.getName()).setCSS("font-weight:bold;"));
105  if(row.isSum()) {
106  for(ReportColumn column : body.getColumns()) {
107  grow.appendChild(new LabelExtended().setCSS("font-weight:bold;")
108  .setFormat("#,##0.00").setDouble(body.getSum(row, column, row.getGroup())));
109  }
110  } else {
111  for(ReportColumn column : body.getColumns()) {
112  grow.appendChild(new LabelExtended().setFormat("#,##0.00")
113  .setDouble(body.getValue(row, column)));
114  }
115  }
116  reportgrid.getRows(true).appendChild(grow);
117  }
118  }
119  }
120 
121 }
static void setContent(File file, String value)
Definition: FileUtil.java:274
static String getContent(File file)
Definition: FileUtil.java:245
Double getValue(ReportRow row, ReportColumn column)
Definition: ReportBody.java:70
void fill(Date start, Date end)
Definition: ReportBody.java:95
static ReportBody load(File file)
TreeSet< ReportColumn > getColumns()
Definition: ReportBody.java:65
Double getSum(ReportRow row, ReportColumn column, String group)
Definition: ReportBody.java:80
Columns getColumns(boolean create)
Rows getRows(boolean create)