BrightSide Workbench Full Report + Source Code
DocumentReport.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.document;
19 
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import net.sf.jasperreports.engine.JRException;
27 import org.turro.string.Strings;
28 import org.turro.action.Contacts;
29 import org.turro.attach.search.AttachCollection;
30 import org.turro.auth.Authentication;
31 import org.turro.command.Command;
32 import org.turro.command.Context;
33 import org.turro.contacts.Contact;
34 import org.turro.contacts.util.MailRecipients;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.impl.util.StringParser;
38 import org.turro.elephant.security.IUser;
39 import org.turro.elephant.util.DateFormats;
40 import org.turro.elephant.util.DecimalFormats;
41 import org.turro.elephant.util.Messages;
42 import org.turro.financials.db.FinancialsPU;
43 import org.turro.financials.entity.Company;
44 import org.turro.financials.entity.Document;
45 import org.turro.financials.entity.DocumentLine;
46 import org.turro.financials.model.business.CompanyWrapper;
47 import org.turro.i18n.I_;
48 import org.turro.log.SystemLogType;
49 import org.turro.log.SystemLogger;
50 import org.turro.plugin.contacts.IContact;
51 import org.turro.util.PhraseBuilder;
52 import org.turro.zkoss.print.ReportPrint;
53 import org.turro.zkoss.print.ReportToolbarbutton;
54 
59 public class DocumentReport extends ReportToolbarbutton {
60 
61  private Document document;
62  private DocumentLinesGrid grid;
63  private boolean sendToParticipants = false, sendToCurrent = false, askBeforeSend = true;
64 
65  public void setDocument(Document document) {
66  this.document = document;
67  }
68 
69  public void setGrid(DocumentLinesGrid grid) {
70  this.grid = grid;
71  }
72 
73  public void setSendToParticipants(boolean sendToParticipants) {
74  this.sendToParticipants = sendToParticipants;
75  }
76 
77  public void setSendToCurrent(boolean sendToCurrent) {
78  this.sendToCurrent = sendToCurrent;
79  }
80 
81  public void setAskBeforeSend(boolean askBeforeSend) {
82  this.askBeforeSend = askBeforeSend;
83  }
84 
85  @Override
86  protected void print(final String type) {
87  final Collection<DocumentLine> lines = getLines();
88  final Document doc = getDocument();
89  if(lines != null && lines.size() > 0) {
90  DocumentLine sample = lines.iterator().next();
91  int fractionDigits = doc.getCurrency().getDefaultFractionDigits();
92  final Map parameters = new HashMap();
93  Company company = CompanyWrapper.getCompanyFrom(sample);
94  Contact companyContact = (Contact) company.getIContact().getContact();
95  parameters.put("company", company);
96  parameters.put("companyContact", companyContact);
97  parameters.put("dateFormat", DateFormats.getDefaultFormat());
98  parameters.put("currencyFormat", DecimalFormats.getStringFormat(fractionDigits));
99  parameters.put("showDescendants", Boolean.TRUE);
100  Contact contractor = (Contact) doc.getContract().getIContractor().getContact();
101  parameters.put("contractor", contractor);
102  parameters.put("exportFormat", type);
103  parameters.put("REPORT_RESOURCE_BUNDLE", Application.getBundle(
104  "labels",
105  ((Contact) doc.getContract().getIContractor().getContact()).getLocale(),
107 // parameters.put("REPORT_RESOURCE_BUNDLE", LabelsBundle.getBundle(
108 // ((Contact) doc.getContract().getIContractor().getContact()).getLocale()));
109  //parameters.put("REPORT_LOCALE", );
110  parameters.put("SUBREPORT_DIR", "../_reports/financials/document/");
111  parameters.put("LOGO_IMAGE", ElephantContext.getRealPath("/WEB-INF/_reports/images/doc_logo.png"));
112  final String file = "/WEB-INF/_reports/financials/document/" + doc.getDocumentDefinition().getName() +
113  (showValues ? "" : "NoValues") + ".jasper";
114  try {
115  if(askBeforeSend) {
116  if(sendToParticipants) {
118  @Override
119  public Object execute(Context context) {
120  MailRecipients mr = (MailRecipients) context.get("component");
121  Collection<IContact> emails = mr.getRecipients();
122  doPrint(emails, doc, lines, parameters, file, type);
123  return null;
124  }
125  });
126  }
127  if(sendToCurrent) {
128  MailRecipients.selectRecipients(List.of(Authentication.getIContact()), new Command() {
129  @Override
130  public Object execute(Context context) {
131  MailRecipients mr = (MailRecipients) context.get("component");
132  Collection<IContact> emails = mr.getRecipients();
133  doPrint(emails, doc, lines, parameters, file, type);
134  return null;
135  }
136  });
137  }
138  } else {
139  if(sendToParticipants) {
140  Collection<IContact> emails = doc.getContract().getIParticipantsEmail();
141  doPrint(emails, doc, lines, parameters, file, type);
142  }
143  if(sendToCurrent && Authentication.isAdministrator()) {
144  Collection<IContact> emails = List.of(Authentication.getIContact());
145  doPrint(emails, doc, lines, parameters, file, type);
146  }
147  }
148  if(!(sendToParticipants || sendToCurrent)) {
150  lines, parameters, file, "/WEB-INF/_reports/financials/document/Document" +
151  (showValues ? "" : "NoValues") + ".jasper",
152  type, "Financials");
153  }
154  } catch (JRException ex) {
155  Logger.getLogger(DocumentReport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
156  }
157  }
158  }
159 
160  private void doPrint(Collection<IContact> emails, final Document doc, final Collection<DocumentLine> lines, final Map parameters, final String file, final String type) {
161  if(emails != null) {
162  String mailString = getMailString(emails);
163  if(!Strings.isBlank(mailString)) {
164  try {
169  lines, parameters, file, "/WEB-INF/_reports/financials/document/Document" +
170  (showValues ? "" : "NoValues") + ".jasper",
171  type, "Financials", emails, StringParser.toHTML(doc.getFullNotes()),
172  amc.getAttachments(), (m) -> {
173  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, doc, "sent", mailString);
174  amc.removeAttachments();
175  });
176  if(askBeforeSend) {
177  Messages.info(I_.get("Send documents")).add(I_.get("Sent")).paragraph().add(mailString.replaceAll("\\,", "\n")).show();
178  }
179  } catch (JRException ex) {
180  Logger.getLogger(DocumentReport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
181  }
182  }
183  }
184  }
185 
186  private Collection<DocumentLine> getLines() {
187  if(grid != null) {
188  return grid.getValues();
189  } else if(document != null) {
190  return document.getDocumentLines();
191  }
192  return null;
193  }
194 
195  private Document getDocument() {
196  if(grid != null) {
197  return grid.getDocument();
198  } else {
199  return document;
200  }
201  }
202 
203  private String getMailString(Collection<IContact> iParticipantsEmail) {
204  PhraseBuilder pb = new PhraseBuilder();
205  for(IContact c : iParticipantsEmail) {
206  String email = c.getConnector(IUser.CONNECTOR_EMAIL);
207  if(Contacts.isValidEmail(email)) {
208  pb.addWord(c.getName() + ": " + email);
209  pb.addPendingSeparator(",");
210  }
211  }
212  return pb.toString();
213  }
214 
215 }
void loadAttachments(String path, boolean publicOnly)
Collection< MailAttachment > getAttachments()
static void selectRecipients(Collection< IContact > recipients, Command command)
static ResourceBundle getBundle(String bundle, Locale locale, String configured)
static String toHTML(String value)
static String getStringFormat(int fractionDigits)
Messages add(String word)
Definition: Messages.java:50
static String getObjectPath(Object object)
void setSendToCurrent(boolean sendToCurrent)
void setSendToParticipants(boolean sendToParticipants)
void setAskBeforeSend(boolean askBeforeSend)
Collection< IContact > getIParticipantsEmail()
Definition: Contract.java:473
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
static String get(String msg)
Definition: I_.java:41
static void print(String title, Collection collection, Map parameters, String jasperFile, String defaultFile, String type, String certificateStore)
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)