BrightSide Workbench Full Report + Source Code
MailQueue.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.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.apache.commons.mail.EmailException;
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.elephant.security.IUser;
32 import org.turro.mail.message.MailMessageTemplate;
33 import org.turro.mail.provider.MailProviders;
34 import org.turro.mail.queue.QueueManager;
35 import org.turro.marker.ElephantMarker;
36 import org.turro.string.Strings;
37 
42 @ElephantPlugin(label = "queue")
43 public class MailQueue 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()) {
50  QueueManager qm = new QueueManager();
51  for(Assistant a : assistants) {
52  if(qm.contactMayWant(a.contact, category.getIdCategory())) {
53  qm.addToQueue(category, a.contact, subject,
54  StringParser.toHTML((Strings.isBlank(assistants.getSubject()) ? "" : assistants.getSubject() + "\n\n")) +
55  StringParser.toHTML(message),
56  null, pool);
57  }
58  }
59  if(onFinish != null) {
60  try {
61  onFinish.accept(message);
62  } catch (Exception ex) {
63  Logger.getLogger(MailQueue.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
64  }
65  }
66  }
67  }
68 
69  /* Template sending */
70 
71  @Override
72  protected void doSendTemplate(NotificationCategory category, Object entity, String template, String subject) throws EmailException {
73  if(category != null && !assistants.isEmpty()) {
74  QueueManager qm = new QueueManager();
76  for(Assistant a : assistants) {
77  if(qm.contactMayWant(a.contact, category.getIdCategory())) {
79  if(!Strings.isBlank(root)) mmt.setRoot(root);
80  mmt.setSubject(subject);
81  mmt.addTo(a.contact.getConnector(IUser.CONNECTOR_EMAIL), a.contact.getName());
82  ElephantMarker em = new ElephantMarker(constructor, true);
83  em.put("entity", entity);
84  em.put("contact", a.contact);
85  em.put("email", a.contact.getConnector(IUser.CONNECTOR_EMAIL));
86  em.putAll(getAttributes());
87  mmt.setMessage(em, template, a.contact);
88  qm.addToQueue(category, a.contact, mmt, reason, pool);
89  }
90  }
91  if(onFinish != null) {
92  try {
93  onFinish.accept(null);
94  } catch (Exception ex) {
95  Logger.getLogger(MailQueue.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
96  }
97  }
98  }
99  }
100 
101 }
static String toHTML(String value)
MailProvider getProvider(String name)
MailItem addToQueue(NotificationCategory category, IContact contact, MailMessage mmt, String reason, String poolName)
boolean contactMayWant(IContact contact, String idCategory)
void doSend(NotificationCategory category, String subject, String message)
Definition: MailQueue.java:48
void doSendTemplate(NotificationCategory category, Object entity, String template, String subject)
Definition: MailQueue.java:72
Object put(Object key, Object value)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27