BrightSide Workbench Full Report + Source Code
IMailRecipients.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.mail.recipients;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Map;
27 import net.sf.jasperreports.engine.JRDataSource;
28 import net.sf.jasperreports.engine.JRException;
29 import org.turro.action.Contacts;
30 import org.turro.command.Command;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.security.IUser;
34 import org.turro.elephant.util.Images;
35 import org.turro.elephant.util.Messages;
36 import org.turro.i18n.I_;
37 import org.turro.mail.message.MailMessage;
38 import org.turro.mail.provider.MailProvider;
39 import org.turro.mail.provider.MailProviders;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.zkoss.contact.IContactCombobox;
42 import org.turro.zkoss.dialog.SelectionDialog;
43 import org.turro.zkoss.layout.GridLayout;
44 import org.turro.zkoss.print.SignedReport;
45 import org.turro.zul.frame.Framework;
46 import org.zkoss.zk.ui.event.Event;
47 import org.zkoss.zk.ui.event.EventListener;
48 import org.zkoss.zk.ui.event.Events;
49 import org.zkoss.zul.A;
50 import org.zkoss.zul.Row;
51 import org.zkoss.zul.Textbox;
52 
57 public class IMailRecipients extends GridLayout {
58 
59  private Collection<IContact> recipients = new ArrayList<>();
60  private Row editRow;
61  private static String comment;
62 
63  public IMailRecipients(Collection<IContact> recipients) {
64  if(recipients != null) {
65  this.recipients.addAll(recipients);
66  }
67  setColumns("350px,40px");
68  addRecipients();
69  }
70 
71  public static void selectRecipients(Collection<IContact> recipients, Command command) {
72  comment = null;
73  IMailRecipients mr = new IMailRecipients(recipients);
74  mr.setStyle("overflow:auto;");
76  Framework.getCurrent().getPage(),
77  I_.get("Recipients"),
78  mr, "425px", "350px", command);
79  }
80 
81  public static String getComment() {
82  return comment;
83  }
84 
85  public static void send(String title, JRDataSource dataSource, Map parameters, String jasperFile,
86  String defaultFile, String type, String certificateStore, Collection<IContact> contacts) throws JRException {
87  try {
88  if(contacts == null) return;
90  SignedReport sr = new SignedReport();
91  sr.setCertificateStore(certificateStore);
92  if(new File(ElephantContext.getRealPath(jasperFile)).exists()) {
93  sr.setSrc(jasperFile);
94  } else {
95  sr.setSrc(defaultFile);
96  }
97  sr.setParameters(parameters);
98  sr.setDatasource(dataSource);
99  sr.setType(type);
100  File attach = File.createTempFile("attach_", "_mail");
101  FileOutputStream fos = new FileOutputStream(attach);
102  InputStream is = sr.getReport().getStreamData();
103  int read;
104  byte[] bytes = new byte[1024];
105  while ((read = is.read(bytes)) != -1) {
106  fos.write(bytes, 0, read);
107  }
108  is.close();
109  fos.flush();
110  fos.close();
111  StringBuilder sb = new StringBuilder();
112  for(IContact c : contacts) {
113  String email = c.getConnector(IUser.CONNECTOR_EMAIL);
114  if(Contacts.isValidEmail(email)) {
115  MailProvider mp = MailProviders.instance().getProvider(certificateStore);
116  MailMessage mm = MailMessage.of(mp);
117  mm.addTo(c.getEmail(), c.getName());
118  sb.append(email).append(": ").append(c.getName()).append("\n");
120  mm.setSubject(ElephantContext.getSiteName() + ": " + title);
121  mm.setMessage(
122  "<p><i>" + I_.get("Automatically generated") + "</i></p>" +
123  "<p><b>" + ElephantContext.getSiteName() + "</b>: " + I_.get("Document") + "</p>");
124  mm.attach(attach.getAbsolutePath(), ElephantContext.getSiteName() + " " + I_.get("Document"), title + ".pdf");
125  mm.send();
126  }
127  }
128  attach.delete();
129  if(sb.length() > 0) {
130  Messages.info(I_.get("Send documents")).add(I_.get("Sent")).paragraph().add(sb.toString()).show();
131  } else {
132  Messages.error(I_.get("Send documents")).add(I_.get("Nothing was sent")).show();
133  }
134  } catch (Exception ex) {
135  }
136  }
137 
138  private void addRecipients() {
139  for(final IContact c : recipients) {
140  if(c.isWebUser() && Contacts.hasValidEmail(c)) {
141  addRecipient(c);
142  addRow();
143  }
144  }
145  editRow = getCurrentRow();
146  final IContactCombobox cc = new IContactCombobox();
147  cc.setWidth("390px");
148  cc.addEventListener(Events.ON_OK, new EventListener<Event>() {
149  @Override
150  public void onEvent(Event event) throws Exception {
151  event.stopPropagation();
152  IContact c = cc.getObjectValue();
153  if(c != null && c.isWebUser() && Contacts.hasValidEmail(c)) {
154  insertBeforeRow(editRow);
155  addRecipient(c);
156  cc.setObjectValue(null);
157  }
158  }
159  });
160  addSpannedComponent(cc, 2);
161  addRow();
162  final Textbox commentBox = new Textbox();
163  commentBox.setWidth("390px");
164  commentBox.setRows(4);
165  commentBox.setPlaceholder(I_.get("Comment"));
166  commentBox.addEventListener(Events.ON_CHANGE, new EventListener<Event>() {
167  @Override
168  public void onEvent(Event event) throws Exception {
169  event.stopPropagation();
170  comment = commentBox.getValue();
171  }
172  });
173  addSpannedComponent(commentBox, 2);
174  }
175 
176  private void addRecipient(final IContact c) {
177  final Row row = getCurrentRow();
178  row.setValue(c);
179  addCaption(c.getName() + " : " + c.getConnector(IUser.CONNECTOR_EMAIL));
180  A del = new A(null, Images.getImage("edit-delete"));
181  del.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
182  @Override
183  public void onEvent(Event event) throws Exception {
184  row.detach();
185  }
186  });
187  addComponent(del);
188  }
189 
190  public Collection<IContact> getRecipients() {
191  recipients = new ArrayList<>();
192  for(Object o : getRows().getChildren()) {
193  if(o instanceof Row) {
194  if(((Row) o).getValue() instanceof IContact) {
195  recipients.add((IContact) ((Row) o).getValue());
196  }
197  }
198  }
199  return recipients;
200  }
201 
202 }
static boolean hasValidEmail(IContact contact)
Definition: Contacts.java:95
static boolean isValidEmail(String email)
Definition: Contacts.java:99
Messages add(String word)
Definition: Messages.java:50
static String get(String msg)
Definition: I_.java:41
MailProvider getProvider(String name)
static void send(String title, JRDataSource dataSource, Map parameters, String jasperFile, String defaultFile, String type, String certificateStore, Collection< IContact > contacts)
IMailRecipients(Collection< IContact > recipients)
static void selectRecipients(Collection< IContact > recipients, Command command)
GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols)
void setCertificateStore(String certificateStore)
static Framework getCurrent()
Definition: Framework.java:203
static final String CONNECTOR_EMAIL
Definition: IUser.java:27