BrightSide Workbench Full Report + Source Code
ExportAsStream.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.jpa.export;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.function.BiFunction;
26 import java.util.stream.Stream;
27 import jxl.Workbook;
28 import jxl.write.DateTime;
29 import jxl.write.Label;
30 import jxl.write.WritableSheet;
31 import jxl.write.WritableWorkbook;
32 import jxl.write.WriteException;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.jpa.Dao;
35 import org.turro.log.WebLoggers;
36 import org.turro.sql.SqlClause;
37 import org.turro.time.Dates;
38 
43 public class ExportAsStream {
44 
45  private static final int MAX_ROWS = 60000;
46 
47  private final Dao dao;
48  private final SqlClause sc;
49  private final WhereClause wc;
50  private final Collection<String> columns;
51 
52  private int countCols = 0, countRows = 0;
53  private BiFunction<Integer, Object, Object> replace = null;
54  private WritableWorkbook ww;
55  private WritableSheet ws;
56 
57  public ExportAsStream(Dao dao, Collection<String> columns, SqlClause sc) {
58  this.dao = dao;
59  this.columns = columns;
60  this.sc = sc;
61  this.wc = null;
62  }
63 
64  public ExportAsStream(Dao dao, Collection<String> columns, WhereClause wc) {
65  this.dao = dao;
66  this.columns = columns;
67  this.sc = null;
68  this.wc = wc;
69  }
70 
71  public void fieldReplacement(BiFunction<Integer, Object, Object> replace) {
72  this.replace = replace;
73  }
74 
75  public void generateExcel(File file) {
76  try {
77  ww = Workbook.createWorkbook(file);
78  writeSheet();
79  ww.write();
80  ww.close();
81  } catch (WriteException | IOException ex) {
82  WebLoggers.severe(this).exception(ex).log();
83  }
84  }
85 
86  private void writeSheet() throws WriteException {
87  checkSheet();
88  for(String s : columns) {
89  ws.addCell(new Label(countCols++, countRows, s));
90  }
91  if(sc != null) {
92  try(Stream stream = dao.stream(Object[].class, sc)) {
93  writeRows(stream);
94  }
95  } else if(wc != null) {
96  try(Stream stream = dao.stream(Object[].class, wc)) {
97  writeRows(stream);
98  }
99  }
100  }
101 
102  private void writeRows(final Stream stream) {
103  stream.forEach(objs -> {
104  countCols = 0;
105  countRows++;
106  checkSheet();
107  for(Object obj : (Object[]) objs) {
108  try {
109  if(replace != null) {
110  obj = replace.apply(countCols, obj);
111  }
112  if(obj instanceof Number) {
113  ws.addCell(new jxl.write.Number(countCols++, actualRow(),
114  ((Number)obj).doubleValue()));
115  } else if(obj instanceof Date) {
116  ws.addCell(new DateTime(countCols++, actualRow(), Dates.fromGTM((Date) obj).get()));
117  } else if(obj instanceof Boolean) {
118  ws.addCell(new Label(countCols++, actualRow(), ((Boolean) obj).toString()));
119  } else {
120  ws.addCell(new Label(countCols++, actualRow(), (String) obj));
121  }
122  } catch (WriteException ex) {
123  WebLoggers.severe(this).exception(ex).log();
124  }
125  }
126  });
127  }
128 
129  private void checkSheet() {
130  if(actualRow() == 0) {
131  int sheetIndex = Math.abs(countRows / MAX_ROWS);
132  ws = ww.createSheet("Sheet %d".formatted(sheetIndex), sheetIndex);
133  }
134  }
135 
136  private int actualRow() {
137  return countRows % MAX_ROWS;
138  }
139 
161 }
ExportAsStream(Dao dao, Collection< String > columns, WhereClause wc)
void fieldReplacement(BiFunction< Integer, Object, Object > replace)
ExportAsStream(Dao dao, Collection< String > columns, SqlClause sc)
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29