178 lines
12 KiB
Java
178 lines
12 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.commons.enums.DmpUserRole;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.DmpEntity;
|
|
import eu.eudat.data.DmpUserEntity;
|
|
import eu.eudat.data.UserEntity;
|
|
import eu.eudat.exceptions.security.UnauthorisedException;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.model.User;
|
|
import eu.eudat.models.data.invitation.Invitation;
|
|
import eu.eudat.query.DmpQuery;
|
|
import eu.eudat.query.UserQuery;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import jakarta.xml.bind.JAXBException;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class InvitationsManager {
|
|
|
|
private ApiContext apiContext;
|
|
private DataManagementPlanManager dataManagementPlanManager;
|
|
private final UserScope userScope;
|
|
private final QueryFactory queryFactory;
|
|
|
|
@Autowired
|
|
public InvitationsManager(ApiContext apiContext, DataManagementPlanManager dataManagementPlanManager, UserScope userScope, QueryFactory queryFactory) {
|
|
this.apiContext = apiContext;
|
|
this.dataManagementPlanManager = dataManagementPlanManager;
|
|
this.userScope = userScope;
|
|
this.queryFactory = queryFactory;
|
|
}
|
|
|
|
public void inviteUsers(Invitation invitation) throws Exception {
|
|
UserEntity principalUser = new UserEntity();
|
|
principalUser.setId(this.userScope.getUserIdSafe());
|
|
invitation.getUsers().stream().filter(item -> item.getId() == null).forEach(item -> {
|
|
UserEntity existingUser = null;
|
|
// try {
|
|
// //TODO: email
|
|
// //existingUser = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().asQueryable().where((builder, root) -> builder.equal(root.get("email"), item.getEmail())).getSingleOrDefault();
|
|
// } catch (InvalidApplicationException e) {
|
|
// throw new RuntimeException(e);
|
|
// }
|
|
if (existingUser != null) {
|
|
item.setId(existingUser.getId());
|
|
}
|
|
});
|
|
List<User> alreadySignedInUsers = invitation.getUsers().stream().filter(item -> item.getId() != null).collect(Collectors.toList());
|
|
List<UserEntity> alreadySignedInUsersEntities = new ArrayList<>(); //alreadySignedInUsers.stream().map(User::toDataModel).collect(Collectors.toList());
|
|
List<DmpUserEntity> userInfoToUserDmp = new LinkedList<>();
|
|
for (UserEntity userInfo : alreadySignedInUsersEntities) {
|
|
DmpUserEntity userDMP = new DmpUserEntity();
|
|
userDMP.setUserId(userInfo.getId());
|
|
userDMP.setRole(DmpUserRole.of(invitation.getRole().shortValue()));
|
|
userInfoToUserDmp.add(userDMP);
|
|
/*if (!apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().areAssociated(principalUser, userInfo)) {
|
|
UserAssociation userAssociation = new UserAssociation();
|
|
userAssociation.setFirstUser(principalUser);
|
|
userAssociation.setSecondUser(userInfo);
|
|
apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().createOrUpdate(userAssociation);
|
|
}*/
|
|
}
|
|
DmpQuery dmpQuery = this.queryFactory.query(DmpQuery.class).ids(invitation.getDataManagementPlan());
|
|
if(dmpQuery != null){
|
|
// DmpEntity dataManagementPlan = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(invitation.getDataManagementPlan());
|
|
DmpEntity dmpEntity = dmpQuery.first();
|
|
UserQuery userQuery = this.queryFactory.query(UserQuery.class).ids(invitation.getUsers().stream().map(user -> user.getId()).collect(Collectors.toList()));
|
|
if (userQuery != null){
|
|
List<UserEntity> userEntities = userQuery.collect();
|
|
// apiContext.getUtilitiesService().getInvitationService().createInvitations(apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao(), invitation.getUsers().stream().map(UserInfoInvitationModel::toDataModel).collect(Collectors.toList()), dmpEntity, invitation.getRole(), principalUser);
|
|
apiContext.getUtilitiesService().getInvitationService().createInvitations(apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao(), userEntities, dmpEntity, invitation.getRole(), principalUser);
|
|
apiContext.getUtilitiesService().getInvitationService().assignToDmp(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao(), userInfoToUserDmp, dmpEntity);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public List<User> getUsers() throws InstantiationException, IllegalAccessException, InvalidApplicationException {
|
|
/*UserInfo principalUser = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
|
|
List<UserInfo> users = apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().getAssociated(principalUser).stream().map(userAssociation -> {
|
|
if (userAssociation.getFirstUser().getId().equals(principal.getId())) {
|
|
return userAssociation.getSecondUser();
|
|
} else {
|
|
return userAssociation.getFirstUser();
|
|
}
|
|
}).collect(Collectors.toList());*/
|
|
List<UserEntity> users = new ArrayList<>();
|
|
//TODO
|
|
// apiContext.getOperationsContext().getDatabaseRepository().getDmpDao()
|
|
// .getAuthenticated(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable()
|
|
// .where(((builder, root) -> builder.notEqual(root.get("isActive"), IsActive.Inactive))), this.userScope.getUserId(), Stream.of(0, 1).collect(Collectors.toList()))
|
|
// .toList().stream().map(DmpEntity::getUsers).flatMap(Collection::stream).map(DmpUser::getUser)
|
|
// .filter(userInfo -> !userInfo.getId().equals(this.userScope.getUserIdSafe())).filter(StreamDistinctBy.distinctByKey(UserInfo::getId)).collect(Collectors.toList());
|
|
List<User> userModels = new ArrayList<>(); //users.stream().map(userInfo -> new User().fromDataModel(userInfo)).collect(Collectors.toList());
|
|
return userModels;
|
|
}
|
|
|
|
// public List<UserInfoInvitationModel> getUsersWithCriteria(UserInfoRequestItem userInfoRequestItem) throws IllegalAccessException, InstantiationException, InvalidApplicationException {
|
|
// //TODO
|
|
//// List<UserInfo> users = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao()
|
|
//// .getAuthenticated(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable()
|
|
//// .where(((builder, root) ->
|
|
//// builder.notEqual(root.get("status"), DMP.DMPStatus.DELETED.getValue()))), this.userScope.getUserId(), Stream.of(0, 1).collect(Collectors.toList()))
|
|
//// .toList().stream().map(DMP::getUsers).flatMap(Collection::stream).map(UserDMP::getUser)
|
|
//// .filter(userInfo -> !userInfo.getId().equals(this.userScope.getUserIdSafe())).filter(StreamDistinctBy.distinctByKey(UserInfo::getId))
|
|
//// .filter(userInfo -> (userInfoRequestItem == null || userInfoRequestItem.getCriteria() == null || userInfoRequestItem.getCriteria().getLike() == null
|
|
//// || userInfo.getName().toLowerCase().contains(userInfoRequestItem.getCriteria().getLike().toLowerCase())
|
|
//// || (userInfo.getEmail().toLowerCase().contains(userInfoRequestItem.getCriteria().getLike().toLowerCase()))))
|
|
//// .collect(Collectors.toList());
|
|
////// .where((builder, root) -> builder.like(builder.upper(root.get("name")), "%" + userInfoRequestItem.getCriteria().getLike().toUpperCase() + "%"))
|
|
////
|
|
//// List<UserInfoInvitationModel> userModels = users.stream().map(userInfo -> new UserInfoInvitationModel().fromDataModel(userInfo)).collect(Collectors.toList());
|
|
//// return userModels;
|
|
// return new ArrayList<>();
|
|
// }
|
|
|
|
public UUID assignUserAcceptedInvitation(UUID invitationID) throws UnauthorisedException, JAXBException, IOException, InvalidApplicationException {
|
|
//TODO
|
|
// eu.eudat.data.old.Invitation invitation = apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao().find(invitationID);
|
|
// if (invitation == null)
|
|
// throw new UnauthorisedException("There is no Data Management Plan assigned to this Link");
|
|
// if (invitation.getAcceptedInvitation()) return invitation.getDmp().getId(); //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(this.userScope.getUserId());
|
|
// 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"), invitedUser.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);
|
|
// datamanagementPlan.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), datamanagementPlan.getId())).toList()));
|
|
// //dataManagementPlanManager.updateIndex(datamanagementPlan); //TODO
|
|
// return datamanagementPlan.getId();
|
|
// }
|
|
// } else {
|
|
// 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())) {
|
|
// UserAssociation userAssociation = new UserAssociation();
|
|
// userAssociation.setFirstUser(invitedUser);
|
|
// userAssociation.setSecondUser(invitation.getUser());
|
|
// apiContext.getOperationsContext().getDatabaseRepository().getUserAssociationDao().createOrUpdate(userAssociation);
|
|
// }*/
|
|
// DmpEntity datamanagementPlan = invitation.getDmp();
|
|
// apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP);
|
|
// apiContext.getUtilitiesService().getInvitationService().assignToDmp(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao(), userDMP, datamanagementPlan);
|
|
// invitation.setAcceptedInvitation(true);
|
|
// apiContext.getOperationsContext().getDatabaseRepository().getInvitationDao().createOrUpdate(invitation);
|
|
// datamanagementPlan.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), datamanagementPlan.getId())).toList()));
|
|
//// dataManagementPlanManager.updateIndex(datamanagementPlan); //TODo
|
|
// return datamanagementPlan.getId();
|
|
// }
|
|
// return invitation.getDmp().getId();
|
|
return null;
|
|
}
|
|
}
|