209 lines
7.5 KiB
Java
209 lines
7.5 KiB
Java
package gr.cite.annotation.query;
|
|
|
|
import gr.cite.annotation.authorization.AuthorizationFlags;
|
|
import gr.cite.annotation.authorization.Permission;
|
|
import gr.cite.annotation.common.enums.AnnotationProtectionType;
|
|
import gr.cite.annotation.common.enums.IsActive;
|
|
import gr.cite.annotation.common.scope.user.UserScope;
|
|
import gr.cite.annotation.data.AnnotationEntity;
|
|
import gr.cite.annotation.data.EntityUserEntity;
|
|
import gr.cite.annotation.model.EntityUser;
|
|
import gr.cite.annotation.query.utils.BuildSubQueryInput;
|
|
import gr.cite.annotation.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.stereotype.Component;
|
|
import org.springframework.web.context.annotation.RequestScope;
|
|
|
|
import java.time.Instant;
|
|
import java.util.*;
|
|
|
|
@Component
|
|
@RequestScope
|
|
public class EntityUserQuery extends QueryBase<EntityUserEntity> {
|
|
|
|
private Collection<UUID> ids, entityIds, userIds;
|
|
|
|
private Collection<IsActive> isActives;;
|
|
|
|
private final AuthorizationService authService;
|
|
|
|
private final UserScope userScope;
|
|
|
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
|
|
|
public EntityUserQuery(AuthorizationService authService, UserScope userScope) {
|
|
this.authService = authService;
|
|
this.userScope = userScope;
|
|
}
|
|
|
|
public EntityUserQuery ids(UUID value) {
|
|
this.ids = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery ids(UUID... value) {
|
|
this.ids = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery ids(Collection<UUID> values) {
|
|
this.ids = values;
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery entityIds(UUID value) {
|
|
this.entityIds = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery entityIds(UUID... value) {
|
|
this.entityIds = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery entityIds(Collection<UUID> values) {
|
|
this.entityIds = values;
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery userIds(UUID value) {
|
|
this.userIds = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery userIds(UUID... value) {
|
|
this.userIds = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery userIds(Collection<UUID> values) {
|
|
this.userIds = values;
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery isActive(IsActive value) {
|
|
this.isActives = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery isActive(IsActive... value) {
|
|
this.isActives = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery isActive(Collection<IsActive> values) {
|
|
this.isActives = values;
|
|
return this;
|
|
}
|
|
|
|
public EntityUserQuery authorize(EnumSet<AuthorizationFlags> values) {
|
|
this.authorize = values;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
protected Boolean isFalseQuery() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
protected Class<EntityUserEntity> entityClass() {
|
|
return EntityUserEntity.class;
|
|
}
|
|
@Override
|
|
protected <X, Y> Predicate applyAuthZ(QueryContext<X, Y> queryContext) {
|
|
if (this.authorize.contains(AuthorizationFlags.None)) return null;
|
|
if (this.authorize.contains(AuthorizationFlags.Permission) && this.authService.authorize(Permission.BrowseAnnotation)) return null;
|
|
UUID userId = null;
|
|
if (this.authorize.contains(AuthorizationFlags.Associated)) userId = this.userScope.getUserIdSafe();
|
|
if (this.authorize.contains(AuthorizationFlags.Owner)) userId = this.userScope.getUserIdSafe();
|
|
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
if (userId != null ) {
|
|
predicates.add( queryContext.CriteriaBuilder.in(queryContext.Root.get(EntityUserEntity._userId)).value(userId));
|
|
}
|
|
if (!predicates.isEmpty()) {
|
|
Predicate[] predicatesArray = predicates.toArray(new Predicate[0]);
|
|
return queryContext.CriteriaBuilder.and(predicatesArray);
|
|
} else {
|
|
return queryContext.CriteriaBuilder.or(); //Creates a false query
|
|
}
|
|
}
|
|
|
|
|
|
@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(EntityUserEntity._id));
|
|
for (UUID item : this.ids)
|
|
inClause.value(item);
|
|
predicates.add(inClause);
|
|
}
|
|
if (this.entityIds != null) {
|
|
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(EntityUserEntity._entityId));
|
|
for (UUID item : this.entityIds)
|
|
inClause.value(item);
|
|
predicates.add(inClause);
|
|
}
|
|
if (this.userIds != null) {
|
|
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(EntityUserEntity._userId));
|
|
for (UUID item : this.userIds)
|
|
inClause.value(item);
|
|
predicates.add(inClause);
|
|
}
|
|
if (this.isActives != null) {
|
|
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(EntityUserEntity._isActive));
|
|
for (IsActive item : this.isActives)
|
|
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 String fieldNameOf(FieldResolver item) {
|
|
if (item.match(EntityUser._id))
|
|
return EntityUserEntity._id;
|
|
else if (item.match(EntityUser._entityId))
|
|
return EntityUserEntity._entityId;
|
|
else if (item.match(EntityUser._user))
|
|
return EntityUserEntity._userId;
|
|
else if (item.prefix(EntityUser._user))
|
|
return EntityUserEntity._userId;
|
|
else if (item.match(EntityUser._createdAt))
|
|
return EntityUserEntity._createdAt;
|
|
else if (item.match(EntityUser._updatedAt))
|
|
return EntityUserEntity._updatedAt;
|
|
else if (item.match(EntityUser._hash))
|
|
return EntityUserEntity._updatedAt;
|
|
else if (item.match(EntityUser._isActive))
|
|
return EntityUserEntity._isActive;
|
|
else
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected EntityUserEntity convert(Tuple tuple, Set<String> columns) {
|
|
EntityUserEntity item = new EntityUserEntity();
|
|
item.setId(QueryBase.convertSafe(tuple, columns, EntityUserEntity._id, UUID.class));
|
|
item.setEntityId(QueryBase.convertSafe(tuple, columns, EntityUserEntity._entityId, UUID.class));
|
|
item.setUserId(QueryBase.convertSafe(tuple, columns, EntityUserEntity._userId, UUID.class));
|
|
item.setCreatedAt(QueryBase.convertSafe(tuple, columns, EntityUserEntity._createdAt, Instant.class));
|
|
item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, EntityUserEntity._updatedAt, Instant.class));
|
|
item.setIsActive(QueryBase.convertSafe(tuple, columns, EntityUserEntity._isActive, IsActive.class));
|
|
return item;
|
|
}
|
|
}
|