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

76 lines
3.7 KiB
Java

package eu.eudat.model.builder.descriptionpropertiesdefinition;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.types.description.PropertyDefinitionEntity;
import eu.eudat.commons.types.descriptiontemplate.DefinitionEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.model.builder.BaseBuilder;
import eu.eudat.model.descriptiontemplatedefinition.Definition;
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionPersist;
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 eu.eudat.model.descriptionproperties.PropertyDefinition;
import java.util.*;
@Component("descriptionpropertiesdefinitionbuilder")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PropertyDefinitionBuilder extends BaseBuilder<PropertyDefinition, PropertyDefinitionEntity> {
private final BuilderFactory builderFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private DefinitionEntity definition;
@Autowired
public PropertyDefinitionBuilder(
ConventionService conventionService, BuilderFactory builderFactory) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(PropertyDefinitionBuilder.class)));
this.builderFactory = builderFactory;
}
public PropertyDefinitionBuilder withDefinition(DefinitionEntity definition) {
this.definition = definition;
return this;
}
public PropertyDefinitionBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<PropertyDefinition> build(FieldSet fields, List<PropertyDefinitionEntity> 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<>();
//Not Bulk Build because is XML no interaction with db
FieldSet fieldsFields = fields.extractPrefixed(this.asPrefix(PropertyDefinition._fieldSets));
List<PropertyDefinition> models = new ArrayList<>();
for (PropertyDefinitionEntity d : data) {
PropertyDefinition m = new PropertyDefinition();
if (!fieldsFields.isEmpty() && d.getFieldSets() != null && !d.getFieldSets().isEmpty()) {
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(PropertyDefinitionFieldSetBuilder.class).authorize(this.authorize).withFieldSetEntity(fieldSetEntity).build(fieldsFields, d.getFieldSets().get(key)));
}
}
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}