BrightSide Workbench Full Report + Source Code
AbstractDossierNotification.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.dossier.project;
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.LinkType;
28 import org.turro.action.queue.NotificationCategory;
29 import org.turro.assistant.Assistant;
30 import org.turro.assistant.AssistantSet;
31 import org.turro.auth.Authentication;
32 import org.turro.dossier.command.DossierEntityInfo;
33 import org.turro.dossier.db.DossierPU;
34 import org.turro.dossier.dossier.DossierWrapper;
35 import org.turro.dossier.entity.Dossier;
36 import org.turro.dossier.entity.Issue;
37 import org.turro.dossier.entity.IssueType;
38 import org.turro.dossier.util.DossierElephantNotification;
39 import org.turro.elephant.context.Application;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.elephant.context.IConstructor;
42 import org.turro.elephant.security.IUser;
43 import org.turro.mail.message.MailMessageTemplate;
44 import org.turro.mail.provider.MailProviders;
45 import org.turro.mail.queue.QueueManager;
46 import org.turro.marker.ElephantMarker;
47 import org.turro.path.Path;
48 import org.turro.plugin.contacts.IContact;
49 
54 public abstract class AbstractDossierNotification {
55 
56  protected final Dossier dossier;
57  protected final IConstructor constructor;
58  protected final IContact loggedContact;
59 
61  this.dossier = dossier;
64  }
65 
66  public void send(String template, String reason) {
67  addToQueue(template, reason);
68  }
69 
70  public String create(IConstructor constructor, Dossier dossier, IContact contact) throws Exception {
71  return create(constructor,
73  contact.getConnector(IUser.CONNECTOR_EMAIL));
74  }
75 
76  public String create(IConstructor constructor, String redir, String email) throws Exception {
77  HashMap<String, String> values = new HashMap<>();
78  values.put(Actions.USER_PAR, email);
79  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
80  return Actions.createAction(values, 7, false);
81  }
82 
83  public String createRef(IConstructor constructor, Dossier dossier, IContact contact) throws Exception {
84  return createRef(constructor,
86  }
87 
88  public String createRef(IConstructor constructor, String redir) throws Exception {
89  return "{liveref:" + redir + "}";
90  }
91 
92  private void addToQueue(String template, String reason) {
93  QueueManager qm = new QueueManager();
95  for(Assistant assistant : getAssistants()) {
96  try {
97  if(qm.contactMayWant(assistant.contact, nc.getIdCategory())) {
99  mmt.setProcessLiveLinks(false);
100  mmt.setRoot("/projects");
101  mmt.setSubject(getSubject(reason));
102  mmt.addTo(assistant.email, assistant.name);
103  ElephantMarker em = new ElephantMarker(constructor, true);
104  em.put("dossier", dossier);
105  em.put("wrapper", new DossierWrapper(dossier));
106  em.put("contact", assistant.contact);
107  em.put("email", assistant.email);
108  em.put("notifier", this);
109  initMarker(em);
110  mmt.setMessage(em, template, assistant.contact);
111  qm.addToQueue(nc, assistant.contact, mmt, reason, "Dossier");
112  }
113  } catch (EmailException ex) {
114  Logger.getLogger(AbstractDossierNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
115  }
116  }
117  }
118 
119  protected String getSubject(String reason) {
120  return String.format("[%s #%d %s] %s",
121  reason,
122  dossier.getId(),
125  }
126 
127  public abstract void sendNotification();
128  protected abstract AssistantSet getAssistants();
129  protected abstract void initMarker(ElephantMarker marker);
130 
132  if(issue != null && issue.getType().equals(IssueType.TYPE_PROPOSITION)) {
133  return new NewOfferNotification(issue.getDossier(), issue);
134  }
135  return null;
136  }
137 
138 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
static String linkForPath(Path path, IContact contact, LinkType type)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
abstract void initMarker(ElephantMarker marker)
String createRef(IConstructor constructor, Dossier dossier, IContact contact)
String createRef(IConstructor constructor, String redir)
String create(IConstructor constructor, Dossier dossier, IContact contact)
String create(IConstructor constructor, String redir, String email)
static AbstractDossierNotification getByGrouping(Issue issue)
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