rda import implementation

This commit is contained in:
amentis 2024-06-18 13:34:09 +03:00
parent ecd69281e6
commit 95710cfdbb
10 changed files with 408 additions and 58 deletions

View File

@ -66,7 +66,7 @@
<dependency>
<groupId>org.opencdmp</groupId>
<artifactId>file-transformer-base</artifactId>
<version>0.0.22</version>
<version>0.0.23</version>
</dependency>
<dependency>
<groupId>gr.cite</groupId>

View File

@ -0,0 +1,88 @@
package org.opencdmp.model.persist;
import gr.cite.tools.validation.specification.Specification;
import org.opencdmp.commons.validation.BaseValidator;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class DescriptionCommonModelConfig {
private String id;
public static final String _id = "id";
private UUID sectionId;
public static final String _sectionId = "_sectionId";
private UUID templateId;
public static final String _templateId = "templateId";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public UUID getSectionId() {
return sectionId;
}
public void setSectionId(UUID sectionId) {
this.sectionId = sectionId;
}
public UUID getTemplateId() {
return templateId;
}
public void setTemplateId(UUID templateId) {
this.templateId = templateId;
}
@Component(DescriptionCommonModelConfigValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DescriptionCommonModelConfigValidator extends BaseValidator<DescriptionCommonModelConfig> {
public static final String ValidatorName = "DescriptionCommonModelConfigValidator";
private final MessageSource messageSource;
protected DescriptionCommonModelConfigValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
super(conventionService, errors);
this.messageSource = messageSource;
}
@Override
protected Class<DescriptionCommonModelConfig> modelClass() {
return DescriptionCommonModelConfig.class;
}
@Override
protected List<Specification> specifications(DescriptionCommonModelConfig item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getId()))
.failOn(DescriptionCommonModelConfig._id).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionCommonModelConfig._id}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isEmpty(item.getId()))
.must(() -> this.isValidGuid(UUID.fromString(item.getId())))
.failOn(DescriptionCommonModelConfig._id).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionCommonModelConfig._id}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getSectionId()))
.failOn(DescriptionCommonModelConfig._sectionId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionCommonModelConfig._sectionId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getTemplateId()))
.failOn(DescriptionCommonModelConfig._templateId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionCommonModelConfig._templateId}, LocaleContextHolder.getLocale()))
);
}
}
}

View File

@ -0,0 +1,127 @@
package org.opencdmp.model.persist;
import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification;
import org.opencdmp.commons.validation.BaseValidator;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.DmpEntity;
import org.opencdmp.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class DmpCommonModelConfig {
private UUID fileId;
public static final String _fileId = "fileId";
private String label;
public static final String _label = "label";
private String repositoryId;
public static final String _repositoryId = "repositoryId";
private UUID blueprintId;
public static final String _blueprintId = "blueprintId";
private List<DescriptionCommonModelConfig> descriptions;
public static final String _descriptions = "descriptions";
public UUID getFileId() {
return fileId;
}
public void setFileId(UUID fileId) {
this.fileId = fileId;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getRepositoryId() {
return repositoryId;
}
public void setRepositoryId(String repositoryId) {
this.repositoryId = repositoryId;
}
public UUID getBlueprintId() {
return blueprintId;
}
public void setBlueprintId(UUID blueprintId) {
this.blueprintId = blueprintId;
}
public List<DescriptionCommonModelConfig> getDescriptions() {
return descriptions;
}
public void setDescriptions(List<DescriptionCommonModelConfig> descriptions) {
this.descriptions = descriptions;
}
@Component(DmpCommonModelConfigValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpCommonModelConfigValidator extends BaseValidator<DmpCommonModelConfig> {
public static final String ValidatorName = "DmpCommonModelConfigValidator";
private final MessageSource messageSource;
private final ValidatorFactory validatorFactory;
protected DmpCommonModelConfigValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.messageSource = messageSource;
this.validatorFactory = validatorFactory;
}
@Override
protected Class<DmpCommonModelConfig> modelClass() {
return DmpCommonModelConfig.class;
}
@Override
protected List<Specification> specifications(DmpCommonModelConfig item) {
return Arrays.asList(
this.spec()
.must(() -> this.isValidGuid(item.getFileId()))
.failOn(DmpCommonModelConfig._fileId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DmpCommonModelConfig._fileId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getLabel()))
.failOn(DmpCommonModelConfig._label).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DmpCommonModelConfig._label}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isEmpty(item.getLabel()))
.must(() -> this.lessEqualLength(item.getLabel(), DmpEntity._labelLength))
.failOn(DmpCommonModelConfig._label).failWith(this.messageSource.getMessage("Validation_MaxLength", new Object[]{DmpCommonModelConfig._label}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getRepositoryId()))
.failOn(DmpCommonModelConfig._repositoryId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DmpCommonModelConfig._repositoryId}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isEmpty(item.getRepositoryId()))
.must(() -> this.isValidGuid(UUID.fromString(item.getRepositoryId())))
.failOn(DmpCommonModelConfig._repositoryId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DmpCommonModelConfig._repositoryId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getBlueprintId()))
.failOn(DmpCommonModelConfig._blueprintId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DmpCommonModelConfig._blueprintId}, LocaleContextHolder.getLocale())),
this.navSpec()
.iff(() -> !this.isListNullOrEmpty(item.getDescriptions()))
.on(DmpCommonModelConfig._descriptions)
.over(item.getDescriptions())
.using((itm) -> this.validatorFactory.validator(DescriptionCommonModelConfig.DescriptionCommonModelConfigValidator.class))
);
}
}
}

View File

@ -1669,15 +1669,18 @@ public class DescriptionServiceImpl implements DescriptionService {
if (!referenceTypeEntity.getCode().equals(model.getType().getCode())) throw new MyApplicationException("Invalid reference for field " + model.getId());
ReferenceEntity referenceEntity = this.queryFactory.query(ReferenceQuery.class).ids(model.getId()).first(); //TODO: optimize
if (referenceEntity == null) referenceEntity = this.queryFactory.query(ReferenceQuery.class).references(model.getReference()).typeIds(referenceTypeEntity.getId()).sources(model.getSource()).first();
ReferenceEntity referenceEntity = model.getId() != null ? this.queryFactory.query(ReferenceQuery.class).ids(model.getId()).first(): null; //TODO: optimize
if (referenceEntity == null && !this.conventionService.isNullOrEmpty(model.getReference())) {
List<ReferenceEntity> referenceEntities = this.queryFactory.query(ReferenceQuery.class).references(model.getReference()).typeIds(referenceTypeEntity.getId()).collect();
if (referenceEntities != null && referenceEntities.size() == 1) referenceEntity = referenceEntities.getFirst();
}
ReferencePersist persist = new ReferencePersist();
persist.setTypeId(referenceTypeEntity.getId());
if (referenceEntity == null) {
persist.setLabel(model.getLabel());
persist.setReference(model.getReference());
persist.setReference(!this.conventionService.isNullOrEmpty(model.getReference()) ? model.getReference() : UUID.randomUUID().toString());
persist.setSource("internal");
persist.setSourceType(ReferenceSourceType.Internal);
} else {

View File

@ -7,12 +7,12 @@ import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.FieldSet;
import jakarta.xml.bind.JAXBException;
import org.opencdmp.commons.types.dmp.importexport.DmpImportExport;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel;
import org.opencdmp.model.DmpUser;
import org.opencdmp.model.DmpValidationResult;
import org.opencdmp.model.dmp.Dmp;
import org.opencdmp.model.persist.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;
import javax.crypto.BadPaddingException;
@ -59,5 +59,7 @@ public interface DmpService {
Dmp importXml(byte[] bytes, String label, FieldSet fields) throws MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, TransformerException, InvalidApplicationException, IOException, InstantiationException, IllegalAccessException, SAXException;
Dmp importJson(MultipartFile file, String label, String repositoryId, String format, FieldSet fields) throws MyForbiddenException, MyNotFoundException, JAXBException, InvalidApplicationException, IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException;
Dmp importJson(DmpCommonModelConfig dmpCommonModelConfig, FieldSet fields) throws MyForbiddenException, MyNotFoundException, JAXBException, InvalidApplicationException, IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException;
PreprocessingDmpModel preprocessingDmp(UUID fileId, String repositoryId) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException;
}

View File

@ -64,6 +64,7 @@ import org.opencdmp.data.*;
import org.opencdmp.errorcode.ErrorThesaurusProperties;
import org.opencdmp.event.DmpTouchedEvent;
import org.opencdmp.event.EventBroker;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel;
import org.opencdmp.integrationevent.outbox.annotationentityremoval.AnnotationEntityRemovalIntegrationEventHandler;
import org.opencdmp.integrationevent.outbox.annotationentitytouch.AnnotationEntityTouchedIntegrationEventHandler;
import org.opencdmp.integrationevent.outbox.notification.NotifyIntegrationEvent;
@ -107,7 +108,6 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;
import javax.crypto.BadPaddingException;
@ -2051,16 +2051,19 @@ public class DmpServiceImpl implements DmpService {
//region Import RDA JSON
public PreprocessingDmpModel preprocessingDmp(UUID fileId, String repositoryId) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException {
return this.fileTransformerService.preprocessingDmp(fileId, repositoryId);
}
public Dmp importJson(MultipartFile file, String label, String repositoryId, String format, FieldSet fields) throws MyForbiddenException, MyNotFoundException, JAXBException, InvalidApplicationException, IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException {
DmpModel model = this.fileTransformerService.importDmp(file, repositoryId, format);
public Dmp importJson(DmpCommonModelConfig dmpCommonModelConfig, FieldSet fields) throws MyForbiddenException, MyNotFoundException, JAXBException, InvalidApplicationException, IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException {
logger.debug(new MapLogEntry("import data").And("file id", dmpCommonModelConfig.getFileId()).And("label", dmpCommonModelConfig.getLabel()).And("fields", fields));
DmpModel model = this.fileTransformerService.importDmp(dmpCommonModelConfig);
if (model == null) throw new MyNotFoundException("Plan Import Error");
logger.debug(new MapLogEntry("import data").And("bytes", file.getBytes()).And("label", label).And("fields", fields));
DmpPersist persist = new DmpPersist();
persist.setLabel(label);
persist.setLabel(dmpCommonModelConfig.getLabel());
persist.setStatus(DmpStatus.Draft);
persist.setDescription(model.getDescription());
switch (model.getAccessType()) {
@ -2163,7 +2166,7 @@ public class DmpServiceImpl implements DmpService {
}
} else {
// custom fields
if (field.getCategory().equals(org.opencdmp.commonmodels.enums.DmpBlueprintFieldCategory.Extra) && commonModel.getProperties() != null && this.conventionService.isListNullOrEmpty(commonModel.getProperties().getDmpBlueprintValues())){
if (field.getCategory().equals(org.opencdmp.commonmodels.enums.DmpBlueprintFieldCategory.Extra) && commonModel.getProperties() != null && !this.conventionService.isListNullOrEmpty(commonModel.getProperties().getDmpBlueprintValues())){
DmpBlueprintValueModel dmpBlueprintValueModel = commonModel.getProperties().getDmpBlueprintValues().stream().filter(x -> x.getFieldId().equals(field.getId())).findFirst().orElse(null);
ExtraFieldModel extraFieldModel = (ExtraFieldModel) field;
if (dmpBlueprintValueModel != null) dmpBlueprintValues.put(dmpBlueprintValueModel.getFieldId(), this.commonModelDmpBlueprintValueToPersist(dmpBlueprintValueModel, extraFieldModel));
@ -2204,18 +2207,21 @@ public class DmpServiceImpl implements DmpService {
private ReferencePersist commonDmpReferenceToReferencePersist(ReferenceModel model, ReferenceTypeEntity referenceTypeEntity) {
if (!referenceTypeEntity.getCode().equals(model.getType().getCode())) throw new MyApplicationException("Invalid reference for field " + model.getId());
if (this.conventionService.isNullOrEmpty(model.getLabel()) && this.conventionService.isNullOrEmpty(model.getReference())) throw new MyApplicationException("Dmp Reference without label and reference id ");
ReferenceEntity referenceEntity = this.queryFactory.query(ReferenceQuery.class).ids(model.getId()).first(); //TODO: optimize
if (referenceEntity == null) referenceEntity = this.queryFactory.query(ReferenceQuery.class).references(model.getReference()).typeIds(referenceTypeEntity.getId()).sources(model.getSource()).first();
ReferenceEntity referenceEntity = model.getId() != null ? this.queryFactory.query(ReferenceQuery.class).ids(model.getId()).first(): null; //TODO: optimize
if (referenceEntity == null && !this.conventionService.isNullOrEmpty(model.getReference())) {
List<ReferenceEntity> referenceEntities = this.queryFactory.query(ReferenceQuery.class).references(model.getReference()).typeIds(referenceTypeEntity.getId()).collect();
if (referenceEntities != null && referenceEntities.size() == 1) referenceEntity = referenceEntities.getFirst();
}
ReferencePersist persist = new ReferencePersist();
persist.setTypeId(referenceTypeEntity.getId());
if (referenceEntity == null) {
persist.setLabel(model.getLabel());
persist.setReference(model.getReference());
persist.setReference(!this.conventionService.isNullOrEmpty(model.getReference()) ? model.getReference() : UUID.randomUUID().toString());
persist.setSource("internal");
persist.setSourceType(ReferenceSourceType.Internal);
} else {

View File

@ -7,6 +7,10 @@ import org.opencdmp.commonmodels.models.description.DescriptionModel;
import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.filetransformerbase.interfaces.FileTransformerClient;
import org.opencdmp.filetransformerbase.interfaces.FileTransformerConfiguration;
import org.opencdmp.filetransformerbase.models.misc.DescriptionImportModel;
import org.opencdmp.filetransformerbase.models.misc.DmpImportModel;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDescriptionModel;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.reactive.function.client.WebClient;
@ -30,9 +34,9 @@ public class FileTransformerRepository implements FileTransformerClient {
}
@Override
public DmpModel importDmp(FileEnvelopeModel fileEnvelope) {
logger.debug(new MapLogEntry("importDmp").And("fileEnvelope", fileEnvelope));
return this.transformerClient.post().uri("/import/dmp").bodyValue(fileEnvelope)
public DmpModel importDmp(DmpImportModel dmpImportModel) {
logger.debug(new MapLogEntry("importDmp").And("fileEnvelope", dmpImportModel.getFile()));
return this.transformerClient.post().uri("/import/dmp").bodyValue(dmpImportModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(DmpModel.class)).block();
}
@ -44,9 +48,9 @@ public class FileTransformerRepository implements FileTransformerClient {
}
@Override
public DescriptionModel importDescription(FileEnvelopeModel fileEnvelope) {
logger.debug(new MapLogEntry("importDescription").And("fileEnvelope", fileEnvelope));
return this.transformerClient.post().uri("/import/description").bodyValue(fileEnvelope)
public DescriptionModel importDescription(DescriptionImportModel descriptionImportModel) {
logger.debug(new MapLogEntry("importDescription").And("fileEnvelope", descriptionImportModel.getFile()));
return this.transformerClient.post().uri("/import/description").bodyValue(descriptionImportModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(DescriptionModel.class)).block();
}
@ -57,5 +61,19 @@ public class FileTransformerRepository implements FileTransformerClient {
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(new ParameterizedTypeReference<FileTransformerConfiguration>() {})).block();
}
@Override
public PreprocessingDmpModel preprocessingDmp(FileEnvelopeModel fileEnvelopeModel) {
logger.debug(new MapLogEntry("preprocessingDmp").And("fileEnvelope", fileEnvelopeModel));
return this.transformerClient.post().uri("/preprocessing/dmp").bodyValue(fileEnvelopeModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(PreprocessingDmpModel.class)).block();
}
@Override
public PreprocessingDescriptionModel preprocessingDescription(FileEnvelopeModel fileEnvelopeModel) {
logger.debug(new MapLogEntry("preprocessingDescription").And("fileEnvelope", fileEnvelopeModel));
return this.transformerClient.post().uri("/preprocessing/description").bodyValue(fileEnvelopeModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(PreprocessingDescriptionModel.class)).block();
}
}

View File

@ -2,8 +2,9 @@ package org.opencdmp.service.filetransformer;
import jakarta.xml.bind.JAXBException;
import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel;
import org.opencdmp.model.file.RepositoryFileFormat;
import org.springframework.web.multipart.MultipartFile;
import org.opencdmp.model.persist.DmpCommonModelConfig;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
@ -23,6 +24,7 @@ public interface FileTransformerService {
org.opencdmp.model.file.FileEnvelope exportDescription(UUID descriptionId, String repositoryId, String format) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException;
DmpModel importDmp(MultipartFile file, String repositoryId, String format) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException, JAXBException;
DmpModel importDmp(DmpCommonModelConfig dmpCommonModelConfig) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException, JAXBException;
PreprocessingDmpModel preprocessingDmp(UUID fileId, String repositoryId) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException;
}

View File

@ -1,22 +1,26 @@
package org.opencdmp.service.filetransformer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeCacheService;
import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeFilterFunction;
import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeModel;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.xml.bind.JAXBException;
import org.apache.commons.io.FilenameUtils;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.authorization.Permission;
import org.opencdmp.commonmodels.models.FileEnvelopeModel;
import org.opencdmp.commonmodels.models.description.DescriptionModel;
import org.opencdmp.commonmodels.models.descriptiotemplate.DescriptionTemplateModel;
import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.commonmodels.models.dmpblueprint.DmpBlueprintModel;
import org.opencdmp.commons.JsonHandlingService;
import org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.enums.StorageType;
@ -26,20 +30,28 @@ import org.opencdmp.commons.scope.user.UserScope;
import org.opencdmp.commons.types.filetransformer.FileTransformerSourceEntity;
import org.opencdmp.commons.types.tenantconfiguration.FileTransformerTenantConfigurationEntity;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.DescriptionTemplateEntity;
import org.opencdmp.data.StorageFileEntity;
import org.opencdmp.data.TenantConfigurationEntity;
import org.opencdmp.event.TenantConfigurationTouchedEvent;
import org.opencdmp.filetransformerbase.interfaces.FileTransformerConfiguration;
import org.opencdmp.filetransformerbase.models.misc.DescriptionImportModel;
import org.opencdmp.filetransformerbase.models.misc.DmpImportModel;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel;
import org.opencdmp.model.StorageFile;
import org.opencdmp.model.builder.commonmodels.description.DescriptionCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.descriptiontemplate.DescriptionTemplateCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.dmp.DmpCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.dmpblueprint.DmpBlueprintCommonModelBuilder;
import org.opencdmp.model.description.Description;
import org.opencdmp.model.dmp.Dmp;
import org.opencdmp.model.dmpblueprint.DmpBlueprint;
import org.opencdmp.model.file.RepositoryFileFormat;
import org.opencdmp.model.persist.DescriptionCommonModelConfig;
import org.opencdmp.model.persist.DmpCommonModelConfig;
import org.opencdmp.model.persist.StorageFilePersist;
import org.opencdmp.model.tenantconfiguration.TenantConfiguration;
import org.opencdmp.query.DescriptionQuery;
import org.opencdmp.query.DmpQuery;
import org.opencdmp.query.TenantConfigurationQuery;
import org.opencdmp.query.*;
import org.opencdmp.service.encryption.EncryptionService;
import org.opencdmp.service.storage.StorageFileService;
import org.opencdmp.service.tenant.TenantProperties;
@ -48,8 +60,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.event.EventListener;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@ -64,6 +78,7 @@ import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class FileTransformerServiceImpl implements FileTransformerService {
@ -119,9 +134,12 @@ public class FileTransformerServiceImpl implements FileTransformerService {
exchangeFilterFunctions.add(tokenExchangeFilterFunction);
exchangeFilterFunctions.add(logRequest());
exchangeFilterFunctions.add(logResponse());
}).codecs(codecs -> codecs
.defaultCodecs()
.maxInMemorySize(source.getMaxInMemorySizeInBytes())
}).codecs(codecs -> {
codecs.defaultCodecs().maxInMemorySize(source.getMaxInMemorySizeInBytes());
codecs.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(new ObjectMapper().registerModule(new JavaTimeModule()), MediaType.APPLICATION_JSON));
codecs.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(new ObjectMapper().registerModule(new JavaTimeModule()), MediaType.APPLICATION_JSON));
}
).build());
this.clients.put(repositoryIdByTenant, repository);
return repository;
@ -295,37 +313,105 @@ public class FileTransformerServiceImpl implements FileTransformerService {
}
@Override
public DmpModel importDmp(MultipartFile file, String repositoryId, String format) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException, JAXBException {
public DmpModel importDmp(DmpCommonModelConfig dmpCommonModelConfig) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException, JAXBException {
this.authorizationService.authorizeForce(Permission.NewDmp);
if (file == null) return null;
StorageFileEntity tempFile = this.queryFactory.query(StorageFileQuery.class).disableTracking().authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(dmpCommonModelConfig.getFileId()).first();
if (tempFile == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{dmpCommonModelConfig.getFileId(), StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
//GK: First get the right client
FileTransformerRepository repository = this.getRepository(repositoryId);
if (repository == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{format, FileTransformerRepository.class.getSimpleName()}, LocaleContextHolder.getLocale()));
FileTransformerRepository repository = this.getRepository(dmpCommonModelConfig.getRepositoryId());
if (repository == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{dmpCommonModelConfig.getRepositoryId(), FileTransformerRepository.class.getSimpleName()}, LocaleContextHolder.getLocale()));
String name = FilenameUtils.removeExtension(file.getOriginalFilename());
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String mimeType = URLConnection.guessContentTypeFromName(file.getOriginalFilename());
DmpBlueprintQuery dmpBlueprintQuery = this.queryFactory.query(DmpBlueprintQuery.class).disableTracking().authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(dmpCommonModelConfig.getBlueprintId());
DmpBlueprintModel dmpBlueprintModel = this.builderFactory.builder(DmpBlueprintCommonModelBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(dmpBlueprintQuery.first());
if (dmpBlueprintModel == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{dmpCommonModelConfig.getBlueprintId(), DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
DmpImportModel dmpImportModel = new DmpImportModel();
dmpImportModel.setBlueprintModel(dmpBlueprintModel);
if (!this.conventionService.isListNullOrEmpty(dmpCommonModelConfig.getDescriptions())){
List<DescriptionTemplateEntity> descriptionTemplateEntities = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(dmpCommonModelConfig.getDescriptions().stream().map(x -> x.getTemplateId()).distinct().collect(Collectors.toList())).collect();
if (descriptionTemplateEntities == null) throw new MyApplicationException("Description Templates Not Exist!");
List<DescriptionImportModel> descriptionImportModels = new ArrayList<>();
for (DescriptionCommonModelConfig descriptionCommonModelConfig : dmpCommonModelConfig.getDescriptions()) {
DescriptionTemplateEntity descriptionTemplateEntity = descriptionTemplateEntities.stream().filter(x -> x.getId().equals(descriptionCommonModelConfig.getTemplateId())).findFirst().orElse(null);
if (descriptionTemplateEntity != null){
DescriptionTemplateModel descriptionTemplateModel = this.builderFactory.builder(DescriptionTemplateCommonModelBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(descriptionTemplateEntity);
DescriptionImportModel descriptionImportModel = new DescriptionImportModel();
descriptionImportModel.setId(descriptionCommonModelConfig.getId());
descriptionImportModel.setSectionId(descriptionCommonModelConfig.getSectionId());
descriptionImportModel.setDescriptionTemplate(descriptionTemplateModel);
descriptionImportModels.add(descriptionImportModel);
}
}
dmpImportModel.setDescriptions(descriptionImportModels);
}
String originalFileName = tempFile.getName() + (tempFile.getExtension().startsWith(".") ? "" : ".") + tempFile.getExtension();
String mimeType = URLConnection.guessContentTypeFromName(originalFileName);
FileEnvelopeModel fileEnvelope = new FileEnvelopeModel();
fileEnvelope.setFile(file.getBytes());
fileEnvelope.setFile(this.storageFileService.readAsBytesSafe(dmpCommonModelConfig.getFileId()));
fileEnvelope.setMimeType(mimeType);
fileEnvelope.setFilename(name + (extension.startsWith(".") ? "" : ".") + extension);
fileEnvelope.setFilename(originalFileName);
if (repository.getConfiguration() != null && repository.getConfiguration().isUseSharedStorage()){
StorageFilePersist storageFilePersist = new StorageFilePersist();
storageFilePersist.setName(name);
storageFilePersist.setExtension(extension);
storageFilePersist.setName(tempFile.getName());
storageFilePersist.setExtension(tempFile.getExtension());
storageFilePersist.setMimeType(mimeType);
storageFilePersist.setOwnerId(this.userScope.getUserIdSafe());
storageFilePersist.setStorageType(StorageType.Transformer);
StorageFile storageFile = this.storageFileService.persistBytes(storageFilePersist, file.getBytes(), new BaseFieldSet(StorageFile._id, StorageFile._fileRef, StorageFile._mimeType, StorageFile._extension, StorageFile._name));
StorageFile storageFile = this.storageFileService.persistBytes(storageFilePersist, fileEnvelope.getFile(), new BaseFieldSet(StorageFile._id, StorageFile._fileRef, StorageFile._mimeType, StorageFile._extension, StorageFile._name));
fileEnvelope.setFileRef(storageFile.getFileRef());
}
return repository.importDmp(fileEnvelope);
dmpImportModel.setFile(fileEnvelope);
return repository.importDmp(dmpImportModel);
}
@Override
public PreprocessingDmpModel preprocessingDmp(UUID fileId, String repositoryId) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException {
this.authorizationService.authorizeForce(Permission.NewDmp);
StorageFileEntity tempFile = this.queryFactory.query(StorageFileQuery.class).disableTracking().authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(fileId).first();
if (tempFile == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{fileId, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
//GK: First get the right client
FileTransformerRepository repository = this.getRepository(repositoryId);
if (repository == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{repositoryId, FileTransformerRepository.class.getSimpleName()}, LocaleContextHolder.getLocale()));
String originalFileName = tempFile.getName() + (tempFile.getExtension().startsWith(".") ? "" : ".") + tempFile.getExtension();
String mimeType = URLConnection.guessContentTypeFromName(originalFileName);
FileEnvelopeModel fileEnvelope = new FileEnvelopeModel();
fileEnvelope.setFile(this.storageFileService.readAsBytesSafe(fileId));
fileEnvelope.setMimeType(mimeType);
fileEnvelope.setFilename(originalFileName);
if (repository.getConfiguration() != null && repository.getConfiguration().isUseSharedStorage()){
StorageFilePersist storageFilePersist = new StorageFilePersist();
storageFilePersist.setName(tempFile.getName());
storageFilePersist.setExtension(tempFile.getExtension());
storageFilePersist.setMimeType(mimeType);
storageFilePersist.setOwnerId(this.userScope.getUserIdSafe());
storageFilePersist.setStorageType(StorageType.Transformer);
StorageFile storageFile = this.storageFileService.persistBytes(storageFilePersist, fileEnvelope.getFile(), new BaseFieldSet(StorageFile._id, StorageFile._fileRef, StorageFile._mimeType, StorageFile._extension, StorageFile._name));
fileEnvelope.setFileRef(storageFile.getFileRef());
}
return repository.preprocessingDmp(fileEnvelope);
}
}

View File

@ -28,6 +28,7 @@ import org.opencdmp.controllers.swagger.SwaggerHelpers;
import org.opencdmp.controllers.swagger.annotation.Swagger400;
import org.opencdmp.controllers.swagger.annotation.Swagger404;
import org.opencdmp.controllers.swagger.annotation.SwaggerErrorResponses;
import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel;
import org.opencdmp.model.DescriptionsToBeFinalized;
import org.opencdmp.model.DmpUser;
import org.opencdmp.model.DmpValidationResult;
@ -453,23 +454,40 @@ public class DmpController {
return model;
}
@PostMapping("json/import")
@Operation(summary = "Import a plan from an json file")
@PostMapping("json/preprocessing")
@Operation(summary = "preprocessing a plan from an json file")
@Transactional
public Dmp importJson(
@RequestParam("file") MultipartFile file,
@RequestParam("label") String label,
@RequestParam("repositoryId") String repositoryId,
@RequestParam("format") String format,
@Parameter(name = "fields", description = SwaggerHelpers.Commons.fieldset_description, required = true) FieldSet fields
) throws InvalidAlgorithmParameterException, JAXBException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, IOException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException {
logger.debug(new MapLogEntry("import json" + Dmp.class.getSimpleName()).And("transformerId", repositoryId).And("file", file).And("label", label));
public PreprocessingDmpModel preprocessing(
@RequestParam("fileId") UUID fileId,
@RequestParam("repositoryId") String repositoryId
) throws InvalidAlgorithmParameterException, JAXBException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, IOException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
logger.debug(new MapLogEntry("preprocessing dmp" + Dmp.class.getSimpleName()).And("transformerId", repositoryId).And("fileId", fileId));
Dmp model = this.dmpService.importJson(file, label, repositoryId, format, fields);
PreprocessingDmpModel model = this.dmpService.preprocessingDmp(fileId, repositoryId);
this.auditService.track(AuditableAction.Dmp_Import, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("transformerId", repositoryId),
new AbstractMap.SimpleEntry<String, Object>("file", file),
new AbstractMap.SimpleEntry<String, Object>("fileId", fileId)
));
return model;
}
@PostMapping("json/import")
@Operation(summary = "Import a plan from an json file")
@ValidationFilterAnnotation(validator = DmpCommonModelConfig.DmpCommonModelConfigValidator.ValidatorName, argumentName = "model")
@Transactional
public Dmp importJson(
@RequestBody DmpCommonModelConfig dmpCommonModelConfig,
@Parameter(name = "fields", description = SwaggerHelpers.Commons.fieldset_description, required = true) FieldSet fields
) throws InvalidAlgorithmParameterException, JAXBException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, IOException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException {
logger.debug(new MapLogEntry("import json" + Dmp.class.getSimpleName()).And("transformerId", dmpCommonModelConfig.getRepositoryId()).And("file id", dmpCommonModelConfig.getFileId()).And("label", dmpCommonModelConfig.getLabel()));
Dmp model = this.dmpService.importJson(dmpCommonModelConfig, fields);
this.auditService.track(AuditableAction.Dmp_Import, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("transformerId", dmpCommonModelConfig.getRepositoryId()),
new AbstractMap.SimpleEntry<String, Object>("file id", dmpCommonModelConfig.getFileId()),
new AbstractMap.SimpleEntry<String, Object>("fields", fields)
));