Merge pull request #9 from Arimihanta/main

Envoyer notification concernant le vote majoritaire
This commit is contained in:
ArenMg 2021-08-06 00:35:39 +03:00 committed by GitHub
commit 9a63b2a814
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 223 additions and 4 deletions

View file

@ -0,0 +1,38 @@
package fr.lirmm.aren.model.vm;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Set;
/**
* @author ANDRIAMBOLAHARIMIHANTA Havana on 01/08/2021
* @project aren-1
*/
public class VMNotification {
private String link ;
private List<String>emails ;
private String expiracy ;
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public String getExpiracy() {
return expiracy;
}
public void setExpiracy(String expiracy) {
this.expiracy = expiracy;
}
}

View file

@ -0,0 +1,125 @@
package fr.lirmm.aren.service.vm;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.lirmm.aren.model.User;
import fr.lirmm.aren.model.vm.VMNotification;
import fr.lirmm.aren.model.vm.VMTheme;
import fr.lirmm.aren.producer.Configurable;
import fr.lirmm.aren.service.MailingService;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.text.MessageFormat;
import java.time.ZonedDateTime;
import java.util.*;
/**
* @author ANDRIAMBOLAHARIMIHANTA Havana on 18/07/2021
* @project aren-1
*/
@ApplicationScoped
public class VMSendNotificationService extends Thread implements Runnable{
@Inject
@Configurable("reverse-proxy")
private String reverseProxy;
public VMSendNotificationService() {
}
@Override
public void run(){
int i=0 ;
try {
VMNotification notification=null ;
while(true){
File file = new File("/aren/tmp/vote_majoritaire.json");
if((file.exists() && notification==null) || notification.getEmails().size()==0){
ObjectMapper mapper = new ObjectMapper();
Map<?, ?> map = (Map<String, Object>) mapper.readValue(file, Map.class);
notification=new VMNotification() ;
for (Map.Entry<?, ?> entry : map.entrySet()) {
switch(entry.getKey().toString()){
case "link":
notification.setLink(entry.getValue().toString());
break;
case "expiracy":
notification.setExpiracy(entry.getValue().toString());
break;
case "emails":
notification.setEmails((List<String>) entry.getValue());
break;
}
}
}
if(notification!=null){
final String link=notification.getLink() ;
ZonedDateTime dateTime=ZonedDateTime.now();
if(dateTime.getHour()==20 && dateTime.getMinute()==0 && dateTime.getSecond()==0) {
notification.getEmails().forEach(email->{
try {
sendNotification(link,email,"mail_vm_notification_subject","mail_vm_notification_body");
} catch (MessagingException e) {
e.printStackTrace();
}
}) ;
}
}
i++ ;
Thread.sleep(1000);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
private void sendNotification(String link, String adress, String subject, String body) throws MessagingException {
Locale currentLocale = Locale.getDefault();
ResourceBundle messages = ResourceBundle.getBundle("messages", currentLocale);
ResourceBundle application_config = ResourceBundle.getBundle("application", currentLocale);
String localSubject;
String localBody;
localSubject = messages.getString(subject);
localBody = MessageFormat.format(messages.getString(body), link, link);
System.out.println(localBody) ;
Properties properties=System.getProperties() ;
properties.put("mail.smtp.host",application_config.getString("smtp.server")) ;
properties.put("mail.smtp.auth", application_config.getString("smtp.auth"));
properties.put("mail.smtp.starttls.enable", application_config.getString("smtp.tls"));
properties.put("mail.smtp.port", application_config.getString("smtp.port"));
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(application_config.getString("smtp.username"), application_config.getString("smtp.password"));
}
};
Session session = Session.getInstance(properties,auth) ;
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(application_config.getString("smtp.username")));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(adress));
// Set Subject: header field
message.setSubject(localSubject);
// Send the actual HTML message, as big as you like
message.setContent(localBody, "text/html");
// Send message
Transport.send(message);
}
}

View file

@ -7,6 +7,8 @@ import fr.lirmm.aren.service.AbstractService;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import java.io.File;
import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -35,8 +37,10 @@ public class VMThemeService extends AbstractService<VMTheme> {
} }
return query ; return query ;
} }
public Set<VMTheme> findAll(boolean withChoices, boolean withVotes) { public Set<VMTheme> findAll(boolean withChoices, boolean withVotes) {
return new HashSet<VMTheme>(generateQuery(null, withChoices,withVotes).getResultList()); List<VMTheme> themes=generateQuery(null, withChoices,withVotes).getResultList() ;
return new HashSet<>(themes);
} }
@Override @Override

View file

@ -6,6 +6,8 @@
package fr.lirmm.aren.servlet; package fr.lirmm.aren.servlet;
import fr.lirmm.aren.service.CommentService; import fr.lirmm.aren.service.CommentService;
import fr.lirmm.aren.service.vm.VMSendNotificationService;
import java.util.Calendar; import java.util.Calendar;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -42,6 +44,9 @@ public class BackgroundJobManager implements ServletContextListener {
this.scheduler.scheduleAtFixedRate(() -> { this.scheduler.scheduleAtFixedRate(() -> {
this.commentService.updateAllTags(); this.commentService.updateAllTags();
}, untilMidnight, millisInDay, TimeUnit.MILLISECONDS); }, untilMidnight, millisInDay, TimeUnit.MILLISECONDS);
VMSendNotificationService sendNotificationService=new VMSendNotificationService() ;
sendNotificationService.start();
} }
@Override @Override

View file

@ -43,6 +43,7 @@ public class Root implements Filter {
String path = request.getRequestURI().substring(request.getContextPath().length()); String path = request.getRequestURI().substring(request.getContextPath().length());
if (!path.startsWith("/caslogin") && !path.startsWith("/ws/") && !path.startsWith("/assets/")) { if (!path.startsWith("/caslogin") && !path.startsWith("/ws/") && !path.startsWith("/assets/")) {
request.getRequestDispatcher("/index.jsp").forward(request, response); request.getRequestDispatcher("/index.jsp").forward(request, response);
} else { } else {
chain.doFilter(request, response); chain.doFilter(request, response);
} }

View file

@ -1,7 +1,12 @@
package fr.lirmm.aren.ws.rest; package fr.lirmm.aren.ws.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.lirmm.aren.model.Team;
import fr.lirmm.aren.model.vm.VMChoice; import fr.lirmm.aren.model.vm.VMChoice;
import fr.lirmm.aren.model.vm.VMNotification;
import fr.lirmm.aren.model.vm.VMTeam;
import fr.lirmm.aren.model.vm.VMTheme; import fr.lirmm.aren.model.vm.VMTheme;
import fr.lirmm.aren.service.TeamService;
import fr.lirmm.aren.service.vm.VMChoiceService; import fr.lirmm.aren.service.vm.VMChoiceService;
import fr.lirmm.aren.service.vm.VMThemeService; import fr.lirmm.aren.service.vm.VMThemeService;
import fr.lirmm.aren.service.vm.VMVoteService; import fr.lirmm.aren.service.vm.VMVoteService;
@ -14,6 +19,9 @@ import javax.inject.Inject;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import java.io.File;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.*; import java.util.*;
/** /**
@ -32,6 +40,9 @@ public class VMThemeRESTFacade extends AbstractRESTFacade<VMTheme>{
@Inject @Inject
VMVoteService voteService ; VMVoteService voteService ;
@Inject
TeamService teamService ;
@Override @Override
protected VMThemeService getService() { protected VMThemeService getService() {
return themeService; return themeService;
@ -41,8 +52,40 @@ public class VMThemeRESTFacade extends AbstractRESTFacade<VMTheme>{
//@RolesAllowed({"ADMIN"}) //@RolesAllowed({"ADMIN"})
@PermitAll @PermitAll
public VMTheme create(VMTheme theme){ public VMTheme create(VMTheme theme){
System.out.println(theme.toString()); VMTheme res= super.create(theme);
return super.create(theme); try{
File file = new File("/aren/tmp/vote_majoritaire.json");
File directory = new File("/aren/tmp");
if (! directory.exists()){
directory.mkdirs() ;
}
System.out.println(file.getAbsolutePath());
if(!file.exists())
file.createNewFile() ;
VMNotification notification=new VMNotification() ;
List<String> emails=new ArrayList<>() ;
Team team=teamService.find(theme.getTeam().getId());
team.getUsers().forEach(user->{
emails.add(user.getEmail()) ;
});
notification.setExpiracy(theme.getExpiracyDate().toString());
String serverRoot = request.getRequestURL().substring(0, request.getRequestURL().length() - "/ws/vm/themes".length());
notification.setLink(serverRoot+"/votemajoritairedetails?id="+res.getId());
notification.setEmails(emails);
String []emails_array=new String[notification.getEmails().size()] ;
for(int j=0 ; j<emails_array.length ; j++){
emails_array[j]=notification.getEmails().get(j) ;
}
Map<String, Object> map = new HashMap<>() ;
map.put("link",notification.getLink()) ;
map.put("expiracy",notification.getExpiracy()) ;
map.put("emails",emails_array) ;
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(file,map);
}catch (IOException e) {
e.printStackTrace();
}
return res ;
} }
@Override @Override

View file

@ -23,3 +23,6 @@ mail_new_user_body: <p>Bonjour,</p><p>Pour poursuivre l''activation de votre com
mail_reset_password_subject: R&eacute;initialisation du mot de passe mail_reset_password_subject: R&eacute;initialisation du mot de passe
mail_reset_password_body: <p>Bonjour,</p><p>Une demande de r&eacute;initialisation de mot de passe &agrave; &eacute;t&eacute; faite en votre nom pour la plateforme AREN.</p><p>Si vous &ecirc;tes l''auteur\u22c5e de cette demande, pour r&eacute;initialiser votre mot de passe, <a href="{0}">cliquer ici</a></p><p>Si le lien ne fonctionne pas, veuillez copier-coller l''url suivante dans la barre d''adresse de votre navigateur : <br><span style="color:blue">{0}</span></p><br><p><b>Attention ce lien n''est valable que pendant 24h</b>.</p><br><p>Si vous n''&ecirc;tes pas l''auteur\u22c5e de cette demande, vous pouvez ignorer cet e-mail.</p><br><p>PS : Veuillez ne pas r&eacute;pondre &agrave; ce mail qui est envoy&eacute; automatiquement par l''application de gestion de comptes informatiques.</p> mail_reset_password_body: <p>Bonjour,</p><p>Une demande de r&eacute;initialisation de mot de passe &agrave; &eacute;t&eacute; faite en votre nom pour la plateforme AREN.</p><p>Si vous &ecirc;tes l''auteur\u22c5e de cette demande, pour r&eacute;initialiser votre mot de passe, <a href="{0}">cliquer ici</a></p><p>Si le lien ne fonctionne pas, veuillez copier-coller l''url suivante dans la barre d''adresse de votre navigateur : <br><span style="color:blue">{0}</span></p><br><p><b>Attention ce lien n''est valable que pendant 24h</b>.</p><br><p>Si vous n''&ecirc;tes pas l''auteur\u22c5e de cette demande, vous pouvez ignorer cet e-mail.</p><br><p>PS : Veuillez ne pas r&eacute;pondre &agrave; ce mail qui est envoy&eacute; automatiquement par l''application de gestion de comptes informatiques.</p>
mail_vm_notification_subject: Notification sur le vote
mail_vm_notification_body : <p>Bonjour,</p><p>Pour poursuivre l''evolution du vote sur la plateforme AREN veuillez <a href="{0}">cliquer ici</a></p><p>Si le lien ne fonctionne pas, veuillez copier-coller l''url suivante dans la barre d''adresse de votre navigateur : <br><span style="font-size: 10px;text-decoration: underline;">{0}</span></p><br><br><p>PS : Veuillez ne pas r&eacute;pondre &agrave; ce mail qui est envoy&eacute; automatiquement par l''application de gestion de comptes informatiques.</p>