BrightSide Workbench Full Report + Source Code
NewsletterSendToSubscribers.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.newsletter.mail;
20 
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.apache.commons.mail.EmailException;
26 import org.turro.action.Contacts;
27 import org.turro.action.IAgreements;
28 import org.turro.action.Plugins;
29 import org.turro.action.queue.NotificationCategory;
30 import org.turro.action.queue.Notifications;
31 import org.turro.action.queue.QueuePeriod;
32 import org.turro.assistant.Assistant;
33 import org.turro.assistant.AssistantSet;
34 import org.turro.elephant.context.Application;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.entities.db.QueueOptions;
37 import org.turro.mail.message.MailMessageTemplate;
38 import org.turro.mail.pool.MailMessagePool;
39 import org.turro.mail.provider.MailProviders;
40 import org.turro.mail.queue.QueueManager;
41 import org.turro.mail.queue.QueuedSender;
42 import org.turro.newsletter.NewsletterTemplate;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.publication.entity.Newsletter;
45 
51 
52  private final Newsletter newsletter;
53 
55  this.newsletter = newsletter;
56  }
57 
58  @Override
59  protected void doSend() {
60  AssistantSet assistants = getAssistants();
62  if(pool != null) {
63  IAgreements agreements = Plugins.loadImplementation(IAgreements.class, "agreements");
65  for(Assistant assistant : assistants) {
66  if(assistant.getContact().acceptsLang(newsletter.getLang())) {
67  agreements.setContact(assistant.getContact());
68  if(agreements.canSendEmails(nc)) {
69  try (NewsletterTemplate nt = new NewsletterTemplate(newsletter)) {
70  MailMessageTemplate mmt = nt.getMailMessage(pool, assistant.getContact());
71  if(newsletter.trigger(assistant.getContact())) {
72  pool.addToPool(mmt);
73  pool.sendPool();
74  pause();
75  }
76  } catch (IOException | EmailException ex) {
77  Logger.getLogger(NewsletterSendToSubscribers.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
78  }
79  }
80  }
81  }
82  newsletter.resetGlobalContext();
83  }
84  }
85 
86  private AssistantSet getAssistants() {
87  QueueManager qm = new QueueManager();
88  Collection<QueueOptions> subs = qm.getSubcribers(newsletter.getIdCategory());
89  AssistantSet assistants = new AssistantSet();
90  for(QueueOptions qo : subs) {
91  if(qo.getIdContact() != null && !QueuePeriod.DONT_SEND.equals(qo.getPeriod())) {
92  IContact c = Contacts.getContactById(qo.getIdContact());
93  if(c.isWebUser()) {
94  assistants.addContact(c, null);
95  }
96  }
97  }
98  return assistants;
99  }
100 
101 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static NotificationCategory getCategory(String id)
void addContact(IContact contact, Object relationEntity)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessagePool getPool(IConstructor constructor, String name)
Collection< QueueOptions > getSubcribers(String idCategory)
boolean trigger(IContact contact)
boolean canSendEmails(NotificationCategory nc)
void setContact(IContact contact)