#8916 - when merging acoounts, save the main profile user as collaborator to elastic dmps/datasets
This commit is contained in:
parent
e01709f886
commit
634d49ea35
|
@ -5,6 +5,10 @@ import eu.eudat.data.entities.EmailConfirmation;
|
||||||
import eu.eudat.data.entities.UserDMP;
|
import eu.eudat.data.entities.UserDMP;
|
||||||
import eu.eudat.data.entities.UserInfo;
|
import eu.eudat.data.entities.UserInfo;
|
||||||
import eu.eudat.data.entities.UserToken;
|
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.HasConfirmedEmailException;
|
||||||
import eu.eudat.exceptions.emailconfirmation.TokenExpiredException;
|
import eu.eudat.exceptions.emailconfirmation.TokenExpiredException;
|
||||||
import eu.eudat.logic.services.ApiContext;
|
import eu.eudat.logic.services.ApiContext;
|
||||||
|
@ -25,17 +29,21 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class MergeEmailConfirmationManager {
|
public class MergeEmailConfirmationManager {
|
||||||
private static Logger logger = LoggerFactory.getLogger(MergeEmailConfirmationManager.class);
|
private static Logger logger = LoggerFactory.getLogger(MergeEmailConfirmationManager.class);
|
||||||
private ApiContext apiContext;
|
private ApiContext apiContext;
|
||||||
private DatabaseRepository databaseRepository;
|
private DatabaseRepository databaseRepository;
|
||||||
|
private DmpRepository dmpRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public MergeEmailConfirmationManager(ApiContext apiContext) {
|
public MergeEmailConfirmationManager(ApiContext apiContext) {
|
||||||
this.apiContext = apiContext;
|
this.apiContext = apiContext;
|
||||||
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
||||||
|
this.dmpRepository = apiContext.getOperationsContext().getElasticRepository().getDmpRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -91,6 +99,38 @@ public class MergeEmailConfirmationManager {
|
||||||
userDmp.setUser(newUser);
|
userDmp.setUser(newUser);
|
||||||
databaseRepository.getUserDmpDao().createOrUpdate(userDmp);
|
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.setUserStatus((short)1);
|
||||||
oldUser.setEmail(null);
|
oldUser.setEmail(null);
|
||||||
List<Credential> credentials = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userInfo"), oldUser)).toList();
|
List<Credential> credentials = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userInfo"), oldUser)).toList();
|
||||||
|
|
Loading…
Reference in New Issue