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

75 lines
3.5 KiB
Java

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.Dmp;
import eu.eudat.model.DmpReference;
import eu.eudat.model.Reference;
import eu.eudat.model.SupportiveMaterial;
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<SupportiveMaterial, SupportiveMaterialEntity>{
private final BuilderFactory builderFactory;
private final QueryFactory queryFactory;
private EnumSet<AuthorizationFlags> 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<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<SupportiveMaterial> build(FieldSet fields, List<SupportiveMaterialEntity> 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<SupportiveMaterial> 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());
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}