argos/dmp-backend/core/src/main/java/eu/eudat/query/SupportiveMaterialQuery.java

219 lines
8.6 KiB
Java

package eu.eudat.query;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.enums.SupportiveMaterialFieldType;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.data.SupportiveMaterialEntity;
import eu.eudat.model.SupportiveMaterial;
import eu.eudat.query.utils.QueryUtilsService;
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 SupportiveMaterialQuery extends QueryBase<SupportiveMaterialEntity> {
private String like;
private Collection<UUID> ids;
private Collection<IsActive> isActives;
private Collection<SupportiveMaterialFieldType> types;
private Collection<String> languageCodes;
private Collection<UUID> excludedIds;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
public SupportiveMaterialQuery like(String value) {
this.like = value;
return this;
}
public SupportiveMaterialQuery ids(UUID value) {
this.ids = List.of(value);
return this;
}
public SupportiveMaterialQuery ids(UUID... value) {
this.ids = Arrays.asList(value);
return this;
}
public SupportiveMaterialQuery ids(Collection<UUID> values) {
this.ids = values;
return this;
}
public SupportiveMaterialQuery isActive(IsActive value) {
this.isActives = List.of(value);
return this;
}
public SupportiveMaterialQuery isActive(IsActive... value) {
this.isActives = Arrays.asList(value);
return this;
}
public SupportiveMaterialQuery isActive(Collection<IsActive> values) {
this.isActives = values;
return this;
}
public SupportiveMaterialQuery types(SupportiveMaterialFieldType value) {
this.types = List.of(value);
return this;
}
public SupportiveMaterialQuery types(SupportiveMaterialFieldType... value) {
this.types = Arrays.asList(value);
return this;
}
public SupportiveMaterialQuery types(Collection<SupportiveMaterialFieldType> values) {
this.types = values;
return this;
}
public SupportiveMaterialQuery languageCodes(String value) {
this.languageCodes = List.of(value);
return this;
}
public SupportiveMaterialQuery languageCodes(String... value) {
this.languageCodes = Arrays.asList(value);
return this;
}
public SupportiveMaterialQuery languageCodes(Collection<String> values) {
this.languageCodes = values;
return this;
}
public SupportiveMaterialQuery excludedIds(Collection<UUID> values) {
this.excludedIds = values;
return this;
}
public SupportiveMaterialQuery excludedIds(UUID value) {
this.excludedIds = List.of(value);
return this;
}
public SupportiveMaterialQuery excludedIds(UUID... value) {
this.excludedIds = Arrays.asList(value);
return this;
}
public SupportiveMaterialQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
private final QueryUtilsService queryUtilsService;
public SupportiveMaterialQuery(
QueryUtilsService queryUtilsService) {
this.queryUtilsService = queryUtilsService;
}
@Override
protected Class<SupportiveMaterialEntity> entityClass() {
return SupportiveMaterialEntity.class;
}
@Override
protected Boolean isFalseQuery() {
return this.isEmpty(this.ids) || this.isEmpty(this.isActives) || this.isEmpty(this.excludedIds) || this.isEmpty(this.types) || this.isEmpty(this.languageCodes);
}
@Override
protected <X, Y> Predicate applyFilters(QueryContext<X, Y> queryContext) {
List<Predicate> predicates = new ArrayList<>();
if (this.ids != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(SupportiveMaterialEntity._id));
for (UUID item : this.ids)
inClause.value(item);
predicates.add(inClause);
}
if (this.like != null && !this.like.isEmpty()) {
predicates.add(queryContext.CriteriaBuilder.or(queryUtilsService.ilike(queryContext.CriteriaBuilder, queryContext.Root.get(SupportiveMaterialEntity._languageCode), this.like),
queryUtilsService.ilike(queryContext.CriteriaBuilder, queryContext.Root.get(SupportiveMaterialEntity._payload), this.like)
));
}
if (this.isActives != null) {
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(SupportiveMaterialEntity._isActive));
for (IsActive item : this.isActives)
inClause.value(item);
predicates.add(inClause);
}
if (this.types != null) {
CriteriaBuilder.In<SupportiveMaterialFieldType> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(SupportiveMaterialEntity._type));
for (SupportiveMaterialFieldType item : this.types)
inClause.value(item);
predicates.add(inClause);
}
if (this.languageCodes != null) {
CriteriaBuilder.In<String> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(SupportiveMaterialEntity._languageCode));
for (String item : this.languageCodes)
inClause.value(item);
predicates.add(inClause);
}
if (this.excludedIds != null) {
CriteriaBuilder.In<UUID> notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(SupportiveMaterialEntity._id));
for (UUID item : this.excludedIds)
notInClause.value(item);
predicates.add(notInClause.not());
}
if (!predicates.isEmpty()) {
Predicate[] predicatesArray = predicates.toArray(new Predicate[0]);
return queryContext.CriteriaBuilder.and(predicatesArray);
} else {
return null;
}
}
@Override
protected SupportiveMaterialEntity convert(Tuple tuple, Set<String> columns) {
SupportiveMaterialEntity item = new SupportiveMaterialEntity();
item.setId(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._id, UUID.class));
item.setTenantId(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._tenantId, UUID.class));
item.setType(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._type, SupportiveMaterialFieldType.class));
item.setLanguageCode(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._languageCode, String.class));
item.setPayload(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._payload, String.class));
item.setCreatedAt(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._createdAt, Instant.class));
item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._updatedAt, Instant.class));
item.setIsActive(QueryBase.convertSafe(tuple, columns, SupportiveMaterialEntity._isActive, IsActive.class));
return item;
}
@Override
protected String fieldNameOf(FieldResolver item) {
if (item.match(SupportiveMaterial._id)) return SupportiveMaterialEntity._id;
else if (item.match(SupportiveMaterial._type)) return SupportiveMaterialEntity._type;
else if (item.match(SupportiveMaterial._languageCode)) return SupportiveMaterialEntity._languageCode;
else if (item.match(SupportiveMaterial._payload)) return SupportiveMaterialEntity._payload;
else if (item.match(SupportiveMaterial._createdAt)) return SupportiveMaterialEntity._createdAt;
else if (item.match(SupportiveMaterial._updatedAt)) return SupportiveMaterialEntity._updatedAt;
else if (item.match(SupportiveMaterial._hash)) return SupportiveMaterialEntity._updatedAt;
else if (item.match(SupportiveMaterial._isActive)) return SupportiveMaterialEntity._isActive;
else if (item.match(SupportiveMaterial._belongsToCurrentTenant)) return SupportiveMaterialEntity._tenantId;
else return null;
}
}