BrightSide Workbench Full Report + Source Code
DossierNotification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.dossier.dossier;
19 
20 import java.net.URLEncoder;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.apache.commons.mail.EmailException;
28 import org.turro.action.Actions;
29 import org.turro.action.Contacts;
30 import org.turro.action.LinkType;
31 import org.turro.action.queue.NotificationCategory;
32 import org.turro.attach.entity.Attachment;
33 import org.turro.attach.search.AttachCollection;
34 import org.turro.dossier.command.DossierEntityInfo;
35 import org.turro.dossier.db.DossierPU;
36 import org.turro.dossier.entity.Dossier;
37 import org.turro.dossier.entity.DossierType;
38 import org.turro.dossier.entity.IDossierParticipant;
39 import org.turro.dossier.entity.Issue;
40 import org.turro.dossier.entity.ParticipantRole;
41 import org.turro.dossier.project.AbstractDossierNotification;
42 import org.turro.dossier.util.DossierElephantNotification;
43 import org.turro.elephant.context.ElephantContext;
44 import org.turro.elephant.context.IConstructor;
45 import org.turro.elephant.security.IUser;
46 import org.turro.i18n.I_;
47 import org.turro.mail.message.MailMessageTemplate;
48 import org.turro.mail.provider.MailProviders;
49 import org.turro.mail.queue.QueueManager;
50 import org.turro.marker.ElephantMarker;
51 import org.turro.path.Path;
52 import org.turro.plugin.contacts.ContactList;
53 import org.turro.plugin.contacts.IContact;
54 
59 public class DossierNotification {
60 
61  private final Dossier dossier;
62  private final DossierActivitySet das;
63  private final Date from;
64 
65  public DossierNotification(Dossier dossier, DossierActivitySet das, Date from) {
66  this.dossier = dossier;
67  this.das = das;
68  this.from = from;
69  }
70 
71  public void sendMail(IConstructor constructor) {
72  addToQueue(constructor);
73  }
74 
75  public String create(IConstructor constructor, Issue issue, IContact contact) throws Exception {
76  return create(constructor,
78  contact.getConnector(IUser.CONNECTOR_EMAIL));
79  }
80 
81  public String create(IConstructor constructor, Dossier dossier, IContact contact) throws Exception {
82  return create(constructor,
84  contact.getConnector(IUser.CONNECTOR_EMAIL));
85  }
86 
87  public String create(IConstructor constructor, String redir, String email) throws Exception {
88  HashMap<String, String> values = new HashMap<>();
89  values.put(Actions.USER_PAR, email);
90  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
91  return Actions.createAction(values, 7, false);
92  }
93 
94  public String createRef(IConstructor constructor, Issue issue, IContact contact) throws Exception {
95  return createRef(constructor,
96  DossierEntityInfo.linkForPath(new Path(DossierPU.getObjectPath(issue)), contact, LinkType.WEB));
97  }
98 
99  public String createRef(IConstructor constructor, Dossier dossier, IContact contact) throws Exception {
100  return createRef(constructor,
101  DossierEntityInfo.linkForPath(new Path(DossierPU.getObjectPath(dossier)), contact, LinkType.WEB));
102  }
103 
104  public String createRef(IConstructor constructor, String redir) throws Exception {
105  return "{liveref:" + redir + "}";
106  }
107 
108  private void addToQueue(IConstructor constructor) {
109  QueueManager qm = new QueueManager();
110  NotificationCategory nc = DossierType.TYPE_PROJECT.equals(dossier.getType()) ?
113  Collection<Attachment> attachments = new AttachCollection().getAttachmentList(DossierPU.getObjectPath(dossier), true, from);
114  for(IContact contact : getPlayers(constructor)) {
115  try {
116  if(qm.contactMayWant(contact, nc.getIdCategory())) {
118  mmt.setRoot("/dossiers");
119  mmt.setProcessLiveLinks(false);
120  mmt.setSubject(getSubject());
121  mmt.addTo(contact.getConnector(IUser.CONNECTOR_EMAIL), contact.getName());
122  ElephantMarker em = new ElephantMarker(constructor, true);
123  em.put("dossier", dossier);
124  em.put("wrapper", new DossierWrapper(dossier));
125  em.put("das", das);
126  em.put("contact", contact);
127  em.put("attachments", attachments);
128  em.put("email", contact.getConnector(IUser.CONNECTOR_EMAIL));
129  em.put("notifier", this);
130  mmt.setMessage(em, "dossier-activity", contact);
131  qm.addToQueue(nc, contact, mmt, null, "Dossier");
132  }
133  } catch (EmailException ex) {
134  Logger.getLogger(AbstractDossierNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
135  }
136  }
137  }
138 
139  private String getSubject() {
140  return "[" + I_.get("Dossier") + " #" + dossier.getId() + " " +
141  I_.get("Dossier notifier") + "] " + dossier.getFullDescription();
142  }
143 
144  private List<IContact> getPlayers(IConstructor constructor) {
145  ContactList list = new ContactList();
146  for(IDossierParticipant p : dossier.getFullParticipants()) {
147  if(!p.getRole().equals(ParticipantRole.PARTICIPANT_SUBJECT) && p.isShowAllIssues()) {
148  IContact ic = Contacts.getContactById(p.getIdContact());
149  if(ic.isValid() && ic.isWebUser()) {
150  list.add(ic);
151  }
152  }
153  }
154  return list;
155  }
156 
157 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
Collection< Attachment > getAttachmentList(String path, boolean publicOnly, Date from)
static String linkForPath(Path path, IContact contact, LinkType type)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
DossierNotification(Dossier dossier, DossierActivitySet das, Date from)
String createRef(IConstructor constructor, Issue issue, IContact contact)
String createRef(IConstructor constructor, String redir)
String create(IConstructor constructor, String redir, String email)
String create(IConstructor constructor, Issue issue, IContact contact)
String createRef(IConstructor constructor, Dossier dossier, IContact contact)
String create(IConstructor constructor, Dossier dossier, IContact contact)
ParticipantSet< IDossierParticipant > getFullParticipants()
Definition: Dossier.java:399
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