argos/dmp-backend/src/main/java/eu/eudat/managers/InvitationsManager.java

49 lines
3.1 KiB
Java
Raw Normal View History

2018-01-05 08:47:52 +01:00
package eu.eudat.managers;
import eu.eudat.entities.DMP;
import eu.eudat.entities.UserInfo;
2018-02-07 10:56:30 +01:00
import eu.eudat.exceptions.security.UnauthorisedException;
2018-01-05 08:47:52 +01:00
import eu.eudat.models.invitation.Invitation;
2018-01-05 16:40:19 +01:00
import eu.eudat.models.security.Principal;
2018-01-05 08:47:52 +01:00
import eu.eudat.models.userinfo.UserInfoInvitationModel;
import eu.eudat.models.userinfo.UserInfoRequestItem;
import eu.eudat.queryable.QueryableList;
import eu.eudat.services.ApiContext;
import eu.eudat.utilities.builders.DomainModelConverter;
import java.util.List;
2018-01-08 12:44:48 +01:00
import java.util.UUID;
2018-01-05 08:47:52 +01:00
import java.util.stream.Collectors;
2018-02-01 10:08:06 +01:00
2018-01-05 08:47:52 +01:00
public class InvitationsManager {
2018-01-17 13:03:51 +01:00
public static void inviteUsers(ApiContext apiContext, Invitation invitation, Principal principal) throws Exception {
2018-01-05 16:40:19 +01:00
UserInfo principalUser = new UserInfo();
principalUser.setId(principal.getId());
2018-01-05 08:47:52 +01:00
List<UserInfoInvitationModel> alreadySignedInUsers = invitation.getUsers().stream().filter(item -> item.getId() != null).collect(Collectors.toList());
List<UserInfo> alreadySignedInUsersEntities = new DomainModelConverter<UserInfo, UserInfoInvitationModel>().toDataModel(alreadySignedInUsers);
DMP dataManagementPlan = apiContext.getDatabaseRepository().getDmpDao().find(invitation.getDataManagementPlan());
2018-01-08 12:44:48 +01:00
apiContext.getInvitationService().createInvitations(apiContext.getDatabaseRepository().getInvitationDao(), apiContext.getMailService(), new DomainModelConverter<UserInfo, UserInfoInvitationModel>().toDataModel(invitation.getUsers()), dataManagementPlan, principalUser);
2018-01-05 08:47:52 +01:00
apiContext.getInvitationService().assignToDmp(apiContext.getDatabaseRepository().getDmpDao(), alreadySignedInUsersEntities, dataManagementPlan);
}
2018-01-08 12:44:48 +01:00
public static List<UserInfoInvitationModel> getUsers(ApiContext apiContext, UserInfoRequestItem userInfoRequestItem) throws InstantiationException, IllegalAccessException {
2018-01-05 08:47:52 +01:00
QueryableList<UserInfo> users = apiContext.getDatabaseRepository().getUserInfoDao().getWithCriteria(userInfoRequestItem.getCriteria());
2018-01-08 12:44:48 +01:00
List<UserInfoInvitationModel> userModels = new DomainModelConverter<UserInfo, UserInfoInvitationModel>().fromDataModel(users.toList(), UserInfoInvitationModel.class);
2018-01-05 08:47:52 +01:00
return userModels;
}
2018-01-08 12:44:48 +01:00
2018-01-08 15:57:21 +01:00
public static UUID assignUserAcceptedInvitation(ApiContext apiContext, UUID invitationID, Principal principal) throws UnauthorisedException {
2018-01-08 12:44:48 +01:00
eu.eudat.entities.Invitation invitation = apiContext.getDatabaseRepository().getInvitationDao().find(invitationID);
2018-01-08 15:57:21 +01:00
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");
2018-01-08 12:44:48 +01:00
UserInfo invitedUser = apiContext.getDatabaseRepository().getUserInfoDao().find(principal.getId());
DMP datamanagementPlan = invitation.getDmp();
apiContext.getInvitationService().assignToDmp(apiContext.getDatabaseRepository().getDmpDao(), invitedUser, datamanagementPlan);
invitation.setAcceptedInvitation(true);
apiContext.getDatabaseRepository().getInvitationDao().createOrUpdate(invitation);
return datamanagementPlan.getId();
}
2018-01-05 08:47:52 +01:00
}