179 lines
8.6 KiB
Java
179 lines
8.6 KiB
Java
package eu.eudat.model.builder;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.convention.ConventionService;
|
|
import eu.eudat.data.DmpEntity;
|
|
import eu.eudat.model.*;
|
|
import eu.eudat.query.DescriptionQuery;
|
|
import eu.eudat.query.DmpReferenceQuery;
|
|
import eu.eudat.query.DmpUserQuery;
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.exception.MyApplicationException;
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
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.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
|
public class DmpBuilder extends BaseBuilder<Dmp, DmpEntity> {
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
|
|
|
@Autowired
|
|
public DmpBuilder(ConventionService conventionService,
|
|
QueryFactory queryFactory,
|
|
BuilderFactory builderFactory) {
|
|
super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpBuilder.class)));
|
|
this.queryFactory = queryFactory;
|
|
this.builderFactory = builderFactory;
|
|
}
|
|
|
|
public DmpBuilder authorize(EnumSet<AuthorizationFlags> values) {
|
|
this.authorize = values;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public List<Dmp> build(FieldSet fields, List<DmpEntity> 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<Dmp> models = new ArrayList<>();
|
|
|
|
FieldSet dmpDescriptionsFields = fields.extractPrefixed(this.asPrefix(Dmp._dmpDescriptions));
|
|
Map<UUID, List<Description>> dmpDescriptionsMap = this.collectDmpDescriptions(dmpDescriptionsFields, data);
|
|
|
|
FieldSet dmpReferencesFields = fields.extractPrefixed(this.asPrefix(Dmp._dmpReferences));
|
|
Map<UUID, List<DmpReference>> dmpReferencesMap = this.collectDmpReferences(dmpReferencesFields, data);
|
|
|
|
FieldSet dmpUsersFields = fields.extractPrefixed(this.asPrefix(Dmp._dmpUsers));
|
|
Map<UUID, List<DmpUser>> dmpUsersMap = this.collectDmpUsers(dmpUsersFields, data);
|
|
|
|
for (DmpEntity d : data) {
|
|
Dmp m = new Dmp();
|
|
if (fields.hasField(this.asIndexer(Dmp._id)))
|
|
m.setId(d.getId());
|
|
if (fields.hasField(this.asIndexer(Dmp._label)))
|
|
m.setLabel(d.getLabel());
|
|
if (fields.hasField(this.asIndexer(Dmp._version)))
|
|
m.setVersion(d.getVersion());
|
|
if (fields.hasField(this.asIndexer(Dmp._status)))
|
|
m.setStatus(d.getStatus());
|
|
if (fields.hasField(this.asIndexer(Dmp._properties)))
|
|
m.setProperties(d.getProperties());
|
|
if (fields.hasField(this.asIndexer(Dmp._groupId)))
|
|
m.setGroupId(d.getGroupId());
|
|
if (fields.hasField(this.asIndexer(Dmp._description)))
|
|
m.setDescription(d.getDescription());
|
|
if (fields.hasField(this.asIndexer(Dmp._createdAt)))
|
|
m.setCreatedAt(d.getCreatedAt());
|
|
if (fields.hasField(this.asIndexer(Dmp._updatedAt)))
|
|
m.setUpdatedAt(d.getUpdatedAt());
|
|
if (fields.hasField(this.asIndexer(Dmp._isActive)))
|
|
m.setIsActive(d.getIsActive());
|
|
if (fields.hasField(this.asIndexer(Dmp._finalizedAt)))
|
|
m.setFinalizedAt(d.getFinalizedAt());
|
|
if (fields.hasField(this.asIndexer(Dmp._accessType)))
|
|
m.setAccessType(d.getAccessType());
|
|
if (fields.hasField(this.asIndexer(Dmp._blueprint)))
|
|
m.setBlueprint(d.getBlueprint());
|
|
if (fields.hasField(this.asIndexer(Dmp._language)))
|
|
m.setLanguage(d.getLanguage());
|
|
if (fields.hasField(this.asIndexer(Dmp._publicAfter)))
|
|
m.setPublicAfter(d.getPublicAfter());
|
|
if (fields.hasField(this.asIndexer(Dmp._hash)))
|
|
m.setHash(this.hashValue(d.getUpdatedAt()));
|
|
|
|
if (dmpDescriptionsMap != null && !dmpDescriptionsMap.isEmpty() && dmpDescriptionsMap.containsKey(d.getId()))
|
|
m.setDmpDescriptions(dmpDescriptionsMap.get(d.getId()));
|
|
|
|
if (dmpReferencesMap != null && !dmpReferencesMap.isEmpty() && dmpReferencesMap.containsKey(d.getId()))
|
|
m.setDmpReferences(dmpReferencesMap.get(d.getId()));
|
|
|
|
if (dmpUsersMap != null && !dmpUsersMap.isEmpty() && dmpUsersMap.containsKey(d.getId()))
|
|
m.setDmpUsers(dmpUsersMap.get(d.getId()));
|
|
|
|
models.add(m);
|
|
}
|
|
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
|
|
|
return models;
|
|
}
|
|
|
|
private Map<UUID, List<Description>> collectDmpDescriptions(FieldSet fields, List<DmpEntity> data) throws MyApplicationException {
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", Description.class.getSimpleName());
|
|
|
|
Map<UUID, List<Description>> itemMap;
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(Description._dmp, Dmp._id));
|
|
DescriptionQuery query = this.queryFactory.query(DescriptionQuery.class).authorize(this.authorize).dmpIds(data.stream().map(DmpEntity::getId).distinct().collect(Collectors.toList()));
|
|
itemMap = this.builderFactory.builder(DescriptionBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getDmp().getId());
|
|
|
|
if (!fields.hasField(this.asIndexer(Description._dmp, Dmp._id))) {
|
|
itemMap.forEach((id, dmps) -> {
|
|
dmps.forEach(description -> {
|
|
if (description != null && description.getDmp() != null)
|
|
description.getDmp().setId(null);
|
|
});
|
|
});
|
|
}
|
|
return itemMap;
|
|
}
|
|
|
|
private Map<UUID, List<DmpReference>> collectDmpReferences(FieldSet fields, List<DmpEntity> data) throws MyApplicationException {
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", DmpReference.class.getSimpleName());
|
|
|
|
Map<UUID, List<DmpReference>> itemMap;
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(DmpReference._dmp, Dmp._id));
|
|
DmpReferenceQuery query = this.queryFactory.query(DmpReferenceQuery.class).authorize(this.authorize).dmpIds(data.stream().map(DmpEntity::getId).distinct().collect(Collectors.toList()));
|
|
itemMap = this.builderFactory.builder(DmpReferenceBuilder.class).authorize(this.authorize).authorize(this.authorize).asMasterKey(query, clone, x -> x.getDmp().getId());
|
|
|
|
if (!fields.hasField(this.asIndexer(DmpReference._dmp, Dmp._id))) {
|
|
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getDmp() != null).peek(x -> {
|
|
x.getDmp().setId(null);
|
|
});
|
|
}
|
|
|
|
return itemMap;
|
|
}
|
|
|
|
private Map<UUID, List<DmpUser>> collectDmpUsers(FieldSet fields, List<DmpEntity> data) throws MyApplicationException {
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", DmpUser.class.getSimpleName());
|
|
|
|
Map<UUID, List<DmpUser>> itemMap;
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(DmpUser._dmp, Dmp._id));
|
|
DmpUserQuery query = this.queryFactory.query(DmpUserQuery.class).authorize(this.authorize).dmpIds(data.stream().map(DmpEntity::getId).distinct().collect(Collectors.toList()));
|
|
itemMap = this.builderFactory.builder(DmpUserBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getDmp().getId());
|
|
|
|
if (!fields.hasField(this.asIndexer(DmpUser._dmp, Dmp._id))) {
|
|
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getDmp() != null).peek(x -> {
|
|
x.getDmp().setId(null);
|
|
});
|
|
}
|
|
|
|
return itemMap;
|
|
}
|
|
|
|
}
|