argos/notification-service/notification/src/main/java/gr/cite/notification/query/NotificationTemplateQuery.java

254 lines
8.9 KiB
Java

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<NotificationTemplateEntity> {
private Collection<UUID> ids;
private Collection<UUID> excludedIds;
private Collection<IsActive> isActives;
private Collection<UUID> notificationTypes;
private Collection<String> languageCodes;
private List<NotificationTemplateChannel> channels;
private List<NotificationTemplateKind> kinds;
private EnumSet<AuthorizationFlags> 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<UUID> 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<IsActive> 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<UUID> 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<String> 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<NotificationTemplateChannel> 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<NotificationTemplateKind> kinds) {
this.kinds = kinds;
return this;
}
public NotificationTemplateQuery excludedIds(Collection<UUID> 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<AuthorizationFlags> 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<NotificationTemplateEntity> entityClass() {
return NotificationTemplateEntity.class;
}
@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(NotificationTemplateEntity._id));
for (UUID item : this.ids)
inClause.value(item);
predicates.add(inClause);
}
if (this.isActives != null) {
CriteriaBuilder.In<IsActive> 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<UUID> 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<String> 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<NotificationTemplateChannel> 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<NotificationTemplateKind> 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<UUID> 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<String> 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;
}
}