682 lines
32 KiB
Java
682 lines
32 KiB
Java
package eu.eudat.service.dmpblueprint;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.authorization.Permission;
|
|
import eu.eudat.commons.XmlHandlingService;
|
|
import eu.eudat.commons.enums.*;
|
|
import eu.eudat.commons.scope.tenant.TenantScope;
|
|
import eu.eudat.commons.types.dmpblueprint.*;
|
|
import eu.eudat.commons.types.dmpblueprint.importexport.*;
|
|
import eu.eudat.convention.ConventionService;
|
|
import eu.eudat.data.DmpBlueprintEntity;
|
|
import eu.eudat.data.TenantEntityManager;
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
import eu.eudat.model.DmpBlueprint;
|
|
import eu.eudat.model.builder.DmpBlueprintBuilder;
|
|
import eu.eudat.model.deleter.DmpBlueprintDeleter;
|
|
import eu.eudat.model.dmpblueprintdefinition.Definition;
|
|
import eu.eudat.model.dmpblueprintdefinition.Field;
|
|
import eu.eudat.model.dmpblueprintdefinition.Section;
|
|
import eu.eudat.model.persist.DmpBlueprintPersist;
|
|
import eu.eudat.model.persist.NewVersionDmpBlueprintPersist;
|
|
import eu.eudat.model.persist.dmpblueprintdefinition.*;
|
|
import eu.eudat.query.DmpBlueprintQuery;
|
|
import eu.eudat.service.responseutils.ResponseUtilsService;
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
import gr.cite.tools.data.deleter.DeleterFactory;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.exception.MyApplicationException;
|
|
import gr.cite.tools.exception.MyForbiddenException;
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
import gr.cite.tools.exception.MyValidationException;
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
import gr.cite.tools.logging.LoggerService;
|
|
import gr.cite.tools.logging.MapLogEntry;
|
|
import gr.cite.tools.validation.ValidatorFactory;
|
|
import jakarta.xml.bind.JAXBException;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.http.ResponseEntity;
|
|
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.nio.charset.StandardCharsets;
|
|
import java.time.Instant;
|
|
import java.util.*;
|
|
|
|
@Service
|
|
public class DmpBlueprintServiceImpl implements DmpBlueprintService {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpBlueprintServiceImpl.class));
|
|
|
|
private final TenantEntityManager entityManager;
|
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
private final DeleterFactory deleterFactory;
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final ConventionService conventionService;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final ResponseUtilsService responseUtilsService;
|
|
|
|
private final XmlHandlingService xmlHandlingService;
|
|
|
|
private final ErrorThesaurusProperties errors;
|
|
|
|
private final ValidatorFactory validatorFactory;
|
|
private final TenantScope tenantScope;
|
|
@Autowired
|
|
public DmpBlueprintServiceImpl(
|
|
TenantEntityManager entityManager,
|
|
AuthorizationService authorizationService,
|
|
DeleterFactory deleterFactory,
|
|
BuilderFactory builderFactory,
|
|
ConventionService conventionService,
|
|
MessageSource messageSource, QueryFactory queryFactory,
|
|
ResponseUtilsService responseUtilsService,
|
|
XmlHandlingService xmlHandlingService,
|
|
ErrorThesaurusProperties errors,
|
|
ValidatorFactory validatorFactory, TenantScope tenantScope) {
|
|
this.entityManager = entityManager;
|
|
this.authorizationService = authorizationService;
|
|
this.deleterFactory = deleterFactory;
|
|
this.builderFactory = builderFactory;
|
|
this.conventionService = conventionService;
|
|
this.messageSource = messageSource;
|
|
this.queryFactory = queryFactory;
|
|
this.responseUtilsService = responseUtilsService;
|
|
this.xmlHandlingService = xmlHandlingService;
|
|
this.errors = errors;
|
|
this.validatorFactory = validatorFactory;
|
|
this.tenantScope = tenantScope;
|
|
}
|
|
|
|
//region Persist
|
|
|
|
public DmpBlueprint persist(DmpBlueprintPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, JsonProcessingException, TransformerException, ParserConfigurationException {
|
|
logger.debug(new MapLogEntry("persisting data").And("model", model).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditDmpBlueprint);
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
DmpBlueprintEntity data;
|
|
if (isUpdate) {
|
|
data = this.entityManager.find(DmpBlueprintEntity.class, model.getId());
|
|
if (data == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash()))
|
|
throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
if (data.getStatus().equals(DmpBlueprintStatus.Finalized))
|
|
throw new MyForbiddenException("Cannot update finalized blueprint");
|
|
} else {
|
|
data = new DmpBlueprintEntity();
|
|
data.setId(UUID.randomUUID());
|
|
data.setStatus(DmpBlueprintStatus.Draft);
|
|
data.setGroupId(UUID.randomUUID());
|
|
data.setVersion((short) 1);
|
|
data.setVersionStatus(DmpBlueprintVersionStatus.NotFinalized);
|
|
data.setCreatedAt(Instant.now());
|
|
data.setIsActive(IsActive.Active);
|
|
}
|
|
|
|
DmpBlueprintStatus previousStatus = data.getStatus();
|
|
|
|
if (model.getDefinition() != null && !model.getDefinition().getSections().stream().anyMatch(x -> x.getHasTemplates())) {
|
|
throw new MyValidationException(this.errors.getDmpBlueprintHasNoDescriptionTemplates().getCode(), this.errors.getDmpBlueprintHasNoDescriptionTemplates().getMessage());
|
|
}
|
|
|
|
data.setLabel(model.getLabel());
|
|
data.setStatus(model.getStatus());
|
|
data.setUpdatedAt(Instant.now());
|
|
data.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(model.getDefinition())));
|
|
|
|
if (isUpdate)
|
|
this.entityManager.merge(data);
|
|
else
|
|
this.entityManager.persist(data);
|
|
|
|
this.entityManager.flush();
|
|
|
|
this.updateVersionStatusAndSave(data, previousStatus, data.getStatus());
|
|
|
|
this.entityManager.flush();
|
|
|
|
return this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, DmpBlueprint._id), data);
|
|
}
|
|
|
|
private void updateVersionStatusAndSave(DmpBlueprintEntity data, DmpBlueprintStatus previousStatus, DmpBlueprintStatus newStatus) throws InvalidApplicationException {
|
|
if (previousStatus.equals(newStatus))
|
|
return;
|
|
if (previousStatus.equals(DmpBlueprintStatus.Finalized))
|
|
throw new MyForbiddenException("Can not update finalized blueprint");
|
|
|
|
if (newStatus.equals(DmpBlueprintStatus.Finalized)) {
|
|
List<DmpBlueprintEntity> latestVersionDmpBlueprints = this.queryFactory.query(DmpBlueprintQuery.class)
|
|
.versionStatuses(DmpBlueprintVersionStatus.Current)
|
|
.isActive(IsActive.Active)
|
|
.groupIds(data.getGroupId())
|
|
.collect();
|
|
if (latestVersionDmpBlueprints.size() > 1)
|
|
throw new MyValidationException("Multiple previous blueprints found");
|
|
DmpBlueprintEntity oldDmpBlueprintEntity = latestVersionDmpBlueprints.stream().findFirst().orElse(null);
|
|
|
|
data.setVersionStatus(DmpBlueprintVersionStatus.Current);
|
|
|
|
if (oldDmpBlueprintEntity != null) {
|
|
data.setVersion((short) (oldDmpBlueprintEntity.getVersion() + 1));
|
|
|
|
oldDmpBlueprintEntity.setVersionStatus(DmpBlueprintVersionStatus.Previous);
|
|
this.entityManager.merge(oldDmpBlueprintEntity);
|
|
} else {
|
|
data.setVersion((short) 1);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private @NotNull DefinitionEntity buildDefinitionEntity(DefinitionPersist persist) {
|
|
DefinitionEntity data = new DefinitionEntity();
|
|
if (persist == null)
|
|
return data;
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getSections())) {
|
|
data.setSections(new ArrayList<>());
|
|
for (SectionPersist sectionPersist : persist.getSections()) {
|
|
data.getSections().add(this.buildSectionEntity(sectionPersist));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull SectionEntity buildSectionEntity(SectionPersist persist) {
|
|
SectionEntity data = new SectionEntity();
|
|
if (persist == null)
|
|
return data;
|
|
|
|
data.setId(persist.getId());
|
|
data.setDescription(persist.getDescription());
|
|
data.setLabel(persist.getLabel());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setHasTemplates(persist.getHasTemplates());
|
|
data.setPrefillingSourcesIds(persist.getPrefillingSourcesIds());
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFields())) {
|
|
data.setFields(new ArrayList<>());
|
|
for (FieldPersist fieldPersist : persist.getFields()) {
|
|
data.getFields().add(this.buildExtraFieldEntity(fieldPersist));
|
|
}
|
|
}
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getDescriptionTemplates())) {
|
|
data.setDescriptionTemplates(new ArrayList<>());
|
|
for (DescriptionTemplatePersist descriptionTemplatePersist : persist.getDescriptionTemplates()) {
|
|
data.getDescriptionTemplates().add(this.buildDescriptionTemplateEntity(descriptionTemplatePersist));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull DescriptionTemplateEntity buildDescriptionTemplateEntity(DescriptionTemplatePersist persist) {
|
|
DescriptionTemplateEntity data = new DescriptionTemplateEntity();
|
|
if (persist == null)
|
|
return data;
|
|
|
|
data.setDescriptionTemplateGroupId(persist.getDescriptionTemplateGroupId());
|
|
data.setLabel(persist.getLabel());
|
|
data.setMaxMultiplicity(persist.getMaxMultiplicity());
|
|
data.setMinMultiplicity(persist.getMinMultiplicity());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull FieldEntity buildExtraFieldEntity(FieldPersist persist) {
|
|
if (persist == null)
|
|
return new ExtraFieldEntity();
|
|
FieldEntity data;
|
|
|
|
switch (persist.getCategory()){
|
|
case Extra -> {
|
|
ExtraFieldEntity dataTyped = new ExtraFieldEntity();
|
|
dataTyped.setType(((ExtraFieldPersist) persist).getDataType());
|
|
data = dataTyped;
|
|
}
|
|
case System -> {
|
|
SystemFieldEntity dataTyped = new SystemFieldEntity();
|
|
dataTyped.setType(((SystemFieldPersist) persist).getSystemFieldType());
|
|
data = dataTyped;
|
|
}
|
|
case ReferenceType -> {
|
|
ReferenceTypeFieldEntity dataTyped = new ReferenceTypeFieldEntity();
|
|
dataTyped.setReferenceTypeId(((ReferenceTypeFieldPersist) persist).getReferenceTypeId());
|
|
dataTyped.setMultipleSelect(((ReferenceTypeFieldPersist) persist).getMultipleSelect());
|
|
data = dataTyped;
|
|
}
|
|
default -> throw new InternalError("unknown type: " + persist.getCategory());
|
|
}
|
|
|
|
data.setId(persist.getId());
|
|
data.setCategory(persist.getCategory());
|
|
data.setLabel(persist.getLabel());
|
|
data.setPlaceholder(persist.getPlaceholder());
|
|
data.setDescription(persist.getDescription());
|
|
data.setSemantics(persist.getSemantics());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setRequired(persist.getRequired());
|
|
|
|
return data;
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region Delete
|
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
logger.debug("deleting : {}", id);
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteDmpBlueprint);
|
|
|
|
this.deleterFactory.deleter(DmpBlueprintDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region FieldInBlueprint
|
|
|
|
public boolean fieldInBlueprint(DmpBlueprintEntity dmpBlueprintEntity, DmpBlueprintSystemFieldType type) {
|
|
|
|
DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, dmpBlueprintEntity.getDefinition());
|
|
if (definition == null || definition.getSections() == null)
|
|
return false;
|
|
|
|
for (SectionEntity section : definition.getSections()) {
|
|
if (section.getFields() == null)
|
|
continue;
|
|
for (FieldEntity field : section.getFields()) {
|
|
if (field.getCategory().equals(DmpBlueprintFieldCategory.System)) {
|
|
SystemFieldEntity systemField = (SystemFieldEntity) field;
|
|
if (systemField.getType().equals(type)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean fieldInBlueprint(UUID id, DmpBlueprintSystemFieldType type) throws InvalidApplicationException {
|
|
DmpBlueprintEntity data = this.entityManager.find(DmpBlueprintEntity.class, id);
|
|
if (data == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
return this.fieldInBlueprint(data, type);
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region Clone
|
|
|
|
public DmpBlueprint buildClone(UUID id, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException {
|
|
logger.debug(new MapLogEntry("persisting data").And("id", id).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.CloneDmpBlueprint);
|
|
|
|
DmpBlueprintQuery query = this.queryFactory.query(DmpBlueprintQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
|
|
DmpBlueprint model = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fields, query.firstAs(fields));
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
model.setLabel(model.getLabel() + " new ");
|
|
model.setId(null);
|
|
model.setHash(null);
|
|
model.setStatus(DmpBlueprintStatus.Draft);
|
|
this.reassignDefinition(model.getDefinition());
|
|
|
|
return model;
|
|
}
|
|
|
|
private void reassignDefinition(Definition model) {
|
|
if (model == null)
|
|
return;
|
|
|
|
if (model.getSections() != null) {
|
|
for (Section section : model.getSections()) {
|
|
this.reassignSection(section);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void reassignSection(Section model) {
|
|
if (model == null)
|
|
return;
|
|
model.setId(UUID.randomUUID());
|
|
|
|
if (model.getFields() != null) {
|
|
for (Field field : model.getFields()) {
|
|
this.reassignField(field);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void reassignField(Field model) {
|
|
if (model == null)
|
|
return;
|
|
model.setId(UUID.randomUUID());
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region NewVersion
|
|
|
|
@Override
|
|
public DmpBlueprint createNewVersion(NewVersionDmpBlueprintPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
|
|
logger.debug(new MapLogEntry("persisting data dmpBlueprint").And("model", model).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.CreateNewVersionDmpBlueprint);
|
|
|
|
DmpBlueprintEntity oldDmpBlueprintEntity = this.entityManager.find(DmpBlueprintEntity.class, model.getId());
|
|
if (oldDmpBlueprintEntity == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
if (!this.conventionService.hashValue(oldDmpBlueprintEntity.getUpdatedAt()).equals(model.getHash()))
|
|
throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
if (!this.tenantScope.isSet() || !Objects.equals(oldDmpBlueprintEntity.getTenantId(), this.tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
|
|
|
List<DmpBlueprintEntity> latestVersionDmpBlueprints = this.queryFactory.query(DmpBlueprintQuery.class)
|
|
.versionStatuses(DmpBlueprintVersionStatus.Current)
|
|
.isActive(IsActive.Active)
|
|
.statuses(DmpBlueprintStatus.Finalized)
|
|
.groupIds(oldDmpBlueprintEntity.getGroupId())
|
|
.collect();
|
|
if (latestVersionDmpBlueprints.isEmpty())
|
|
throw new MyValidationException("Previous blueprint not found");
|
|
if (latestVersionDmpBlueprints.size() > 1)
|
|
throw new MyValidationException("Multiple previous blueprints found");
|
|
if (!latestVersionDmpBlueprints.getFirst().getVersion().equals(oldDmpBlueprintEntity.getVersion()))
|
|
throw new MyValidationException(this.errors.getDmpBlueprintNewVersionConflict().getCode(), this.errors.getDmpBlueprintNewVersionConflict().getMessage());
|
|
Long notFinalizedCount = this.queryFactory.query(DmpBlueprintQuery.class)
|
|
.versionStatuses(DmpBlueprintVersionStatus.NotFinalized)
|
|
.groupIds(oldDmpBlueprintEntity.getGroupId())
|
|
.isActive(IsActive.Active)
|
|
.count();
|
|
if (notFinalizedCount > 0)
|
|
throw new MyValidationException("Already created draft for this blueprint");
|
|
|
|
DmpBlueprintEntity data = new DmpBlueprintEntity();
|
|
data.setId(UUID.randomUUID());
|
|
data.setLabel(model.getLabel());
|
|
data.setStatus(DmpBlueprintStatus.Draft);
|
|
data.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(model.getDefinition())));
|
|
data.setGroupId(oldDmpBlueprintEntity.getGroupId());
|
|
data.setVersion((short) (oldDmpBlueprintEntity.getVersion() + 1));
|
|
data.setVersionStatus(DmpBlueprintVersionStatus.NotFinalized);
|
|
data.setCreatedAt(Instant.now());
|
|
data.setUpdatedAt(Instant.now());
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
this.entityManager.flush();
|
|
|
|
this.updateVersionStatusAndSave(data, DmpBlueprintStatus.Draft, data.getStatus());
|
|
|
|
this.entityManager.flush();
|
|
|
|
return this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, DmpBlueprint._id), data);
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region Export
|
|
|
|
public ResponseEntity<byte[]> exportXml(UUID id) throws MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException, TransformerException, InvalidApplicationException {
|
|
logger.debug(new MapLogEntry("persisting data").And("id", id));
|
|
|
|
this.authorizationService.authorizeForce(Permission.ExportDmpBlueprint);
|
|
DmpBlueprintEntity data = this.entityManager.find(DmpBlueprintEntity.class, id);
|
|
if (data == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
DefinitionEntity dmpDefinition = this.xmlHandlingService.fromXml(DefinitionEntity.class, data.getDefinition());
|
|
String xml = this.xmlHandlingService.toXml(this.definitionXmlToExport(dmpDefinition));
|
|
return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml");
|
|
}
|
|
|
|
private DefinitionImportExport definitionXmlToExport(DefinitionEntity entity) {
|
|
if (entity == null)
|
|
return null;
|
|
DefinitionImportExport xml = new DefinitionImportExport();
|
|
List<SectionImportExport> dmpBlueprintSections = new ArrayList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(entity.getSections())) {
|
|
for (SectionEntity section : entity.getSections()) {
|
|
dmpBlueprintSections.add(this.sectionXmlToExport(section));
|
|
}
|
|
}
|
|
xml.setSections(dmpBlueprintSections);
|
|
return xml;
|
|
}
|
|
|
|
private SectionImportExport sectionXmlToExport(SectionEntity entity) {
|
|
SectionImportExport xml = new SectionImportExport();
|
|
xml.setId(entity.getId());
|
|
xml.setLabel(entity.getLabel());
|
|
xml.setDescription(entity.getDescription());
|
|
xml.setOrdinal(entity.getOrdinal());
|
|
xml.setHasTemplates(entity.getHasTemplates());
|
|
List<SystemFieldImportExport> dmpBlueprintSystemFieldModels = new LinkedList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(entity.getFields())) {
|
|
for (SystemFieldEntity systemField : entity.getFields().stream().filter(x -> x.getCategory() == DmpBlueprintFieldCategory.System).map(x -> (SystemFieldEntity) x).toList()) {
|
|
dmpBlueprintSystemFieldModels.add(this.systemFieldXmlToExport(systemField));
|
|
}
|
|
}
|
|
xml.setSystemFields(dmpBlueprintSystemFieldModels);
|
|
|
|
List<ExtraFieldImportExport> dmpBlueprintExtraFieldModels = new LinkedList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(entity.getFields())) {
|
|
for (ExtraFieldEntity systemField : entity.getFields().stream().filter(x -> x.getCategory() == DmpBlueprintFieldCategory.Extra).map(x -> (ExtraFieldEntity) x).toList()) {
|
|
dmpBlueprintExtraFieldModels.add(this.extraFieldXmlToExport(systemField));
|
|
}
|
|
}
|
|
xml.setExtraFields(dmpBlueprintExtraFieldModels);
|
|
|
|
List<ReferenceTypeFieldImportExport> dmpBlueprintReferenceFieldModels = new LinkedList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(entity.getFields())) {
|
|
for (ReferenceTypeFieldEntity referenceTypeFieldEntity : entity.getFields().stream().filter(x -> x.getCategory() == DmpBlueprintFieldCategory.ReferenceType).map(x -> (ReferenceTypeFieldEntity) x).toList()) {
|
|
dmpBlueprintReferenceFieldModels.add(this.referenceFieldXmlToExport(referenceTypeFieldEntity));
|
|
}
|
|
}
|
|
xml.setReferenceFields(dmpBlueprintReferenceFieldModels);
|
|
|
|
List<DescriptionTemplateImportExport> dmpBlueprintDescriptionTemplates = new LinkedList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(entity.getDescriptionTemplates())) {
|
|
for (DescriptionTemplateEntity descriptionTemplate : entity.getDescriptionTemplates()) {
|
|
dmpBlueprintDescriptionTemplates.add(this.descriptionTemplateXmlToExport(descriptionTemplate));
|
|
}
|
|
}
|
|
xml.setDescriptionTemplates(dmpBlueprintDescriptionTemplates);
|
|
return xml;
|
|
}
|
|
|
|
private DescriptionTemplateImportExport descriptionTemplateXmlToExport(DescriptionTemplateEntity entity) {
|
|
DescriptionTemplateImportExport xml = new DescriptionTemplateImportExport();
|
|
xml.setDescriptionTemplateGroupId(entity.getDescriptionTemplateGroupId());
|
|
xml.setLabel(entity.getLabel());
|
|
if (entity.getMinMultiplicity() != null ) xml.setMinMultiplicity(entity.getMinMultiplicity());
|
|
if (entity.getMaxMultiplicity() != null ) xml.setMaxMultiplicity(entity.getMaxMultiplicity());
|
|
return xml;
|
|
}
|
|
|
|
private ExtraFieldImportExport extraFieldXmlToExport(ExtraFieldEntity entity) {
|
|
ExtraFieldImportExport xml = new ExtraFieldImportExport();
|
|
xml.setId(entity.getId());
|
|
xml.setType(entity.getType());
|
|
xml.setLabel(entity.getLabel());
|
|
xml.setPlaceholder(entity.getPlaceholder());
|
|
xml.setDescription(entity.getDescription());
|
|
xml.setOrdinal(entity.getOrdinal());
|
|
xml.setRequired(entity.isRequired());
|
|
return xml;
|
|
}
|
|
|
|
private ReferenceTypeFieldImportExport referenceFieldXmlToExport(ReferenceTypeFieldEntity entity) {
|
|
ReferenceTypeFieldImportExport xml = new ReferenceTypeFieldImportExport();
|
|
xml.setId(entity.getId());
|
|
xml.setReferenceTypeId(entity.getReferenceTypeId());
|
|
xml.setLabel(entity.getLabel());
|
|
xml.setPlaceholder(entity.getPlaceholder());
|
|
xml.setDescription(entity.getDescription());
|
|
xml.setOrdinal(entity.getOrdinal());
|
|
xml.setRequired(entity.isRequired());
|
|
xml.setMultipleSelect(entity.getMultipleSelect());
|
|
return xml;
|
|
}
|
|
|
|
private SystemFieldImportExport systemFieldXmlToExport(SystemFieldEntity entity) {
|
|
SystemFieldImportExport xml = new SystemFieldImportExport();
|
|
xml.setId(entity.getId());
|
|
xml.setType(entity.getType());
|
|
xml.setLabel(entity.getLabel());
|
|
xml.setPlaceholder(entity.getPlaceholder());
|
|
xml.setDescription(entity.getDescription());
|
|
xml.setOrdinal(entity.getOrdinal());
|
|
xml.setRequired(entity.isRequired());
|
|
return xml;
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region Import
|
|
|
|
public DmpBlueprint importXml(byte[] bytes, String label, FieldSet fields) throws MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, TransformerException, InvalidApplicationException, IOException, InstantiationException, IllegalAccessException, SAXException {
|
|
logger.debug(new MapLogEntry("import data").And("bytes", bytes).And("label", label).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.ImportDmpBlueprint);
|
|
|
|
DmpBlueprintPersist persist = new DmpBlueprintPersist();
|
|
|
|
persist.setLabel(label);
|
|
persist.setStatus(DmpBlueprintStatus.Draft);
|
|
|
|
BlueprintImportExport dmpDefinition = this.xmlHandlingService.fromXml(BlueprintImportExport.class, new String(bytes, StandardCharsets.UTF_8));
|
|
if (dmpDefinition != null)
|
|
persist.setDefinition(this.xmlDefinitionToPersist(dmpDefinition.getDmpBlueprintDefinition()));
|
|
|
|
this.validatorFactory.validator(DmpBlueprintPersist.DmpBlueprintPersistValidator.class).validateForce(persist);
|
|
return this.persist(persist, fields);
|
|
}
|
|
|
|
private DefinitionPersist xmlDefinitionToPersist(DefinitionImportExport importXml) {
|
|
if (importXml == null)
|
|
return null;
|
|
DefinitionPersist persist = new DefinitionPersist();
|
|
List<SectionPersist> dmpBlueprintSections = new ArrayList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(importXml.getSections())) {
|
|
for (SectionImportExport section : importXml.getSections()) {
|
|
dmpBlueprintSections.add(this.xmlSectionToPersist(section));
|
|
}
|
|
}
|
|
persist.setSections(dmpBlueprintSections);
|
|
return persist;
|
|
}
|
|
|
|
private SectionPersist xmlSectionToPersist(SectionImportExport importXml) {
|
|
SectionPersist persist = new SectionPersist();
|
|
persist.setId(importXml.getId());
|
|
persist.setLabel(importXml.getLabel());
|
|
persist.setDescription(importXml.getDescription());
|
|
persist.setOrdinal(importXml.getOrdinal());
|
|
persist.setHasTemplates(importXml.isHasTemplates());
|
|
List<FieldPersist> dmpBlueprintFieldModels = new LinkedList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(importXml.getSystemFields())) {
|
|
for (SystemFieldImportExport systemField : importXml.getSystemFields()) {
|
|
dmpBlueprintFieldModels.add(this.xmlSystemFieldToPersist(systemField));
|
|
}
|
|
}
|
|
if (!this.conventionService.isListNullOrEmpty(importXml.getReferenceFields())) {
|
|
for (ReferenceTypeFieldImportExport referenceField : importXml.getReferenceFields()) {
|
|
dmpBlueprintFieldModels.add(this.xmlReferenceFieldToPersist(referenceField));
|
|
}
|
|
}
|
|
if (!this.conventionService.isListNullOrEmpty(importXml.getExtraFields())) {
|
|
for (ExtraFieldImportExport extraField : importXml.getExtraFields()) {
|
|
dmpBlueprintFieldModels.add(this.xmlExtraFieldToPersist(extraField));
|
|
}
|
|
}
|
|
persist.setFields(dmpBlueprintFieldModels);
|
|
List<DescriptionTemplatePersist> dmpBlueprintDescriptionTemplates = new LinkedList<>();
|
|
if (!this.conventionService.isListNullOrEmpty(importXml.getDescriptionTemplates())) {
|
|
for (DescriptionTemplateImportExport descriptionTemplate : importXml.getDescriptionTemplates()) {
|
|
dmpBlueprintDescriptionTemplates.add(this.xmlDescriptionTemplateToPersist(descriptionTemplate));
|
|
}
|
|
}
|
|
persist.setDescriptionTemplates(dmpBlueprintDescriptionTemplates);
|
|
return persist;
|
|
}
|
|
|
|
private DescriptionTemplatePersist xmlDescriptionTemplateToPersist(DescriptionTemplateImportExport importXml) {
|
|
DescriptionTemplatePersist persist = new DescriptionTemplatePersist();
|
|
persist.setDescriptionTemplateGroupId(importXml.getDescriptionTemplateGroupId());
|
|
persist.setLabel(importXml.getLabel());
|
|
persist.setMinMultiplicity(importXml.getMinMultiplicity());
|
|
persist.setMaxMultiplicity(importXml.getMaxMultiplicity());
|
|
return persist;
|
|
}
|
|
|
|
private FieldPersist xmlExtraFieldToPersist(ExtraFieldImportExport importXml) {
|
|
ExtraFieldPersist persist = new ExtraFieldPersist();
|
|
persist.setId(importXml.getId());
|
|
persist.setCategory(DmpBlueprintFieldCategory.Extra);
|
|
persist.setDataType(importXml.getType());
|
|
persist.setLabel(importXml.getLabel());
|
|
persist.setPlaceholder(importXml.getPlaceholder());
|
|
persist.setDescription(importXml.getDescription());
|
|
persist.setOrdinal(importXml.getOrdinal());
|
|
persist.setRequired(importXml.isRequired());
|
|
return persist;
|
|
}
|
|
|
|
private FieldPersist xmlSystemFieldToPersist(SystemFieldImportExport importXml) {
|
|
SystemFieldPersist persist = new SystemFieldPersist();
|
|
persist.setId(importXml.getId());
|
|
persist.setCategory(DmpBlueprintFieldCategory.System);
|
|
persist.setSystemFieldType(importXml.getType());
|
|
persist.setLabel(importXml.getLabel());
|
|
persist.setPlaceholder(importXml.getPlaceholder());
|
|
persist.setDescription(importXml.getDescription());
|
|
persist.setOrdinal(importXml.getOrdinal());
|
|
persist.setRequired(importXml.isRequired());
|
|
return persist;
|
|
}
|
|
|
|
private FieldPersist xmlReferenceFieldToPersist(ReferenceTypeFieldImportExport importXml) {
|
|
ReferenceTypeFieldPersist persist = new ReferenceTypeFieldPersist();
|
|
persist.setId(importXml.getId());
|
|
persist.setCategory(DmpBlueprintFieldCategory.ReferenceType);
|
|
persist.setReferenceTypeId(importXml.getReferenceTypeId());
|
|
persist.setLabel(importXml.getLabel());
|
|
persist.setPlaceholder(importXml.getPlaceholder());
|
|
persist.setDescription(importXml.getDescription());
|
|
persist.setOrdinal(importXml.getOrdinal());
|
|
persist.setRequired(importXml.isRequired());
|
|
persist.setMultipleSelect(importXml.getMultipleSelect());
|
|
return persist;
|
|
}
|
|
|
|
//endregion
|
|
}
|
|
|