dmp migration changes
This commit is contained in:
parent
e47a370088
commit
9433beaaa6
|
@ -6,6 +6,7 @@ import eu.eudat.commons.enums.DmpVersionStatus;
|
|||
import eu.eudat.commons.enums.IsActive;
|
||||
import eu.eudat.data.converters.enums.DmpAccessTypeConverter;
|
||||
import eu.eudat.data.converters.enums.DmpStatusConverter;
|
||||
import eu.eudat.data.converters.enums.DmpVersionStatusConverter;
|
||||
import eu.eudat.data.converters.enums.IsActiveConverter;
|
||||
import eu.eudat.data.old.queryableentity.DataEntity;
|
||||
import jakarta.persistence.*;
|
||||
|
@ -35,7 +36,7 @@ public class DmpEntity implements DataEntity<DmpEntity, UUID> {
|
|||
public static final String _version = "version";
|
||||
|
||||
@Column(name = "version_status", nullable = false)
|
||||
@Convert(converter = DmpStatusConverter.class)
|
||||
@Convert(converter = DmpVersionStatusConverter.class)
|
||||
private DmpVersionStatus versionStatus;
|
||||
|
||||
public static final String _versionStatus = "versionStatus";
|
||||
|
|
|
@ -1,51 +1,61 @@
|
|||
ALTER TABLE public."DescriptionTemplate" RENAME "ID" TO id;
|
||||
DO $$DECLARE
|
||||
this_version CONSTANT varchar := '00.01.010';
|
||||
BEGIN
|
||||
PERFORM * FROM "DBVersion" WHERE version = this_version;
|
||||
IF FOUND THEN RETURN; END IF;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Label" TO label;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "ID" TO id;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Definition" TO definition;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Label" TO label;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Status" TO status;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Definition" TO definition;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Created" TO created_at;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Status" TO status;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Modified" TO updated_at;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Created" TO created_at;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Description" TO description;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Modified" TO updated_at;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "GroupId" TO group_id;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Description" TO description;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Version" TO version;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "GroupId" TO group_id;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Language" TO "language";
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Version" TO version;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Type" TO "type";
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Language" TO "language";
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN created_at TYPE timestamp without time zone ;
|
||||
ALTER TABLE public."DescriptionTemplate" RENAME "Type" TO "type";
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN updated_at TYPE timestamp without time zone ;
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN created_at TYPE timestamp without time zone ;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN group_id DROP DEFAULT;
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN updated_at TYPE timestamp without time zone ;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ADD COLUMN is_active smallint;
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN group_id DROP DEFAULT;
|
||||
|
||||
UPDATE public."DescriptionTemplate" SET is_active = 1;
|
||||
ALTER TABLE public."DescriptionTemplate" ADD COLUMN is_active smallint;
|
||||
|
||||
UPDATE public."DescriptionTemplate" SET is_active = 0 where status = 99;
|
||||
UPDATE public."DescriptionTemplate" SET status = 0 where is_active = 0;
|
||||
UPDATE public."DescriptionTemplate" SET is_active = 1;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN is_active SET NOT NULL;
|
||||
UPDATE public."DescriptionTemplate" SET is_active = 0 where status = 99;
|
||||
UPDATE public."DescriptionTemplate" SET status = 0 where is_active = 0;
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ADD COLUMN version_status smallint;
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN is_active SET NOT NULL;
|
||||
|
||||
UPDATE public."DescriptionTemplate" SET version_status = 1;
|
||||
ALTER TABLE public."DescriptionTemplate" ADD COLUMN version_status smallint;
|
||||
|
||||
UPDATE public."DescriptionTemplate" SET version_status = 0 where id in (
|
||||
UPDATE public."DescriptionTemplate" SET version_status = 1;
|
||||
|
||||
UPDATE public."DescriptionTemplate" SET version_status = 0 where id in (
|
||||
select dt.id from public."DescriptionTemplate" as dt
|
||||
where dt.version =
|
||||
(
|
||||
select Max(f.version)
|
||||
from public."DescriptionTemplate" as f where f.group_id=dt.group_id
|
||||
)
|
||||
);
|
||||
);
|
||||
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN version_status SET NOT NULL;
|
||||
ALTER TABLE public."DescriptionTemplate" ALTER COLUMN version_status SET NOT NULL;
|
||||
|
||||
INSERT INTO public."DBVersion" VALUES ('DMPDB', '00.01.010', '2023-11-02 12:00:00.000000+02', now(), 'Aling DescriptionTemplate table.');
|
||||
|
||||
END$$;
|
|
@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS public."DmpDescriptionTemplate"
|
|||
"id" uuid NOT NULL,
|
||||
"dmp" uuid NOT NULL,
|
||||
"description_template" uuid NOT NULL,
|
||||
"data" text COLLATE pg_catalog."default" NOT NULL,
|
||||
"section_id" uuid NOT NULL,
|
||||
"created_at" timestamp without time zone NOT NULL DEFAULT now(),
|
||||
"updated_at" timestamp without time zone NOT NULL DEFAULT now(),
|
||||
"is_active" smallint NOT NULL DEFAULT 1,
|
||||
|
|
|
@ -49,7 +49,7 @@ public class DatasetMigrationService {
|
|||
|
||||
public List<DatasetMigrationCollectedInfo> collectDatasetsInfo() throws JsonProcessingException {
|
||||
List<DatasetMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
||||
List<DatasetWizardModel> datasets = getDatasets(0, 4);
|
||||
List<DatasetWizardModel> datasets = getDatasets(0, Integer.MAX_VALUE);
|
||||
for (DatasetWizardModel datasetWizardModel : datasets) {
|
||||
DatasetMigrationCollectedInfo datasetMigrationCollectedInfo = new DatasetMigrationCollectedInfo();
|
||||
|
||||
|
|
|
@ -3,87 +3,199 @@ package eu.old.eudat.migration;
|
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import eu.eudat.commons.JsonHandlingService;
|
||||
import eu.eudat.commons.enums.DescriptionStatus;
|
||||
import eu.eudat.commons.XmlHandlingService;
|
||||
import eu.eudat.commons.enums.IsActive;
|
||||
import eu.eudat.data.DescriptionEntity;
|
||||
import eu.eudat.commons.enums.ReferenceFieldDataType;
|
||||
import eu.eudat.commons.enums.ReferenceSourceType;
|
||||
import eu.eudat.commons.enums.ReferenceType;
|
||||
import eu.eudat.commons.types.reference.FieldEntity;
|
||||
import eu.eudat.data.DescriptionTemplateEntity;
|
||||
import eu.eudat.data.DmpBlueprintEntity;
|
||||
import eu.eudat.data.DmpDescriptionTemplateEntity;
|
||||
import eu.eudat.data.ReferenceEntity;
|
||||
import eu.eudat.model.DmpBlueprint;
|
||||
import eu.eudat.query.DescriptionTemplateQuery;
|
||||
import eu.eudat.query.DmpBlueprintQuery;
|
||||
import eu.old.eudat.data.dao.entities.DmpDatasetProfileDao;
|
||||
import eu.old.eudat.data.dao.entities.ResearcherDao;
|
||||
import eu.old.eudat.data.entities.DMPDatasetProfile;
|
||||
import eu.old.eudat.data.entities.Dataset;
|
||||
import eu.old.eudat.data.entities.Researcher;
|
||||
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
||||
import eu.old.eudat.models.data.datasetwizard.DatasetWizardModel;
|
||||
import eu.old.eudat.queryable.QueryableList;
|
||||
import gr.cite.tools.data.query.QueryFactory;
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.xml.sax.SAXException;
|
||||
import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class DmpDatasetProfileMigrationService {
|
||||
|
||||
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpDatasetProfileMigrationService.class));
|
||||
private final DatabaseRepository databaseRepository;
|
||||
|
||||
private final JsonHandlingService jsonHandlingService;
|
||||
|
||||
private final QueryFactory queryFactory;
|
||||
private final XmlHandlingService xmlHandlingService;
|
||||
private static final int PageSize = 500;
|
||||
private static final boolean TestMode = false;
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DmpDatasetProfileMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
|
||||
public DmpDatasetProfileMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, QueryFactory queryFactory, XmlHandlingService xmlHandlingService, EntityManager entityManager) {
|
||||
this.databaseRepository = databaseRepository;
|
||||
this.jsonHandlingService = jsonHandlingService;
|
||||
this.queryFactory = queryFactory;
|
||||
this.xmlHandlingService = xmlHandlingService;
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
public QueryableList<DMPDatasetProfile> dmpDatasetProfileQueryableList() {
|
||||
return databaseRepository.getDmpDatasetProfileDao().asQueryable();
|
||||
public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
|
||||
DmpDatasetProfileDao dmpDatasetProfileDao = databaseRepository.getDmpDatasetProfileDao();
|
||||
long total = dmpDatasetProfileDao.asQueryable().count();
|
||||
logger.debug("Migrate DmpDatasetProfile Total : " + total);
|
||||
int page = 0;
|
||||
|
||||
List<DMPDatasetProfile> items;
|
||||
do {
|
||||
items = dmpDatasetProfileDao.asQueryable().orderBy((builder, root) -> builder.asc(root.get("ID"))).skip(page * PageSize).take(PageSize).toList();
|
||||
if (items != null && !items.isEmpty()) {
|
||||
logger.debug("Migrate DmpDatasetProfile " + page * PageSize + " of " + total);
|
||||
|
||||
List<DmpBlueprintEntity> dmpBlueprints = this.queryFactory.query(DmpBlueprintQuery.class).ids(items.stream().map(x-> x.getDmp().getProfile().getId()).distinct().toList()).collect();
|
||||
Map<UUID, DefinitionEntity> dmpBlueprintsMap = new HashMap<>();
|
||||
for (DmpBlueprintEntity dmpBlueprint : dmpBlueprints) {
|
||||
DefinitionEntity definitionEntity = this.xmlHandlingService.fromXml(DefinitionEntity.class, dmpBlueprint.getDefinition());
|
||||
dmpBlueprintsMap.put(dmpBlueprint.getId(), definitionEntity);
|
||||
}
|
||||
|
||||
public List<DMPDatasetProfile> getDatasets(Integer skip, Integer take) {
|
||||
return dmpDatasetProfileQueryableList()
|
||||
// .orderBy((builder, root) -> builder.desc(root.get("created")))
|
||||
.skip(skip)
|
||||
.take(take)
|
||||
.toList();
|
||||
for (DMPDatasetProfile item : items) {
|
||||
entityManager.detach(item);
|
||||
|
||||
DmpDatasetProfileData profileData = jsonHandlingService.fromJson(DmpDatasetProfileData.class, item.getData());
|
||||
|
||||
if (profileData == null || profileData.dmpSectionIndex == null || profileData.dmpSectionIndex.isEmpty()){
|
||||
logger.error("Migrate DmpDatasetProfile " + item.getId() + " failed no section info ");
|
||||
throw new MyApplicationException();
|
||||
}
|
||||
DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
|
||||
if (definition == null){
|
||||
logger.error("Migrate DmpDatasetProfile " + item.getId() + " failed blueprint definition not found for blueprint " + item.getDmp().getProfile().getId());
|
||||
}
|
||||
|
||||
public List<DmpDatasetProfileMigrationCollectedInfo> collectDatasetsInfo() throws JsonProcessingException {
|
||||
List<DmpDatasetProfileMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
||||
List<DMPDatasetProfile> dmpDatasetProfiles = getDatasets(0, 4);
|
||||
for (DMPDatasetProfile dmpDatasetProfile : dmpDatasetProfiles) {
|
||||
DmpDatasetProfileMigrationCollectedInfo dmpDatasetProfileMigrationCollectedInfo = new DmpDatasetProfileMigrationCollectedInfo();
|
||||
|
||||
DmpDatasetProfileData data = jsonHandlingService.fromJson(DmpDatasetProfileData.class, dmpDatasetProfile.getData());
|
||||
|
||||
//Collect basic information
|
||||
DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity = new DmpDescriptionTemplateEntity();
|
||||
dmpDescriptionTemplateEntity.setDescriptionTemplateId(dmpDatasetProfile.getDatasetprofile().getId());
|
||||
dmpDescriptionTemplateEntity.setDmpId(dmpDatasetProfile.getDmp().getId());
|
||||
dmpDescriptionTemplateEntity.setCreatedAt(Instant.now());
|
||||
dmpDescriptionTemplateEntity.setUpdatedAt(Instant.now());
|
||||
dmpDescriptionTemplateEntity.setSectionId(null); //TODO
|
||||
dmpDescriptionTemplateEntity.setIsActive(IsActive.Active);
|
||||
|
||||
dmpDatasetProfileMigrationCollectedInfo.dmpDescriptionTemplateEntity = dmpDescriptionTemplateEntity;
|
||||
|
||||
collectedInfoList.add(dmpDatasetProfileMigrationCollectedInfo);
|
||||
for (int sectionIndex: profileData.dmpSectionIndex) {
|
||||
if (definition == null || definition.getSections() == null || definition.getSections().size() < sectionIndex) {
|
||||
logger.error("Migrate DmpDatasetProfile " + item.getId() + " can nto found section id for section " + sectionIndex);
|
||||
throw new MyApplicationException();
|
||||
}
|
||||
return collectedInfoList;
|
||||
UUID sectionId = definition.getSections().get(sectionIndex).getId();
|
||||
if (sectionId == null) {
|
||||
logger.error("Migrate DmpDatasetProfile " + item.getId() + " can nto found section id for section " + sectionIndex);
|
||||
throw new MyApplicationException();
|
||||
}
|
||||
|
||||
public String migrate() throws JsonProcessingException {
|
||||
for (DmpDatasetProfileMigrationCollectedInfo collectedInfo : collectDatasetsInfo()) {
|
||||
// this.entityManager.persist(collectedInfo.dmpDescriptionTemplateEntity);
|
||||
DmpDescriptionTemplateEntity data = new DmpDescriptionTemplateEntity();
|
||||
data.setId(UUID.randomUUID());
|
||||
data.setDescriptionTemplateId(item.getDatasetprofile().getId());
|
||||
data.setDmpId(item.getDmp().getId());
|
||||
data.setCreatedAt(Instant.now());
|
||||
data.setUpdatedAt(Instant.now());
|
||||
data.setSectionId(sectionId);
|
||||
data.setIsActive(IsActive.Active);
|
||||
this.entityManager.persist(data);
|
||||
}
|
||||
}
|
||||
this.entityManager.flush();
|
||||
return "Migrated dmps";
|
||||
|
||||
page++;
|
||||
}
|
||||
|
||||
public static class DmpDatasetProfileMigrationCollectedInfo {
|
||||
|
||||
public DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity;
|
||||
|
||||
} while (items != null && !items.isEmpty() && !TestMode);
|
||||
}
|
||||
|
||||
//
|
||||
// public QueryableList<DMPDatasetProfile> dmpDatasetProfileQueryableList() {
|
||||
// return databaseRepository.getDmpDatasetProfileDao().asQueryable();
|
||||
// }
|
||||
//
|
||||
// public List<DMPDatasetProfile> getDatasets(Integer skip, Integer take) {
|
||||
// return dmpDatasetProfileQueryableList()
|
||||
//// .orderBy((builder, root) -> builder.desc(root.get("created")))
|
||||
// .skip(skip)
|
||||
// .take(take)
|
||||
// .toList();
|
||||
// }
|
||||
//
|
||||
// public List<DmpDatasetProfileMigrationCollectedInfo> collectDatasetsInfo() throws Exception {
|
||||
// List<DmpDatasetProfileMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
||||
// List<DMPDatasetProfile> items = getDatasets(0, 4);
|
||||
//
|
||||
// List<DmpBlueprintEntity> dmpBlueprints = this.queryFactory.query(DmpBlueprintQuery.class).ids(items.stream().map(x-> x.getDmp().getProfile().getId()).distinct().toList()).collect();
|
||||
// Map<UUID, DefinitionEntity> dmpBlueprintsMap = new HashMap<>();
|
||||
// for (DmpBlueprintEntity dmpBlueprint : dmpBlueprints) {
|
||||
// DefinitionEntity definitionEntity = this.xmlHandlingService.fromXml(DefinitionEntity.class, dmpBlueprint.getDefinition());
|
||||
// dmpBlueprintsMap.put(dmpBlueprint.getId(), definitionEntity);
|
||||
// }
|
||||
//
|
||||
// for (DMPDatasetProfile item : items) {
|
||||
// this.entityManager.detach(item);
|
||||
//
|
||||
// DmpDatasetProfileMigrationCollectedInfo dmpDatasetProfileMigrationCollectedInfo = new DmpDatasetProfileMigrationCollectedInfo();
|
||||
//
|
||||
// DmpDatasetProfileData profileData = jsonHandlingService.fromJson(DmpDatasetProfileData.class, item.getData());
|
||||
//
|
||||
// if (profileData == null || profileData.dmpSectionIndex == null || profileData.dmpSectionIndex.isEmpty()){
|
||||
// throw new Exception();
|
||||
// }
|
||||
// DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
|
||||
// if (definition == null){
|
||||
// throw new Exception();
|
||||
// }
|
||||
//
|
||||
// for (int sectionIndex: profileData.dmpSectionIndex) {
|
||||
// if ( definition.getSections().size() < sectionIndex) {
|
||||
// throw new Exception();
|
||||
// }
|
||||
// UUID sectionId = definition.getSections().get(sectionIndex).getId();
|
||||
//
|
||||
// DmpDescriptionTemplateEntity data = new DmpDescriptionTemplateEntity();
|
||||
// data.setDescriptionTemplateId(item.getDatasetprofile().getId());
|
||||
// data.setDmpId(item.getDmp().getId());
|
||||
// data.setCreatedAt(Instant.now());
|
||||
// data.setUpdatedAt(Instant.now());
|
||||
// data.setSectionId(sectionId); //TODO
|
||||
// data.setIsActive(IsActive.Active);
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// dmpDatasetProfileMigrationCollectedInfo.dmpDescriptionTemplateEntity = data;
|
||||
//
|
||||
// collectedInfoList.add(dmpDatasetProfileMigrationCollectedInfo);
|
||||
// }
|
||||
// return collectedInfoList;
|
||||
// }
|
||||
//
|
||||
// public String migrate() throws JsonProcessingException {
|
||||
// for (DmpDatasetProfileMigrationCollectedInfo collectedInfo : collectDatasetsInfo()) {
|
||||
//// this.entityManager.persist(collectedInfo.dmpDescriptionTemplateEntity);
|
||||
// }
|
||||
// this.entityManager.flush();
|
||||
// return "Migrated dmps";
|
||||
// }
|
||||
//
|
||||
// public static class DmpDatasetProfileMigrationCollectedInfo {
|
||||
//
|
||||
// public DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity;
|
||||
//
|
||||
// }
|
||||
//
|
||||
@JsonIgnoreProperties({"validationErrorModel"})
|
||||
public static class DmpDatasetProfileData {
|
||||
|
||||
|
|
|
@ -9,27 +9,43 @@ import eu.eudat.commons.enums.IsActive;
|
|||
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
|
||||
import eu.eudat.commons.types.dmp.DmpContactEntity;
|
||||
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
|
||||
import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
|
||||
import eu.eudat.data.DmpBlueprintEntity;
|
||||
import eu.eudat.data.DmpDescriptionTemplateEntity;
|
||||
import eu.eudat.data.DmpEntity;
|
||||
import eu.eudat.query.DmpBlueprintQuery;
|
||||
import eu.old.eudat.data.dao.entities.DMPDao;
|
||||
import eu.old.eudat.data.dao.entities.DmpDatasetProfileDao;
|
||||
import eu.old.eudat.data.entities.DMP;
|
||||
import eu.old.eudat.data.entities.DMPDatasetProfile;
|
||||
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
||||
import eu.old.eudat.models.data.dmp.DataManagementPlan;
|
||||
import eu.old.eudat.queryable.QueryableList;
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class DmpMigrationService {
|
||||
|
||||
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpMigrationService.class));
|
||||
private final DatabaseRepository databaseRepository;
|
||||
|
||||
private final JsonHandlingService jsonHandlingService;
|
||||
|
||||
private final EntityManager entityManager;
|
||||
private static final int PageSize = 500;
|
||||
private static final boolean TestMode = false;
|
||||
|
||||
public DmpMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
|
||||
this.databaseRepository = databaseRepository;
|
||||
|
@ -37,98 +53,189 @@ public class DmpMigrationService {
|
|||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
public QueryableList<DMP> dmpQueryableList() {
|
||||
return databaseRepository.getDmpDao().asQueryable();
|
||||
}
|
||||
public void migrate() throws JsonProcessingException {
|
||||
DMPDao dmpDao = databaseRepository.getDmpDao();
|
||||
long total = dmpDao.asQueryable().count();
|
||||
logger.debug("Migrate Dmp Total : " + total);
|
||||
int page = 0;
|
||||
|
||||
public List<DataManagementPlan> getDmps(Integer skip, Integer take) {
|
||||
List<DMP> dmps = dmpQueryableList()
|
||||
.orderBy((builder, root) -> builder.desc(root.get("created")))
|
||||
.skip(skip)
|
||||
.take(take)
|
||||
.toList();
|
||||
List<DMP> items;
|
||||
do {
|
||||
items = dmpDao.asQueryable().orderBy((builder, root) -> builder.asc(root.get("created"))).orderBy((builder, root) -> builder.asc(root.get("ID"))).skip(page * PageSize).take(PageSize).toList();
|
||||
if (items != null && !items.isEmpty()) {
|
||||
logger.debug("Migrate Dmp " + page * PageSize + " of " + total);
|
||||
|
||||
return dmps.stream().map(x -> {
|
||||
DataManagementPlan dmp = new DataManagementPlan();
|
||||
dmp.fromDataModel(x);
|
||||
return dmp;
|
||||
}).toList();
|
||||
}
|
||||
for (DMP item : items) {
|
||||
//entityManager.detach(item);
|
||||
|
||||
public List<DmpMigrationCollectedInfo> collectDmpsInfo() throws JsonProcessingException {
|
||||
List<DmpMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
||||
List<DataManagementPlan> dmps = getDmps(0, 4);
|
||||
for (DataManagementPlan dataManagementPlan : dmps) {
|
||||
DmpMigrationCollectedInfo dmpMigrationCollectedInfo = new DmpMigrationCollectedInfo();
|
||||
DataManagementPlan model = new DataManagementPlan();
|
||||
model.fromDataModel(item);
|
||||
|
||||
DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
|
||||
dmpProperties.setDmpBlueprintValues(new ArrayList<>());
|
||||
dmpProperties.setContacts(new ArrayList<>());
|
||||
|
||||
//Collect basic dmp information
|
||||
DmpEntity dmpEntity = new DmpEntity();
|
||||
dmpEntity.setId(dataManagementPlan.getId());
|
||||
dmpEntity.setLabel(dataManagementPlan.getLabel());
|
||||
dmpEntity.setDescription(dataManagementPlan.getDescription());
|
||||
if (dataManagementPlan.getCreator() != null)
|
||||
dmpEntity.setCreatorId(dataManagementPlan.getCreator().getId());
|
||||
dmpEntity.setGroupId(dataManagementPlan.getGroupId());
|
||||
dmpEntity.setVersion((short) dataManagementPlan.getVersion());
|
||||
dmpEntity.setVersionStatus(DmpVersionStatus.of((short) dataManagementPlan.getVersionStatus()));
|
||||
if (dataManagementPlan.getCreated() != null)
|
||||
dmpEntity.setCreatedAt(dataManagementPlan.getCreated().toInstant());
|
||||
if (dataManagementPlan.getModified() != null)
|
||||
dmpEntity.setUpdatedAt(dataManagementPlan.getModified().toInstant());
|
||||
if (dataManagementPlan.getFinalized() != null)
|
||||
dmpEntity.setFinalizedAt(dataManagementPlan.getFinalized().toInstant());
|
||||
dmpEntity.setBlueprintId(dataManagementPlan.getProfile().getId());
|
||||
if (dataManagementPlan.getExtraProperties() != null) {
|
||||
if (dataManagementPlan.getExtraProperties().containsKey("language") && dataManagementPlan.getExtraProperties().get("language") != null)
|
||||
dmpEntity.setLanguage((String) dataManagementPlan.getExtraProperties().get("language"));
|
||||
if (dataManagementPlan.getExtraProperties().containsKey("visible") && dataManagementPlan.getExtraProperties().get("visible") != null)
|
||||
dmpEntity.setAccessType((boolean) dataManagementPlan.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
|
||||
if (dataManagementPlan.getExtraProperties().containsKey("contact") && dataManagementPlan.getExtraProperties().get("contact") != null) {
|
||||
DmpEntity data = new DmpEntity();
|
||||
data.setId(model.getId());
|
||||
data.setLabel(model.getLabel());
|
||||
data.setDescription(model.getDescription());
|
||||
if (model.getCreator() != null)
|
||||
data.setCreatorId(model.getCreator().getId());
|
||||
data.setGroupId(model.getGroupId());
|
||||
data.setVersion((short) model.getVersion());
|
||||
data.setVersionStatus(DmpVersionStatus.of((short) model.getVersionStatus()));
|
||||
if (model.getCreated() != null)
|
||||
data.setCreatedAt(model.getCreated().toInstant());
|
||||
if (model.getModified() != null)
|
||||
data.setUpdatedAt(model.getModified().toInstant());
|
||||
if (model.getFinalized() != null)
|
||||
data.setFinalizedAt(model.getFinalized().toInstant());
|
||||
data.setBlueprintId(model.getProfile().getId());
|
||||
if (model.getExtraProperties() != null) {
|
||||
if (model.getExtraProperties().containsKey("language") && model.getExtraProperties().get("language") != null)
|
||||
data.setLanguage((String) model.getExtraProperties().get("language"));
|
||||
if (model.getExtraProperties().containsKey("visible") && model.getExtraProperties().get("visible") != null)
|
||||
data.setAccessType((boolean) model.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
|
||||
if (model.getExtraProperties().containsKey("contact") && model.getExtraProperties().get("contact") != null) {
|
||||
DmpContactEntity contactEntity = new DmpContactEntity();
|
||||
contactEntity.setUserId((String) dataManagementPlan.getExtraProperties().get("contact"));
|
||||
contactEntity.setUserId((String) model.getExtraProperties().get("contact"));
|
||||
dmpProperties.getContacts().add(contactEntity);
|
||||
}
|
||||
}
|
||||
if (dataManagementPlan.getProperties() != null) {
|
||||
dataManagementPlan.getProperties().forEach((key,val) -> {
|
||||
if (model.getProperties() != null) {
|
||||
model.getProperties().forEach((key,val) -> {
|
||||
DmpBlueprintValueEntity valueEntity = new DmpBlueprintValueEntity();
|
||||
valueEntity.setFieldId(key);
|
||||
valueEntity.setValue((String) val);
|
||||
dmpProperties.getDmpBlueprintValues().add(valueEntity);
|
||||
});
|
||||
}
|
||||
if (dataManagementPlan.getStatus() == 99) {
|
||||
dmpEntity.setIsActive(IsActive.Inactive);
|
||||
if (model.getStatus() == 99) {
|
||||
data.setIsActive(IsActive.Inactive);
|
||||
data.setStatus(DmpStatus.Draft);
|
||||
} else {
|
||||
dmpEntity.setIsActive(IsActive.Active);
|
||||
dmpEntity.setStatus(DmpStatus.of((short) dataManagementPlan.getStatus()));
|
||||
data.setIsActive(IsActive.Active);
|
||||
data.setStatus(DmpStatus.of((short) model.getStatus()));
|
||||
}
|
||||
dmpEntity.setProperties(jsonHandlingService.toJson(dmpProperties));
|
||||
dmpMigrationCollectedInfo.dmpEntity = dmpEntity;
|
||||
data.setProperties(jsonHandlingService.toJson(dmpProperties));
|
||||
|
||||
//Collect dmp Organization info
|
||||
|
||||
collectedInfoList.add(dmpMigrationCollectedInfo);
|
||||
if (data.getAccessType() == null) {
|
||||
logger.warn("AccessType not found set to default for dmp " + item.getId());
|
||||
data.setAccessType(DmpAccessType.Restricted); //TODO
|
||||
}
|
||||
return collectedInfoList;
|
||||
if (data.getLanguage() == null) {
|
||||
logger.warn("Language not found set to default for dmp " + item.getId());
|
||||
data.setLanguage(""); //TODO
|
||||
}
|
||||
if (data.getCreatorId() == null){
|
||||
logger.warn("Migration skipped creator not found " + item.getId());
|
||||
continue;
|
||||
}
|
||||
|
||||
public String migrate() throws JsonProcessingException {
|
||||
for (DmpMigrationCollectedInfo collectedInfo : collectDmpsInfo()) {
|
||||
this.entityManager.persist(collectedInfo.dmpEntity);
|
||||
}
|
||||
|
||||
this.entityManager.persist(data);
|
||||
this.entityManager.flush();
|
||||
return "Migrated dmps";
|
||||
}
|
||||
|
||||
public static class DmpMigrationCollectedInfo {
|
||||
|
||||
public DmpEntity dmpEntity;
|
||||
|
||||
page++;
|
||||
}
|
||||
} while (items != null && !items.isEmpty() && !TestMode);
|
||||
}
|
||||
|
||||
// public QueryableList<DMP> dmpQueryableList() {
|
||||
// return databaseRepository.getDmpDao().asQueryable();
|
||||
// }
|
||||
//
|
||||
// public List<DataManagementPlan> getDmps(Integer skip, Integer take) {
|
||||
// List<DMP> dmps = dmpQueryableList()
|
||||
// .orderBy((builder, root) -> builder.desc(root.get("created")))
|
||||
// .skip(skip)
|
||||
// .take(take)
|
||||
// .toList();
|
||||
//
|
||||
// return dmps.stream().map(x -> {
|
||||
// DataManagementPlan dmp = new DataManagementPlan();
|
||||
// dmp.fromDataModel(x);
|
||||
// return dmp;
|
||||
// }).toList();
|
||||
// }
|
||||
|
||||
// public List<DmpMigrationCollectedInfo> collectDmpsInfo() throws JsonProcessingException {
|
||||
// List<DmpMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
||||
// List<DataManagementPlan> dmps = getDmps(0, Integer.MAX_VALUE);
|
||||
// for (DataManagementPlan dataManagementPlan : dmps) {
|
||||
// DmpMigrationCollectedInfo dmpMigrationCollectedInfo = new DmpMigrationCollectedInfo();
|
||||
//
|
||||
// DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
|
||||
// dmpProperties.setDmpBlueprintValues(new ArrayList<>());
|
||||
// dmpProperties.setContacts(new ArrayList<>());
|
||||
//
|
||||
// //Collect basic dmp information
|
||||
// DmpEntity dmpEntity = new DmpEntity();
|
||||
// dmpEntity.setId(dataManagementPlan.getId());
|
||||
// dmpEntity.setLabel(dataManagementPlan.getLabel());
|
||||
// dmpEntity.setDescription(dataManagementPlan.getDescription());
|
||||
// if (dataManagementPlan.getCreator() != null)
|
||||
// dmpEntity.setCreatorId(dataManagementPlan.getCreator().getId());
|
||||
// dmpEntity.setGroupId(dataManagementPlan.getGroupId());
|
||||
// dmpEntity.setVersion((short) dataManagementPlan.getVersion());
|
||||
// dmpEntity.setVersionStatus(DmpVersionStatus.of((short) dataManagementPlan.getVersionStatus()));
|
||||
// if (dataManagementPlan.getCreated() != null)
|
||||
// dmpEntity.setCreatedAt(dataManagementPlan.getCreated().toInstant());
|
||||
// if (dataManagementPlan.getModified() != null)
|
||||
// dmpEntity.setUpdatedAt(dataManagementPlan.getModified().toInstant());
|
||||
// if (dataManagementPlan.getFinalized() != null)
|
||||
// dmpEntity.setFinalizedAt(dataManagementPlan.getFinalized().toInstant());
|
||||
// dmpEntity.setBlueprintId(dataManagementPlan.getProfile().getId());
|
||||
// if (dataManagementPlan.getExtraProperties() != null) {
|
||||
// if (dataManagementPlan.getExtraProperties().containsKey("language") && dataManagementPlan.getExtraProperties().get("language") != null)
|
||||
// dmpEntity.setLanguage((String) dataManagementPlan.getExtraProperties().get("language"));
|
||||
// if (dataManagementPlan.getExtraProperties().containsKey("visible") && dataManagementPlan.getExtraProperties().get("visible") != null)
|
||||
// dmpEntity.setAccessType((boolean) dataManagementPlan.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
|
||||
// if (dataManagementPlan.getExtraProperties().containsKey("contact") && dataManagementPlan.getExtraProperties().get("contact") != null) {
|
||||
// DmpContactEntity contactEntity = new DmpContactEntity();
|
||||
// contactEntity.setUserId((String) dataManagementPlan.getExtraProperties().get("contact"));
|
||||
// dmpProperties.getContacts().add(contactEntity);
|
||||
// }
|
||||
// }
|
||||
// if (dataManagementPlan.getProperties() != null) {
|
||||
// dataManagementPlan.getProperties().forEach((key,val) -> {
|
||||
// DmpBlueprintValueEntity valueEntity = new DmpBlueprintValueEntity();
|
||||
// valueEntity.setFieldId(key);
|
||||
// valueEntity.setValue((String) val);
|
||||
// dmpProperties.getDmpBlueprintValues().add(valueEntity);
|
||||
// });
|
||||
// }
|
||||
// if (dataManagementPlan.getStatus() == 99) {
|
||||
// dmpEntity.setIsActive(IsActive.Inactive);
|
||||
// dmpEntity.setStatus(DmpStatus.Draft);
|
||||
// } else {
|
||||
// dmpEntity.setIsActive(IsActive.Active);
|
||||
// dmpEntity.setStatus(DmpStatus.of((short) dataManagementPlan.getStatus()));
|
||||
// }
|
||||
// dmpEntity.setProperties(jsonHandlingService.toJson(dmpProperties));
|
||||
// dmpMigrationCollectedInfo.dmpEntity = dmpEntity;
|
||||
//
|
||||
// //Collect dmp Organization info
|
||||
//
|
||||
// collectedInfoList.add(dmpMigrationCollectedInfo);
|
||||
// }
|
||||
// return collectedInfoList;
|
||||
// }
|
||||
//
|
||||
// public String migrate() throws JsonProcessingException {
|
||||
// for (DmpMigrationCollectedInfo collectedInfo : collectDmpsInfo()) {
|
||||
// this.entityManager.persist(collectedInfo.dmpEntity);
|
||||
// }
|
||||
// this.entityManager.flush();
|
||||
// return "Migrated dmps";
|
||||
// }
|
||||
//
|
||||
// public static class DmpMigrationCollectedInfo {
|
||||
//
|
||||
// public DmpEntity dmpEntity;
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -3,11 +3,16 @@ package eu.old.eudat.publicapi.migration;
|
|||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import eu.old.eudat.migration.*;
|
||||
import io.swagger.annotations.Api;
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
|
||||
@Api(tags = "Migration")
|
||||
@RestController
|
||||
|
@ -56,10 +61,11 @@ public class MigrationController {
|
|||
this.organizationMigrationService = organizationMigrationService;
|
||||
}
|
||||
|
||||
@GetMapping("dmps/migrate")
|
||||
@GetMapping("dmps")
|
||||
@Transactional
|
||||
public String migrateDmps() throws JsonProcessingException {
|
||||
return this.dmpMigrationService.migrate();
|
||||
public boolean migrateDmps() throws JsonProcessingException {
|
||||
this.dmpMigrationService.migrate();
|
||||
return true;
|
||||
}
|
||||
|
||||
@GetMapping("datasets/migrate")
|
||||
|
@ -68,19 +74,15 @@ public class MigrationController {
|
|||
return this.datasetMigrationService.migrate();
|
||||
}
|
||||
|
||||
@GetMapping("dmp-dataset-profiles/migrate")
|
||||
@GetMapping("dmp-dataset-profiles")
|
||||
@Transactional
|
||||
public String migrateDmpDatasetProfiles() throws JsonProcessingException {
|
||||
return this.dmpDatasetProfileMigrationService.migrate();
|
||||
}
|
||||
public boolean migrateDmpDatasetProfiles() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
|
||||
this.dmpDatasetProfileMigrationService.migrate();
|
||||
|
||||
@GetMapping("organizations/migrate")
|
||||
@Transactional
|
||||
public boolean migrateOrganizations() {
|
||||
this.organizationMigrationService.migrate();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("references")
|
||||
@Transactional
|
||||
public boolean migrateReferences() {
|
||||
|
@ -96,6 +98,14 @@ public class MigrationController {
|
|||
return true;
|
||||
}
|
||||
|
||||
@GetMapping("organizations")
|
||||
@Transactional
|
||||
public boolean migrateOrganizations() {
|
||||
this.organizationMigrationService.migrate();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("dataRepositories")
|
||||
@Transactional
|
||||
public boolean migrateDataRepositories() {
|
||||
|
|
Loading…
Reference in New Issue