BrightSide Workbench Full Report + Source Code
AbstractChallengeNotification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.students.mail;
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.MailSenders;
29 import org.turro.action.queue.NotificationCategory;
30 import org.turro.assistant.Assistant;
31 import org.turro.assistant.AssistantSet;
32 import org.turro.auth.Authentication;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.context.IConstructor;
36 import org.turro.elephant.security.IUser;
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.path.Path;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.students.command.StudentsEntityInfo;
45 import org.turro.students.db.StudentsPU;
46 import org.turro.students.entities.Challenge;
47 import org.turro.students.entities.Response;
48 import org.turro.students.wrappers.ChallengeWrapper;
49 
54 public abstract class AbstractChallengeNotification {
55 
56  protected final Challenge challenge;
57  protected final Response response;
58  protected final IConstructor constructor;
59  protected final IContact loggedContact;
60 
62  this.challenge = challenge;
63  this.response = null;
66  }
67 
69  this.response = response;
70  this.challenge = response.getChallenge();
73  }
74 
75  public void send(String template, String reason) {
76  addToQueue(template, reason);
77  }
78 
79  public String create(IConstructor constructor, Challenge challenge, IContact contact) throws Exception {
80  return create(constructor,
82  contact.getConnector(IUser.CONNECTOR_EMAIL));
83  }
84 
85  public String create(IConstructor constructor, String redir, String email) throws Exception {
86  HashMap<String, String> values = new HashMap<>();
87  values.put(Actions.USER_PAR, email);
88  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
89  return Actions.createAction(values, 7, false);
90  }
91 
92  public String createRef(IConstructor constructor, Challenge challenge, IContact contact) throws Exception {
93  return createRef(constructor,
95  }
96 
97  public String createRef(IConstructor constructor, String redir) throws Exception {
98  return "{liveref:" + redir + "}";
99  }
100 
101  private void addToQueue(String template, String reason) {
102  QueueManager qm = new QueueManager();
104  for(Assistant assistant : getAssistants()) {
105  try {
106  if(qm.contactMayWant(assistant.contact, nc.getIdCategory())) {
108  mmt.setProcessLiveLinks(false);
109  mmt.setRoot("/challenges");
110  mmt.setSubject(getSubject(reason));
111  mmt.addTo(assistant.email, assistant.name);
112  ElephantMarker em = new ElephantMarker(constructor, true);
113  em.put("challenge", challenge);
114  em.put("response", response);
115  em.put("wrapper", new ChallengeWrapper(challenge));
116  em.put("contact", assistant.contact);
117  em.put("email", assistant.email);
118  em.put("notifier", this);
119  initMarker(em);
120  mmt.setMessage(em, template, assistant.contact);
121  qm.addToQueue(nc, assistant.contact, mmt, reason, "Students");
122  }
123  } catch (EmailException ex) {
124  Logger.getLogger(AbstractChallengeNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
125  }
126  }
127  }
128 
129  protected String getSubject(String reason) {
130  return String.format("[%s #%d %s] %s",
131  reason,
132  challenge.getId(),
135  }
136 
137  public abstract void sendNotification();
138  protected abstract AssistantSet getAssistants();
139  protected abstract void initMarker(ElephantMarker marker);
140 
141 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
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 String linkForPath(Path path, IContact contact, LinkType type)
static String getObjectPath(Object object)
Definition: StudentsPU.java:59
String createRef(IConstructor constructor, Challenge challenge, IContact contact)
String createRef(IConstructor constructor, String redir)
String create(IConstructor constructor, String redir, String email)
abstract void initMarker(ElephantMarker marker)
String create(IConstructor constructor, Challenge challenge, IContact contact)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27