#8916 - when merging acoounts, save the main profile user as collaborator to elastic dmps/datasets

This commit is contained in:
Bernaldo Mihasi 2023-07-27 10:07:40 +03:00
parent e01709f886
commit 634d49ea35
1 changed files with 40 additions and 0 deletions

View File

@ -5,6 +5,10 @@ import eu.eudat.data.entities.EmailConfirmation;
import eu.eudat.data.entities.UserDMP;
import eu.eudat.data.entities.UserInfo;
import eu.eudat.data.entities.UserToken;
import eu.eudat.elastic.criteria.DmpCriteria;
import eu.eudat.elastic.entities.Collaborator;
import eu.eudat.elastic.entities.Dmp;
import eu.eudat.elastic.repository.DmpRepository;
import eu.eudat.exceptions.emailconfirmation.HasConfirmedEmailException;
import eu.eudat.exceptions.emailconfirmation.TokenExpiredException;
import eu.eudat.logic.services.ApiContext;
@ -25,17 +29,21 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
public class MergeEmailConfirmationManager {
private static Logger logger = LoggerFactory.getLogger(MergeEmailConfirmationManager.class);
private ApiContext apiContext;
private DatabaseRepository databaseRepository;
private DmpRepository dmpRepository;
@Autowired
public MergeEmailConfirmationManager(ApiContext apiContext) {
this.apiContext = apiContext;
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.dmpRepository = apiContext.getOperationsContext().getElasticRepository().getDmpRepository();
}
@Transactional
@ -91,6 +99,38 @@ public class MergeEmailConfirmationManager {
userDmp.setUser(newUser);
databaseRepository.getUserDmpDao().createOrUpdate(userDmp);
});
try {
DmpCriteria dmpCriteria = new DmpCriteria();
dmpCriteria.setCollaborators(Collections.singletonList(oldUser.getId()));
List<Dmp> elasticDmpsIds = dmpRepository.query(dmpCriteria);
for(Dmp dmpId: elasticDmpsIds){
Dmp dmp = dmpRepository.findDocument(dmpId.getId().toString());
if(dmp.getDatasets() != null) {
dmp.getDatasets().forEach(dataset -> {
if(dataset.getCollaborators() != null) {
for (Collaborator collaborator : dataset.getCollaborators()) {
if (collaborator.getId().equals(oldUser.getId().toString())) {
collaborator.setId(newUser.getId().toString());
collaborator.setName(newUser.getName());
}
}
}
});
}
if(dmp.getCollaborators() != null) {
for (Collaborator collaborator : dmp.getCollaborators()) {
if (collaborator.getId().equals(oldUser.getId().toString())) {
collaborator.setId(newUser.getId().toString());
collaborator.setName(newUser.getName());
}
}
}
dmpRepository.createOrUpdate(dmp);
}
}
catch (IOException e){
logger.warn("Warning: Could not fetch dmps from elastic.", e);
}
oldUser.setUserStatus((short)1);
oldUser.setEmail(null);
List<Credential> credentials = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userInfo"), oldUser)).toList();