Fixed invitation system and made it to check if a user is already invited to the DMP

This commit is contained in:
George Kalampokis 2020-07-10 13:53:27 +03:00
parent 7b039da231
commit fcbb0dfdf8
3 changed files with 72 additions and 23 deletions

View File

@ -7,8 +7,10 @@ import eu.eudat.exceptions.security.UnauthorisedException;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.helpers.StreamDistinctBy;
import eu.eudat.models.data.invitation.Invitation;
import eu.eudat.models.data.invitation.Properties;
import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.userinfo.UserInfoInvitationModel;
import eu.eudat.queryable.QueryableList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -76,15 +78,29 @@ public class InvitationsManager {
if (invitation == null)
throw new UnauthorisedException("There is no Data Management Plan assigned to this Link");
if (invitation.getAcceptedInvitation()) throw new UnauthorisedException("This Url Has Expired");
JAXBContext context = JAXBContext.newInstance(Properties.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Properties properties = (Properties) unmarshaller.unmarshal(new StringReader(invitation.getProperties()));
UserInfo invitedUser = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
QueryableList<UserDMP> userDMPQueryableList = apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where(((builder, root) -> builder.and(builder.equal(root.get("dmp").get("id"), invitation.getDmp().getId()), builder.equal(root.get("user").get("id"), invitation.getUser().getId()))));
UserDMP existingUserDMP = userDMPQueryableList.getSingleOrDefault();
if (existingUserDMP != null) {
if (properties.getRole() != null && existingUserDMP.getRole() > properties.getRole()) {
existingUserDMP.setRole(properties.getRole());
DMP datamanagementPlan = invitation.getDmp();
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(existingUserDMP);
apiContext.getUtilitiesService().getInvitationService().assignToDmp(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao(), existingUserDMP, datamanagementPlan);
invitation.setAcceptedInvitation(true);
apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao().createOrUpdate(invitation);
return datamanagementPlan.getId();
}
} else {
UserDMP userDMP = new UserDMP();
userDMP.setUser(invitedUser);
userDMP.setDmp(invitation.getDmp());
JAXBContext context = JAXBContext.newInstance(HashMap.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Map<String, Object> properties = (Map<String, Object>) unmarshaller.unmarshal(new StringReader(invitation.getProperties()));
if (properties.get("role") != null) {
userDMP.setRole((Integer) properties.get("role"));
if (properties.getRole() != null) {
userDMP.setRole(properties.getRole());
} else {
userDMP.setRole(UserDMP.UserDMPRoles.USER.getValue());
}
@ -101,4 +117,6 @@ public class InvitationsManager {
apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao().createOrUpdate(invitation);
return datamanagementPlan.getId();
}
return invitation.getDmp().getId();
}
}

View File

@ -6,6 +6,7 @@ import eu.eudat.data.dao.entities.InvitationDao;
import eu.eudat.data.entities.DMP;
import eu.eudat.data.entities.Invitation;
import eu.eudat.data.entities.UserDMP;
import eu.eudat.models.data.invitation.Properties;
import eu.eudat.models.data.mail.SimpleMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,6 +15,9 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -55,8 +59,17 @@ public class InvitationServiceImpl implements InvitationService {
invitation.setUser(creator);
invitation.setToken(UUID.randomUUID());
invitation.setAcceptedInvitation(false);
invitation.setProperties("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
invitation.setProperties(invitation.getProperties() + "\n" +"<role>" + role + "</role>");
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);
}
invitationDao.createOrUpdate(invitation);
sendInvitationAsync(dmp, invitation, userInfo.getName(), mailService, role);
}
@ -66,8 +79,8 @@ public class InvitationServiceImpl implements InvitationService {
public CompletableFuture sendInvitationAsync(DMP dmp, Invitation invitation, String recipient, MailService mailService, Integer role) {
return CompletableFuture.runAsync(() -> {
SimpleMail mail = new SimpleMail();
mail.setSubject(createSubject(dmp, mailService.getMailTemplateSubject(), role));
mail.setContent(createContent(invitation.getId(), dmp, recipient, mailService.getMailTemplateContent("classpath:templates/email/email.html")));
mail.setSubject(createSubject(dmp, mailService.getMailTemplateSubject()));
mail.setContent(createContent(invitation.getId(), dmp, recipient, mailService.getMailTemplateContent("classpath:templates/email/email.html"), role));
mail.setTo(invitation.getInvitationEmail());
try {
mailService.sendSimpleMail(mail);
@ -77,17 +90,17 @@ public class InvitationServiceImpl implements InvitationService {
});
}
private String createSubject(DMP dmp, String templateSubject, Integer role) {
private String createSubject(DMP dmp, String templateSubject) {
String subject = templateSubject.replace("{dmpname}", dmp.getLabel());
subject = subject.replace("{dmprole}", UserDMP.UserDMPRoles.fromInteger(role).name());
return subject;
}
private String createContent(UUID invitationID, DMP dmp, String recipient, String templateContent) {
private String createContent(UUID invitationID, DMP dmp, String recipient, String templateContent, Integer role) {
String content = templateContent.replace("{dmpname}", dmp.getLabel());
content = content.replace("{invitationID}", invitationID.toString());
content = content.replace("{recipient}", recipient);
content = content.replace("{host}", this.environment.getProperty("dmp.domain"));
content = content.replace("{dmprole}", UserDMP.UserDMPRoles.fromInteger(role).name());
return content;
}

View File

@ -0,0 +1,18 @@
package eu.eudat.models.data.invitation;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Properties {
private Integer role;
@XmlElement(name = "role")
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
}