BrightSide Workbench Full Report + Source Code
ServiceSender.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.contacts.service;
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.Contacts;
25 import org.turro.action.queue.NotificationCategory;
26 import org.turro.action.queue.Notifications;
27 import org.turro.assistant.Assistant;
28 import org.turro.assistant.AssistantSet;
29 import org.turro.contacts.ContactService;
30 import org.turro.contacts.db.ContactsPU;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.ElephantProperties;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.db.WhereClause;
36 import org.turro.entities.Entities;
37 import org.turro.log.SystemLogger;
38 import org.turro.mail.message.MailMessageTemplate;
39 import org.turro.mail.pool.MailMessagePool;
40 import org.turro.mail.provider.MailProviders;
41 import org.turro.mail.queue.AssistantQueueUtils;
42 import org.turro.mail.queue.QueueManager;
43 import org.turro.marker.ElephantMarker;
44 import org.turro.plugin.contacts.IContact;
45 
50 public class ServiceSender {
51 
52  private final static String
53  PROP_CTX = "service-context",
54  KEY_CATEGORY = "service.mail.category",
55  KEY_POOL = "service.mail.pool";
56 
57  private final ContactService entity;
58  private final AssistantSet assistants;
59  private final String category, mailPool;
60  private final boolean useQueue;
61 
62  public ServiceSender(ContactService entity, AssistantSet assistants, boolean useQueue) {
63  this.entity = entity;
64  this.assistants = assistants;
65  this.useQueue = useQueue;
66  this.category = ElephantProperties.getContextProperty(PROP_CTX, KEY_CATEGORY);
67  this.mailPool = ElephantProperties.getContextProperty(PROP_CTX, KEY_POOL);
68  }
69 
70  public void send() {
71  sendMail(null);
72  }
73 
74  public void send(IContact contact) {
75  sendMail(contact);
76  }
77 
78  private void sendMail(IContact contact) {
80  QueueManager qm = useQueue ? new QueueManager() : null;
81  MailMessagePool pool = useQueue ? null : MailProviders.instance().getPool(Application.getApplication().getConstructor(), mailPool);
82  if(contact != null) {
83  createMail(new Assistant(contact, entity), nc, qm, pool);
84  } else {
85  for(Assistant assistant : assistants) {
86  createMail(assistant, nc, qm, pool);
87  }
88  increaseSent(entity);
89  }
90  if(pool != null) {
91  pool.sendPool();
92  }
93  SystemLogger.info().entity(entity).comment("sent").log();
94  }
95 
96  private void createMail(Assistant assistant, NotificationCategory nc, QueueManager qm, MailMessagePool pool) {
97  try {
98  if(Contacts.isValidEmail(assistant.email)) {
99  MailMessageTemplate mmt = MailMessageTemplate.of(MailProviders.instance().getProvider(mailPool));
100  mmt.setSubject(entity.getTitle());
101  mmt.addTo(assistant.email, assistant.name);
102  ElephantMarker em = new ElephantMarker(Application.getApplication().getConstructor(), true);
103  em.put("service", entity);
104  em.put("contact", assistant.contact);
105  em.put("email", assistant.email);
106  em.put("notifier", this);
107  mmt.setMessage(em, "service", assistant.contact);
108  if(qm != null) {
109  qm.addToQueue(nc, assistant.contact, mmt, null, mailPool);
110  }
111  if(pool != null) {
112  pool.addToPool(mmt);
113  }
114  }
115  } catch (EmailException ex) {
116  Logger.getLogger(ServiceSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
117  }
118  }
119 
120  public String createRef(IConstructor constructor, ContactService service, IContact contact) throws Exception {
121  return createRef(constructor, service.getTimesSent() >=1 ?
122  Entities.getController(service).getEntityUrl() :
123  Entities.getController(service).getMemberUrl());
124  }
125 
126  public String createRef(IConstructor constructor, String redir) throws Exception {
127  return "{liveref:" + redir + "}";
128  }
129 
130  public static AssistantSet getAsAssistants(ContactService service) {
131  AssistantSet assistants = new AssistantSet();
132  assistants.addContact(service.getResponsible().getIContact(), service);
133  assistants.addContacts(Contacts.getBySyndication("contacts_admin"));
134  AssistantQueueUtils.addFromCategory(ElephantProperties.getContextProperty(PROP_CTX, KEY_CATEGORY), assistants);
135  return assistants;
136  }
137 
138  private void increaseSent(ContactService service) {
139  WhereClause wc = new WhereClause();
140  wc.addClause("update ContactService");
141  wc.addClause("set timesSent = timesSent + 1");
142  wc.addClause("where id = :id");
143  wc.addNamedValue("id", service.getId());
144  new ContactsPU().executeUpdate(wc);
145  }
146 
147 }
static List< IContact > getBySyndication(String syndication)
Definition: Contacts.java:130
static NotificationCategory getCategory(String id)
void addContacts(Collection< IContact > contacts)
void addContact(IContact contact, Object relationEntity)
ServiceSender(ContactService entity, AssistantSet assistants, boolean useQueue)
String createRef(IConstructor constructor, ContactService service, IContact contact)
String createRef(IConstructor constructor, String redir)
static AssistantSet getAsAssistants(ContactService service)
static String getContextProperty(String context, String property)
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463
MailMessagePool getPool(IConstructor constructor, String name)
static void addFromCategory(String idCategory, AssistantSet assistants)