handle common models with date, number blueprint values

This commit is contained in:
amentis 2024-05-24 13:58:30 +03:00
parent 161826401b
commit b9c4648d8f
5 changed files with 76 additions and 15 deletions

View File

@ -51,12 +51,12 @@
<dependency>
<groupId>org.opencdmp</groupId>
<artifactId>repositorydepositbase</artifactId>
<version>2.0.13</version>
<version>2.0.14</version>
</dependency>
<dependency>
<groupId>org.opencdmp</groupId>
<artifactId>common-models</artifactId>
<version>0.0.16</version>
<version>0.0.17</version>
</dependency>
<dependency>
<groupId>gr.cite</groupId>
@ -66,7 +66,7 @@
<dependency>
<groupId>org.opencdmp</groupId>
<artifactId>file-transformer-base</artifactId>
<version>0.0.20</version>
<version>0.0.21</version>
</dependency>
<dependency>
<groupId>gr.cite</groupId>

View File

@ -2,7 +2,12 @@ package org.opencdmp.model.builder.commonmodels.dmp;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.commonmodels.models.dmp.DmpBlueprintValueModel;
import org.opencdmp.commons.enums.DmpBlueprintExtraFieldDataType;
import org.opencdmp.commons.enums.DmpBlueprintFieldCategory;
import org.opencdmp.commons.types.dmp.DmpBlueprintValueEntity;
import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity;
import org.opencdmp.commons.types.dmpblueprint.ExtraFieldEntity;
import org.opencdmp.commons.types.dmpblueprint.FieldEntity;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.model.builder.commonmodels.BaseCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.CommonModelBuilderItemResponse;
@ -23,6 +28,8 @@ import java.util.Optional;
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DmpBlueprintValueCommonModelBuilder extends BaseCommonModelBuilder<DmpBlueprintValueModel, DmpBlueprintValueEntity> {
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private DefinitionEntity definition;
@Autowired
public DmpBlueprintValueCommonModelBuilder(
ConventionService conventionService
@ -35,6 +42,11 @@ public class DmpBlueprintValueCommonModelBuilder extends BaseCommonModelBuilder<
return this;
}
public DmpBlueprintValueCommonModelBuilder withDefinition(DefinitionEntity definition) {
this.definition = definition;
return this;
}
@Override
protected List<CommonModelBuilderItemResponse<DmpBlueprintValueModel, DmpBlueprintValueEntity>> buildInternal(List<DmpBlueprintValueEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
@ -42,11 +54,22 @@ public class DmpBlueprintValueCommonModelBuilder extends BaseCommonModelBuilder<
List<CommonModelBuilderItemResponse<DmpBlueprintValueModel, DmpBlueprintValueEntity>> models = new ArrayList<>();
for (DmpBlueprintValueEntity d : data) {
FieldEntity fieldEntity = this.definition != null ? this.definition.getFieldById(d.getFieldId()).stream().findFirst().orElse(null) : null;
if (fieldEntity != null && fieldEntity.getCategory().equals(DmpBlueprintFieldCategory.Extra)) {
ExtraFieldEntity extraFieldEntity = (ExtraFieldEntity) fieldEntity;
DmpBlueprintValueModel m = new DmpBlueprintValueModel();
m.setValue(d.getValue());
m.setFieldId(d.getFieldId());
if (extraFieldEntity != null && DmpBlueprintExtraFieldDataType.isDateType(extraFieldEntity.getType())){
m.setDateValue(d.getDateValue());
} else if (extraFieldEntity != null && DmpBlueprintExtraFieldDataType.isNumberType(extraFieldEntity.getType())){
m.setNumberValue(d.getNumberValue());
} else {
m.setValue(d.getValue());
}
models.add(new CommonModelBuilderItemResponse<>(m, d));
}
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));

View File

@ -18,8 +18,10 @@ import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.commonmodels.models.dmpblueprint.DmpBlueprintModel;
import org.opencdmp.commonmodels.models.dmpreference.DmpReferenceModel;
import org.opencdmp.commons.JsonHandlingService;
import org.opencdmp.commons.XmlHandlingService;
import org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.types.dmp.DmpPropertiesEntity;
import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.*;
import org.opencdmp.model.DmpUser;
@ -30,6 +32,7 @@ import org.opencdmp.model.builder.commonmodels.dmpblueprint.DmpBlueprintCommonMo
import org.opencdmp.model.builder.commonmodels.dmpreference.DmpReferenceCommonModelBuilder;
import org.opencdmp.model.description.Description;
import org.opencdmp.model.dmp.Dmp;
import org.opencdmp.model.dmpblueprint.DmpBlueprint;
import org.opencdmp.model.dmpreference.DmpReference;
import org.opencdmp.query.*;
import org.slf4j.LoggerFactory;
@ -49,6 +52,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
private final BuilderFactory builderFactory;
private final JsonHandlingService jsonHandlingService;
private final XmlHandlingService xmlHandlingService;
private FileEnvelopeModel pdfFile;
private FileEnvelopeModel rdaJsonFile;
private String repositoryId;
@ -58,11 +62,12 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
@Autowired
public DmpCommonModelBuilder(ConventionService conventionService,
QueryFactory queryFactory,
BuilderFactory builderFactory, JsonHandlingService jsonHandlingService) {
BuilderFactory builderFactory, JsonHandlingService jsonHandlingService, XmlHandlingService xmlHandlingService) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpCommonModelBuilder.class)));
this.queryFactory = queryFactory;
this.builderFactory = builderFactory;
this.jsonHandlingService = jsonHandlingService;
this.xmlHandlingService = xmlHandlingService;
}
public DmpCommonModelBuilder authorize(EnumSet<AuthorizationFlags> values) {
@ -110,6 +115,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
Map<UUID, List<EntityDoiModel>> entityDois = this.collectDmpEntityDois(data);
Map<UUID, UserModel> creators = this.collectCreators(data);
Map<UUID, DmpBlueprintModel> dmpBlueprints = this.collectDmpBlueprints(data);
Map<UUID, DefinitionEntity> definitionEntityMap = this.collectDmpBlueprintDefinitions(data);
for (DmpEntity d : data) {
DmpModel m = new DmpModel();
@ -131,7 +137,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
if (d.getProperties() != null){
//TODO Update with the new logic of property definition
DmpPropertiesEntity propertyDefinition = this.jsonHandlingService.fromJsonSafe(DmpPropertiesEntity.class, d.getProperties());
m.setProperties(this.builderFactory.builder(DmpPropertiesCommonModelBuilder.class).authorize(this.authorize).build(propertyDefinition));
m.setProperties(this.builderFactory.builder(DmpPropertiesCommonModelBuilder.class).withDefinition(definitionEntityMap != null ? definitionEntityMap.getOrDefault(d.getBlueprintId(), null) : null).authorize(this.authorize).build(propertyDefinition));
}
m.setPublicAfter(d.getPublicAfter());
m.setUpdatedAt(d.getUpdatedAt());
@ -237,4 +243,20 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder<DmpModel, DmpE
return itemMap;
}
private Map<UUID, DefinitionEntity> collectDmpBlueprintDefinitions(List<DmpEntity> data) throws MyApplicationException {
if (data.isEmpty())
return null;
this.logger.debug("checking related - {}", DefinitionEntity.class.getSimpleName());
Map<java.util.UUID, DefinitionEntity> itemMap = new HashMap<>();
DmpBlueprintQuery q = this.queryFactory.query(DmpBlueprintQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().map(DmpEntity::getBlueprintId).distinct().collect(Collectors.toList()));
List<DmpBlueprintEntity> items = q.collectAs(new BaseFieldSet().ensure(DmpBlueprint._id).ensure(DmpBlueprint._definition));
for (DmpBlueprintEntity item : items){
DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, item.getDefinition());
itemMap.put(item.getId(), definition);
}
return itemMap;
}
}

View File

@ -3,6 +3,7 @@ package org.opencdmp.model.builder.commonmodels.dmp;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.commonmodels.models.dmp.DmpPropertiesModel;
import org.opencdmp.commons.types.dmp.DmpPropertiesEntity;
import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.model.builder.commonmodels.BaseCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.CommonModelBuilderItemResponse;
@ -10,6 +11,7 @@ import org.opencdmp.model.builder.commonmodels.descriptiontemplate.PageCommonMod
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService;
import org.opencdmp.model.builder.dmp.DmpPropertiesBuilder;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -26,6 +28,7 @@ import java.util.Optional;
public class DmpPropertiesCommonModelBuilder extends BaseCommonModelBuilder<DmpPropertiesModel, DmpPropertiesEntity> {
private final BuilderFactory builderFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private DefinitionEntity definition;
@Autowired
public DmpPropertiesCommonModelBuilder(
ConventionService conventionService, BuilderFactory builderFactory
@ -34,6 +37,11 @@ public class DmpPropertiesCommonModelBuilder extends BaseCommonModelBuilder<DmpP
this.builderFactory = builderFactory;
}
public DmpPropertiesCommonModelBuilder withDefinition(DefinitionEntity definition) {
this.definition = definition;
return this;
}
public DmpPropertiesCommonModelBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
@ -47,7 +55,7 @@ public class DmpPropertiesCommonModelBuilder extends BaseCommonModelBuilder<DmpP
List<CommonModelBuilderItemResponse<DmpPropertiesModel, DmpPropertiesEntity>> models = new ArrayList<>();
for (DmpPropertiesEntity d : data) {
DmpPropertiesModel m = new DmpPropertiesModel();
if (d.getDmpBlueprintValues() != null) m.setDmpBlueprintValues(this.builderFactory.builder(DmpBlueprintValueCommonModelBuilder.class).authorize(this.authorize).build(d.getDmpBlueprintValues()));
if (d.getDmpBlueprintValues() != null) m.setDmpBlueprintValues(this.builderFactory.builder(DmpBlueprintValueCommonModelBuilder.class).withDefinition(definition).authorize(this.authorize).build(d.getDmpBlueprintValues()));
if (d.getContacts() != null) m.setContacts(this.builderFactory.builder(DmpContactCommonModelBuilder.class).authorize(this.authorize).build(d.getContacts()));
models.add(new CommonModelBuilderItemResponse<>(m, d));

View File

@ -27,6 +27,7 @@ import org.opencdmp.commonmodels.models.description.DescriptionModel;
import org.opencdmp.commonmodels.models.dmp.DmpBlueprintValueModel;
import org.opencdmp.commonmodels.models.dmp.DmpContactModel;
import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.commonmodels.models.dmpblueprint.ExtraFieldModel;
import org.opencdmp.commonmodels.models.dmpblueprint.FieldModel;
import org.opencdmp.commonmodels.models.dmpblueprint.ReferenceTypeFieldModel;
import org.opencdmp.commonmodels.models.dmpblueprint.SectionModel;
@ -1992,9 +1993,10 @@ public class DmpServiceImpl implements DmpService {
}
} else {
// custom fields
if (model.getProperties() != null && this.conventionService.isListNullOrEmpty(model.getProperties().getDmpBlueprintValues())){
if (field.getCategory().equals(DmpBlueprintFieldCategory.Extra) && model.getProperties() != null && this.conventionService.isListNullOrEmpty(model.getProperties().getDmpBlueprintValues())){
DmpBlueprintValueModel dmpBlueprintValueModel = model.getProperties().getDmpBlueprintValues().stream().filter(x -> x.getFieldId().equals(field.getId())).findFirst().orElse(null);
if (dmpBlueprintValueModel != null) dmpBlueprintValues.put(dmpBlueprintValueModel.getFieldId(), this.commonDmpBlueprintValueToPersist(dmpBlueprintValueModel));
ExtraFieldModel extraFieldModel = (ExtraFieldModel) field;
if (dmpBlueprintValueModel != null && extraFieldModel != null) dmpBlueprintValues.put(dmpBlueprintValueModel.getFieldId(), this.commonDmpBlueprintValueToPersist(dmpBlueprintValueModel, extraFieldModel));
}
}
}
@ -2051,14 +2053,20 @@ public class DmpServiceImpl implements DmpService {
return persist;
}
private DmpBlueprintValuePersist commonDmpBlueprintValueToPersist(DmpBlueprintValueModel model) {
if (model == null)
private DmpBlueprintValuePersist commonDmpBlueprintValueToPersist(DmpBlueprintValueModel model, ExtraFieldModel extraFieldModel) {
if (model == null || extraFieldModel == null)
return null;
DmpBlueprintValuePersist persist = new DmpBlueprintValuePersist();
persist.setFieldId(model.getFieldId());
if (extraFieldModel.getDataType().equals(org.opencdmp.commonmodels.enums.DmpBlueprintExtraFieldDataType.Date)){
persist.setDateValue(model.getDateValue());
} else if (extraFieldModel.getDataType().equals(org.opencdmp.commonmodels.enums.DmpBlueprintExtraFieldDataType.Number)){
persist.setNumberValue(model.getNumberValue());
} else {
persist.setFieldValue(model.getValue());
}
return persist;
}