BrightSide Workbench Full Report + Source Code
ContactQueueSender.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.queue;
20 
21 import java.util.Date;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.apache.commons.mail.EmailException;
26 import org.turro.action.IAgreements;
27 import org.turro.action.Plugins;
28 import org.turro.action.queue.NotificationCategory;
29 import org.turro.action.queue.Notifications;
30 import org.turro.action.queue.QueuePeriod;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.context.IConstructor;
33 import org.turro.elephant.entities.db.MailItem;
34 import org.turro.elephant.security.IUser;
35 import org.turro.i18n.I_;
36 import org.turro.mail.message.MailMessageTemplate;
37 import org.turro.mail.pool.MailMessagePool;
38 import org.turro.mail.provider.MailProviders;
39 import org.turro.marker.ElephantMarker;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.util.CompareUtil;
42 
47 public class ContactQueueSender extends QueuedSender {
48 
49  private final IConstructor constructor;
50  private final QueueManager manager;
51  private final QueuePeriod period;
52  private final CategoryMap cm;
53 
54  public ContactQueueSender(IConstructor constructor, QueueManager manager, QueuePeriod period, CategoryMap cm) {
55  this.constructor = constructor;
56  this.manager = manager;
57  this.period = period;
58  this.cm = cm;
59  }
60 
61  @Override
62  protected void doSend() {
63  IAgreements agreements = Plugins.loadImplementation(IAgreements.class, "agreements");
64  for(String category : cm.keySet()) {
66  agreements.setContact(cm.getIContact());
67  if(agreements.canSendEmails(nc)) {
68  try {
69  IContact contact = cm.getIContact();
70  MailItemSet mis = cm.get(category);
71  MailMessagePool pool = MailProviders.instance().getPool(constructor, Strings.isBlank(mis.getPoolName(), MailProviders.GENERIC));
72  if(pool != null) {
74  mmt.setRoot("/queue");
75  mmt.addTo(contact.getConnector(IUser.CONNECTOR_EMAIL), contact.getName());
76  mmt.setSubject(getSubject(mis.getCategory()));
77  ElephantMarker em = new ElephantMarker(constructor, true);
78  em.put("contact", contact);
79  em.put("messages", mis);
80  em.put("notifier", this);
81  mmt.setMessage(em, "messages", contact);
82  pool.addToPool(mmt);
83  pool.sendPool();
84  pause();
85  }
86  } catch (EmailException | NullPointerException ex) {
87  Logger.getLogger(ContactQueueSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
88  }
89  manager.removeMails(period, cm.getIContact().getId(), category);
90  } else {
91  agreements.sendIfNecessary(constructor);
92  if(agreements.declinedNotifications()) {
93  manager.removeMails(period, cm.getIContact().getId(), category);
94  }
95  }
96  }
97  }
98 
99  public String getSubject(String category) {
100  return "[" + I_.get(category) + " " + ElephantContext.getSiteName() + "]";
101  }
102 
103  private transient String reason, subject;
104  private transient Date itemDate;
105 
106  public boolean isReasonDifferent(MailItem mailItem) {
107  int result = CompareUtil.compare(reason, mailItem.getReason());
108  reason = mailItem.getReason();
109  if(result != 0) {
110  itemDate = null;
111  subject = null;
112  }
113  return result != 0;
114  }
115 
116  public String getReason(MailItem mailItem) {
117  return !Strings.isBlank(mailItem.getReason()) ? mailItem.getReason() : I_.get("Others");
118  }
119 
120  public boolean isSubjectDifferent(MailItem mailItem) {
121  int result = CompareUtil.compareDate(itemDate, mailItem.getItemDate());
122  if(result == 0) {
123  result = CompareUtil.compare(subject, mailItem.getSubject());
124  }
125  itemDate = mailItem.getItemDate();
126  subject = mailItem.getSubject();
127  return result != 0;
128  }
129 
130 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static NotificationCategory getCategory(String id)
static String get(String msg)
Definition: I_.java:41
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessagePool getPool(IConstructor constructor, String name)
boolean isSubjectDifferent(MailItem mailItem)
boolean isReasonDifferent(MailItem mailItem)
ContactQueueSender(IConstructor constructor, QueueManager manager, QueuePeriod period, CategoryMap cm)
void removeMails(QueuePeriod period, String idContact, String idCategory)
Object put(Object key, Object value)
boolean canSendEmails(NotificationCategory nc)
void sendIfNecessary(IConstructor constructor)
void setContact(IContact contact)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27