183 lines
6.0 KiB
Java
183 lines
6.0 KiB
Java
package eu.eudat.query;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.commons.enums.DmpUserRole;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.DmpUserEntity;
|
|
import eu.eudat.model.DmpUser;
|
|
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.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 DmpUserQuery extends QueryBase<DmpUserEntity> {
|
|
|
|
private Collection<UUID> ids;
|
|
|
|
private Collection<UUID> dmpIds;
|
|
|
|
private Collection<UUID> userIds;
|
|
|
|
private Collection<DmpUserRole> userRoles;
|
|
|
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
|
|
|
|
|
public DmpUserQuery ids(UUID value) {
|
|
this.ids = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery ids(UUID... value) {
|
|
this.ids = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery ids(Collection<UUID> values) {
|
|
this.ids = values;
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery dmpIds(UUID value) {
|
|
this.dmpIds = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery dmpIds(UUID... value) {
|
|
this.dmpIds = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery dmpIds(Collection<UUID> values) {
|
|
this.dmpIds = values;
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery userRoles(DmpUserRole value) {
|
|
this.userRoles = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery userRoles(DmpUserRole... value) {
|
|
this.userRoles = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery userRoles(Collection<DmpUserRole> values) {
|
|
this.userRoles = values;
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery userIds(UUID value) {
|
|
this.userIds = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery userIds(UUID... value) {
|
|
this.userIds = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery userIds(Collection<UUID> values) {
|
|
this.userIds = values;
|
|
return this;
|
|
}
|
|
|
|
public DmpUserQuery authorize(EnumSet<AuthorizationFlags> values) {
|
|
this.authorize = values;
|
|
return this;
|
|
}
|
|
|
|
private final UserScope userScope;
|
|
|
|
private final AuthorizationService authService;
|
|
|
|
public DmpUserQuery(
|
|
UserScope userScope,
|
|
AuthorizationService authService
|
|
) {
|
|
this.userScope = userScope;
|
|
this.authService = authService;
|
|
}
|
|
|
|
@Override
|
|
protected Class<DmpUserEntity> entityClass() {
|
|
return DmpUserEntity.class;
|
|
}
|
|
|
|
@Override
|
|
protected Boolean isFalseQuery() {
|
|
return this.isEmpty(this.ids) || this.isEmpty(this.dmpIds) || this.isEmpty(this.userIds);
|
|
}
|
|
|
|
@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(DmpUserEntity._id));
|
|
for (UUID item : this.ids)
|
|
inClause.value(item);
|
|
predicates.add(inClause);
|
|
}
|
|
if (this.dmpIds != null) {
|
|
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpUserEntity._dmp));
|
|
for (UUID item : this.dmpIds)
|
|
inClause.value(item);
|
|
predicates.add(inClause);
|
|
}
|
|
if (this.userIds != null) {
|
|
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpUserEntity._user));
|
|
for (UUID item : this.userIds)
|
|
inClause.value(item);
|
|
predicates.add(inClause);
|
|
}
|
|
if (this.userRoles != null) {
|
|
CriteriaBuilder.In<DmpUserRole> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpUserEntity._role));
|
|
for (DmpUserRole item : this.userRoles)
|
|
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 DmpUserEntity convert(Tuple tuple, Set<String> columns) {
|
|
DmpUserEntity item = new DmpUserEntity();
|
|
item.setId(QueryBase.convertSafe(tuple, columns, DmpUserEntity._id, UUID.class));
|
|
item.setDmp(QueryBase.convertSafe(tuple, columns, DmpUserEntity._dmp, UUID.class));
|
|
item.setUser(QueryBase.convertSafe(tuple, columns, DmpUserEntity._user, UUID.class));
|
|
item.setRole(QueryBase.convertSafe(tuple, columns, DmpUserEntity._role, DmpUserRole.class));
|
|
item.setCreatedAt(QueryBase.convertSafe(tuple, columns, DmpUserEntity._createdAt, Instant.class));
|
|
item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, DmpUserEntity._updatedAt, Instant.class));
|
|
return item;
|
|
}
|
|
|
|
@Override
|
|
protected String fieldNameOf(FieldResolver item) {
|
|
if (item.match(DmpUser._id)) return DmpUserEntity._id;
|
|
else if (item.prefix(DmpUser._dmp)) return DmpUserEntity._dmp;
|
|
else if (item.prefix(DmpUser._user)) return DmpUserEntity._user;
|
|
else if (item.match(DmpUser._role)) return DmpUserEntity._role;
|
|
else if (item.match(DmpUser._createdAt)) return DmpUserEntity._createdAt;
|
|
else if (item.match(DmpUser._updatedAt)) return DmpUserEntity._updatedAt;
|
|
else return null;
|
|
}
|
|
|
|
}
|