BrightSide Workbench Full Report + Source Code
NewsletterSendable.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.entities;
20 
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.apache.commons.mail.EmailException;
25 import org.turro.assistant.Assistant;
26 import org.turro.assistant.AssistantSet;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.HeadlessApplication;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.entities.db.Sendable;
31 import org.turro.mail.message.MailMessageTemplate;
32 import org.turro.mail.pool.MailMessagePool;
33 import org.turro.mail.provider.MailProviders;
34 import org.turro.marker.ElephantMarker;
35 import org.turro.newsletter.NewsletterTemplate;
36 import org.turro.publication.entity.Newsletter;
37 import org.turro.sendable.AbstractSendable;
38 import org.turro.sendable.Sendables;
39 
44 public class NewsletterSendable extends AbstractSendable<Newsletter> {
45 
46  public NewsletterSendable(Newsletter entity) {
47  super(entity);
48  }
49 
50  public NewsletterSendable(String entityPath) {
51  super(entityPath);
52  }
53 
54  @Override
55  protected boolean doSend(Sendable sendable, Newsletter entity, IConstructor constructor, AssistantSet assistants, boolean checkSent) {
56  if(ElephantMarker.existsTemplate(constructor, true, "envelope", "header")) {
57  MailMessagePool pool = MailProviders.instance().getPool(constructor, "Publication");
58  if(pool != null) {
59  for(Assistant assistant : assistants) {
60  try (NewsletterTemplate nt = new NewsletterTemplate(entity)) {
61  MailMessageTemplate mmt = nt.getMailMessage(pool, assistant.getContact());
62  pool.addToPool(mmt);
63  pool.sendPool();
64  if(checkSent) Sendables.delivered(sendable.getEntityPath(), assistant.contact.getId());
65  } catch (IOException | EmailException ex) {
66  Logger.getLogger(NewsletterSendable.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
67  }
68  }
69  entity.resetGlobalContext();
70  }
71  }
72  return checkSent;
73  }
74 
75  @Override
76  protected void addAssistants(AssistantSet as, Newsletter entity) {
77  // do nothing
78  }
79 
80 }
void addAssistants(AssistantSet as, Newsletter entity)
boolean doSend(Sendable sendable, Newsletter entity, IConstructor constructor, AssistantSet assistants, boolean checkSent)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessagePool getPool(IConstructor constructor, String name)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
static void delivered(String entityPath, String contactId)
Definition: Sendables.java:113