207 lines
9.1 KiB
Java
207 lines
9.1 KiB
Java
package eu.old.eudat.migration;
|
|
|
|
import org.opencdmp.commons.XmlHandlingService;
|
|
import org.opencdmp.commons.enums.FieldType;
|
|
import org.opencdmp.commons.types.descriptiontemplate.*;
|
|
import org.opencdmp.commons.types.descriptiontemplate.fielddata.BaseFieldDataEntity;
|
|
import org.opencdmp.commons.types.descriptiontemplate.fielddata.LabelDataEntity;
|
|
import org.opencdmp.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity;
|
|
import org.opencdmp.convention.ConventionService;
|
|
import org.opencdmp.data.DescriptionTemplateEntity;
|
|
import org.opencdmp.data.ReferenceTypeEntity;
|
|
import org.opencdmp.model.DescriptionTemplate;
|
|
import org.opencdmp.model.ReferenceType;
|
|
import org.opencdmp.query.DescriptionTemplateQuery;
|
|
import org.opencdmp.query.ReferenceTypeQuery;
|
|
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.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.xml.sax.SAXException;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import javax.xml.transform.TransformerException;
|
|
import java.io.IOException;
|
|
import java.net.URISyntaxException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
public class DescriptionTemplateXmlCleanInvalidReferenceTypesService {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateXmlCleanInvalidReferenceTypesService.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 DescriptionTemplateXmlCleanInvalidReferenceTypesService(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, URISyntaxException {
|
|
long total = this.queryFactory.query(DescriptionTemplateQuery.class).count();
|
|
logger.debug("Clean old reference types from DescriptionTemplate Total : " + total);
|
|
int page = 0;
|
|
|
|
List<UUID> existingReferenceTypeIds = this.queryFactory.query(ReferenceTypeQuery.class).collectAs(new BaseFieldSet().ensure(ReferenceType._id)).stream().map(ReferenceTypeEntity::getId).toList();
|
|
List<DescriptionTemplateEntity> items;
|
|
do {
|
|
DescriptionTemplateQuery query = this.queryFactory.query(DescriptionTemplateQuery.class);
|
|
query.setOrder(new Ordering().addDescending(DescriptionTemplate._createdAt));
|
|
query.setPage(new Paging(page * PageSize, PageSize));
|
|
items = query.collect();
|
|
if (items != null && !items.isEmpty()) {
|
|
|
|
logger.debug("Migrate DescriptionTemplate " + page * PageSize + " of " + total);
|
|
|
|
for (DescriptionTemplateEntity item : items) {
|
|
if (this.conventionService.isNullOrEmpty(item.getDefinition())) continue;
|
|
DefinitionEntity definition = this.xmlHandlingService.fromXml(DefinitionEntity.class, item.getDefinition());
|
|
item.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(definition, existingReferenceTypeIds)));
|
|
this.entityManager.merge(item);
|
|
}
|
|
|
|
this.entityManager.flush();
|
|
page++;
|
|
}
|
|
} while (items != null && !items.isEmpty() && !TestMode);
|
|
|
|
this.entityManager.flush();
|
|
}
|
|
|
|
private @NotNull DefinitionEntity buildDefinitionEntity(DefinitionEntity persist, List<UUID> existingReferenceTypeIds) {
|
|
DefinitionEntity data = new DefinitionEntity();
|
|
if (persist == null)
|
|
return data;
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getPages())) {
|
|
data.setPages(new ArrayList<>());
|
|
for (PageEntity pagePersist : persist.getPages()) {
|
|
data.getPages().add(this.buildPageEntity(pagePersist, existingReferenceTypeIds));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull SectionEntity buildSectionEntity(SectionEntity persist, List<UUID> existingReferenceTypeIds) {
|
|
SectionEntity data = new SectionEntity();
|
|
if (persist == null)
|
|
return data;
|
|
|
|
data.setId(persist.getId());
|
|
data.setDescription(persist.getDescription());
|
|
data.setExtendedDescription(persist.getExtendedDescription());
|
|
data.setNumbering(persist.getNumbering());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setDefaultVisibility(persist.isDefaultVisibility());
|
|
data.setTitle(persist.getTitle());
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getSections())) {
|
|
data.setSections(new ArrayList<>());
|
|
for (SectionEntity sectionPersist : persist.getSections()) {
|
|
data.getSections().add(this.buildSectionEntity(sectionPersist, existingReferenceTypeIds));
|
|
}
|
|
}
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFieldSets())) {
|
|
data.setFieldSets(new ArrayList<>());
|
|
for (FieldSetEntity fieldSetPersist : persist.getFieldSets()) {
|
|
data.getFieldSets().add(this.buildFieldSetEntity(fieldSetPersist, existingReferenceTypeIds));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull FieldSetEntity buildFieldSetEntity(FieldSetEntity persist, List<UUID> existingReferenceTypeIds) {
|
|
FieldSetEntity data = new FieldSetEntity();
|
|
if (persist == null)
|
|
return data;
|
|
|
|
data.setId(persist.getId());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setNumbering(persist.getNumbering());
|
|
data.setTitle(persist.getTitle());
|
|
data.setExtendedDescription(persist.getExtendedDescription());
|
|
data.setAdditionalInformation(persist.getAdditionalInformation());
|
|
data.setHasCommentField(persist.getHasCommentField());
|
|
data.setMultiplicity(persist.getMultiplicity());
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFields())) {
|
|
data.setFields(new ArrayList<>());
|
|
for (FieldEntity fieldPersist : persist.getFields()) {
|
|
data.getFields().add(this.buildFieldEntity(fieldPersist, existingReferenceTypeIds));
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
private @NotNull FieldEntity buildFieldEntity(FieldEntity persist, List<UUID> existingReferenceTypeIds) {
|
|
FieldEntity data = new FieldEntity();
|
|
if (persist == null)
|
|
return data;
|
|
|
|
data.setId(persist.getId());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setSchematics(persist.getSchematics());
|
|
data.setNumbering(persist.getNumbering());
|
|
data.setDefaultValue(persist.getDefaultValue());
|
|
data.setValidations(persist.getValidations());
|
|
data.setIncludeInExport(persist.getIncludeInExport());
|
|
data.setVisibilityRules(persist.getVisibilityRules());
|
|
if (persist.getData() != null) data.setData(this.buildFieldDataEntity(persist.getData(), existingReferenceTypeIds));
|
|
|
|
return data;
|
|
}
|
|
|
|
private BaseFieldDataEntity buildFieldDataEntity(BaseFieldDataEntity persist, List<UUID> existingReferenceTypeIds) {
|
|
if (persist.getFieldType().equals(FieldType.REFERENCE_TYPES)){
|
|
ReferenceTypeDataEntity entity = (ReferenceTypeDataEntity)persist;
|
|
if (existingReferenceTypeIds.contains(entity.getReferenceTypeId())) return entity;
|
|
else {
|
|
LabelDataEntity data = new LabelDataEntity();
|
|
data.setLabel(entity.getLabel());
|
|
data.setFieldType(FieldType.TEXT_AREA);
|
|
return data;
|
|
}
|
|
} else {
|
|
return persist;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PageEntity buildPageEntity(PageEntity persist, List<UUID> existingReferenceTypeIds) {
|
|
PageEntity data = new PageEntity();
|
|
if (persist == null)
|
|
return data;
|
|
|
|
data.setId(persist.getId());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setTitle(persist.getTitle());
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getSections())) {
|
|
data.setSections(new ArrayList<>());
|
|
for (SectionEntity sectionPersist : persist.getSections()) {
|
|
data.getSections().add(this.buildSectionEntity(sectionPersist, existingReferenceTypeIds));
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
}
|