70 lines
3.6 KiB
Java
70 lines
3.6 KiB
Java
package eu.eudat.model.builder;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.commons.XmlHandlingService;
|
|
import eu.eudat.commons.types.tenant.TenantConfigEntity;
|
|
import eu.eudat.convention.ConventionService;
|
|
import eu.eudat.data.TenantEntity;
|
|
import eu.eudat.model.Tenant;
|
|
import eu.eudat.model.builder.tenantconfig.TenantConfigBuilder;
|
|
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<Tenant, TenantEntity> {
|
|
|
|
private final BuilderFactory builderFactory;
|
|
private final XmlHandlingService xmlHandlingService;
|
|
private EnumSet<AuthorizationFlags> 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<AuthorizationFlags> values){
|
|
this.authorize = values;
|
|
return this;
|
|
}
|
|
@Override
|
|
public List<Tenant> build(FieldSet fields, List<TenantEntity> 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<>();
|
|
|
|
FieldSet configFields = fields.extractPrefixed(this.asPrefix(Tenant._config));
|
|
|
|
List<Tenant> 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 (!configFields.isEmpty() && d.getConfig() != null){
|
|
TenantConfigEntity config = this.xmlHandlingService.fromXmlSafe(TenantConfigEntity.class, d.getConfig());
|
|
m.setConfig(this.builderFactory.builder(TenantConfigBuilder.class).authorize(this.authorize).build(configFields, config));
|
|
}
|
|
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;
|
|
}
|
|
}
|