package gr.cite.notification.query; import gr.cite.notification.authorization.AuthorizationFlags; import gr.cite.notification.common.enums.*; import gr.cite.notification.data.NotificationTemplateEntity; import gr.cite.notification.model.NotificationTemplate; import gr.cite.tools.data.query.FieldResolver; import gr.cite.tools.data.query.QueryBase; import gr.cite.tools.data.query.QueryContext; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import jakarta.persistence.Tuple; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.Predicate; import java.time.Instant; import java.util.*; @Component @Scope(BeanDefinition.SCOPE_PROTOTYPE) public class NotificationTemplateQuery extends QueryBase { private Collection ids; private Collection excludedIds; private Collection isActives; private Collection notificationTypes; private Collection languageCodes; private List channels; private List kinds; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); public NotificationTemplateQuery ids(UUID value) { this.ids = List.of(value); return this; } public NotificationTemplateQuery ids(UUID... value) { this.ids = Arrays.asList(value); return this; } public NotificationTemplateQuery ids(Collection values) { this.ids = values; return this; } public NotificationTemplateQuery isActive(IsActive value) { this.isActives = List.of(value); return this; } public NotificationTemplateQuery isActive(IsActive... value) { this.isActives = Arrays.asList(value); return this; } public NotificationTemplateQuery isActive(Collection values) { this.isActives = values; return this; } public NotificationTemplateQuery notificationTypes(UUID value) { this.notificationTypes = List.of(value); return this; } public NotificationTemplateQuery notificationTypes(UUID... value) { this.notificationTypes = Arrays.asList(value); return this; } public NotificationTemplateQuery notificationTypes(Collection values) { this.notificationTypes = values; return this; } public NotificationTemplateQuery languageCodes(String value) { this.languageCodes = List.of(value); return this; } public NotificationTemplateQuery languageCodes(String... value) { this.languageCodes = Arrays.asList(value); return this; } public NotificationTemplateQuery languageCodes(Collection values) { this.languageCodes = values; return this; } public NotificationTemplateQuery channels(NotificationTemplateChannel... channels) { this.channels = List.of(channels); return this; } public NotificationTemplateQuery channels(NotificationTemplateChannel channels) { this.channels = List.of(channels); return this; } public NotificationTemplateQuery channels(List channels) { this.channels = channels; return this; } public NotificationTemplateQuery kinds(NotificationTemplateKind... kinds) { this.kinds = List.of(kinds); return this; } public NotificationTemplateQuery kinds(NotificationTemplateKind kinds) { this.kinds = List.of(kinds); return this; } public NotificationTemplateQuery kinds(List kinds) { this.kinds = kinds; return this; } public NotificationTemplateQuery excludedIds(Collection values) { this.excludedIds = values; return this; } public NotificationTemplateQuery excludedIds(UUID value) { this.excludedIds = List.of(value); return this; } public NotificationTemplateQuery excludedIds(UUID... value) { this.excludedIds = Arrays.asList(value); return this; } public NotificationTemplateQuery authorize(EnumSet values) { this.authorize = values; return this; } @Override protected Boolean isFalseQuery() { return this.isNullOrEmpty(this.ids) && this.isNullOrEmpty(this.isActives) && this.isNullOrEmpty(this.kinds) && this.isNullOrEmpty(this.notificationTypes) && this.isNullOrEmpty(this.languageCodes) && this.isNullOrEmpty(this.channels); } @Override protected Class entityClass() { return NotificationTemplateEntity.class; } @Override protected Predicate applyFilters(QueryContext queryContext) { List predicates = new ArrayList<>(); if (this.ids != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._id)); for (UUID item : this.ids) inClause.value(item); predicates.add(inClause); } if (this.isActives != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._isActive)); for (IsActive item : this.isActives) inClause.value(item); predicates.add(inClause); } if (this.notificationTypes != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._notificationType)); for (UUID item : this.notificationTypes) inClause.value(item); predicates.add(inClause); } if (this.languageCodes != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._languageCode)); for (String item : this.languageCodes) inClause.value(item); predicates.add(inClause); } if (this.channels != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._channel)); for (NotificationTemplateChannel item : this.channels) inClause.value(item); predicates.add(inClause); } if (this.kinds != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._kind)); for (NotificationTemplateKind item : this.kinds) inClause.value(item); predicates.add(inClause); } if (this.excludedIds != null) { CriteriaBuilder.In notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(NotificationTemplateEntity._id)); for (UUID item : this.excludedIds) notInClause.value(item); predicates.add(notInClause.not()); } if (predicates.size() > 0) { Predicate[] predicatesArray = predicates.toArray(new Predicate[0]); return queryContext.CriteriaBuilder.and(predicatesArray); } else { return null; } } @Override protected String fieldNameOf(FieldResolver item) { if (item.match(NotificationTemplate._id)) return NotificationTemplateEntity._id; else if (item.match(NotificationTemplate._channel)) return NotificationTemplateEntity._channel; else if (item.match(NotificationTemplate._kind)) return NotificationTemplateEntity._kind; else if (item.match(NotificationTemplate._notificationType)) return NotificationTemplateEntity._notificationType; else if (item.match(NotificationTemplate._languageCode)) return NotificationTemplateEntity._languageCode; else if (item.prefix(NotificationTemplate._value)) return NotificationTemplateEntity._value; else if (item.match(NotificationTemplate._createdAt)) return NotificationTemplateEntity._createdAt; else if (item.match(NotificationTemplate._updatedAt)) return NotificationTemplateEntity._updatedAt; else if (item.match(NotificationTemplate._isActive)) return NotificationTemplateEntity._isActive; else if (item.prefix(NotificationTemplate._tenant)) return NotificationTemplateEntity._tenantId; else return null; } @Override protected NotificationTemplateEntity convert(Tuple tuple, Set columns) { NotificationTemplateEntity item = new NotificationTemplateEntity(); item.setId(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._id, UUID.class)); item.setChannel(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._channel, NotificationTemplateChannel.class)); item.setKind(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._kind, NotificationTemplateKind.class)); item.setNotificationType(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._notificationType, UUID.class)); item.setValue(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._value, String.class)); item.setLanguageCode(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._languageCode, String.class)); item.setCreatedAt(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._createdAt, Instant.class)); item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._updatedAt, Instant.class)); item.setIsActive(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._isActive, IsActive.class)); item.setTenantId(QueryBase.convertSafe(tuple, columns, NotificationTemplateEntity._tenantId, UUID.class)); return item; } }