BrightSide Workbench Full Report + Source Code
WorksheetReport.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.erp.task;
20 
21 import java.util.Collection;
22 import java.util.HashMap;
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.command.Command;
29 import org.turro.command.Context;
30 import org.turro.contacts.Contact;
31 import org.turro.elephant.util.DateFormats;
32 import org.turro.elephant.util.DecimalFormats;
33 import org.turro.erp.entity.HumanResource;
34 import org.turro.financials.document.DocumentReport;
35 import org.turro.financials.entity.Company;
36 import org.turro.financials.model.business.CompanyWrapper;
37 import org.turro.contacts.util.MailRecipients;
38 import org.turro.elephant.context.Application;
39 import org.turro.elephant.context.ElephantContext;
40 import org.turro.elephant.security.IUser;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.plugin.contacts.MailContact;
43 import org.turro.util.PhraseBuilder;
44 import org.turro.zkoss.print.ReportPrint;
45 import org.turro.zkoss.print.ReportTypeEnum;
46 
51 public class WorksheetReport {
52 
53  private WorkloadSet workloads;
54  private HumanResource operator;
55  private IContact subOperator;
56  private boolean sendToParticipants = false;
57 
58  public void setWorkloads(WorkloadSet workloads) {
59  this.workloads = workloads;
60  }
61 
62  public void setSendToParticipants(boolean sendToParticipants) {
63  this.sendToParticipants = sendToParticipants;
64  }
65 
66  public void setOperator(HumanResource operator) {
67  this.operator = operator;
68  if(operator != null) {
69  subOperator = operator.getOperator() == null ? null : operator.getOperator().getContract().getIContractor();
70  }
71  }
72 
73  public void print() {
74  if(workloads != null && !workloads.isEmpty()) {
75  final WorksheetSet wss = new WorksheetSet(workloads);
76  final Map parameters = new HashMap();
78  Contact companyContact = (Contact) company.getIContact().getContact();
79  parameters.put("company", company);
80  parameters.put("companyContact", companyContact);
81  parameters.put("dateFormat", DateFormats.getDefaultFormat());
82  parameters.put("currencyFormat", DecimalFormats.getStringFormat(2));
83  parameters.put("operator", operator);
84  parameters.put("subOperator", subOperator);
85  parameters.put("exportFormat", ReportTypeEnum.PRINT_PDF.getType());
86  parameters.put("REPORT_RESOURCE_BUNDLE", Application.getApplication().getLabels().getBundle(
87  "labels",
88  ((Contact) operator.getContract().getIContractor().getContact()).getLocale(),
90  //parameters.put("REPORT_LOCALE", );
91  parameters.put("SUBREPORT_DIR", "../_reports/erp/worksheet/");
92  parameters.put("LOGO_IMAGE", ElephantContext.getRealPath("/WEB-INF/_reports/images/doc_logo.png"));
93  final String file = "/WEB-INF/_reports/erp/worksheet/Worksheet_" + Strings.toFile(operator.getName()) + ".jasper";
94  try {
95  if(sendToParticipants) {
96  Collection<IContact> cemails = operator.getContract().getIParticipantsEmail();
97  if(subOperator != null && subOperator.isWebUser()) {
98  cemails.add(subOperator);
99  }
100  String mailString = getMailString(operator.getContract().getIParticipantsEmail());
101  MailRecipients.selectRecipients(cemails, new Command() {
102  @Override
103  public Object execute(Context context) {
104  MailRecipients mr = (MailRecipients) context.get("component");
105  Collection<IContact> emails = mr.getRecipients();
106  if(emails != null) {
107  try {
108  ReportPrint.send(Application.getString("lWorksheet"),
109  wss, parameters, file, "/WEB-INF/_reports/erp/worksheet/Worksheet.jasper",
110  ReportTypeEnum.PRINT_PDF.getType(), "Erp", emails, null, null, null);
111  //Application.logZkoss(LogType.LOG_INFO, FinancialsPU.getObjectPath(document), "sent", mailString);
112  } catch (JRException ex) {
113  Logger.getLogger(WorksheetReport.class.getName()).log(Level.SEVERE, null, ex);
114  }
115  }
116  return null;
117  }
118  });
119  } else {
120  ReportPrint.print(Application.getString("lWorksheet"),
121  wss, parameters, file, "/WEB-INF/_reports/erp/worksheet/Worksheet.jasper",
122  ReportTypeEnum.PRINT_PDF.getType(), "Erp");
123  }
124  } catch (JRException ex) {
125  Logger.getLogger(DocumentReport.class.getName()).log(Level.SEVERE, null, ex);
126  }
127  }
128  }
129 
130  private String getMailString(Collection<IContact> iParticipantsEmail) {
131  PhraseBuilder pb = new PhraseBuilder();
132  for(IContact c : iParticipantsEmail) {
133  if(c.isWebUser()) {
134  pb.addWord(c.getName() + ": " + c.getConnector(IUser.CONNECTOR_EMAIL));
135  pb.addPendingSeparator(",");
136  }
137  }
138  return pb.toString();
139  }
140 
141 }
static void selectRecipients(Collection< IContact > recipients, Command command)
static ResourceBundle getBundle(String bundle, Locale locale, String configured)
static String getString(String key)
static String getStringFormat(int fractionDigits)
void setOperator(HumanResource operator)
void setWorkloads(WorkloadSet workloads)
void setSendToParticipants(boolean sendToParticipants)
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)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27