144 lines
6.3 KiB
Java
144 lines
6.3 KiB
Java
package eu.eudat.model.builder;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.convention.ConventionService;
|
|
import eu.eudat.data.DmpUserEntity;
|
|
import eu.eudat.data.old.UserInfo;
|
|
import eu.eudat.model.Dmp;
|
|
import eu.eudat.model.DmpUser;
|
|
import eu.eudat.model.Reference;
|
|
import eu.eudat.query.DmpQuery;
|
|
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 DmpUserBuilder extends BaseBuilder<DmpUser, DmpUserEntity>{
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
|
|
|
@Autowired
|
|
public DmpUserBuilder(
|
|
ConventionService conventionService,
|
|
BuilderFactory builderFactory, QueryFactory queryFactory) {
|
|
super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpUserBuilder.class)));
|
|
this.builderFactory = builderFactory;
|
|
this.queryFactory = queryFactory;
|
|
}
|
|
|
|
public DmpUserBuilder authorize(EnumSet<AuthorizationFlags> values) {
|
|
this.authorize = values;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public List<DmpUser> build(FieldSet fields, List<DmpUserEntity> 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 userFields = fields.extractPrefixed(this.asPrefix(DmpUser._user));
|
|
Map<UUID, UserInfo> userItemsMap = this.collectUsers(userFields, data);
|
|
|
|
FieldSet dmpFields = fields.extractPrefixed(this.asPrefix(DmpUser._dmp));
|
|
Map<UUID, Dmp> dmpItemsMap = this.collectDmps(dmpFields, data);
|
|
|
|
List<DmpUser> models = new ArrayList<>();
|
|
for (DmpUserEntity d : data) {
|
|
DmpUser m = new DmpUser();
|
|
if (fields.hasField(this.asIndexer(DmpUser._id)))
|
|
m.setId(d.getId());
|
|
if (fields.hasField(this.asIndexer(DmpUser._role)))
|
|
m.setRole(d.getRole());
|
|
if (fields.hasField(this.asIndexer(DmpUser._createdAt)))
|
|
m.setCreatedAt(d.getCreatedAt());
|
|
if (fields.hasField(this.asIndexer(DmpUser._updatedAt)))
|
|
m.setUpdatedAt(d.getUpdatedAt());
|
|
if (!userFields.isEmpty() && userItemsMap != null && userItemsMap.containsKey(d.getUser())) {
|
|
m.setUser(userItemsMap.get(d.getUser()));
|
|
}
|
|
if (!dmpFields.isEmpty() && dmpItemsMap != null && dmpItemsMap.containsKey(d.getDmp())) {
|
|
m.setDmp(dmpItemsMap.get(d.getDmp()));
|
|
}
|
|
models.add(m);
|
|
}
|
|
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
|
return models;
|
|
}
|
|
|
|
//TODO: Hookup user info when refactored
|
|
private Map<UUID, UserInfo> collectUsers(FieldSet fields, List<DmpUserEntity> data) throws MyApplicationException {
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", UserInfo.class.getSimpleName());
|
|
|
|
Map<UUID, UserInfo> itemMap;
|
|
if (!fields.hasOtherField(this.asIndexer("id"))) {
|
|
itemMap = this.asEmpty(
|
|
data.stream().map(DmpUserEntity::getUser).distinct().collect(Collectors.toList()),
|
|
x -> {
|
|
UserInfo item = new UserInfo();
|
|
item.setId(x);
|
|
return item;
|
|
},
|
|
UserInfo::getId);
|
|
} else {
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure("id");
|
|
// ReferenceQuery q = this.queryFactory.query(ReferenceQuery.class).authorize(this.authorize).ids(data.stream().map(DmpReferenceEntity::getReferenceId).distinct().collect(Collectors.toList()));
|
|
// itemMap = this.builderFactory.builder(ReferenceBuilder.class).authorize(this.authorize).asForeignKey(q, clone, Reference::getId);
|
|
}
|
|
if (!fields.hasField(Reference._id)) {
|
|
// itemMap.values().stream().filter(Objects::nonNull).peek(x -> x.setId(null)).collect(Collectors.toList());
|
|
}
|
|
|
|
// return itemMap;
|
|
return new HashMap<>();
|
|
}
|
|
|
|
private Map<UUID, Dmp> collectDmps(FieldSet fields, List<DmpUserEntity> data) throws MyApplicationException {
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", Dmp.class.getSimpleName());
|
|
|
|
Map<UUID, Dmp> itemMap;
|
|
if (!fields.hasOtherField(this.asIndexer(Dmp._id))) {
|
|
itemMap = this.asEmpty(
|
|
data.stream().map(DmpUserEntity::getDmp).distinct().collect(Collectors.toList()),
|
|
x -> {
|
|
Dmp item = new Dmp();
|
|
item.setId(x);
|
|
return item;
|
|
},
|
|
Dmp::getId);
|
|
} else {
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(Dmp._id);
|
|
DmpQuery q = this.queryFactory.query(DmpQuery.class).authorize(this.authorize).ids(data.stream().map(DmpUserEntity::getDmp).distinct().collect(Collectors.toList()));
|
|
itemMap = this.builderFactory.builder(DmpBuilder.class).authorize(this.authorize).asForeignKey(q, clone, Dmp::getId);
|
|
}
|
|
if (!fields.hasField(Dmp._id)) {
|
|
itemMap.values().stream().filter(Objects::nonNull).peek(x -> x.setId(null)).collect(Collectors.toList());
|
|
}
|
|
|
|
return itemMap;
|
|
}
|
|
|
|
}
|