BrightSide Workbench Full Report + Source Code
ReportPrint.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.zkoss.print;
19 
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.InputStream;
23 import java.util.Collection;
24 import java.util.Map;
25 import java.util.function.Consumer;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import net.sf.jasperreports.engine.JRDataSource;
29 import net.sf.jasperreports.engine.JRException;
30 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
31 import org.turro.action.MailAttachment;
32 import org.turro.action.MailSenders;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.zkoss.Modal;
36 import org.turro.elephant.zkoss.ModalWindow;
37 import org.turro.plugin.contacts.IContact;
38 import org.zkoss.zkex.zul.Jasperreport;
39 
44 public class ReportPrint extends ModalWindow {
45 
46  private Jasperreport report;
47 
48  public ReportPrint() throws InterruptedException {
49  super();
50  }
51 
52  public ReportPrint(String title, String border, boolean closable) throws InterruptedException {
53  super(title, border, closable);
54  }
55 
56  public Jasperreport getReport() {
57  return report;
58  }
59 
60  public void setReport(Jasperreport report) {
61  this.report = report;
62  }
63 
64  public static void print(String title, Collection collection, Map parameters, String jasperFile,
65  String defaultFile, String type, String certificateStore) throws JRException {
66  print(title, new JRBeanCollectionDataSource(collection), parameters, jasperFile, defaultFile, type, certificateStore);
67  }
68 
69  public static void print(String title, JRDataSource dataSource, Map parameters, String jasperFile,
70  String defaultFile, String type, String certificateStore) throws JRException {
71  ReportPrint reportView = (ReportPrint) Modal.getModal("/WEB-INF/_zul/www/report.zul");
72  reportView.setTitle(title);
73  if(reportView.getReport() instanceof SignedReport) {
74  ((SignedReport) reportView.getReport()).setCertificateStore(certificateStore);
75  }
76  if (new File(ElephantContext.getRealPath(jasperFile)).exists()) {
77  reportView.getReport().setSrc(jasperFile);
78  } else {
79  reportView.getReport().setSrc(defaultFile);
80  }
81  reportView.getReport().setParameters(parameters);
82  reportView.getReport().setDatasource(dataSource);
83  reportView.getReport().setType(type);
84  Modal.doModal(reportView, null);
85  }
86 
87  public static void send(String title, Collection collection, Map parameters, String jasperFile,
88  String defaultFile, String type, String certificateStore, Collection<IContact> contacts, String comment,
89  Collection<MailAttachment> attachments, Consumer onFinish) throws JRException {
90  send(title, new JRBeanCollectionDataSource(collection), parameters, jasperFile, defaultFile,
91  type, certificateStore, contacts, comment, attachments, onFinish);
92  }
93 
94  public static void send(String title, JRDataSource dataSource, Map parameters, String jasperFile,
95  String defaultFile, String type, String certificateStore, Collection<IContact> contacts, String comment,
96  Collection<MailAttachment> attachments, Consumer onFinish) throws JRException {
97  try {
98  if(contacts == null) return;
100  SignedReport sr = new SignedReport();
101  sr.setCertificateStore(certificateStore);
102  if(new File(ElephantContext.getRealPath(jasperFile)).exists()) {
103  sr.setSrc(jasperFile);
104  } else {
105  sr.setSrc(defaultFile);
106  }
107  sr.setParameters(parameters);
108  sr.setDatasource(dataSource);
109  sr.setType(type);
110  File attach = File.createTempFile("attach_", "_mail");
111  FileOutputStream fos = new FileOutputStream(attach);
112  InputStream is = sr.getReport().getStreamData();
113  int read;
114  byte[] bytes = new byte[1024];
115  while ((read = is.read(bytes)) != -1) {
116  fos.write(bytes, 0, read);
117  }
118  is.close();
119  fos.flush();
120  fos.close();
121  MailAttachment ma = new MailAttachment(attach.getAbsolutePath(), title + ".pdf", null);
122  ma.attachment = attach;
123  attachments.add(ma);
125  .setRoot("/financials")
126  .addContacts(contacts)
127  .addMailAttachments(attachments)
128  .onFinish(onFinish)
129  .put("comment", comment)
130  .put("title", title)
131  .put("mas", attachments)
132  .sendTemplate("document", ElephantContext.getSiteName() + ": " + title);
133  } catch (Exception ex) {
134  Logger.getLogger(ReportPrint.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("Sending mails"), ex);
135  }
136  }
137 
138 }
static IMailSender getPool()
static int doModal(String file)
Definition: Modal.java:38
static ModalWindow getModal(String file)
Definition: Modal.java:109
void setReport(Jasperreport report)
static void print(String title, JRDataSource dataSource, Map parameters, String jasperFile, String defaultFile, String type, String certificateStore)
static void send(String title, JRDataSource dataSource, Map parameters, String jasperFile, String defaultFile, String type, String certificateStore, Collection< IContact > contacts, String comment, Collection< MailAttachment > attachments, Consumer onFinish)
static void print(String title, Collection collection, Map parameters, String jasperFile, String defaultFile, String type, String certificateStore)
ReportPrint(String title, String border, boolean closable)
static void send(String title, Collection collection, Map parameters, String jasperFile, String defaultFile, String type, String certificateStore, Collection< IContact > contacts, String comment, Collection< MailAttachment > attachments, Consumer onFinish)
void setCertificateStore(String certificateStore)
T addContacts(Collection< IContact > contacts)
IMailSender setRoot(String root)