BrightSide Workbench Full Report + Source Code
SendAttachments.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.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.function.Consumer;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.Strings;
29 import org.apache.commons.mail.EmailException;
30 import org.turro.action.Contacts;
31 import org.turro.action.MailAttachment;
32 import org.turro.action.MailSenders;
33 import org.turro.command.Command;
34 import org.turro.command.Context;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.security.IUser;
37 import org.turro.elephant.util.Messages;
38 import org.turro.i18n.I_;
39 import org.turro.log.SystemLogType;
40 import org.turro.log.SystemLogger;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.util.PhraseBuilder;
43 
48 public abstract class SendAttachments {
49 
50  private final ArrayList<MailAttachment> attachments = new ArrayList<>();
51 
52  private final String title;
53 
54  public SendAttachments(String title) {
55  this.title = title;
56  }
57 
58  public void addAttachment(Object entity, String path, String name) {
59  attachments.add(new MailAttachment(path, name, entity));
60  }
61 
62  public void sendAttachments(Collection<IContact> contacts) throws IOException {
63  if(attachments.isEmpty()) {
64  return;
65  }
66  IMailRecipients.selectRecipients(contacts, new Command() {
67  @Override
68  public Object execute(Context context) {
69  IMailRecipients mr = (IMailRecipients) context.get("component");
70  Collection<IContact> contacts = mr.getRecipients();
71  if(contacts != null) {
72  String mailString = getMailString(contacts);
73  if(!Strings.isBlank(mailString)) {
74  for(MailAttachment ma : attachments) {
75  try {
76  ma.attachment = File.createTempFile("attach_", "_mail");
77  fillAttachment(ma.attachment, ma.entity);
78  } catch (IOException ex) {
79  Logger.getLogger(SendAttachments.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(mailString), ex);
80  }
81  }
82  sendAttachments(contacts, attachments, IMailRecipients.getComment(), mailString, (m) -> {
83  for(MailAttachment ma : attachments) {
84  ma.attachment.delete();
85  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, ma.path, "sent", mailString);
86  }
87  });
88  }
89  }
90  return null;
91  }
92  });
93  }
94 
95  protected abstract void fillAttachment(File attachment, Object entity);
96 
97  private String getMailString(Collection<IContact> iParticipantsEmail) {
98  PhraseBuilder pb = new PhraseBuilder();
99  for(IContact c : iParticipantsEmail) {
100  String email = c.getConnector(IUser.CONNECTOR_EMAIL);
101  if(Contacts.isValidEmail(email)) {
102  pb.addWord(c.getName() + ": " + email);
103  pb.addPendingSeparator(",");
104  }
105  }
106  return pb.toString();
107  }
108 
109  private void sendAttachments(Collection<IContact> contacts, ArrayList<MailAttachment> attachments,
110  String comment, String mailString, Consumer command) {
111  try {
112  MailSenders.getPool()
113  .addContacts(contacts)
114  .addMailAttachments(attachments)
115  .onFinish(command)
116  .setRoot("/attachments")
117  .put("comment", comment)
118  .put("mas", attachments)
119  .sendTemplate("send", ElephantContext.getSiteName() + ": " + title);
120  Messages.info(I_.get("Send documents")).add(I_.get("Sent")).paragraph().add(mailString.replaceAll("\\,", "\n")).show();
121  } catch (EmailException ex) {
122  Logger.getLogger(SendAttachments.class.getName()).log(Level.SEVERE, null, ex);
123  }
124  }
125 
126 }
static boolean isValidEmail(String email)
Definition: Contacts.java:99
static void selectRecipients(Collection< IContact > recipients, Command command)
abstract void fillAttachment(File attachment, Object entity)
void addAttachment(Object entity, String path, String name)
void sendAttachments(Collection< IContact > contacts)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27