package eu.eudat.model.builder.tenantconfig; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.commons.types.tenant.TenantSourceEntity; import eu.eudat.convention.ConventionService; import eu.eudat.model.builder.BaseBuilder; import eu.eudat.model.tenantconfig.TenantSource; import eu.eudat.service.tenant.TenantService; 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 TenantSourceBuilder extends BaseBuilder { private final BuilderFactory builderFactory; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); @Autowired public TenantSourceBuilder( ConventionService conventionService, BuilderFactory builderFactory, TenantService tenantService) { super(conventionService, new LoggerService(LoggerFactory.getLogger(TenantSourceBuilder.class))); this.builderFactory = builderFactory; } public TenantSourceBuilder 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 (TenantSourceEntity d : data) { TenantSource m = new TenantSource(); if (fields.hasField(this.asIndexer(TenantSource._url))) m.setUrl(d.getUrl()); if (fields.hasField(this.asIndexer(TenantSource._codes))) m.setCodes(d.getCodes()); if (fields.hasField(this.asIndexer(TenantSource._issuerUrl))) m.setIssuerUrl(d.getIssuerUrl()); if (fields.hasField(this.asIndexer(TenantSource._clientId))) m.setClientId(d.getClientId()); if (fields.hasField(this.asIndexer(TenantSource._clientSecret))) m.setClientSecret(d.getClientSecret()); if (fields.hasField(this.asIndexer(TenantSource._scope))) m.setScope(d.getScope()); models.add(m); } this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); return models; } }