BrightSide Workbench Full Report + Source Code
GenerateDocument.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.model.document;
20 
21 import java.io.File;
22 import java.io.OutputStream;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import javax.servlet.http.HttpServletResponse;
27 import org.turro.contacts.Contact;
28 import org.turro.elephant.context.Application;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.util.DateFormats;
31 import org.turro.elephant.util.DecimalFormats;
32 import org.turro.financials.entity.Company;
33 import org.turro.financials.entity.Document;
34 import org.turro.financials.entity.DocumentLine;
35 import org.turro.financials.model.business.CompanyWrapper;
36 import org.turro.pdf.GeneratePDF;
37 
42 public class GenerateDocument {
43 
44  private Collection<DocumentLine> lines;
45  private String file;
46  private Map parameters;
47 
48  public void outputDocument(Document doc, boolean showValues, OutputStream out) {
49  lines = doc.getDocumentLines();
50  if(lines != null && lines.size() > 0) {
51  prepareValues(doc, showValues);
52  GeneratePDF.exportToStream(out, ElephantContext.getRealPath(file), lines, parameters);
53  }
54  }
55 
56  public void outputDocument(Document doc, boolean showValues, HttpServletResponse response) {
57  lines = doc.getDocumentLines();
58  if(lines != null && lines.size() > 0) {
59  prepareValues(doc, showValues);
60  GeneratePDF.writeAsResponse(response, ElephantContext.getRealPath(file), lines, parameters);
61  }
62  }
63 
64  private void prepareValues(Document doc, boolean showValues) {
65  DocumentLine sample = lines.iterator().next();
66  int fractionDigits = doc.getCurrency().getDefaultFractionDigits();
67  parameters = new HashMap();
68  Company company = CompanyWrapper.getCompanyFrom(sample);
69  Contact companyContact = (Contact) company.getIContact().getContact();
70  parameters.put("company", company);
71  parameters.put("companyContact", companyContact);
72  parameters.put("dateFormat", DateFormats.getDefaultFormat());
73  parameters.put("currencyFormat", DecimalFormats.getStringFormat(fractionDigits));
74  parameters.put("showDescendants", Boolean.TRUE);
75  Contact contractor = (Contact) doc.getContract().getIContractor().getContact();
76  parameters.put("contractor", contractor);
77  parameters.put("exportFormat", "pdf");
78  parameters.put("REPORT_RESOURCE_BUNDLE", Application.getBundle(
79  "labels",
80  ((Contact) doc.getContract().getIContractor().getContact()).getLocale(),
82  parameters.put("SUBREPORT_DIR", "../_reports/financials/document/");
83  parameters.put("LOGO_IMAGE", ElephantContext.getRealPath("/WEB-INF/_reports/images/doc_logo.png"));
84  file = "/WEB-INF/_reports/financials/document/" + doc.getDocumentDefinition().getName() +
85  (showValues ? "" : "NoValues") + ".jasper";
86  if(!new File(ElephantContext.getRealPath(file)).exists()) {
87  file = "/WEB-INF/_reports/financials/document/Document" +
88  (showValues ? "" : "NoValues") + ".jasper";
89  }
90  }
91 
92 }
static ResourceBundle getBundle(String bundle, Locale locale, String configured)
static String getStringFormat(int fractionDigits)
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
void outputDocument(Document doc, boolean showValues, HttpServletResponse response)
void outputDocument(Document doc, boolean showValues, OutputStream out)
static void exportToStream(OutputStream out, String jasperFile, Collection collection, Map parameters)
static void writeAsResponse(HttpServletResponse response, String jasperFile, Collection collection, Map parameters)