BrightSide Workbench Full Report + Source Code
AbstractMailSender.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 java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.function.Consumer;
28 import java.util.function.Supplier;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import org.apache.commons.mail.EmailException;
32 import org.turro.action.Contacts;
33 import org.turro.action.IMailSender;
34 import org.turro.action.MailAttachment;
35 import org.turro.action.queue.NotificationCategory;
36 import org.turro.action.queue.Notifications;
37 import org.turro.assistant.Assistant;
38 import org.turro.assistant.AssistantSet;
39 import org.turro.assistant.Assistants;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.entities.Entities;
42 import org.turro.mail.message.MailUtils;
43 import org.turro.mail.queue.GenericElephantNotification;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.security.SecurityGroup;
46 
51 public abstract class AbstractMailSender implements IMailSender {
52 
53  public final AssistantSet assistants = new AssistantSet();
54 
55  protected String defaultCategory = GenericElephantNotification.SYSTEM_NOTIFICATION;
56  protected String reason, pool, root;
57  protected Set<File> attachments = new HashSet<>();
58  protected Set<MailAttachment> mailAttachments = new HashSet<>();
59  protected Consumer onStart, onCancel, onBuild, onFinish;
60  protected IContact from;
61 
62  @Override
63  public IMailSender setCategory(String idCategory) {
64  defaultCategory = idCategory;
65  return this;
66  }
67 
68  @Override
69  public IMailSender setReason(String reason) {
70  this.reason = reason;
71  return this;
72  }
73 
74  @Override
75  public IMailSender setPool(String pool) {
76  this.pool = pool;
77  return this;
78  }
79 
80  @Override
82  this.from = from;
83  return this;
84  }
85 
86  protected boolean isFromValid() {
87  return from != null && from.isWebUser();
88  }
89 
90  protected String getFromString() {
91  return isFromValid() ? from.getEmail() : null;
92  }
93 
100  @Override
101  public IMailSender setRoot(String root) {
102  this.root = root;
103  return this;
104  }
105 
106  @Override
108  addBySyndication(SecurityGroup.BRIGHTSIDE_ADMIN);
109  return this;
110  }
111 
112  @Override
113  public IMailSender addBySyndication(String syndication) {
115  return this;
116  }
117 
118  @Override
119  public IMailSender addByRole(String role) {
121  return this;
122  }
123 
124  @Override
125  public IMailSender addByEntity(Object entity, Object data) {
126  addByEntity(entity, true, data);
127  return this;
128  }
129 
130  @Override
131  public IMailSender addByEntity(Object entity, boolean deep, Object data) {
132  addByEntity(Entities.getController(entity).getPath(), deep, data);
133  return this;
134  }
135 
136  @Override
137  public IMailSender addByEntity(String path, Object data) {
138  addByEntity(path, true, data);
139  return this;
140  }
141 
142  @Override
143  public IMailSender addByEntity(String path, boolean deep, Object data) {
144  Assistants.addAssistants(path, true, assistants, data);
145  return this;
146  }
147 
148  @Override
149  public IMailSender addContact(IContact contact) {
150  assistants.addContact(contact, null);
151  return this;
152  }
153 
154  @Override
155  public IMailSender addContacts(Collection<IContact> contacts) {
156  assistants.addContacts(contacts);
157  return this;
158  }
159 
160  @Override
161  public IMailSender addAssistant(Assistant assistant) {
162  this.assistants.add(assistant);
163  return this;
164  }
165 
166  @Override
168  for(Assistant assistant : assistants) {
169  this.assistants.add(assistant);
170  }
171  return this;
172  }
173 
174  @Override
175  public IMailSender addUser(String name, String email) {
176  assistants.addUser(name, email, null);
177  return this;
178  }
179 
180 
181  @Override
182  public IMailSender addAttachment(File file) {
183  attachments.add(file);
184  return this;
185  }
186 
187  @Override
188  public IMailSender addAttachments(Collection<File> files) {
189  attachments.addAll(files);
190  return this;
191  }
192 
193  @Override
194  public IMailSender addMailAttachments(Collection<MailAttachment> mas) {
195  mailAttachments.addAll(mas);
196  return this;
197  }
198 
199  @Override
200  public IMailSender onStart(Consumer command) {
201  onStart = command;
202  return this;
203  }
204 
205  @Override
206  public IMailSender onCancel(Consumer command) {
207  onCancel = command;
208  return this;
209  }
210 
211  @Override
212  public IMailSender onBuild(Consumer command) {
213  onBuild = command;
214  return this;
215  }
216 
217  @Override
218  public IMailSender onFinish(Consumer command) {
219  onFinish = command;
220  return this;
221  }
222 
223  @Override
225  return assistants;
226  }
227 
228  @Override
229  public void removeAttachments() {
230  attachments.forEach(file -> {
231  file.delete();
232  });
233  }
234 
235  /* Attributes */
236 
237  private final Map attributes = new HashMap();
238 
239  @Override
240  public IMailSender putIf(Object key, Supplier<Boolean> condition, Supplier value) {
241  if(condition.get()) put(key, value.get());
242  return this;
243  }
244 
245  @Override
246  public IMailSender put(Object key, Object value) {
247  if(key != null && value != null) attributes.put(key, value);
248  return this;
249  }
250 
251  @Override
252  public IMailSender putAll(Map attributes) {
253  this.attributes.putAll(attributes);
254  return this;
255  }
256 
257  protected Map getAttributes() {
258  return attributes;
259  }
260 
261  /* Direct sending */
262 
263  @Override
264  public void send(String subject, String message) {
266  send(category, subject, message);
267  }
268 
269  @Override
270  public void send(NotificationCategory category, String subject, String message) {
271  doSend(category, subject, message);
272  }
273 
274  protected abstract void doSend(NotificationCategory category, String subject, String message);
275 
276  /* Template sending */
277 
278  @Override
279  public void sendTemplate(String template, String subject) throws EmailException {
281  sendTemplate(category, null, template, subject);
282  }
283 
284  @Override
285  public void sendTemplate(Object entity, String template, String subject) throws EmailException {
287  sendTemplate(category, entity, template, subject);
288  }
289 
290  @Override
291  public void sendTemplate(NotificationCategory category, Object entity, String template, String subject) throws EmailException {
292  doSendTemplate(category, entity, template, subject);
293  }
294 
295  @Override
296  public void silentSendTemplate(String template, String subject) {
297  try {
298  sendTemplate(template, subject);
299  } catch (EmailException ex) {
300  Logger.getLogger(AbstractMailSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
301  }
302  }
303 
304  @Override
305  public void silentSendTemplate(Object entity, String template, String subject) {
306  try {
307  sendTemplate(entity, template, subject);
308  } catch (EmailException ex) {
309  Logger.getLogger(AbstractMailSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
310  }
311  }
312 
313  @Override
314  public void silentSendTemplate(NotificationCategory category, Object entity, String template, String subject) {
315  try {
316  sendTemplate(category, entity, template, subject);
317  } catch (EmailException ex) {
318  Logger.getLogger(AbstractMailSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
319  }
320  }
321 
322  protected abstract void doSendTemplate(NotificationCategory category, Object entity, String template,
323  String subject) throws EmailException;
324 
325  /* Utils */
326 
327  @Override
328  public String processMacros(IContact contact, String text, boolean web) {
329  return web ? MailUtils.processMacrosForWeb(contact, text) :
330  MailUtils.processMacros(contact, text);
331  }
332 
333 }
static List< IContact > getByRole(String role)
Definition: Contacts.java:134
static List< IContact > getBySyndication(String syndication)
Definition: Contacts.java:130
static NotificationCategory getCategory(String id)
void addUser(String name, String email, Object relationEntity)
void addContacts(Collection< IContact > contacts)
void addContact(IContact contact, Object relationEntity)
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
static IElephantEntity getController(String path)
Definition: Entities.java:78
void silentSendTemplate(NotificationCategory category, Object entity, String template, String subject)
IMailSender addByEntity(String path, boolean deep, Object data)
void send(NotificationCategory category, String subject, String message)
abstract void doSend(NotificationCategory category, String subject, String message)
abstract void doSendTemplate(NotificationCategory category, Object entity, String template, String subject)
IMailSender setCategory(String idCategory)
IMailSender put(Object key, Object value)
IMailSender addAssistants(AssistantSet assistants)
void silentSendTemplate(String template, String subject)
String processMacros(IContact contact, String text, boolean web)
IMailSender addByEntity(Object entity, boolean deep, Object data)
void silentSendTemplate(Object entity, String template, String subject)
void send(String subject, String message)
IMailSender putIf(Object key, Supplier< Boolean > condition, Supplier value)
IMailSender addContacts(Collection< IContact > contacts)
void sendTemplate(NotificationCategory category, Object entity, String template, String subject)
IMailSender addAssistant(Assistant assistant)
IMailSender addMailAttachments(Collection< MailAttachment > mas)
IMailSender addByEntity(Object entity, Object data)
IMailSender addUser(String name, String email)
IMailSender addBySyndication(String syndication)
void sendTemplate(String template, String subject)
IMailSender addByEntity(String path, Object data)
IMailSender addContact(IContact contact)
IMailSender addAttachments(Collection< File > files)
void sendTemplate(Object entity, String template, String subject)
IMailSender put(Object key, Object value)
IMailSender putAll(Map attributes)