argos/dmp-backend/core/src/main/java/eu/eudat/model/builder/DmpBlueprintBuilder.java

92 lines
4.5 KiB
Java
Raw Normal View History

2023-10-20 18:13:57 +02:00
package eu.eudat.model.builder;
import eu.eudat.authorization.AuthorizationFlags;
2023-10-24 17:00:11 +02:00
import eu.eudat.commons.XmlHandlingService;
2024-04-03 12:22:22 +02:00
import eu.eudat.commons.scope.tenant.TenantScope;
2023-10-20 18:13:57 +02:00
import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DmpBlueprintEntity;
import eu.eudat.model.DmpBlueprint;
2023-10-23 17:13:34 +02:00
import eu.eudat.model.builder.dmpblueprintdefinition.DefinitionBuilder;
import gr.cite.tools.data.builder.BuilderFactory;
2023-10-20 18:13:57 +02:00
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
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.util.*;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DmpBlueprintBuilder extends BaseBuilder<DmpBlueprint, DmpBlueprintEntity> {
2023-10-23 17:13:34 +02:00
private final BuilderFactory builderFactory;
2023-10-24 17:00:11 +02:00
private final XmlHandlingService xmlHandlingService;
2024-04-03 12:22:22 +02:00
private final TenantScope tenantScope;
2023-10-20 18:13:57 +02:00
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public DmpBlueprintBuilder(
2024-04-03 12:22:22 +02:00
ConventionService conventionService,
BuilderFactory builderFactory, XmlHandlingService xmlHandlingService, TenantScope tenantScope) {
2023-10-20 18:13:57 +02:00
super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpBlueprintBuilder.class)));
2023-10-23 17:13:34 +02:00
this.builderFactory = builderFactory;
2023-10-24 17:00:11 +02:00
this.xmlHandlingService = xmlHandlingService;
2024-04-03 12:22:22 +02:00
this.tenantScope = tenantScope;
2023-10-20 18:13:57 +02:00
}
public DmpBlueprintBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<DmpBlueprint> build(FieldSet fields, List<DmpBlueprintEntity> data) throws MyApplicationException {
this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0));
this.logger.trace(new DataLogEntry("requested fields", fields));
if (fields == null || data == null || fields.isEmpty())
return new ArrayList<>();
2023-10-20 18:13:57 +02:00
FieldSet definitionFields = fields.extractPrefixed(this.asPrefix(DmpBlueprint._definition));
List<DmpBlueprint> models = new ArrayList<>();
for (DmpBlueprintEntity d : data) {
DmpBlueprint m = new DmpBlueprint();
if (fields.hasField(this.asIndexer(DmpBlueprint._id)))
m.setId(d.getId());
if (fields.hasField(this.asIndexer(DmpBlueprint._label)))
m.setLabel(d.getLabel());
if (fields.hasField(this.asIndexer(DmpBlueprint._status)))
m.setStatus(d.getStatus());
if (fields.hasField(this.asIndexer(DmpBlueprint._groupId)))
m.setGroupId(d.getGroupId());
if (fields.hasField(this.asIndexer(DmpBlueprint._version)))
m.setVersion(d.getVersion());
if (fields.hasField(this.asIndexer(DmpBlueprint._versionStatus)))
m.setVersionStatus(d.getVersionStatus());
if (fields.hasField(this.asIndexer(DmpBlueprint._createdAt)))
m.setCreatedAt(d.getCreatedAt());
if (fields.hasField(this.asIndexer(DmpBlueprint._updatedAt)))
m.setUpdatedAt(d.getUpdatedAt());
if (fields.hasField(this.asIndexer(DmpBlueprint._isActive)))
m.setIsActive(d.getIsActive());
if (fields.hasField(this.asIndexer(DmpBlueprint._hash)))
m.setHash(this.hashValue(d.getUpdatedAt()));
2024-04-03 12:22:22 +02:00
if (fields.hasField(this.asIndexer(DmpBlueprint._belongsToCurrentTenant))) m.setBelongsToCurrentTenant(this.getBelongsToCurrentTenant(d, this.tenantScope));
if (!definitionFields.isEmpty() && d.getDefinition() != null) {
2023-10-27 17:46:34 +02:00
DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, d.getDefinition());
2023-10-23 17:13:34 +02:00
m.setDefinition(this.builderFactory.builder(DefinitionBuilder.class).authorize(this.authorize).build(definitionFields, definition));
}
2023-10-20 18:13:57 +02:00
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}