package eu.eudat.query; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.commons.enums.LockTargetType; import eu.eudat.data.LockEntity; import eu.eudat.model.Lock; 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 LockQuery extends QueryBase { private String like; private Collection ids; private Collection targetIds; private Collection lockedByIds; private Collection targetTypes; private Collection excludedIds; private Collection userIds; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); public LockQuery like(String value) { this.like = value; return this; } public LockQuery ids(UUID value) { this.ids = List.of(value); return this; } public LockQuery ids(UUID... value) { this.ids = Arrays.asList(value); return this; } public LockQuery ids(Collection values) { this.ids = values; return this; } public LockQuery lockedByIds(UUID value) { this.lockedByIds = List.of(value); return this; } public LockQuery lockedByIds(UUID... value) { this.lockedByIds = Arrays.asList(value); return this; } public LockQuery lockedByIds(Collection values) { this.lockedByIds = values; return this; } public LockQuery targetIds(UUID value) { this.targetIds = List.of(value); return this; } public LockQuery targetIds(UUID... value) { this.targetIds = Arrays.asList(value); return this; } public LockQuery targetIds(Collection values) { this.targetIds = values; return this; } public LockQuery targetTypes(LockTargetType value) { this.targetTypes = List.of(value); return this; } public LockQuery targetTypes(LockTargetType... value) { this.targetTypes = Arrays.asList(value); return this; } public LockQuery targetTypes(Collection values) { this.targetTypes = values; return this; } public LockQuery excludedIds(Collection values) { this.excludedIds = values; return this; } public LockQuery excludedIds(UUID value) { this.excludedIds = List.of(value); return this; } public LockQuery excludedIds(UUID... value) { this.excludedIds = Arrays.asList(value); return this; } public LockQuery userIds(UUID value) { this.userIds = List.of(value); return this; } public LockQuery userIds(UUID... value) { this.userIds = Arrays.asList(value); return this; } public LockQuery userIds(Collection values) { this.userIds = values; return this; } public LockQuery authorize(EnumSet values) { this.authorize = values; return this; } public LockQuery() { } @Override protected Class entityClass() { return LockEntity.class; } @Override protected Boolean isFalseQuery() { return this.isEmpty(this.ids) || this.isEmpty(this.lockedByIds) || this.isEmpty(this.targetIds) || this.isEmpty(this.excludedIds) || this.isEmpty(this.targetTypes); } @Override protected Predicate applyFilters(QueryContext queryContext) { List predicates = new ArrayList<>(); if (this.like != null && !this.like.isEmpty()) { predicates.add(queryContext.CriteriaBuilder.like(queryContext.Root.get(LockEntity._id), this.like)); } if (this.ids != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(LockEntity._id)); for (UUID item : this.ids) inClause.value(item); predicates.add(inClause); } if (this.lockedByIds != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(LockEntity._lockedBy)); for (UUID item : this.lockedByIds) inClause.value(item); predicates.add(inClause); } if (this.targetIds != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(LockEntity._target)); for (UUID item : this.targetIds) inClause.value(item); predicates.add(inClause); } if (this.targetTypes != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(LockEntity._targetType)); for (LockTargetType item : this.targetTypes) inClause.value(item); predicates.add(inClause); } if (this.excludedIds != null) { CriteriaBuilder.In notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(LockEntity._id)); for (UUID item : this.excludedIds) notInClause.value(item); predicates.add(notInClause.not()); } if (this.userIds != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(LockEntity._lockedBy)); for (UUID item : this.userIds) 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 LockEntity convert(Tuple tuple, Set columns) { LockEntity item = new LockEntity(); item.setId(QueryBase.convertSafe(tuple, columns, LockEntity._id, UUID.class)); item.setTarget(QueryBase.convertSafe(tuple, columns, LockEntity._target, UUID.class)); item.setTargetType(QueryBase.convertSafe(tuple, columns, LockEntity._targetType, LockTargetType.class)); item.setLockedBy(QueryBase.convertSafe(tuple, columns, LockEntity._lockedBy, UUID.class)); item.setLockedAt(QueryBase.convertSafe(tuple, columns, LockEntity._lockedAt, Instant.class)); item.setTouchedAt(QueryBase.convertSafe(tuple, columns, LockEntity._touchedAt, Instant.class)); item.setTenantId(QueryBase.convertSafe(tuple, columns, LockEntity._tenantId, UUID.class)); return item; } @Override protected String fieldNameOf(FieldResolver item) { if (item.match(Lock._id)) return LockEntity._id; else if (item.match(Lock._target)) return LockEntity._target; else if (item.match(Lock._targetType)) return LockEntity._targetType; else if (item.prefix(Lock._lockedBy)) return LockEntity._lockedBy; else if (item.match(Lock._lockedAt)) return LockEntity._lockedAt; else if (item.match(Lock._touchedAt)) return LockEntity._touchedAt; else if (item.prefix(Lock._tenant)) return LockEntity._tenantId; else if (item.match(Lock._hash)) return LockEntity._lockedAt; else return null; } }