BrightSide Workbench Full Report + Source Code
IssueMail.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.issue;
19 
20 import java.net.URLEncoder;
21 import java.util.HashMap;
22 import java.util.List;
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.Contacts;
28 import org.turro.action.LinkType;
29 import org.turro.action.MailAttachment;
30 import org.turro.attach.search.AttachCollection;
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.entity.IDossierParticipant;
35 import org.turro.dossier.entity.Issue;
36 import org.turro.dossier.entity.IssueParticipant;
37 import org.turro.elephant.context.Application;
38 import org.turro.elephant.context.ElephantContext;
39 import org.turro.elephant.context.IConstructor;
40 import org.turro.elephant.security.IUser;
41 import org.turro.i18n.I_;
42 import org.turro.mail.message.MailMessageTemplate;
43 import org.turro.mail.pool.MailMessagePool;
44 import org.turro.mail.provider.MailProviders;
45 import org.turro.marker.ElephantMarker;
46 import org.turro.path.Path;
47 import org.turro.plugin.contacts.ContactList;
48 import org.turro.plugin.contacts.IContact;
49 import org.turro.registry.Changes;
50 import org.turro.util.Chars;
51 
56 public class IssueMail {
57 
58  private final Changes changes;
59  private final Issue issue;
60  private final double expenses, hours, price;
61 
62  public IssueMail(Changes changes, Issue issue, double expenses, double hours, double price) {
63  this.changes = changes;
64  this.issue = issue;
65  this.expenses = expenses;
66  this.hours = hours;
67  this.price = price;
68  }
69 
70  public void sendMail() {
71  sendMailTemplate();
72  }
73 
74  public String create(IConstructor constructor, Issue issue, IContact contact) throws Exception {
75  return create(constructor,
77  contact.getConnector(IUser.CONNECTOR_EMAIL));
78  }
79 
80  public String create(IConstructor constructor, String redir, String email) throws Exception {
81  HashMap<String, String> values = new HashMap<>();
82  values.put(Actions.USER_PAR, email);
83  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
84  return Actions.createAction(values, 7, false);
85  }
86 
87  private void sendMailTemplate() {
89  IConstructor constructor = app.getConstructor();
90  IContact realContact = Authentication.getIContact();
91  final AttachCollection amc = new AttachCollection();
92  amc.loadAttachments(DossierPU.getObjectPath(issue), false, issue.getModification());
93  MailMessagePool pool = MailProviders.instance().getPool(constructor, "Dossier");
94  if(pool != null) {
95  for(IContact contact : getPlayers()) {
96  try {
98  mmt.setRoot("/issues");
99  mmt.setSubject(getSubject(realContact));
100  mmt.addTo(contact.getConnector(IUser.CONNECTOR_EMAIL), contact.getName());
101  ElephantMarker em = new ElephantMarker(constructor, true);
102  em.put("last", issue.getLastComment());
103  em.put("issue", issue);
104  em.put("wrapper", new IssueWrapper(issue));
105  em.put("contact", contact);
106  em.put("realContact", realContact);
107  em.put("email", contact.getConnector(IUser.CONNECTOR_EMAIL));
108  em.put("notifier", this);
109  mmt.setMessage(em, "issue-notification", contact);
110  for(MailAttachment ma : amc.getAttachments()) {
111  mmt.attach(ma.attachment.getAbsolutePath(), ma.name, ma.name);
112  }
113  pool.addToPool(mmt);
114  } catch (EmailException ex) {
115  Logger.getLogger(IssueMail.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
116  }
117  }
118  pool.sendPool((m) -> {
119  amc.removeAttachments();
120  });
121  } else {
122  amc.removeAttachments();
123  }
124  }
125 
126  private String getSubject(IContact realContact) {
127  return "[" + I_.get("Issue") + " #" + issue.getId() + " " +
128  realContact.getName() + "] " + issue.getDescription() +
129  (issue.getDossier() != null ? Chars.forward().spaced() + issue.getDossier().getFullDescription() : "");
130  }
131 
132  private List<IContact> getPlayers() {
133  ContactList list = new ContactList();
134  for(IssueParticipant ip : issue.getParticipants()) {
135  IContact ic = Contacts.getContactById(ip.getIdContact());
136  if(ic.isValid() && ic.isWebUser()) {
137  ic.setAttribute("showParticipants", Boolean.TRUE);
138  list.add(ic);
139  }
140  }
141  if(issue.getDossier() != null) {
142  for(IDossierParticipant p : issue.getDossier().getFullParticipants()) {
143  if(p.isReceiveAllEmails()) {
144  IContact ic = Contacts.getContactById(p.getIdContact());
145  if(ic.isValid() && ic.isWebUser()) {
146  ic.setAttribute("showParticipants", p.isShowParticipants());
147  list.add(ic);
148  }
149  }
150  }
151  }
152  return list;
153  }
154 
155 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
void loadAttachments(String path, boolean publicOnly)
Collection< MailAttachment > getAttachments()
static String linkForPath(Path path, IContact contact, LinkType type)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
ParticipantSet< IDossierParticipant > getFullParticipants()
Definition: Dossier.java:399
Set< IssueParticipant > getParticipants()
Definition: Issue.java:114
IssueComment getLastComment()
Definition: Issue.java:370
String create(IConstructor constructor, String redir, String email)
Definition: IssueMail.java:80
IssueMail(Changes changes, Issue issue, double expenses, double hours, double price)
Definition: IssueMail.java:62
String create(IConstructor constructor, Issue issue, IContact contact)
Definition: IssueMail.java:74
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessagePool getPool(IConstructor constructor, String name)
Object put(Object key, Object value)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27