package eu.eudat.query; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.commons.enums.IsActive; import eu.eudat.data.PrefillingSourceEntity; import eu.eudat.model.PrefillingSource; 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 PrefillingSourceQuery extends QueryBase { private String like; private Collection ids; private Collection isActives; private Collection excludedIds; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); public PrefillingSourceQuery like(String value) { this.like = value; return this; } public PrefillingSourceQuery ids(UUID value) { this.ids = List.of(value); return this; } public PrefillingSourceQuery ids(UUID... value) { this.ids = Arrays.asList(value); return this; } public PrefillingSourceQuery ids(Collection values) { this.ids = values; return this; } public PrefillingSourceQuery isActive(IsActive value) { this.isActives = List.of(value); return this; } public PrefillingSourceQuery isActive(IsActive... value) { this.isActives = Arrays.asList(value); return this; } public PrefillingSourceQuery isActive(Collection values) { this.isActives = values; return this; } public PrefillingSourceQuery excludedIds(Collection values) { this.excludedIds = values; return this; } public PrefillingSourceQuery excludedIds(UUID value) { this.excludedIds = List.of(value); return this; } public PrefillingSourceQuery excludedIds(UUID... value) { this.excludedIds = Arrays.asList(value); return this; } public PrefillingSourceQuery authorize(EnumSet values) { this.authorize = values; return this; } public PrefillingSourceQuery( ) { } @Override protected Class entityClass() { return PrefillingSourceEntity.class; } @Override protected Boolean isFalseQuery() { return this.isEmpty(this.ids) || this.isEmpty(this.isActives) || this.isEmpty(this.excludedIds); } @Override protected Predicate applyFilters(QueryContext queryContext) { List predicates = new ArrayList<>(); if (this.ids != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PrefillingSourceEntity._id)); for (UUID item : this.ids) inClause.value(item); predicates.add(inClause); } if (this.like != null && !this.like.isEmpty()) { predicates.add(queryContext.CriteriaBuilder.like(queryContext.Root.get(PrefillingSourceEntity._label), this.like)); } if (this.isActives != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PrefillingSourceEntity._isActive)); for (IsActive item : this.isActives) inClause.value(item); predicates.add(inClause); } if (this.excludedIds != null) { CriteriaBuilder.In notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PrefillingSourceEntity._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 PrefillingSourceEntity convert(Tuple tuple, Set columns) { PrefillingSourceEntity item = new PrefillingSourceEntity(); item.setId(QueryBase.convertSafe(tuple, columns, PrefillingSourceEntity._id, UUID.class)); item.setLabel(QueryBase.convertSafe(tuple, columns, PrefillingSourceEntity._label, String.class)); item.setDefinition(QueryBase.convertSafe(tuple, columns, PrefillingSourceEntity._definition, String.class)); item.setCreatedAt(QueryBase.convertSafe(tuple, columns, PrefillingSourceEntity._createdAt, Instant.class)); item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, PrefillingSourceEntity._updatedAt, Instant.class)); item.setIsActive(QueryBase.convertSafe(tuple, columns, PrefillingSourceEntity._isActive, IsActive.class)); return item; } @Override protected String fieldNameOf(FieldResolver item) { if (item.match(PrefillingSource._id)) return PrefillingSourceEntity._id; else if (item.match(PrefillingSource._label)) return PrefillingSourceEntity._label; else if (item.prefix(PrefillingSource._definition)) return PrefillingSourceEntity._definition; else if (item.match(PrefillingSource._createdAt)) return PrefillingSourceEntity._createdAt; else if (item.match(PrefillingSource._updatedAt)) return PrefillingSourceEntity._updatedAt; else if (item.match(PrefillingSource._hash)) return PrefillingSourceEntity._updatedAt; else if (item.match(PrefillingSource._isActive)) return PrefillingSourceEntity._isActive; else return null; } }