2018-06-27 12:29:21 +02:00
|
|
|
package eu.eudat.logic.services.utilities;
|
2018-01-05 08:47:52 +01:00
|
|
|
|
2020-06-10 12:18:04 +02:00
|
|
|
|
2018-03-21 11:57:56 +01:00
|
|
|
import eu.eudat.data.dao.entities.DMPDao;
|
|
|
|
import eu.eudat.data.dao.entities.InvitationDao;
|
|
|
|
import eu.eudat.data.entities.DMP;
|
|
|
|
import eu.eudat.data.entities.Invitation;
|
2020-07-10 09:11:41 +02:00
|
|
|
import eu.eudat.data.entities.UserDMP;
|
2020-07-10 12:53:27 +02:00
|
|
|
import eu.eudat.models.data.invitation.Properties;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.models.data.mail.SimpleMail;
|
2020-01-16 16:46:24 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-10-09 12:04:03 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.core.env.Environment;
|
2018-01-05 08:47:52 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2018-08-30 13:09:36 +02:00
|
|
|
import javax.mail.MessagingException;
|
2022-01-21 09:58:29 +01:00
|
|
|
import jakarta.xml.bind.JAXBContext;
|
|
|
|
import jakarta.xml.bind.Marshaller;
|
2020-07-10 12:53:27 +02:00
|
|
|
import java.io.StringWriter;
|
2018-01-05 08:47:52 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2018-10-09 12:04:03 +02:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2018-10-10 09:48:41 +02:00
|
|
|
import java.util.stream.Collectors;
|
2018-01-05 08:47:52 +01:00
|
|
|
|
2018-02-01 10:08:06 +01:00
|
|
|
|
2018-01-05 08:47:52 +01:00
|
|
|
@Service("invitationService")
|
|
|
|
public class InvitationServiceImpl implements InvitationService {
|
2020-01-16 16:46:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(InvitationServiceImpl.class);
|
2018-10-09 12:04:03 +02:00
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
@Autowired
|
2020-06-10 12:18:04 +02:00
|
|
|
public InvitationServiceImpl(Environment environment) {
|
2018-10-09 12:04:03 +02:00
|
|
|
this.environment = environment;
|
|
|
|
}
|
|
|
|
|
2018-01-05 08:47:52 +01:00
|
|
|
@Override
|
2019-05-10 10:33:48 +02:00
|
|
|
public void assignToDmp(DMPDao dmpDao, List<eu.eudat.data.entities.UserDMP> users, DMP dmp) {
|
|
|
|
for (eu.eudat.data.entities.UserDMP user : users) {
|
2018-01-05 08:47:52 +01:00
|
|
|
dmp.getUsers().add(user);
|
|
|
|
}
|
|
|
|
dmpDao.createOrUpdate(dmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-10 10:33:48 +02:00
|
|
|
public void assignToDmp(DMPDao dmpDao, eu.eudat.data.entities.UserDMP user, DMP dmp) {
|
2019-05-13 12:30:35 +02:00
|
|
|
if (!dmp.getUsers().stream().map(x -> x.getUser().getId()).collect(Collectors.toList()).contains(user.getId())) {
|
2018-10-10 09:48:41 +02:00
|
|
|
dmp.getUsers().add(user);
|
|
|
|
dmpDao.createOrUpdate(dmp);
|
|
|
|
}
|
2018-01-05 08:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-10 09:11:41 +02:00
|
|
|
public void createInvitations(InvitationDao invitationDao, MailService mailService, List<eu.eudat.data.entities.UserInfo> users, DMP dmp, Integer role, eu.eudat.data.entities.UserInfo creator) throws MessagingException {
|
2018-03-21 11:57:56 +01:00
|
|
|
for (eu.eudat.data.entities.UserInfo userInfo : users) {
|
2018-01-05 08:47:52 +01:00
|
|
|
Invitation invitation = new Invitation();
|
|
|
|
invitation.setDmp(dmp);
|
|
|
|
invitation.setInvitationEmail(userInfo.getEmail());
|
2018-01-05 16:40:19 +01:00
|
|
|
invitation.setUser(creator);
|
2018-01-05 08:47:52 +01:00
|
|
|
invitation.setToken(UUID.randomUUID());
|
2018-01-08 15:57:21 +01:00
|
|
|
invitation.setAcceptedInvitation(false);
|
2020-07-10 12:53:27 +02:00
|
|
|
Properties properties = new Properties();
|
|
|
|
properties.setRole(role);
|
|
|
|
try {
|
|
|
|
JAXBContext context = JAXBContext.newInstance(Properties.class);
|
|
|
|
Marshaller marshaller = context.createMarshaller();
|
|
|
|
StringWriter propertyWriter = new StringWriter();
|
|
|
|
marshaller.marshal(properties, propertyWriter);
|
|
|
|
invitation.setProperties(propertyWriter.toString());
|
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
2018-01-05 08:47:52 +01:00
|
|
|
invitationDao.createOrUpdate(invitation);
|
2020-07-10 09:11:41 +02:00
|
|
|
sendInvitationAsync(dmp, invitation, userInfo.getName(), mailService, role);
|
2018-01-05 08:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-10 09:11:41 +02:00
|
|
|
public CompletableFuture sendInvitationAsync(DMP dmp, Invitation invitation, String recipient, MailService mailService, Integer role) {
|
2018-10-09 12:04:03 +02:00
|
|
|
return CompletableFuture.runAsync(() -> {
|
|
|
|
SimpleMail mail = new SimpleMail();
|
2020-07-10 12:53:27 +02:00
|
|
|
mail.setSubject(createSubject(dmp, mailService.getMailTemplateSubject()));
|
2021-10-25 15:06:08 +02:00
|
|
|
mail.setContent(createContent(invitation.getId(), dmp, recipient, mailService.getMailTemplateContent(this.environment.getProperty("email.invite")), role));
|
2018-10-09 12:04:03 +02:00
|
|
|
mail.setTo(invitation.getInvitationEmail());
|
|
|
|
try {
|
|
|
|
mailService.sendSimpleMail(mail);
|
2018-10-10 09:48:41 +02:00
|
|
|
} catch (Exception ex) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(ex.getMessage(), ex);
|
2018-10-09 12:04:03 +02:00
|
|
|
}
|
|
|
|
});
|
2018-01-08 12:44:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 12:53:27 +02:00
|
|
|
private String createSubject(DMP dmp, String templateSubject) {
|
2018-01-08 12:44:48 +01:00
|
|
|
String subject = templateSubject.replace("{dmpname}", dmp.getLabel());
|
|
|
|
return subject;
|
|
|
|
}
|
2018-01-05 08:47:52 +01:00
|
|
|
|
2020-07-10 12:53:27 +02:00
|
|
|
private String createContent(UUID invitationID, DMP dmp, String recipient, String templateContent, Integer role) {
|
2018-01-08 12:44:48 +01:00
|
|
|
String content = templateContent.replace("{dmpname}", dmp.getLabel());
|
2018-01-08 15:57:21 +01:00
|
|
|
content = content.replace("{invitationID}", invitationID.toString());
|
2018-08-30 13:09:36 +02:00
|
|
|
content = content.replace("{recipient}", recipient);
|
2018-10-09 12:04:03 +02:00
|
|
|
content = content.replace("{host}", this.environment.getProperty("dmp.domain"));
|
2020-07-10 12:53:27 +02:00
|
|
|
content = content.replace("{dmprole}", UserDMP.UserDMPRoles.fromInteger(role).name());
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-01-08 12:44:48 +01:00
|
|
|
return content;
|
2018-01-05 08:47:52 +01:00
|
|
|
}
|
|
|
|
}
|