migration changes

This commit is contained in:
Efstratios Giannopoulos 2024-01-26 14:41:19 +02:00
parent ac932ffb05
commit f58832e3e9
2 changed files with 241 additions and 16 deletions

View File

@ -0,0 +1,208 @@
package eu.old.eudat.migration;
import eu.eudat.commons.XmlHandlingService;
import eu.eudat.commons.enums.DmpBlueprintExtraFieldDataType;
import eu.eudat.commons.enums.DmpBlueprintFieldCategory;
import eu.eudat.commons.enums.DmpBlueprintSystemFieldType;
import eu.eudat.commons.types.dmpblueprint.*;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionTemplateEntity;
import eu.eudat.data.DmpBlueprintEntity;
import eu.eudat.model.DmpBlueprint;
import eu.eudat.query.DescriptionTemplateQuery;
import eu.eudat.query.DmpBlueprintQuery;
import eu.old.eudat.logic.utilities.builders.XmlBuilder;
import eu.old.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.*;
import eu.old.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.FieldCategory;
import gr.cite.tools.data.query.Ordering;
import gr.cite.tools.data.query.Paging;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.management.InvalidApplicationException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.util.*;
@Service
public class DmpBlueprintMigrationService {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpBlueprintMigrationService.class));
private final EntityManager entityManager;
private final ConventionService conventionService;
private final XmlHandlingService xmlHandlingService;
private final QueryFactory queryFactory;
private static final int PageSize = 500;
private static final boolean TestMode = false;
public DmpBlueprintMigrationService(EntityManager entityManager, ConventionService conventionService, XmlHandlingService xmlHandlingService, QueryFactory queryFactory) {
this.entityManager = entityManager;
this.conventionService = conventionService;
this.xmlHandlingService = xmlHandlingService;
this.queryFactory = queryFactory;
}
public void migrate() throws IOException, NoSuchFieldException, IllegalAccessException, JAXBException, ParserConfigurationException, InstantiationException, SAXException, InvalidApplicationException, TransformerException {
long total = this.queryFactory.query(DmpBlueprintQuery.class).count();
logger.debug("Migrate DmpBlueprint Total : " + total);
int page = 0;
Map<String, UUID> licenseIdByName = new HashMap<>();
List<DmpBlueprintEntity> items;
List<DescriptionTemplateEntity> descriptionTemplateEntities = this.queryFactory.query(DescriptionTemplateQuery.class).collectAs(new BaseFieldSet().ensure(eu.eudat.model.DescriptionTemplate._id).ensure(eu.eudat.model.DescriptionTemplate._groupId));
Map<UUID, UUID> descriptionTemplateGroupMap = new HashMap<>();
for (DescriptionTemplateEntity descriptionTemplateEntity: descriptionTemplateEntities) {
descriptionTemplateGroupMap.put(descriptionTemplateEntity.getId(), descriptionTemplateEntity.getGroupId());
}
do {
DmpBlueprintQuery query = this.queryFactory.query(DmpBlueprintQuery.class);
query.setOrder(new Ordering().addDescending(DmpBlueprint._createdAt));
query.setPage(new Paging(page * PageSize, PageSize));
items = query.collect();
if (items != null && !items.isEmpty()) {
logger.debug("Migrate DmpBlueprint " + page * PageSize + " of " + total);
for (DmpBlueprintEntity item : items) {
if (this.conventionService.isNullOrEmpty(item.getDefinition())) continue;
Document document = XmlBuilder.fromXml(item.getDefinition());
if (document == null){
logger.error("Migrate DmpBlueprint " + item.getId() + " failed read xml");
continue;
}
DataManagementPlanBlueprint dataManagementPlanBlueprint =new DataManagementPlanBlueprint().fromXml(document.getDocumentElement());
item.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(dataManagementPlanBlueprint, descriptionTemplateGroupMap)));
this.entityManager.merge(item);
this.entityManager.flush();
}
page++;
}
} while (items != null && !items.isEmpty() && !TestMode);
}
private @NotNull eu.eudat.commons.types.dmpblueprint.DefinitionEntity buildDefinitionEntity(DataManagementPlanBlueprint dataManagementPlanBlueprint, Map<UUID, UUID> descriptionTemplateGroupMap ) {
eu.eudat.commons.types.dmpblueprint.DefinitionEntity data = new DefinitionEntity();
if (dataManagementPlanBlueprint == null)
return data;
if (!this.conventionService.isListNullOrEmpty(dataManagementPlanBlueprint.getSections())) {
data.setSections(new ArrayList<>());
for (Section section : dataManagementPlanBlueprint.getSections()) {
data.getSections().add(this.buildSectionEntity(section, descriptionTemplateGroupMap));
}
}
return data;
}
private @NotNull SectionEntity buildSectionEntity(Section itm, Map<UUID, UUID> descriptionTemplateGroupMap ) {
SectionEntity data = new SectionEntity();
if (itm == null)
return data;
data.setId(itm.getId());
data.setDescription(itm.getDescription());
data.setLabel(itm.getLabel());
data.setOrdinal(itm.getOrdinal());
data.setHasTemplates(itm.getHasTemplates());
if (!this.conventionService.isListNullOrEmpty(itm.getFields())) {
data.setFields(new ArrayList<>());
for (FieldModel fieldModel : itm.getFields()) {
data.getFields().add(this.buildExtraFieldEntity(fieldModel));
}
}
if (!this.conventionService.isListNullOrEmpty(itm.getDescriptionTemplates())) {
data.setDescriptionTemplates(new ArrayList<>());
for (DescriptionTemplate descriptionTemplate : itm.getDescriptionTemplates()) {
data.getDescriptionTemplates().add(this.buildDescriptionTemplateEntity(descriptionTemplate, descriptionTemplateGroupMap));
}
}
return data;
}
private @NotNull eu.eudat.commons.types.dmpblueprint.DescriptionTemplateEntity buildDescriptionTemplateEntity(DescriptionTemplate itm, Map<UUID, UUID> descriptionTemplateGroupMap ) {
eu.eudat.commons.types.dmpblueprint.DescriptionTemplateEntity data = new eu.eudat.commons.types.dmpblueprint.DescriptionTemplateEntity();
if (itm == null)
return data;
UUID groupId = descriptionTemplateGroupMap.getOrDefault(itm.getDescriptionTemplateId(), null);
if (groupId == null){
logger.error("Migrate DmpBlueprint, DescriptionTemplate " + itm.getDescriptionTemplateId() + " group not found");
data.setDescriptionTemplateGroupId(itm.getDescriptionTemplateId());
} else {
data.setDescriptionTemplateGroupId(groupId);
}
data.setLabel(itm.getLabel());
data.setMaxMultiplicity(itm.getMaxMultiplicity());
data.setMinMultiplicity(itm.getMinMultiplicity());
return data;
}
private @NotNull eu.eudat.commons.types.dmpblueprint.FieldEntity buildExtraFieldEntity(FieldModel itm) {
if (itm == null)
return new ExtraFieldEntity();
FieldEntity data;
if (FieldCategory.EXTRA.equals(itm.getCategory())) {
ExtraFieldEntity dataTyped = new ExtraFieldEntity();
ExtraField extraField = itm.toExtraField();
switch (extraField.getType()){
case TEXT -> dataTyped.setType(DmpBlueprintExtraFieldDataType.Text);
case DATE -> dataTyped.setType(DmpBlueprintExtraFieldDataType.Date);
case NUMBER -> dataTyped.setType(DmpBlueprintExtraFieldDataType.Number);
case RICH_TEXT -> dataTyped.setType(DmpBlueprintExtraFieldDataType.RichTex);
default -> throw new MyApplicationException("Type not found " + extraField.getType());
}
dataTyped.setRequired(extraField.getRequired());
data = dataTyped;
} else {
SystemFieldEntity dataTyped = new SystemFieldEntity();
SystemField systemField = itm.toSystemField();
switch (systemField.getType()){
case TEXT -> dataTyped.setType(DmpBlueprintSystemFieldType.Text);
case ACCESS_RIGHTS -> dataTyped.setType(DmpBlueprintSystemFieldType.AccessRights);
case CONTACT -> dataTyped.setType(DmpBlueprintSystemFieldType.Contact);
case FUNDER -> dataTyped.setType(DmpBlueprintSystemFieldType.Funder);
case GRANT -> dataTyped.setType(DmpBlueprintSystemFieldType.Grant);
case HTML_TEXT -> dataTyped.setType(DmpBlueprintSystemFieldType.HtmlText);
case LANGUAGE -> dataTyped.setType(DmpBlueprintSystemFieldType.Language);
case LICENSE -> dataTyped.setType(DmpBlueprintSystemFieldType.License);
case ORGANIZATIONS -> dataTyped.setType(DmpBlueprintSystemFieldType.Organizations);
case PROJECT -> dataTyped.setType(DmpBlueprintSystemFieldType.Project);
case RESEARCHERS -> dataTyped.setType(DmpBlueprintSystemFieldType.Researchers);
default -> throw new MyApplicationException("Type not found " + systemField.getType());
}
dataTyped.setRequired(systemField.isRequired());
data = dataTyped;
}
data.setId(itm.getId());
switch (itm.getCategory()){
case EXTRA -> data.setCategory(DmpBlueprintFieldCategory.Extra);
case SYSTEM -> data.setCategory(DmpBlueprintFieldCategory.System);
default -> throw new MyApplicationException("Type not found " + itm.getCategory());
}
data.setLabel(itm.getLabel());
data.setPlaceholder(itm.getPlaceholder());
data.setDescription(itm.getDescription());
data.setOrdinal(itm.getOrdinal());
return data;
}
}

View File

@ -11,7 +11,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;
import javax.management.InvalidApplicationException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
@Api(tags = "Migration")
@ -37,6 +39,7 @@ public class MigrationController {
private final UserContactInfoMigrationService userContactInfoMigrationService;
private final DmpUserMigrationService dmpUserMigrationService;
private final UserMigrationService userMigrationService;
private final DmpBlueprintMigrationService dmpBlueprintMigrationService;
public MigrationController(
DmpMigrationService dmpMigrationService,
@ -53,7 +56,9 @@ public class MigrationController {
OrganizationMigrationService organizationMigrationService,
ReferenceMigrationService referenceMigrationService,
UserContactInfoMigrationService userContactInfoMigrationService,
DmpUserMigrationService dmpUserMigrationService, UserMigrationService userMigrationService) {
DmpUserMigrationService dmpUserMigrationService,
UserMigrationService userMigrationService,
DmpBlueprintMigrationService dmpBlueprintMigrationService) {
this.dmpMigrationService = dmpMigrationService;
this.datasetMigrationService = datasetMigrationService;
this.dmpDatasetProfileMigrationService = dmpDatasetProfileMigrationService;
@ -71,11 +76,12 @@ public class MigrationController {
this.userContactInfoMigrationService = userContactInfoMigrationService;
this.dmpUserMigrationService = dmpUserMigrationService;
this.userMigrationService = userMigrationService;
this.dmpBlueprintMigrationService = dmpBlueprintMigrationService;
}
@GetMapping("all")
@Transactional
public boolean migrateAll() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException, NoSuchFieldException {
public boolean migrateAll() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException, NoSuchFieldException, InvalidApplicationException, TransformerException {
this.dataRepositoryMigrationService.migrate();
this.externalDatasetMigrationService.migrate();
this.funderMigrationService.migrate();
@ -97,9 +103,20 @@ public class MigrationController {
this.userContactInfoMigrationService.migrate();
this.userMigrationService.migrate();
//XML recreate
this.dmpBlueprintMigrationService.migrate();
return true;
}
@GetMapping("dmp-blueprints")
@Transactional
public boolean migrateDmpBlueprint() throws JAXBException, InvalidApplicationException, IOException, ParserConfigurationException, NoSuchFieldException, TransformerException, IllegalAccessException, InstantiationException, SAXException {
this.dmpBlueprintMigrationService.migrate();
return true;
}
@GetMapping("contacts")
@Transactional
public boolean migrateContacts() {