Migrating licenses from dmps extra properties, while avoiding creating duplicates
This commit is contained in:
parent
a72770868e
commit
e4d2cde296
|
@ -35,6 +35,8 @@ public class ReferenceQuery extends QueryBase<ReferenceEntity> {
|
||||||
|
|
||||||
private Collection<ReferenceType> referenceTypes;
|
private Collection<ReferenceType> referenceTypes;
|
||||||
|
|
||||||
|
private Collection<String> references;
|
||||||
|
|
||||||
private Collection<UUID> excludedIds;
|
private Collection<UUID> excludedIds;
|
||||||
|
|
||||||
|
|
||||||
|
@ -94,6 +96,21 @@ public class ReferenceQuery extends QueryBase<ReferenceEntity> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReferenceQuery references(String value) {
|
||||||
|
this.references = List.of(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReferenceQuery references(String... value) {
|
||||||
|
this.references = Arrays.asList(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReferenceQuery references(Collection<String> values) {
|
||||||
|
this.references = values;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ReferenceQuery excludedIds(Collection<UUID> values) {
|
public ReferenceQuery excludedIds(Collection<UUID> values) {
|
||||||
this.excludedIds = values;
|
this.excludedIds = values;
|
||||||
return this;
|
return this;
|
||||||
|
@ -196,6 +213,12 @@ public class ReferenceQuery extends QueryBase<ReferenceEntity> {
|
||||||
inClause.value(item);
|
inClause.value(item);
|
||||||
predicates.add(inClause);
|
predicates.add(inClause);
|
||||||
}
|
}
|
||||||
|
if (this.references != null) {
|
||||||
|
CriteriaBuilder.In<String> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(ReferenceEntity._reference));
|
||||||
|
for (String item : this.references)
|
||||||
|
inClause.value(item);
|
||||||
|
predicates.add(inClause);
|
||||||
|
}
|
||||||
if (this.excludedIds != null) {
|
if (this.excludedIds != null) {
|
||||||
CriteriaBuilder.In<UUID> notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(ReferenceEntity._id));
|
CriteriaBuilder.In<UUID> notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(ReferenceEntity._id));
|
||||||
for (UUID item : this.excludedIds)
|
for (UUID item : this.excludedIds)
|
||||||
|
|
|
@ -2,14 +2,14 @@ package eu.old.eudat.migration;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import eu.eudat.commons.JsonHandlingService;
|
import eu.eudat.commons.JsonHandlingService;
|
||||||
import eu.eudat.commons.enums.DmpAccessType;
|
import eu.eudat.commons.enums.*;
|
||||||
import eu.eudat.commons.enums.DmpStatus;
|
|
||||||
import eu.eudat.commons.enums.DmpVersionStatus;
|
|
||||||
import eu.eudat.commons.enums.IsActive;
|
|
||||||
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
|
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
|
||||||
import eu.eudat.commons.types.dmp.DmpContactEntity;
|
import eu.eudat.commons.types.dmp.DmpContactEntity;
|
||||||
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
|
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
|
||||||
import eu.eudat.data.DmpEntity;
|
import eu.eudat.data.DmpEntity;
|
||||||
|
import eu.eudat.data.DmpReferenceEntity;
|
||||||
|
import eu.eudat.data.ReferenceEntity;
|
||||||
|
import eu.eudat.query.ReferenceQuery;
|
||||||
import eu.old.eudat.data.dao.entities.DMPDao;
|
import eu.old.eudat.data.dao.entities.DMPDao;
|
||||||
import eu.old.eudat.data.entities.DMP;
|
import eu.old.eudat.data.entities.DMP;
|
||||||
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
||||||
|
@ -21,6 +21,8 @@ import jakarta.persistence.EntityManager;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -31,16 +33,20 @@ public class DmpMigrationService {
|
||||||
|
|
||||||
private final JsonHandlingService jsonHandlingService;
|
private final JsonHandlingService jsonHandlingService;
|
||||||
private final EntityManager entityManager;
|
private final EntityManager entityManager;
|
||||||
|
|
||||||
|
private final QueryFactory queryFactory;
|
||||||
|
|
||||||
private static final int PageSize = 500;
|
private static final int PageSize = 500;
|
||||||
private static final boolean TestMode = false;
|
private static final boolean TestMode = false;
|
||||||
|
|
||||||
public DmpMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
|
public DmpMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager, QueryFactory queryFactory) {
|
||||||
this.databaseRepository = databaseRepository;
|
this.databaseRepository = databaseRepository;
|
||||||
this.jsonHandlingService = jsonHandlingService;
|
this.jsonHandlingService = jsonHandlingService;
|
||||||
this.entityManager = entityManager;
|
this.entityManager = entityManager;
|
||||||
|
this.queryFactory = queryFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void migrate() throws JsonProcessingException {
|
public void migrate() throws JsonProcessingException, NoSuchFieldException, IllegalAccessException {
|
||||||
DMPDao dmpDao = databaseRepository.getDmpDao();
|
DMPDao dmpDao = databaseRepository.getDmpDao();
|
||||||
long total = dmpDao.asQueryable().count();
|
long total = dmpDao.asQueryable().count();
|
||||||
logger.debug("Migrate Dmp Total : " + total);
|
logger.debug("Migrate Dmp Total : " + total);
|
||||||
|
@ -108,6 +114,52 @@ public class DmpMigrationService {
|
||||||
contactEntity.setUserId((String) model.getExtraProperties().get("contact"));
|
contactEntity.setUserId((String) model.getExtraProperties().get("contact"));
|
||||||
dmpProperties.getContacts().add(contactEntity);
|
dmpProperties.getContacts().add(contactEntity);
|
||||||
}
|
}
|
||||||
|
if (model.getExtraProperties().containsKey("license") && model.getExtraProperties().get("license") != null) {
|
||||||
|
Object license = model.getExtraProperties().get("license");
|
||||||
|
ReferenceEntity referenceEntity = new ReferenceEntity();
|
||||||
|
if (Arrays.stream(license.getClass().getFields()).map(Field::getName).toList().contains("pid")) {
|
||||||
|
referenceEntity.setReference(String.valueOf(license.getClass().getField("pid").get(license)));
|
||||||
|
}
|
||||||
|
ReferenceQuery referenceQuery = queryFactory.query(ReferenceQuery.class)
|
||||||
|
.references(referenceEntity.getReference())
|
||||||
|
.types(ReferenceType.Licenses)
|
||||||
|
.isActive(IsActive.Active);
|
||||||
|
List<ReferenceEntity> foundReferences = referenceQuery.collect();
|
||||||
|
boolean licenseExists = foundReferences != null && !foundReferences.isEmpty();
|
||||||
|
if (!licenseExists) {
|
||||||
|
if (Arrays.stream(license.getClass().getFields()).map(Field::getName).toList().contains("name")) {
|
||||||
|
referenceEntity.setLabel(String.valueOf(license.getClass().getField("name").get(license)));
|
||||||
|
}
|
||||||
|
if (Arrays.stream(license.getClass().getFields()).map(Field::getName).toList().contains("uri")) {
|
||||||
|
referenceEntity.setSource(String.valueOf(license.getClass().getField("uri").get(license)));
|
||||||
|
}
|
||||||
|
if (Arrays.stream(license.getClass().getFields()).map(Field::getName).toList().contains("abbreviation")) {
|
||||||
|
referenceEntity.setAbbreviation(String.valueOf(license.getClass().getField("abbreviation").get(license)));
|
||||||
|
}
|
||||||
|
referenceEntity.setId(UUID.randomUUID());
|
||||||
|
referenceEntity.setSourceType(ReferenceSourceType.External);
|
||||||
|
referenceEntity.setType(ReferenceType.Licenses);
|
||||||
|
referenceEntity.setCreatedAt(Instant.now());
|
||||||
|
referenceEntity.setUpdatedAt(Instant.now());
|
||||||
|
referenceEntity.setIsActive(IsActive.Active);
|
||||||
|
}
|
||||||
|
|
||||||
|
DmpReferenceEntity dmpReferenceEntity = new DmpReferenceEntity();
|
||||||
|
dmpReferenceEntity.setId(UUID.randomUUID());
|
||||||
|
dmpReferenceEntity.setDmpId(data.getId());
|
||||||
|
if (licenseExists) {
|
||||||
|
dmpReferenceEntity.setReferenceId(foundReferences.getFirst().getId());
|
||||||
|
} else {
|
||||||
|
dmpReferenceEntity.setReferenceId(referenceEntity.getId());
|
||||||
|
}
|
||||||
|
dmpReferenceEntity.setCreatedAt(Instant.now());
|
||||||
|
dmpReferenceEntity.setUpdatedAt(Instant.now());
|
||||||
|
dmpReferenceEntity.setIsActive(IsActive.Active);
|
||||||
|
|
||||||
|
if (!licenseExists)
|
||||||
|
entityManager.persist(referenceEntity);
|
||||||
|
entityManager.persist(dmpReferenceEntity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (model.getProperties() != null) {
|
if (model.getProperties() != null) {
|
||||||
model.getProperties().forEach((key,val) -> {
|
model.getProperties().forEach((key,val) -> {
|
||||||
|
|
|
@ -73,7 +73,7 @@ public class MigrationController {
|
||||||
|
|
||||||
@GetMapping("all")
|
@GetMapping("all")
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean migrateAll() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
|
public boolean migrateAll() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException, NoSuchFieldException {
|
||||||
this.dataRepositoryMigrationService.migrate();
|
this.dataRepositoryMigrationService.migrate();
|
||||||
this.externalDatasetMigrationService.migrate();
|
this.externalDatasetMigrationService.migrate();
|
||||||
this.funderMigrationService.migrate();
|
this.funderMigrationService.migrate();
|
||||||
|
@ -117,7 +117,7 @@ public class MigrationController {
|
||||||
|
|
||||||
@GetMapping("dmps")
|
@GetMapping("dmps")
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean migrateDmps() throws JsonProcessingException {
|
public boolean migrateDmps() throws JsonProcessingException, NoSuchFieldException, IllegalAccessException {
|
||||||
this.dmpMigrationService.migrate();
|
this.dmpMigrationService.migrate();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue