153 lines
5.3 KiB
Java
153 lines
5.3 KiB
Java
|
package eu.eudat.query;
|
||
|
|
||
|
import eu.eudat.authorization.AuthorizationFlags;
|
||
|
import eu.eudat.commons.scope.user.UserScope;
|
||
|
import eu.eudat.data.DmpExternalReferenceEntity;
|
||
|
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.util.*;
|
||
|
|
||
|
@Component
|
||
|
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||
|
public class DmpExternalReferenceQuery extends QueryBase<DmpExternalReferenceEntity> {
|
||
|
|
||
|
private Collection<UUID> ids;
|
||
|
|
||
|
private Collection<UUID> dmpIds;
|
||
|
|
||
|
private Collection<UUID> referenceIds;
|
||
|
|
||
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
||
|
|
||
|
|
||
|
public DmpExternalReferenceQuery ids(UUID value) {
|
||
|
this.ids = List.of(value);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery ids(UUID... value) {
|
||
|
this.ids = Arrays.asList(value);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery ids(Collection<UUID> values) {
|
||
|
this.ids = values;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery dmpIds(UUID value) {
|
||
|
this.dmpIds = List.of(value);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery dmpIds(UUID... value) {
|
||
|
this.dmpIds = Arrays.asList(value);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery dmpIds(Collection<UUID> values) {
|
||
|
this.dmpIds = values;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery referenceIds(UUID value) {
|
||
|
this.referenceIds = List.of(value);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery referenceIds(UUID... value) {
|
||
|
this.referenceIds = Arrays.asList(value);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery referenceIds(Collection<UUID> values) {
|
||
|
this.referenceIds = values;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public DmpExternalReferenceQuery authorize(EnumSet<AuthorizationFlags> values) {
|
||
|
this.authorize = values;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
private final UserScope userScope;
|
||
|
|
||
|
private final AuthorizationService authService;
|
||
|
|
||
|
public DmpExternalReferenceQuery(
|
||
|
UserScope userScope,
|
||
|
AuthorizationService authService
|
||
|
) {
|
||
|
this.userScope = userScope;
|
||
|
this.authService = authService;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected Class<DmpExternalReferenceEntity> entityClass() {
|
||
|
return DmpExternalReferenceEntity.class;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected Boolean isFalseQuery() {
|
||
|
return this.isEmpty(this.ids) || this.isEmpty(this.dmpIds) || this.isEmpty(this.referenceIds);
|
||
|
}
|
||
|
|
||
|
@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(DmpExternalReferenceEntity._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(DmpExternalReferenceEntity._dmpId));
|
||
|
for (UUID item : this.ids)
|
||
|
inClause.value(item);
|
||
|
predicates.add(inClause);
|
||
|
}
|
||
|
if (this.referenceIds != null) {
|
||
|
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpExternalReferenceEntity._referenceId));
|
||
|
for (UUID item : this.ids)
|
||
|
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 DmpExternalReferenceEntity convert(Tuple tuple, Set<String> columns) {
|
||
|
DmpExternalReferenceEntity item = new DmpExternalReferenceEntity();
|
||
|
item.setId(QueryBase.convertSafe(tuple, columns, DmpExternalReferenceEntity._id, UUID.class));
|
||
|
item.setReferenceId(QueryBase.convertSafe(tuple, columns, DmpExternalReferenceEntity._dmpId, UUID.class));
|
||
|
item.setReferenceId(QueryBase.convertSafe(tuple, columns, DmpExternalReferenceEntity._referenceId, UUID.class));
|
||
|
item.setData(QueryBase.convertSafe(tuple, columns, DmpExternalReferenceEntity._data, String.class));
|
||
|
return item;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected String fieldNameOf(FieldResolver item) {
|
||
|
if (item.match(DmpExternalReferenceEntity._id)) return DmpExternalReferenceEntity._id;
|
||
|
else if (item.prefix(DmpExternalReferenceEntity._dmpId)) return DmpExternalReferenceEntity._dmpId;
|
||
|
else if (item.prefix(DmpExternalReferenceEntity._referenceId)) return DmpExternalReferenceEntity._referenceId;
|
||
|
else if (item.match(DmpExternalReferenceEntity._data)) return DmpExternalReferenceEntity._data;
|
||
|
else return null;
|
||
|
}
|
||
|
|
||
|
}
|