BrightSide Workbench Full Report + Source Code
AbstractServiceNotification.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.net.URLEncoder;
22 import java.util.HashMap;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.apache.commons.mail.EmailException;
26 import org.turro.action.Actions;
27 import org.turro.action.queue.NotificationCategory;
28 import org.turro.assistant.Assistant;
29 import org.turro.assistant.AssistantSet;
30 import org.turro.auth.Authentication;
31 import org.turro.contacts.ContactService;
32 import org.turro.elephant.context.Application;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.security.IUser;
36 import org.turro.entities.Entities;
37 import org.turro.mail.message.MailMessageTemplate;
38 import org.turro.mail.provider.MailProviders;
39 import org.turro.mail.queue.GenericElephantNotification;
40 import org.turro.mail.queue.QueueManager;
41 import org.turro.marker.ElephantMarker;
42 import org.turro.plugin.contacts.IContact;
43 
48 public abstract class AbstractServiceNotification {
49 
51  protected final IConstructor constructor;
52  protected final IContact loggedContact;
53 
55  this.service = service;
58  }
59 
60  public void send(String template, String reason) {
61  addToQueue(template, reason);
62  }
63 
64  public String create(IConstructor constructor, ContactService service, IContact contact) throws Exception {
65  return create(constructor,
67  contact.getConnector(IUser.CONNECTOR_EMAIL));
68  }
69 
70  public String create(IConstructor constructor, String redir, String email) throws Exception {
71  HashMap<String, String> values = new HashMap<>();
72  values.put(Actions.USER_PAR, email);
73  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
74  return Actions.createAction(values, 7, false);
75  }
76 
77  public String createRef(IConstructor constructor, ContactService service, IContact contact) throws Exception {
78  return createRef(constructor, service.getTimesSent() >=1 ?
81  }
82 
83  public String createRef(IConstructor constructor, String redir) throws Exception {
84  return "{liveref:" + redir + "}";
85  }
86 
87  private void addToQueue(String template, String reason) {
88  QueueManager qm = new QueueManager();
90  for(Assistant assistant : getAssistants()) {
91  try {
92  if(qm.contactMayWant(assistant.contact, nc.getIdCategory())) {
94  mmt.setProcessLiveLinks(false);
95  mmt.setRoot("/services");
96  mmt.setSubject(getSubject(reason));
97  mmt.addTo(assistant.email, assistant.name);
99  em.put("service", service);
100  em.put("wrapper", new ContactServiceWrapper(service));
101  em.put("contact", assistant.contact);
102  em.put("email", assistant.email);
103  em.put("face", service.getImage());
104  em.put("notifier", this);
105  initMarker(em);
106  mmt.setMessage(em, template, assistant.contact);
107  qm.addToQueue(nc, assistant.contact, mmt, reason, null);
108  }
109  } catch (EmailException ex) {
110  Logger.getLogger(AbstractServiceNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
111  }
112  }
113  }
114 
115  protected String getSubject(String reason) {
116  return String.format("[%s %s] %s",
117  reason,
119  service.getTitle());
120  }
121 
122  public abstract void sendNotification();
123  protected abstract AssistantSet getAssistants();
124  protected abstract void initMarker(ElephantMarker marker);
125 
126 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
String create(IConstructor constructor, ContactService service, IContact contact)
String create(IConstructor constructor, String redir, String email)
abstract void initMarker(ElephantMarker marker)
String createRef(IConstructor constructor, ContactService service, IContact contact)
String createRef(IConstructor constructor, String redir)
static IElephantEntity getController(String path)
Definition: Entities.java:78
MailProvider getProvider(String name)
MailItem addToQueue(NotificationCategory category, IContact contact, MailMessage mmt, String reason, String poolName)
boolean contactMayWant(IContact contact, String idCategory)
Object put(Object key, Object value)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27