19 package org.turro.publication.util;
22 import java.net.URLEncoder;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.stream.Collectors;
30 import org.apache.commons.mail.EmailException;
31 import org.turro.action.Actions;
32 import org.turro.action.Contacts;
33 import org.turro.action.IAgreements;
34 import org.turro.action.MailAttachment;
35 import org.turro.action.Plugins;
36 import org.turro.action.queue.ConstraintKeys;
37 import org.turro.action.queue.NotificationCategory;
38 import org.turro.action.queue.Notifications;
39 import org.turro.action.queue.QueuePeriod;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.elephant.context.IConstructor;
42 import org.turro.elephant.entities.db.QueueOptions;
43 import org.turro.elephant.impl.repository.Repository;
44 import org.turro.elephant.security.IUser;
45 import org.turro.file.util.FileAttach;
46 import org.turro.jpa.entity.EntityWebUrls;
47 import org.turro.mail.message.MailMessageTemplate;
48 import org.turro.mail.pool.MailMessagePool;
49 import org.turro.mail.provider.MailProviders;
50 import org.turro.mail.queue.QueueManager;
51 import org.turro.mail.queue.QueuedSender;
52 import org.turro.marker.ElephantMarker;
53 import org.turro.plugin.contacts.IContact;
54 import org.turro.publication.db.PublicationPU;
55 import org.turro.publication.entity.Publication;
56 import org.turro.publication.entity.PublicationCategory;
65 private final String path;
66 private final List<Publication> pubs;
71 this.constructor = constructor;
74 this.category = category;
87 String redir = path +
"?pubid=" + publication.getId() +
"&item=" + publication.getId();
88 HashMap<String, String> values =
new HashMap<>();
89 values.put(
Actions.REDIR_PAR, URLEncoder.encode(redir,
"UTF-8"));
94 String redir = path +
"?pubid=" + publication.getId() +
"&item=" + publication.getId();
95 return create(redir, contact);
99 HashMap<String, String> values =
new HashMap<>();
101 values.put(
Actions.REDIR_PAR, URLEncoder.encode(redir,
"UTF-8"));
109 if(ewu !=
null && entityUrl !=
null) {
110 redir = entityUrl + ewu;
112 redir = path +
"?pubid=" + publication.getId() +
"&item=" + publication.getId();
117 public String
createRef(String redir)
throws Exception {
118 return "{liveref:" + redir +
"}";
127 for(
IContact contact : getPlayers()) {
128 List<Publication> publications = filterLangs(contact);
136 em.
put(
"pubs", publications);
137 em.
put(
"contact", contact);
139 em.
put(
"notifier",
this);
142 contact, mmt,
null,
"Publication");
143 }
catch (EmailException | NullPointerException ex) {
153 List<MailAttachment> attached = getAttached(pubs);
154 for(
IContact contact : getPlayers()) {
155 List<Publication> publications = filterLangs(contact);
165 em.
put(
"pubs", publications);
166 em.
put(
"contact", contact);
168 em.
put(
"notifier",
this);
171 mmt.
attach(ma.getPath(), ma.getName(), ma.getName());
176 }
catch (EmailException | NullPointerException ex) {
184 private String getSubject(List<Publication> pubs) {
185 if(pubs !=
null && pubs.size() == 1) {
186 return pubs.get(0).getTitle();
188 return category.
getName() +
" - " + ElephantContext.getSiteName();
192 private List<IContact> getPlayers() {
193 QueueManager qm =
new QueueManager();
194 Collection<QueueOptions> subs = qm.getSubcribers(PublicationElephantNotification.convertId(category));
195 ArrayList<IContact> list =
new ArrayList<>();
196 for(QueueOptions qo : subs) {
197 if(qo.getIdContact() !=
null && !QueuePeriod.DONT_SEND.equals(qo.getPeriod())) {
198 IContact c = Contacts.getContactById(qo.getIdContact());
199 if(c.isWebUser() && category.
strongBond(ConstraintKeys.from(c))) {
207 private List<MailAttachment> getAttached(List<Publication> publications) {
208 List<MailAttachment> list =
new ArrayList<>();
209 for(Publication pub : publications) {
210 File[] attached =
new FileAttach(PublicationPU.getObjectPath(pub))
211 .getPublishableRepository(constructor).getRoot(
"/attached").getFiles(
"*.*");
212 if(attached !=
null) {
213 for(File file : attached) {
214 list.add(
new MailAttachment(file.getAbsolutePath(), file.getName(), file));
221 private List<Publication> filterLangs(IContact contact) {
222 return pubs.stream().filter(p -> contact.acceptsLang(p.getLang())).collect(Collectors.toList());
static String createAction(String email, String redir)
static final String USER_PAR
static< T > T loadImplementation(Class< T > jclass)
static NotificationCategory getCategory(String id)
static String logMsg(String msg)
static String getEntityWebContext(String path)
Repository getPublishableRepository(IConstructor constructor)
static String getUrlFromEntity(Object entity)
void setProcessLiveLinks(boolean processLiveLinks)
void setMessage(ElephantMarker marker, IContact contact)
static MailMessageTemplate of(MailProvider provider)
void setRoot(String root)
void attach(String path, String description, String name)
Email addTo(String email, String name)
void setSubject(String subject)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailProvider getProvider()
MailProvider getProvider(String name)
static MailProviders instance()
MailMessagePool getPool(IConstructor constructor, String name)
MailItem addToQueue(NotificationCategory category, IContact contact, MailMessage mmt, String reason, String poolName)
Object put(Object key, Object value)
static String getObjectPath(Object object)
boolean strongBond(ConstraintKeys keys)
static String convertId(PublicationCategory category)
PublicationNotification(IConstructor constructor, String path, List< Publication > pubs, PublicationCategory category)
Repository repository(Publication pub)
String createRef(String redir)
String create(Publication publication, IContact contact)
String create(String redir, IContact contact)
String createRef(Publication publication)
String create(Publication publication)
boolean canSendEmails(NotificationCategory nc)
void setContact(IContact contact)
static final String CONNECTOR_EMAIL