argos/annotation-service/annotation/src/main/java/gr/cite/annotation/model/builder/AnnotationBuilder.java

79 lines
3.6 KiB
Java

package gr.cite.annotation.model.builder;
import gr.cite.annotation.authorization.AuthorizationFlags;
import gr.cite.annotation.convention.ConventionService;
import gr.cite.annotation.data.AnnotationEntity;
import gr.cite.annotation.model.Annotation;
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.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AnnotationBuilder extends BaseBuilder<Annotation, AnnotationEntity> {
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
public AnnotationBuilder(ConventionService conventionService, LoggerService logger) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(AnnotationBuilder.class)));
}
public AnnotationBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<Annotation> build(FieldSet fields, List<AnnotationEntity> 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 || fields.isEmpty())
return new ArrayList<>();
List<Annotation> models = new ArrayList<>();
if (data == null)
return models;
for (AnnotationEntity d : data) {
Annotation m = new Annotation();
if (fields.hasField(this.asIndexer(Annotation._id)))
m.setId(d.getId());
if (fields.hasField(this.asIndexer(Annotation._entityId)))
m.setEntityId(d.getEntityId());
if (fields.hasField(this.asIndexer(Annotation._entityType)))
m.setEntityType(d.getEntityType());
if (fields.hasField(this.asIndexer(Annotation._anchor)))
m.setAnchor(d.getAnchor());
if (fields.hasField(this.asIndexer(Annotation._payload)))
m.setPayload(d.getPayload());
if (fields.hasField(this.asIndexer(Annotation._subjectId)))
m.setSubjectId(d.getSubjectId());
if (fields.hasField(this.asIndexer(Annotation._threadId)))
m.setThreadId(d.getThreadId());
if (fields.hasField(this.asIndexer(Annotation._parentId)))
m.setParentId(d.getParentId());
if (fields.hasField(this.asIndexer(Annotation._protectionType)))
m.setProtectionType(d.getProtectionType());
if (fields.hasField(this.asIndexer(Annotation._timeStamp)))
m.setTimeStamp(d.getTimeStamp());
if (fields.hasField(this.asIndexer(Annotation._createdAt)))
m.setCreatedAt(d.getCreatedAt());
if (fields.hasField(this.asIndexer(Annotation._updatedAt)))
m.setUpdatedAt(d.getUpdatedAt());
if (fields.hasField(this.asIndexer(Annotation._isActive)))
m.setIsActive(d.getIsActive());
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}