55 lines
2.2 KiB
Java
55 lines
2.2 KiB
Java
|
package eu.eudat.model.builder.descriptiontemplatedefinition;
|
||
|
|
||
|
import eu.eudat.authorization.AuthorizationFlags;
|
||
|
import eu.eudat.commons.types.descriptiontemplate.RuleEntity;
|
||
|
import eu.eudat.convention.ConventionService;
|
||
|
import eu.eudat.model.builder.BaseBuilder;
|
||
|
import eu.eudat.model.descriptiontemplatedefinition.Rule;
|
||
|
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 RuleBuilder extends BaseBuilder<Rule, RuleEntity> {
|
||
|
|
||
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
||
|
|
||
|
@Autowired
|
||
|
public RuleBuilder(
|
||
|
ConventionService conventionService) {
|
||
|
super(conventionService, new LoggerService(LoggerFactory.getLogger(RuleBuilder.class)));
|
||
|
}
|
||
|
|
||
|
public RuleBuilder authorize(EnumSet<AuthorizationFlags> values) {
|
||
|
this.authorize = values;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public List<Rule> build(FieldSet fields, List<RuleEntity> 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<Rule> models = new ArrayList<>();
|
||
|
for (RuleEntity d : data) {
|
||
|
Rule m = new Rule();
|
||
|
if (fields.hasField(this.asIndexer(Rule._target))) m.setTarget(d.getTarget());
|
||
|
if (fields.hasField(this.asIndexer(Rule._value))) m.setValue(d.getValue());
|
||
|
models.add(m);
|
||
|
}
|
||
|
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
||
|
return models;
|
||
|
}
|
||
|
}
|