common models changes

This commit is contained in:
Efstratios Giannopoulos 2024-03-12 11:07:47 +02:00
parent c592226e77
commit e3ac2394c2
11 changed files with 109 additions and 43 deletions

View File

@ -50,7 +50,7 @@
<dependency>
<groupId>gr.cite.opendmp</groupId>
<artifactId>common-models</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>gr.cite.opendmp</groupId>

View File

@ -64,6 +64,13 @@ public class DescriptionCommonModelBuilder extends BaseCommonModelBuilder<Descri
return this;
}
private boolean useSharedStorage;
public DescriptionCommonModelBuilder useSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<DescriptionModel, DescriptionEntity>> buildInternal(List<DescriptionEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -95,7 +102,7 @@ public class DescriptionCommonModelBuilder extends BaseCommonModelBuilder<Descri
if (d.getProperties() != null){
//TODO Update with the new logic of property definition
PropertyDefinitionEntity propertyDefinition = this.jsonHandlingService.fromJsonSafe(PropertyDefinitionEntity.class, d.getProperties());
m.setProperties(this.builderFactory.builder(PropertyDefinitionCommonModelBuilder.class).withDefinition(definitionEntityMap != null ? definitionEntityMap.getOrDefault(d.getDescriptionTemplateId(), null) : null).authorize(this.authorize).build(propertyDefinition));
m.setProperties(this.builderFactory.builder(PropertyDefinitionCommonModelBuilder.class).useSharedStorage(useSharedStorage).withDefinition(definitionEntityMap != null ? definitionEntityMap.getOrDefault(d.getDescriptionTemplateId(), null) : null).authorize(this.authorize).build(propertyDefinition));
}
models.add(new CommonModelBuilderItemResponse<>(m, d));
}
@ -143,7 +150,7 @@ public class DescriptionCommonModelBuilder extends BaseCommonModelBuilder<Descri
Map<UUID, DmpModel> itemMap;
DmpQuery q = this.queryFactory.query(DmpQuery.class).authorize(this.authorize).ids(data.stream().map(DescriptionEntity::getDmpId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(DmpCommonModelBuilder.class).setDisableDescriptions(true).authorize(this.authorize).asForeignKey(q, DmpEntity::getId);
itemMap = this.builderFactory.builder(DmpCommonModelBuilder.class).useSharedStorage(useSharedStorage).setDisableDescriptions(true).authorize(this.authorize).asForeignKey(q, DmpEntity::getId);
return itemMap;
}

View File

@ -1,27 +1,42 @@
package eu.eudat.model.builder.commonmodels.description;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commonmodels.models.FileEnvelopeModel;
import eu.eudat.commonmodels.models.description.FieldModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.commons.enums.FieldType;
import eu.eudat.commons.enums.StorageType;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.commons.types.description.FieldEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.ReferenceEntity;
import eu.eudat.data.StorageFileEntity;
import eu.eudat.model.Reference;
import eu.eudat.model.StorageFile;
import eu.eudat.model.builder.commonmodels.BaseCommonModelBuilder;
import eu.eudat.model.builder.commonmodels.CommonModelBuilderItemResponse;
import eu.eudat.model.builder.commonmodels.reference.ReferenceCommonModelBuilder;
import eu.eudat.model.persist.StorageFilePersist;
import eu.eudat.query.ReferenceQuery;
import eu.eudat.query.StorageFileQuery;
import eu.eudat.service.storage.StorageFileProperties;
import eu.eudat.service.storage.StorageFileService;
import gr.cite.tools.data.builder.BuilderFactory;
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 gr.cite.tools.validation.ValidatorFactory;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URLConnection;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
@ -32,24 +47,38 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
private final QueryFactory queryFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity;
private final StorageFileService storageFileService;
private final UserScope userScope;
private final ValidatorFactory validatorFactory;
private final StorageFileProperties storageFileProperties;
@Autowired
public FieldCommonModelBuilder(
ConventionService conventionService, BuilderFactory builderFactory, QueryFactory queryFactory
ConventionService conventionService, BuilderFactory builderFactory, QueryFactory queryFactory, StorageFileService storageFileService, UserScope userScope, ValidatorFactory validatorFactory, StorageFileProperties storageFileProperties
) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(FieldCommonModelBuilder.class)));
this.builderFactory = builderFactory;
this.queryFactory = queryFactory;
this.storageFileService = storageFileService;
this.userScope = userScope;
this.validatorFactory = validatorFactory;
this.storageFileProperties = storageFileProperties;
}
public FieldCommonModelBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
public FieldCommonModelBuilder withFieldEntity(eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity) {
this.fieldEntity = fieldEntity;
return this;
}
private boolean useSharedStorage;
public FieldCommonModelBuilder useSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<FieldModel, FieldEntity>> buildInternal(List<FieldEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -72,6 +101,24 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
if (referenceItemsMap.containsKey(referenceId)) m.getReferences().add(referenceItemsMap.get(referenceId));
}
}
if (FieldType.UPLOAD.equals(fieldType) && d.getTextValue() != null && !d.getTextValue().isEmpty()) {
try {
byte[] bytes = this.storageFileService.readAsBytesSafe(UUID.fromString(d.getTextValue()));
FileEnvelopeModel fileEnvelopeModel = new FileEnvelopeModel();
StorageFileEntity storageFile = this.queryFactory.query(StorageFileQuery.class).ids(UUID.fromString(d.getTextValue())).first();
fileEnvelopeModel.setFile(bytes);
fileEnvelopeModel.setFilename(storageFile.getName() + (storageFile.getExtension().startsWith(".") ? "" : ".") + storageFile.getExtension());
fileEnvelopeModel.setMimeType(storageFile.getMimeType());
if (!useSharedStorage){
fileEnvelopeModel.setFile(bytes);
} else {
fileEnvelopeModel.setFileRef(this.addFileToSharedStorage(bytes, storageFile));
}
}catch (Exception e){
logger.error(e.getMessage());
}
}
if (d.getExternalIdentifier() != null && FieldType.isExternalIdentifierType(fieldType)) m.setExternalIdentifier(this.builderFactory.builder(ExternalIdentifierCommonModelBuilder.class).authorize(this.authorize).build(d.getExternalIdentifier()));
models.add(new CommonModelBuilderItemResponse<>(m, d));
@ -82,6 +129,19 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
return models;
}
private String addFileToSharedStorage(byte[] bytes, StorageFileEntity storageFile) throws IOException {
StorageFilePersist storageFilePersist = new StorageFilePersist();
storageFilePersist.setName(FilenameUtils.removeExtension(storageFile.getName()));
storageFilePersist.setExtension(FilenameUtils.getExtension(storageFile.getExtension()));
storageFilePersist.setMimeType(URLConnection.guessContentTypeFromName(storageFile.getName() + (storageFile.getExtension().startsWith(".") ? "" : ".") + storageFile.getExtension()));
storageFilePersist.setOwnerId(this.userScope.getUserIdSafe());
storageFilePersist.setStorageType(StorageType.Temp);
storageFilePersist.setLifetime(Duration.ofSeconds(this.storageFileProperties.getTempStoreLifetimeSeconds())); //TODO
this.validatorFactory.validator(StorageFilePersist.StorageFilePersistValidator.class).validateForce(storageFilePersist);
StorageFile persisted = this.storageFileService.persistBytes(storageFilePersist, bytes, new BaseFieldSet(StorageFile._id, StorageFile._fileRef));
return persisted.getFileRef();
}
private Map<UUID, ReferenceModel> collectReferences(List<FieldEntity> data) throws MyApplicationException {
if (data.isEmpty())
return null;

View File

@ -42,6 +42,13 @@ public class PropertyDefinitionCommonModelBuilder extends BaseCommonModelBuilder
this.definition = definition;
return this;
}
private boolean useSharedStorage;
public PropertyDefinitionCommonModelBuilder useSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<PropertyDefinitionModel, PropertyDefinitionEntity>> buildInternal(List<PropertyDefinitionEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -53,7 +60,7 @@ public class PropertyDefinitionCommonModelBuilder extends BaseCommonModelBuilder
m.setFieldSets(new HashMap<>());
for (String key : d.getFieldSets().keySet()){
FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById(key).stream().findFirst().orElse(null) : null;
m.getFieldSets().put(key, this.builderFactory.builder(PropertyDefinitionFieldSetCommonModelBuilder.class).authorize(this.authorize).withFieldSetEntity(fieldSetEntity).build(d.getFieldSets().get(key)));
m.getFieldSets().put(key, this.builderFactory.builder(PropertyDefinitionFieldSetCommonModelBuilder.class).useSharedStorage(useSharedStorage).authorize(this.authorize).withFieldSetEntity(fieldSetEntity).build(d.getFieldSets().get(key)));
}
models.add(new CommonModelBuilderItemResponse<>(m, d));
}

View File

@ -42,6 +42,13 @@ public class PropertyDefinitionFieldSetCommonModelBuilder extends BaseCommonMode
this.fieldSetEntity = fieldSetEntity;
return this;
}
private boolean useSharedStorage;
public PropertyDefinitionFieldSetCommonModelBuilder useSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<PropertyDefinitionFieldSetModel, PropertyDefinitionFieldSetEntity>> buildInternal(List<PropertyDefinitionFieldSetEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -50,7 +57,7 @@ public class PropertyDefinitionFieldSetCommonModelBuilder extends BaseCommonMode
List<CommonModelBuilderItemResponse<PropertyDefinitionFieldSetModel, PropertyDefinitionFieldSetEntity>> models = new ArrayList<>();
for (PropertyDefinitionFieldSetEntity d : data) {
PropertyDefinitionFieldSetModel m = new PropertyDefinitionFieldSetModel();
if (d.getItems() != null) m.setItems(this.builderFactory.builder(PropertyDefinitionFieldSetItemModelCommonModelBuilder.class).withFieldSetEntity(this.fieldSetEntity).authorize(this.authorize).build(d.getItems()));
if (d.getItems() != null) m.setItems(this.builderFactory.builder(PropertyDefinitionFieldSetItemModelCommonModelBuilder.class).useSharedStorage(useSharedStorage).withFieldSetEntity(this.fieldSetEntity).authorize(this.authorize).build(d.getItems()));
models.add(new CommonModelBuilderItemResponse<>(m, d));
}

View File

@ -45,6 +45,13 @@ public class PropertyDefinitionFieldSetItemModelCommonModelBuilder extends BaseC
}
private boolean useSharedStorage;
public PropertyDefinitionFieldSetItemModelCommonModelBuilder useSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<PropertyDefinitionFieldSetItemModel, PropertyDefinitionFieldSetItemEntity>> buildInternal(List<PropertyDefinitionFieldSetItemEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -60,7 +67,7 @@ public class PropertyDefinitionFieldSetItemModelCommonModelBuilder extends BaseC
m.setFields(new HashMap<>());
for (String key : d.getFields().keySet()){
FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getAllField().stream().findFirst().orElse(null) : null;
m.getFields().put(key, this.builderFactory.builder(FieldCommonModelBuilder.class).authorize(this.authorize).withFieldEntity(fieldEntity).build(d.getFields().get(key)));
m.getFields().put(key, this.builderFactory.builder(FieldCommonModelBuilder.class).useSharedStorage(useSharedStorage).authorize(this.authorize).withFieldEntity(fieldEntity).build(d.getFields().get(key)));
}
}
models.add(new CommonModelBuilderItemResponse<>(m, d));

View File

@ -11,7 +11,6 @@ import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.commonmodels.models.dmpblueprint.DmpBlueprintModel;
import eu.eudat.commonmodels.models.dmpreference.DmpReferenceModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
@ -22,7 +21,6 @@ import eu.eudat.model.builder.commonmodels.*;
import eu.eudat.model.builder.commonmodels.description.DescriptionCommonModelBuilder;
import eu.eudat.model.builder.commonmodels.dmpblueprint.DmpBlueprintCommonModelBuilder;
import eu.eudat.model.builder.commonmodels.dmpreference.DmpReferenceCommonModelBuilder;
import eu.eudat.model.builder.commonmodels.reference.ReferenceCommonModelBuilder;
import eu.eudat.query.*;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.Ordering;
@ -88,6 +86,13 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
return this;
}
private boolean useSharedStorage;
public DmpCommonModelBuilder useSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<DmpModel, DmpEntity>> buildInternal(List<DmpEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -190,7 +195,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
Map<UUID, List<DescriptionModel>> itemMap;
DescriptionQuery query = this.queryFactory.query(DescriptionQuery.class).isActive(IsActive.Active).authorize(this.authorize).dmpIds(data.stream().map(DmpEntity::getId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(DescriptionCommonModelBuilder.class).authorize(this.authorize).asMasterKey(query, DescriptionEntity::getDmpId);
itemMap = this.builderFactory.builder(DescriptionCommonModelBuilder.class).useSharedStorage(useSharedStorage).authorize(this.authorize).asMasterKey(query, DescriptionEntity::getDmpId);
return itemMap;
}

View File

@ -170,7 +170,9 @@ public class DepositServiceImpl implements DepositService {
FileEnvelopeModel jsonEnvelope = new FileEnvelopeModel();
pdfEnvelope.setFilename(pdfFile.getFilename());
jsonEnvelope.setMimeType("application/pdf");
jsonEnvelope.setFilename(rda.getFilename());
jsonEnvelope.setMimeType("application/json");
if (!depositClient.getConfiguration().isUseSharedStorage()){
pdfEnvelope.setFile(pdfFile.getFile());
jsonEnvelope.setFile(rda.getFile());
@ -180,28 +182,9 @@ public class DepositServiceImpl implements DepositService {
}
//GK: Fifth Transform them to the DepositModel
DmpModel depositModel = this.builderFactory.builder(DmpCommonModelBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic)
DmpModel depositModel = this.builderFactory.builder(DmpCommonModelBuilder.class).useSharedStorage(depositClient.getConfiguration().isUseSharedStorage()).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic)
.setRepositoryId(dmpDepositModel.getRepositoryId()).setPdfFile(pdfEnvelope).setRdaJsonFile(jsonEnvelope).build(dmpEntity);
//TODO
// if (depositModel != null && !this.conventionService.isListNullOrEmpty(depositModel.getDescriptions())){
// for (DescriptionModel descriptionModel :depositModel.getDescriptions()){
// DescriptionTemplateEntity descriptionTemplate = this.queryFactory.query(DescriptionTemplateQuery.class).ids(descriptionModel.getDescriptionTemplate().getId()).firstAs(new BaseFieldSet().ensure(DescriptionTemplate._definition));
// if (descriptionTemplate == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionModel.getDescriptionTemplate().getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
//
// if (descriptionModel.getProperties() != null && descriptionModel.getProperties().getFieldSets() != null){
// for(PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel : descriptionModel.getProperties().getFieldSets().values().stream().filter(x-> x.getItems() != null).map(PropertyDefinitionFieldSetModel::getItems).flatMap(Collection::stream).toList()){
// if (propertyDefinitionFieldSetItemModel.getFields() != null){
// for(Map.Entry<String, FieldModel> entrySet : propertyDefinitionFieldSetItemModel.getFields().entrySet()){
//
// }
// }
// }
// }
//
// }
//
// }
//GK: Sixth Perform the deposit
String doi = depositClient.deposit(depositModel, dmpDepositModel.getAccessToken());

View File

@ -197,17 +197,8 @@ public class StorageFileProperties {
}
public static class StaticFilesConfig{
private String externalUrls;
private String semantics;
public String getExternalUrls() {
return externalUrls;
}
public void setExternalUrls(String externalUrls) {
this.externalUrls = externalUrls;
}
public String getSemantics() {
return semantics;
}

View File

@ -135,7 +135,7 @@ public class FileTransformerServiceImpl implements FileTransformerService {
if (repository == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{repositoryId, TransformerRepository.class.getSimpleName()}, LocaleContextHolder.getLocale()));
//GK: Second get the Target Data Management Plan
DmpQuery query = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(dmpId);
DmpModel dmpFileTransformerModel = this.builderFactory.builder(DmpCommonModelBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(query.first());
DmpModel dmpFileTransformerModel = this.builderFactory.builder(DmpCommonModelBuilder.class).useSharedStorage(repository.getConfiguration().isUseSharedStorage()).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(query.first());
if (dmpFileTransformerModel == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{dmpId, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
FileEnvelopeModel fileEnvelope = repository.exportDmp(dmpFileTransformerModel, format);
@ -156,7 +156,7 @@ public class FileTransformerServiceImpl implements FileTransformerService {
//GK: Second get the Target Data Management Plan
DescriptionQuery query = this.queryFactory.query(DescriptionQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(descriptionId);
DescriptionModel descriptionFileTransformerModel = this.builderFactory.builder(DescriptionCommonModelBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(query.first());
DescriptionModel descriptionFileTransformerModel = this.builderFactory.builder(DescriptionCommonModelBuilder.class).useSharedStorage(repository.getConfiguration().isUseSharedStorage()).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(query.first());
if (descriptionFileTransformerModel == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionId, Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
FileEnvelopeModel fileEnvelope = repository.exportDescription(descriptionFileTransformerModel, format);

View File

@ -11,7 +11,6 @@ storage:
- type: Deposit
basePath: ${FILE_STORAGE}/deposit
static-files:
externalUrls: dmp-backend/web/src/main/resources/externalUrls/ExternalUrls.xml
semantics: dmp-backend/web/src/main/resources/Semantics.json
material-files:
userGuide: dmp-backend/web/src/main/resources/material/user-guide/