package eu.eudat.model.builder; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.commons.XmlHandlingService; import eu.eudat.commons.scope.tenant.TenantScope; import eu.eudat.commons.types.dmpblueprint.DefinitionEntity; import eu.eudat.convention.ConventionService; import eu.eudat.data.DmpBlueprintEntity; import eu.eudat.model.DmpBlueprint; import eu.eudat.model.builder.dmpblueprintdefinition.DefinitionBuilder; import gr.cite.tools.data.builder.BuilderFactory; 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 { private final BuilderFactory builderFactory; private final XmlHandlingService xmlHandlingService; private final TenantScope tenantScope; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); @Autowired public DmpBlueprintBuilder( ConventionService conventionService, BuilderFactory builderFactory, XmlHandlingService xmlHandlingService, TenantScope tenantScope) { super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpBlueprintBuilder.class))); this.builderFactory = builderFactory; this.xmlHandlingService = xmlHandlingService; this.tenantScope = tenantScope; } public DmpBlueprintBuilder authorize(EnumSet values) { this.authorize = values; return this; } @Override public List build(FieldSet fields, List 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<>(); FieldSet definitionFields = fields.extractPrefixed(this.asPrefix(DmpBlueprint._definition)); List 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())); if (fields.hasField(this.asIndexer(DmpBlueprint._belongsToCurrentTenant))) m.setBelongsToCurrentTenant(this.getBelongsToCurrentTenant(d, this.tenantScope)); if (!definitionFields.isEmpty() && d.getDefinition() != null) { DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, d.getDefinition()); m.setDefinition(this.builderFactory.builder(DefinitionBuilder.class).authorize(this.authorize).build(definitionFields, definition)); } models.add(m); } this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); return models; } }