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.services.ApiContext;
import eu.eudat.logic.utilities.helpers.StreamDistinctBy; import eu.eudat.logic.utilities.helpers.StreamDistinctBy;
import eu.eudat.models.data.invitation.Invitation; 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.security.Principal;
import eu.eudat.models.data.userinfo.UserInfoInvitationModel; import eu.eudat.models.data.userinfo.UserInfoInvitationModel;
import eu.eudat.queryable.QueryableList;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -76,29 +78,45 @@ public class InvitationsManager {
if (invitation == null) if (invitation == null)
throw new UnauthorisedException("There is no Data Management Plan assigned to this Link"); throw new UnauthorisedException("There is no Data Management Plan assigned to this Link");
if (invitation.getAcceptedInvitation()) throw new UnauthorisedException("This Url Has Expired"); if (invitation.getAcceptedInvitation()) throw new UnauthorisedException("This Url Has Expired");
UserInfo invitedUser = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId()); JAXBContext context = JAXBContext.newInstance(Properties.class);
UserDMP userDMP = new UserDMP();
userDMP.setUser(invitedUser);
userDMP.setDmp(invitation.getDmp());
JAXBContext context = JAXBContext.newInstance(HashMap.class);
Unmarshaller unmarshaller = context.createUnmarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller();
Map<String, Object> properties = (Map<String, Object>) unmarshaller.unmarshal(new StringReader(invitation.getProperties())); Properties properties = (Properties) unmarshaller.unmarshal(new StringReader(invitation.getProperties()));
if (properties.get("role") != null) { UserInfo invitedUser = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
userDMP.setRole((Integer) properties.get("role")); 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 { } else {
userDMP.setRole(UserDMP.UserDMPRoles.USER.getValue()); UserDMP userDMP = new UserDMP();
} userDMP.setUser(invitedUser);
userDMP.setDmp(invitation.getDmp());
if (properties.getRole() != null) {
userDMP.setRole(properties.getRole());
} else {
userDMP.setRole(UserDMP.UserDMPRoles.USER.getValue());
}
/*if (!apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().areAssociated(invitedUser, invitation.getUser())) { /*if (!apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().areAssociated(invitedUser, invitation.getUser())) {
UserAssociation userAssociation = new UserAssociation(); UserAssociation userAssociation = new UserAssociation();
userAssociation.setFirstUser(invitedUser); userAssociation.setFirstUser(invitedUser);
userAssociation.setSecondUser(invitation.getUser()); userAssociation.setSecondUser(invitation.getUser());
apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().createOrUpdate(userAssociation); apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().createOrUpdate(userAssociation);
}*/ }*/
DMP datamanagementPlan = invitation.getDmp(); DMP datamanagementPlan = invitation.getDmp();
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP); apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP);
apiContext.getUtilitiesService().getInvitationService().assignToDmp(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao(), userDMP, datamanagementPlan); apiContext.getUtilitiesService().getInvitationService().assignToDmp(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao(), userDMP, datamanagementPlan);
invitation.setAcceptedInvitation(true); invitation.setAcceptedInvitation(true);
apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao().createOrUpdate(invitation); apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao().createOrUpdate(invitation);
return datamanagementPlan.getId(); 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.DMP;
import eu.eudat.data.entities.Invitation; import eu.eudat.data.entities.Invitation;
import eu.eudat.data.entities.UserDMP; import eu.eudat.data.entities.UserDMP;
import eu.eudat.models.data.invitation.Properties;
import eu.eudat.models.data.mail.SimpleMail; import eu.eudat.models.data.mail.SimpleMail;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -14,6 +15,9 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.mail.MessagingException; 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.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -55,8 +59,17 @@ public class InvitationServiceImpl implements InvitationService {
invitation.setUser(creator); invitation.setUser(creator);
invitation.setToken(UUID.randomUUID()); invitation.setToken(UUID.randomUUID());
invitation.setAcceptedInvitation(false); invitation.setAcceptedInvitation(false);
invitation.setProperties("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); Properties properties = new Properties();
invitation.setProperties(invitation.getProperties() + "\n" +"<role>" + role + "</role>"); 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); invitationDao.createOrUpdate(invitation);
sendInvitationAsync(dmp, invitation, userInfo.getName(), mailService, role); 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) { public CompletableFuture sendInvitationAsync(DMP dmp, Invitation invitation, String recipient, MailService mailService, Integer role) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
SimpleMail mail = new SimpleMail(); SimpleMail mail = new SimpleMail();
mail.setSubject(createSubject(dmp, mailService.getMailTemplateSubject(), role)); mail.setSubject(createSubject(dmp, mailService.getMailTemplateSubject()));
mail.setContent(createContent(invitation.getId(), dmp, recipient, mailService.getMailTemplateContent("classpath:templates/email/email.html"))); mail.setContent(createContent(invitation.getId(), dmp, recipient, mailService.getMailTemplateContent("classpath:templates/email/email.html"), role));
mail.setTo(invitation.getInvitationEmail()); mail.setTo(invitation.getInvitationEmail());
try { try {
mailService.sendSimpleMail(mail); 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()); String subject = templateSubject.replace("{dmpname}", dmp.getLabel());
subject = subject.replace("{dmprole}", UserDMP.UserDMPRoles.fromInteger(role).name());
return subject; 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()); String content = templateContent.replace("{dmpname}", dmp.getLabel());
content = content.replace("{invitationID}", invitationID.toString()); content = content.replace("{invitationID}", invitationID.toString());
content = content.replace("{recipient}", recipient); content = content.replace("{recipient}", recipient);
content = content.replace("{host}", this.environment.getProperty("dmp.domain")); content = content.replace("{host}", this.environment.getProperty("dmp.domain"));
content = content.replace("{dmprole}", UserDMP.UserDMPRoles.fromInteger(role).name());
return content; 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;
}
}