BrightSide Workbench Full Report + Source Code
PublicationNotification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.publication.util;
20 
21 import java.io.File;
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;
57 
62 public class PublicationNotification extends QueuedSender {
63 
64  private final IConstructor constructor;
65  private final String path;
66  private final List<Publication> pubs;
67  private final PublicationCategory category;
68 
69  public PublicationNotification(IConstructor constructor, String path,
70  List<Publication> pubs, PublicationCategory category) {
71  this.constructor = constructor;
72  this.path = path;
73  this.pubs = pubs;
74  this.category = category;
75  }
76 
77  @Override
78  protected void doSend() {
79  if(category.isAvoidQueue()) {
80  sendMail();
81  } else {
82  addToQueue();
83  }
84  }
85 
86  public String create(Publication publication) throws Exception {
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"));
90  return Actions.createAction(values, false);
91  }
92 
93  public String create(Publication publication, IContact contact) throws Exception {
94  String redir = path + "?pubid=" + publication.getId() + "&item=" + publication.getId();
95  return create(redir, contact);
96  }
97 
98  public String create(String redir, IContact contact) throws Exception {
99  HashMap<String, String> values = new HashMap<>();
100  values.put(Actions.USER_PAR, contact.getConnector(IUser.CONNECTOR_EMAIL));
101  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
102  return Actions.createAction(values, 7, false);
103  }
104 
105  public String createRef(Publication publication) throws Exception {
106  String redir;
107  String entityUrl = ElephantContext.getEntityWebContext("/publication");
108  String ewu = EntityWebUrls.getUrlFromEntity(publication);
109  if(ewu != null && entityUrl != null) {
110  redir = entityUrl + ewu;
111  } else {
112  redir = path + "?pubid=" + publication.getId() + "&item=" + publication.getId();
113  }
114  return createRef(redir);
115  }
116 
117  public String createRef(String redir) throws Exception {
118  return "{liveref:" + redir + "}";
119  }
120 
122  return new FileAttach(PublicationPU.getObjectPath(pub)).getPublishableRepository(constructor);
123  }
124 
125  public void addToQueue() {
126  QueueManager qm = new QueueManager();
127  for(IContact contact : getPlayers()) {
128  List<Publication> publications = filterLangs(contact);
129  try {
131  mmt.setRoot("/publications");
132  mmt.setProcessLiveLinks(false);
133  mmt.setSubject(getSubject(null));
134  mmt.addTo(contact.getConnector(IUser.CONNECTOR_EMAIL), contact.getName());
135  ElephantMarker em = new ElephantMarker(constructor, true);
136  em.put("pubs", publications);
137  em.put("contact", contact);
138  em.put("email", contact.getConnector(IUser.CONNECTOR_EMAIL));
139  em.put("notifier", this);
140  mmt.setMessage(em, "publication", contact);
142  contact, mmt, null, "Publication");
143  } catch (EmailException | NullPointerException ex) {
144  Logger.getLogger(PublicationNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
145  }
146  }
147  }
148 
149  public void sendMail() {
150  MailMessagePool pool = MailProviders.instance().getPool(constructor, "Publication");
151  if(pool != null) {
152  IAgreements agreements = Plugins.loadImplementation(IAgreements.class, "agreements");
153  List<MailAttachment> attached = getAttached(pubs);
154  for(IContact contact : getPlayers()) {
155  List<Publication> publications = filterLangs(contact);
157  agreements.setContact(contact);
158  if(agreements.canSendEmails(nc)) {
159  try {
161  mmt.setRoot("/publications");
162  mmt.setSubject(getSubject(publications));
163  mmt.addTo(contact.getConnector(IUser.CONNECTOR_EMAIL), contact.getName());
164  ElephantMarker em = new ElephantMarker(constructor, true);
165  em.put("pubs", publications);
166  em.put("contact", contact);
167  em.put("email", contact.getConnector(IUser.CONNECTOR_EMAIL));
168  em.put("notifier", this);
169  mmt.setMessage(em, "sendable", contact);
170  for(MailAttachment ma : attached) {
171  mmt.attach(ma.getPath(), ma.getName(), ma.getName());
172  }
173  pool.addToPool(mmt);
174  pool.sendPool();
175  pause();
176  } catch (EmailException | NullPointerException ex) {
177  Logger.getLogger(PublicationNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
178  }
179  }
180  }
181  }
182  }
183 
184  private String getSubject(List<Publication> pubs) {
185  if(pubs != null && pubs.size() == 1) {
186  return pubs.get(0).getTitle();
187  } else {
188  return category.getName() + " - " + ElephantContext.getSiteName();
189  }
190  }
191 
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))) {
200  list.add(c);
201  }
202  }
203  }
204  return list;
205  }
206 
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));
215  }
216  }
217  }
218  return list;
219  }
220 
221  private List<Publication> filterLangs(IContact contact) {
222  return pubs.stream().filter(p -> contact.acceptsLang(p.getLang())).collect(Collectors.toList());
223  }
224 
225 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static NotificationCategory getCategory(String id)
static String getEntityWebContext(String path)
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
static String getUrlFromEntity(Object entity)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailProvider getProvider(String name)
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)
PublicationNotification(IConstructor constructor, String path, List< Publication > pubs, PublicationCategory category)
String create(Publication publication, IContact contact)
boolean canSendEmails(NotificationCategory nc)
void setContact(IContact contact)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27