Adding builder and query for 'description' entity

This commit is contained in:
Thomas Georgios Giannos 2023-10-24 13:50:53 +03:00
parent e3536285aa
commit 485f7311cd
4 changed files with 379 additions and 0 deletions

View File

@ -68,6 +68,10 @@ public class Description {
public static final String _finalizedAt = "finalizedAt"; public static final String _finalizedAt = "finalizedAt";
private String hash;
public static final String _hash = "hash";
public UUID getId() { public UUID getId() {
return id; return id;
} }
@ -188,4 +192,11 @@ public class Description {
this.finalizedAt = finalizedAt; this.finalizedAt = finalizedAt;
} }
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
} }

View File

@ -0,0 +1,84 @@
package eu.eudat.model.builder;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.model.Description;
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 DescriptionBuilder extends BaseBuilder<Description, DescriptionEntity> {
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public DescriptionBuilder(
ConventionService conventionService) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionBuilder.class)));
}
public DescriptionBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<Description> build(FieldSet fields, List<DescriptionEntity> 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<Description> models = new ArrayList<>();
for (DescriptionEntity d : data) {
Description m = new Description();
if (fields.hasField(this.asIndexer(Description._id)))
m.setId(d.getId());
if (fields.hasField(this.asIndexer(Description._label)))
m.setLabel(d.getLabel());
if (fields.hasField(this.asIndexer(Description._dmp)))
m.setDmp(d.getDmp());
if (fields.hasField(this.asIndexer(Description._uri)))
m.setUri(d.getUri());
if (fields.hasField(this.asIndexer(Description._properties)))
m.setProperties(d.getProperties());
if (fields.hasField(this.asIndexer(Description._profile)))
m.setProfile(d.getProfile());
if (fields.hasField(this.asIndexer(Description._reference)))
m.setReference(d.getReference());
if (fields.hasField(this.asIndexer(Description._status)))
m.setStatus(d.getStatus());
if (fields.hasField(this.asIndexer(Description._description)))
m.setDescription(d.getDescription());
if (fields.hasField(this.asIndexer(Description._creator)))
m.setCreator(d.getCreator());
if (fields.hasField(this.asIndexer(Description._dmpSectionIndex)))
m.setDmpSectionIndex(d.getDmpSectionIndex());
if (fields.hasField(this.asIndexer(Description._createdAt)))
m.setCreatedAt(d.getCreatedAt());
if (fields.hasField(this.asIndexer(Description._updatedAt)))
m.setUpdatedAt(d.getUpdatedAt());
if (fields.hasField(this.asIndexer(Description._isActive)))
m.setIsActive(d.getIsActive());
if (fields.hasField(this.asIndexer(Description._finalizedAt)))
m.setFinalizedAt(d.getFinalizedAt());
if (fields.hasField(this.asIndexer(Description._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;
}
}

View File

@ -0,0 +1,206 @@
package eu.eudat.query;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.model.Description;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.query.FieldResolver;
import gr.cite.tools.data.query.QueryBase;
import gr.cite.tools.data.query.QueryContext;
import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.*;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionQuery extends QueryBase<DescriptionEntity> {
private String like;
private Collection<UUID> ids;
private Collection<UUID> excludedIds;
private Collection<IsActive> isActives;
private Collection<DescriptionStatus> statuses;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private final UserScope userScope;
private final AuthorizationService authService;
public DescriptionQuery(UserScope userScope, AuthorizationService authService) {
this.userScope = userScope;
this.authService = authService;
}
public DescriptionQuery like(String value) {
this.like = value;
return this;
}
public DescriptionQuery ids(UUID value) {
this.ids = List.of(value);
return this;
}
public DescriptionQuery ids(UUID... value) {
this.ids = Arrays.asList(value);
return this;
}
public DescriptionQuery ids(Collection<UUID> values) {
this.ids = values;
return this;
}
public DescriptionQuery excludedIds(Collection<UUID> values) {
this.excludedIds = values;
return this;
}
public DescriptionQuery excludedIds(UUID value) {
this.excludedIds = List.of(value);
return this;
}
public DescriptionQuery excludedIds(UUID... value) {
this.excludedIds = Arrays.asList(value);
return this;
}
public DescriptionQuery isActive(IsActive value) {
this.isActives = List.of(value);
return this;
}
public DescriptionQuery isActive(IsActive... value) {
this.isActives = Arrays.asList(value);
return this;
}
public DescriptionQuery isActive(Collection<IsActive> values) {
this.isActives = values;
return this;
}
public DescriptionQuery statuses(DescriptionStatus value) {
this.statuses = List.of(value);
return this;
}
public DescriptionQuery statuses(DescriptionStatus... value) {
this.statuses = Arrays.asList(value);
return this;
}
public DescriptionQuery statuses(Collection<DescriptionStatus> values) {
this.statuses = values;
return this;
}
public DescriptionQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
protected Boolean isFalseQuery() {
return this.isEmpty(this.ids) || this.isEmpty(this.isActives) || this.isEmpty(this.excludedIds) || this.isEmpty(this.statuses);
}
@Override
protected Class<DescriptionEntity> entityClass() {
return DescriptionEntity.class;
}
@Override
protected <X, Y> Predicate applyFilters(QueryContext<X, Y> queryContext) {
List<Predicate> predicates = new ArrayList<>();
if (this.like != null && !this.like.isEmpty()) {
predicates.add(queryContext.CriteriaBuilder.like(queryContext.Root.get(DescriptionEntity._label), this.like));
}
if (this.ids != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionEntity._id));
for (UUID item : this.ids)
inClause.value(item);
predicates.add(inClause);
}if (this.excludedIds != null) {
CriteriaBuilder.In<UUID> notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionEntity._id));
for (UUID item : this.excludedIds)
notInClause.value(item);
predicates.add(notInClause.not());
}
if (this.isActives != null) {
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionEntity._isActive));
for (IsActive item : this.isActives)
inClause.value(item);
predicates.add(inClause);
}
if (this.statuses != null) {
CriteriaBuilder.In<DescriptionStatus> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionEntity._status));
for (DescriptionStatus item : this.statuses)
inClause.value(item);
predicates.add(inClause);
}
if (!predicates.isEmpty()) {
Predicate[] predicatesArray = predicates.toArray(new Predicate[0]);
return queryContext.CriteriaBuilder.and(predicatesArray);
} else {
return null;
}
}
@Override
protected String fieldNameOf(FieldResolver item) {
if (item.match(Description._id)) return DescriptionEntity._id;
else if (item.match(Description._label)) return DescriptionEntity._label;
else if (item.match(Description._dmp)) return DescriptionEntity._dmp;
else if (item.match(Description._uri)) return DescriptionEntity._uri;
else if (item.match(Description._properties)) return DescriptionEntity._properties;
else if (item.match(Description._profile)) return DescriptionEntity._profile;
else if (item.match(Description._reference)) return DescriptionEntity._reference;
else if (item.match(Description._status)) return DescriptionEntity._status;
else if (item.match(Description._description)) return DescriptionEntity._description;
else if (item.match(Description._dmpSectionIndex)) return DescriptionEntity._dmpSectionIndex;
else if (item.match(Description._creator)) return DescriptionEntity._creator;
else if (item.match(Description._createdAt)) return DescriptionEntity._createdAt;
else if (item.match(Description._updatedAt)) return DescriptionEntity._updatedAt;
else if (item.match(Description._isActive)) return DescriptionEntity._isActive;
else if (item.match(Description._finalizedAt)) return DescriptionEntity._finalizedAt;
else return null;
}
@Override
protected DescriptionEntity convert(Tuple tuple, Set<String> columns) {
DescriptionEntity item = new DescriptionEntity();
item.setId(QueryBase.convertSafe(tuple, columns, DescriptionEntity._id, UUID.class));
item.setLabel(QueryBase.convertSafe(tuple, columns, DescriptionEntity._label, String.class));
item.setDmp(QueryBase.convertSafe(tuple, columns, DescriptionEntity._dmp, UUID.class));
item.setUri(QueryBase.convertSafe(tuple, columns, DescriptionEntity._uri, String.class));
item.setProperties(QueryBase.convertSafe(tuple, columns, DescriptionEntity._properties, String.class));
item.setProfile(QueryBase.convertSafe(tuple, columns, DescriptionEntity._profile, UUID.class));
item.setReference(QueryBase.convertSafe(tuple, columns, DescriptionEntity._reference, String.class));
item.setStatus(QueryBase.convertSafe(tuple, columns, DescriptionEntity._status, DescriptionStatus.class));
item.setDescription(QueryBase.convertSafe(tuple, columns, DescriptionEntity._description, String.class));
item.setDmpSectionIndex(QueryBase.convertSafe(tuple, columns, DescriptionEntity._dmpSectionIndex, Integer.class));
item.setCreator(QueryBase.convertSafe(tuple, columns, DescriptionEntity._creator, UUID.class));
item.setCreatedAt(QueryBase.convertSafe(tuple, columns, DescriptionEntity._createdAt, Instant.class));
item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, DescriptionEntity._updatedAt, Instant.class));
item.setIsActive(QueryBase.convertSafe(tuple, columns, DescriptionEntity._isActive, IsActive.class));
item.setFinalizedAt(QueryBase.convertSafe(tuple, columns, DescriptionEntity._finalizedAt, Instant.class));
return item;
}
}

View File

@ -0,0 +1,78 @@
package eu.eudat.query.lookup;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.query.DescriptionQuery;
import eu.eudat.query.DmpQuery;
import gr.cite.tools.data.query.Lookup;
import gr.cite.tools.data.query.QueryFactory;
import java.util.List;
import java.util.UUID;
public class DescriptionLookup extends Lookup {
private String like;
private List<UUID> ids;
private List<UUID> excludedIds;
private List<IsActive> isActive;
private List<DescriptionStatus> statuses;
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public List<UUID> getIds() {
return ids;
}
public void setIds(List<UUID> ids) {
this.ids = ids;
}
public List<UUID> getExcludedIds() {
return excludedIds;
}
public void setExcludedIds(List<UUID> excludedIds) {
this.excludedIds = excludedIds;
}
public List<IsActive> getIsActive() {
return isActive;
}
public void setIsActive(List<IsActive> isActive) {
this.isActive = isActive;
}
public List<DescriptionStatus> getStatuses() {
return statuses;
}
public void setStatuses(List<DescriptionStatus> statuses) {
this.statuses = statuses;
}
public DescriptionQuery enrich(QueryFactory queryFactory) {
DescriptionQuery query = queryFactory.query(DescriptionQuery.class);
if (this.like != null) query.like(this.like);
if (this.ids != null) query.ids(this.ids);
if (this.excludedIds != null) query.excludedIds(this.excludedIds);
if (this.isActive != null) query.isActive(this.isActive);
if (this.statuses != null) query.statuses(this.statuses);
this.enrichCommon(query);
return query;
}
}