package eu.eudat.model.builder; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.convention.ConventionService; import eu.eudat.data.DmpReferenceEntity; import eu.eudat.data.SupportiveMaterialEntity; import eu.eudat.model.*; import eu.eudat.query.DmpQuery; import eu.eudat.query.ReferenceQuery; 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.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.*; import java.util.stream.Collectors; @Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class SupportiveMaterialBuilder extends BaseBuilder{ private final BuilderFactory builderFactory; private final QueryFactory queryFactory; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); @Autowired public SupportiveMaterialBuilder( ConventionService conventionService, BuilderFactory builderFactory, QueryFactory queryFactory) { super(conventionService, new LoggerService(LoggerFactory.getLogger(SupportiveMaterialBuilder.class))); this.builderFactory = builderFactory; this.queryFactory = queryFactory; } public SupportiveMaterialBuilder 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<>(); List models = new ArrayList<>(); for (SupportiveMaterialEntity d : data) { SupportiveMaterial m = new SupportiveMaterial(); if (fields.hasField(this.asIndexer(SupportiveMaterial._id))) m.setId(d.getId()); if (fields.hasField(this.asIndexer(SupportiveMaterial._type))) m.setType(d.getType()); if (fields.hasField(this.asIndexer(SupportiveMaterial._languageCode))) m.setLanguageCode(d.getLanguageCode()); if (fields.hasField(this.asIndexer(SupportiveMaterial._payload))) m.setPayload(d.getPayload()); if (fields.hasField(this.asIndexer(SupportiveMaterial._createdAt))) m.setCreatedAt(d.getCreatedAt()); if (fields.hasField(this.asIndexer(SupportiveMaterial._updatedAt))) m.setUpdatedAt(d.getUpdatedAt()); if (fields.hasField(this.asIndexer(SupportiveMaterial._isActive))) m.setIsActive(d.getIsActive()); if (fields.hasField(this.asIndexer(SupportiveMaterial._hash))) m.setHash(this.hashValue(d.getUpdatedAt())); models.add(m); } this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); return models; } }