19 package org.turro.mail.queue;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantLock;
27 import org.amic.util.date.CheckDate;
28 import org.turro.action.Contacts;
29 import org.turro.action.queue.ConstraintKeys;
30 import org.turro.string.Strings;
31 import org.turro.action.queue.NotificationCategory;
32 import org.turro.action.queue.Notifications;
33 import org.turro.action.queue.QueuePeriod;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.db.ElephantPU;
36 import org.turro.elephant.db.WhereClause;
37 import org.turro.elephant.entities.db.MailItem;
38 import org.turro.elephant.entities.db.QueueOptions;
39 import org.turro.jpa.Dao;
40 import org.turro.mail.message.MailMessage;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.sql.SqlClause;
51 boolean lockAcquired = QUEUEMANAGER_LOCK.tryLock();
58 for(String idContact : cm.keySet()) {
62 QUEUEMANAGER_LOCK.unlock();
84 return getDao().saveObject(mi);
90 String subject, String message, String reason, String poolName) {
106 return getDao().saveObject(mi);
125 if(qo !=
null && !
override)
return;
138 if(nc.wideBond(keys)) {
140 }
else if(!nc.strongBond(keys)) {
148 wc.
addClause(
"select o from QueueOptions o");
149 wc.
addClause(
"where o.idContact = :idContact");
150 wc.
addClause(
"and o.idCategory = :idCategory");
157 return getDao().saveObject(qo);
172 wc.
addClause(
"select m from MailItem m, QueueOptions o");
173 wc.
addClause(
"where o.period = :period");
174 wc.
addClause(
"and m.idContact = o.idContact");
175 wc.
addClause(
"and m.idCategory = o.idCategory");
176 wc.
addClause(
"order by m.idContact, m.idCategory");
178 return getDao().getResultList(wc);
192 wc.
addClause(
"where m.idContact = :contact");
193 wc.
addClause(
"and m.idCategory = :category");
202 wc.
addClause(
"where m.idContact = :contact");
203 wc.
addClause(
"and m.idCategory = :category");
205 wc.
addClause(
"select o from QueueOptions o");
206 wc.
addClause(
"where o.period = :period");
207 wc.
addClause(
"and m.idContact = o.idContact");
208 wc.
addClause(
"and m.idCategory = o.idCategory");
219 wc.
addClause(
"where m.idContact = :contact");
228 wc.
addClause(
"select o from QueueOptions o");
229 wc.
addClause(
"where m.idContact = o.idContact");
230 wc.
addClause(
"and m.idCategory = o.idCategory");
239 wc.
addClause(
"select o from QueueOptions o");
240 wc.
addClause(
"where m.idContact = o.idContact");
241 wc.
addClause(
"and m.idCategory = o.idCategory");
244 wc.
addClause(
"or (o.period = :asGenerated and m.itemDate < :oneDay)");
245 wc.
addClause(
"or (o.period = :daily and m.itemDate < :oneDay)");
246 wc.
addClause(
"or (o.period = :weekly and m.itemDate < :oneWeek)");
247 wc.
addClause(
"or (o.period = :fortnightly and m.itemDate < :twoWeeks)");
248 wc.
addClause(
"or (o.period = :monthly and m.itemDate < :oneMonth)");
249 wc.
addClause(
"or (o.period = :quarterly and m.itemDate < :threeMonths)");
250 wc.
addClause(
"or (o.period = :everySix and m.itemDate < :sixMonths)");
251 wc.
addClause(
"or (o.period = :yearly and m.itemDate < :oneYear)");
256 wc.
addNamedValue(
"oneDay",
new CheckDate().addDays(-2).getDate());
258 wc.
addNamedValue(
"oneWeek",
new CheckDate().addWeeks(-1).addDays(-2).getDate());
260 wc.
addNamedValue(
"twoWeeks",
new CheckDate().addWeeks(-2).addDays(-2).getDate());
262 wc.
addNamedValue(
"oneMonth",
new CheckDate().addMonths(-1).addDays(-2).getDate());
264 wc.
addNamedValue(
"threeMonths",
new CheckDate().addMonths(-3).addDays(-2).getDate());
266 wc.
addNamedValue(
"sixMonths",
new CheckDate().addMonths(-6).addDays(-2).getDate());
268 wc.
addNamedValue(
"oneYear",
new CheckDate().addYears(-1).addDays(-2).getDate());
274 wc.
addClause(
"delete from QueueOptions o");
275 wc.
addClause(
"where o.idCategory = :category");
284 wc.
addClause(
"where o.idCategory = :category");
293 List<QueueOptions> toDelete =
new ArrayList<>();
294 SqlClause.select(
"o").from(
"QueueOptions o")
295 .where().equal(
"o.idCategory", idCategory)
301 ConstraintKeys keys = ConstraintKeys.from(contact);
302 if(!nc.strongBond(keys)) {
316 wc.
addClause(
"select o from QueueOptions o");
317 wc.
addClause(
"where o.idCategory = :category");
319 return getDao().getResultList(wc);
324 wc.
addClause(
"select o.id from QueueOptions o");
325 wc.
addClause(
"where o.idCategory = :category");
327 return getDao().getResultList(wc);
353 getDao().deleteObject(mi);
357 if(qo !=
null) getDao().deleteObject(qo);
360 private String cleanSubject(String reason) {
361 if(Strings.isBlank(reason)) {
364 int p = reason.indexOf(
":");
365 return (p > -1 ? reason.substring(0, p) : reason);
373 wc.
addClause(
"select o from QueueOptions o");
374 wc.
addClause(
"where o.idCategory like :category");
376 return getDao().getResultList(wc);
383 private Dao getDao() {
392 private static final Lock QUEUEMANAGER_LOCK =
new ReentrantLock();
NotificationConstraintSet getConstraints()
QueuePeriod getDefaultPeriod()
static NotificationCategory getCategory(String id)
static Collection< NotificationCategory > getCategories()
void addClause(String clause)
void addNamedValue(String name, Object value)
void setSubject(String subject)
void setMessage(String message)
void setIContact(IContact contact)
void setCategory(String category)
void setDescription(String description)
void setReason(String reason)
void setItemDate(Date itemDate)
void setPoolName(String poolName)
void setIdCategory(String idCategory)
void setPeriod(QueuePeriod period)
void setSingleMail(boolean singleMail)
void setIdCategory(String idCategory)
void setIdContact(String idContact)
int executeUpdate(String query)
void deleteCollection(Collection objs)
Object getSingleResultOrNull(SqlClause sc)
static void send(IConstructor constructor, QueuePeriod period)
void removeOldsByPeriod()
void removeMails(QueuePeriod period, String idContact, String idCategory)
void setPeriod(IContact contact, String idCategory, QueuePeriod queuePeriod, boolean override)
QueueOptions configQueue(QueueOptions qo)
void checkConfiguration(IContact contact, String idCategory, QueuePeriod queuePeriod)
QueueOptions getConfiguration(IContact contact, String idCategory)
void removeOptions(QueueOptions qo)
void removeWithoutOptions()
MailItem addToQueue(NotificationCategory category, IContact contact, MailMessage mmt, String reason, String poolName)
void removeMail(MailItem mi)
List< MailItem > getPending(QueuePeriod period)
Collection< QueueOptions > getSubcribersByRoot(String root)
void removeContactMails(String idContact)
void setPeriodToAll(String idCategory, QueuePeriod period)
void removeOptions(String idCategory)
void checkConstraints(String idCategory)
ContactMap getContactMap(List< MailItem > list)
boolean contactWants(IContact contact, String idCategory)
MailItem addToQueue(NotificationCategory category, IContact contact, String subject, String message, String reason, String poolName)
void subscribeDefaults(ConstraintKeys keys)
Collection< QueueOptions > getSubcribers(String idCategory)
boolean contactMayWant(IContact contact, String idCategory)
Collection< Long > getSubcribersId(String idCategory)
void removeUniquesFor(String idCategory, String idContact)