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

65 lines
3.4 KiB
Java

package eu.eudat.model.builder.referencetypedefinition;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.types.referencetype.AuthenticationConfigurationEntity;
import eu.eudat.commons.types.referencetype.QueryConfigEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.model.builder.BaseBuilder;
import eu.eudat.model.referencetypedefinition.AuthenticationConfiguration;
import eu.eudat.model.referencetypedefinition.QueryConfig;
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 AuthenticationConfigurationBuilder extends BaseBuilder<AuthenticationConfiguration, AuthenticationConfigurationEntity> {
private final BuilderFactory builderFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public AuthenticationConfigurationBuilder(
ConventionService conventionService, BuilderFactory builderFactory) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(AuthenticationConfigurationBuilder.class)));
this.builderFactory = builderFactory;
}
public AuthenticationConfigurationBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<AuthenticationConfiguration> build(FieldSet fields, List<AuthenticationConfigurationEntity> 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<AuthenticationConfiguration> models = new ArrayList<>();
for (AuthenticationConfigurationEntity d : data) {
AuthenticationConfiguration m = new AuthenticationConfiguration();
if (fields.hasField(this.asIndexer(AuthenticationConfiguration._enabled))) m.setEnabled(d.getEnabled());
if (fields.hasField(this.asIndexer(AuthenticationConfiguration._authUrl))) m.setAuthUrl(d.getAuthUrl());
if (fields.hasField(this.asIndexer(AuthenticationConfiguration._authMethod))) m.setAuthMethod(d.getAuthMethod());
if (fields.hasField(this.asIndexer(AuthenticationConfiguration._authTokenPath))) m.setAuthTokenPath(d.getAuthTokenPath());
if (fields.hasField(this.asIndexer(AuthenticationConfiguration._authRequestBody))) m.setAuthRequestBody(d.getAuthRequestBody());
if (fields.hasField(this.asIndexer(AuthenticationConfiguration._type))) m.setType(d.getType());
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}