BrightSide Workbench Full Report + Source Code
AbstractPracticalWorkNotification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.jobboard;
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.queue.NotificationCategory;
28 import org.turro.assistant.Assistant;
29 import org.turro.assistant.AssistantSet;
30 import org.turro.auth.Authentication;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.security.IUser;
35 import org.turro.entities.Entities;
36 import org.turro.mail.message.MailMessageTemplate;
37 import org.turro.mail.provider.MailProviders;
38 import org.turro.mail.queue.GenericElephantNotification;
39 import org.turro.mail.queue.QueueManager;
40 import org.turro.marker.ElephantMarker;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.students.entities.PracticalWork;
43 
48 public abstract class AbstractPracticalWorkNotification {
49 
51  protected final IConstructor constructor;
52  protected final IContact loggedContact;
53 
55  this.practicalWork = practicalWork;
58  }
59 
60  public void send(String template, String reason) {
61  addToQueue(template, reason);
62  }
63 
64  public String create(IConstructor constructor, PracticalWork practicalWork, IContact contact) throws Exception {
65  return create(constructor,
67  contact.getConnector(IUser.CONNECTOR_EMAIL));
68  }
69 
70  public String create(IConstructor constructor, String redir, String email) throws Exception {
71  HashMap<String, String> values = new HashMap<>();
72  values.put(Actions.USER_PAR, email);
73  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, "UTF-8"));
74  return Actions.createAction(values, 7, false);
75  }
76 
77  public String createRef(IConstructor constructor, PracticalWork practicalWork, IContact contact) throws Exception {
81  }
82 
83  public String createRef(IConstructor constructor, String redir) throws Exception {
84  return "{liveref:" + redir + "}";
85  }
86 
87  private void addToQueue(String template, String reason) {
88  QueueManager qm = new QueueManager();
90  for(Assistant assistant : getAssistants()) {
91  try {
92  if(qm.contactMayWant(assistant.contact, nc.getIdCategory())) {
94  mmt.setProcessLiveLinks(false);
95  mmt.setRoot("/practicalworks");
96  mmt.setSubject(getSubject(reason));
97  mmt.addTo(assistant.email, assistant.name);
99  em.put("practicalWork", practicalWork);
100  em.put("wrapper", new PracticalWorkWrapper(practicalWork));
101  em.put("contact", assistant.contact);
102  em.put("email", assistant.email);
103  em.put("notifier", this);
104  initMarker(em);
105  mmt.setMessage(em, template, assistant.contact);
106  qm.addToQueue(nc, assistant.contact, mmt, reason, null);
107  }
108  } catch (EmailException ex) {
109  Logger.getLogger(AbstractPracticalWorkNotification.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
110  }
111  }
112  }
113 
114  protected String getSubject(String reason) {
115  return String.format("[%s %s] %s",
116  reason,
119  }
120 
121  public abstract void sendNotification();
122  protected abstract AssistantSet getAssistants();
123  protected abstract void initMarker(ElephantMarker marker);
124 
125 }
126 
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
static IElephantEntity getController(String path)
Definition: Entities.java:78
abstract void initMarker(ElephantMarker marker)
String create(IConstructor constructor, PracticalWork practicalWork, IContact contact)
String createRef(IConstructor constructor, PracticalWork practicalWork, IContact contact)
String create(IConstructor constructor, String redir, String email)
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