package eu.eudat.model.builder; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.commons.XmlHandlingService; import eu.eudat.convention.ConventionService; import eu.eudat.data.TenantEntity; import eu.eudat.model.Tenant; 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 TenantBuilder extends BaseBuilder { private final BuilderFactory builderFactory; private final XmlHandlingService xmlHandlingService; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); @Autowired public TenantBuilder(ConventionService conventionService, BuilderFactory builderFactory, XmlHandlingService xmlHandlingService) { super(conventionService, new LoggerService(LoggerFactory.getLogger(TenantBuilder.class))); this.builderFactory = builderFactory; this.xmlHandlingService = xmlHandlingService; } public TenantBuilder 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(TenantEntity d : data){ Tenant m = new Tenant(); if(fields.hasField(this.asIndexer(Tenant._id))) m.setId(d.getId()); if(fields.hasField(this.asIndexer(Tenant._code))) m.setCode(d.getCode()); if(fields.hasField(this.asIndexer(Tenant._name))) m.setName(d.getName()); if(fields.hasField(this.asIndexer(Tenant._description))) m.setDescription(d.getDescription()); if(fields.hasField(this.asIndexer(Tenant._isActive))) m.setIsActive(d.getIsActive()); if(fields.hasField(this.asIndexer(Tenant._createdAt))) m.setCreatedAt(d.getCreatedAt()); if(fields.hasField(this.asIndexer(Tenant._updatedAt))) m.setUpdatedAt(d.getUpdatedAt()); if(fields.hasField(this.asIndexer(Tenant._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; } }