BrightSide Workbench Full Report + Source Code
WebIssuesNotification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.issue;
20 
21 import java.net.URLEncoder;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
26 import org.amic.util.date.CheckDate;
27 import org.apache.commons.mail.EmailException;
28 import org.turro.action.Actions;
29 import org.turro.commentit.CommentItUtil;
30 import org.turro.dossier.db.DossierPU;
31 import org.turro.dossier.entity.Issue;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.elephant.security.IUser;
36 import org.turro.i18n.I_;
37 import org.turro.jpa.DaoUtil;
38 import org.turro.mail.message.MailMessageTemplate;
39 import org.turro.mail.pool.MailMessagePool;
40 import org.turro.mail.provider.MailProviders;
41 import org.turro.marker.ElephantMarker;
42 import org.turro.plugin.contacts.ContactList;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.starit.StarItUtil;
45 import org.turro.voteit.VoteItInfo;
46 import org.turro.voteit.VoteItUtil;
47 
52 public class WebIssuesNotification {
53 
54  private final ContactList contacts;
55  private final Long category, dossier, days;
56  private final String template;
57 
58  public WebIssuesNotification(ContactList contacts, Long category, Long dossier, Long days, String template) {
59  this.contacts = contacts;
60  this.category = category;
61  this.dossier = dossier;
62  this.days = days;
63  this.template = template;
64  }
65 
66  public void sendMail(IConstructor constructor) throws EmailException {
67  List<Issue> issues = getIssues();
68  if(issues != null && !issues.isEmpty()) {
69  MailMessagePool pool = MailProviders.instance().getPool(constructor, "Dossier");
70  if(pool != null) {
71  for(IContact contact : contacts) {
73  mmt.setSubject(getSubject(constructor));
74  mmt.addTo(contact.getConnector(IUser.CONNECTOR_EMAIL), contact.getName());
75  ElephantMarker em = new ElephantMarker(constructor, true);
76  em.put("issues", issues);
77  em.put("contact", contact);
78  em.put("email", contact.getConnector(IUser.CONNECTOR_EMAIL));
79  em.put("dateActivity", new CheckDate().addDays(-days.intValue()).getDate());
80  em.put("link", this);
81  mmt.setMessage(em, template, contact);
82  pool.addToPool(mmt);
83  }
84  }
85  pool.sendPool();
86  }
87  }
88 
89  public String create(IConstructor constructor, Issue issue, String email) throws Exception {
90  return create(constructor, issue.getIssueURL(), email);
91  }
92 
93  public String create(IConstructor constructor, String redir, String email) throws Exception {
94  HashMap<String, String> values = new HashMap<>();
95  values.put(Actions.USER_PAR, email);
96  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
97  return Actions.createAction(values, days.intValue(), false);
98  }
99 
100  public VoteItInfo votes(Issue issue, IContact contact) {
101  return VoteItUtil.info(DossierPU.getObjectPath(issue), contact);
102  }
103 
104  private String getSubject(IConstructor constructor) {
105  return "[" + I_.get("Web issue notifier") + " " + ElephantContext.getSiteName() + "]";
106  }
107 
108  private List<Issue> getIssues() {
109  WhereClause wc = new WhereClause();
110  wc.addClause("select i from Issue as i");
111  wc.addClause("where i.publishable = TRUE");
112  boolean condition = false;
113  if(category > 0) {
114  wc.addClause("and i.dossier.category.id = :catid");
115  wc.addNamedValue("catid", category);
116  condition = true;
117  }
118  if(dossier > 0) {
119  wc.addClause("and i.dossier.id = :dosid");
120  wc.addNamedValue("dosid", dossier);
121  condition = true;
122  }
123  if(!condition) {
124  return null;
125  }
126  condition = false;
127  List<String> list = new ArrayList<String>();
128  list.addAll((List<String>) CommentItUtil.paths("/issue", days.intValue()));
129  list.addAll((List<String>) VoteItUtil.paths("/issue", days.intValue()));
130  list.addAll((List<String>) StarItUtil.paths("/issue", days.intValue()));
131  wc.addClause("and (");
132  if(!list.isEmpty()) {
133  wc.addClause((condition ? "or " : "") + "i.id in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
134  condition = true;
135  }
136  if(days > 0) {
137  wc.addClause((condition ? "or " : ""));
138  wc.addClause("(i.delivery <= :delivery1 and i.delivery >= :delivery2)");
139  wc.addNamedValue("delivery1", new CheckDate().addDays(days.intValue()).getDate());
140  wc.addNamedValue("delivery2", new Date());
141  wc.addClause("or i.solvedDate >= :solved");
142  wc.addNamedValue("solved", new CheckDate().addDays(-days.intValue()).getDate());
143  wc.addClause("or i.issueDate >= :created");
144  wc.addNamedValue("created", new CheckDate().addDays(-days.intValue()).getDate());
145  condition = true;
146  }
147  wc.addClause(")");
148  if(condition) {
149  return new DossierPU().getResultList(wc);
150  } else {
151  return null;
152  }
153  }
154 
155 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
static String getObjectPath(Object object)
Definition: DossierPU.java:66
VoteItInfo votes(Issue issue, IContact contact)
String create(IConstructor constructor, Issue issue, String email)
String create(IConstructor constructor, String redir, String email)
WebIssuesNotification(ContactList contacts, Long category, Long dossier, Long days, String template)
static String get(String msg)
Definition: I_.java:41
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 VoteItInfo info(Object entity, IContact contact)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27