BrightSide Workbench Full Report + Source Code
elephant-mail/src/main/java/org/turro/mail/sender/MailPool.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.sender;
20 
21 import java.io.File;
22 import org.apache.commons.mail.EmailException;
23 import org.turro.action.MailAttachment;
24 import org.turro.action.queue.NotificationCategory;
25 import org.turro.annotation.ElephantPlugin;
26 import org.turro.assistant.Assistant;
27 import org.turro.elephant.context.Application;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.impl.util.StringParser;
31 import org.turro.mail.message.MailMessage;
32 import org.turro.mail.message.MailMessageTemplate;
33 import org.turro.mail.pool.MailMessagePool;
34 import org.turro.mail.provider.MailProviders;
35 import org.turro.marker.ElephantMarker;
36 import org.turro.string.Strings;
37 
42 @ElephantPlugin(label = "pool")
43 public class MailPool extends AbstractMailSender {
44 
45  /* Direct sending */
46 
47  @Override
48  protected void doSend(NotificationCategory category, String subject, String message) {
49  if(category != null && !assistants.isEmpty()) {
51  MailMessagePool mp = MailProviders.instance().getPool(constructor, Strings.isBlank(pool, MailProviders.GENERIC));
52  for(Assistant a : assistants) {
53  MailMessage mmt = mp.addToPoolTemplate(getFromString(), a.email, null, subject + " : " + ElephantContext.getSiteName(),
54  StringParser.toHTML((Strings.isBlank(assistants.getSubject()) ? "" : assistants.getSubject() + "\n\n")) +
55  StringParser.toHTML(message),
56  null, a.name, null);
57  for(File file : attachments) {
58  mmt.attach(file.getAbsolutePath(), file.getName(), file.getName());
59  }
60  for(MailAttachment ma : mailAttachments) {
61  mmt.attach(ma.attachment.getAbsolutePath(), ma.name, ma.name);
62  }
63  }
64  mp.sendPool(onFinish);
65  }
66  }
67 
68  /* Template sending */
69 
70  @Override
71  protected void doSendTemplate(NotificationCategory category, Object entity, String template, String subject) throws EmailException {
72  if(category != null && !assistants.isEmpty()) {
74  MailMessagePool mp = MailProviders.instance().getPool(constructor, Strings.isBlank(pool, MailProviders.GENERIC));
75  for(Assistant a : assistants) {
77  if(!Strings.isBlank(root)) mmt.setRoot(root);
78  mmt.setSubject(subject);
79  mmt.addTo(a.email, a.name);
80  if(isFromValid()) {
81  mmt.setFrom(from.getEmail(), from.getName());
82  }
83  ElephantMarker em = new ElephantMarker(constructor, true);
84  if(entity != null) em.put("entity", entity);
85  if(a.contact != null) em.put("contact", a.contact);
86  em.put("email", a.email);
87  em.put("name", a.name);
88  em.putAll(getAttributes());
89  mmt.setMessage(em, template, a.email, a.name);
90  for(File file : attachments) {
91  mmt.attach(file.getAbsolutePath(), file.getName(), file.getName());
92  }
93  for(MailAttachment ma : mailAttachments) {
94  mmt.attach(ma.attachment.getAbsolutePath(), ma.name, ma.name);
95  }
96  mp.addToPool(mmt);
97  }
98  mp.sendPool(onFinish);
99  }
100  }
101 
102 }
static String toHTML(String value)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessage addToPoolTemplate(String from, String to, String cc, String subject, String message, String template, String name, String login)
MailMessagePool getPool(IConstructor constructor, String name)
void doSendTemplate(NotificationCategory category, Object entity, String template, String subject)
void doSend(NotificationCategory category, String subject, String message)
Object put(Object key, Object value)