274 lines
11 KiB
Java
274 lines
11 KiB
Java
package eu.eudat.elastic.query;
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.commons.enums.DmpAccessType;
|
|
import eu.eudat.commons.enums.DmpStatus;
|
|
import eu.eudat.commons.enums.DmpVersionStatus;
|
|
import eu.eudat.elastic.data.DmpElasticEntity;
|
|
import eu.eudat.elastic.data.nested.NestedDmpElasticEntity;
|
|
import gr.cite.tools.data.query.FieldResolver;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.elastic.configuration.ElasticProperties;
|
|
import gr.cite.tools.elastic.mapper.FieldBasedMapper;
|
|
import gr.cite.tools.elastic.query.ElasticField;
|
|
import gr.cite.tools.elastic.query.ElasticFields;
|
|
import gr.cite.tools.elastic.query.ElasticInnerObjectQuery;
|
|
import gr.cite.tools.elastic.query.ElasticNestedQuery;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
import org.springframework.context.annotation.Scope;
|
|
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
//Like in C# make it Transient
|
|
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
|
|
public class InnerObjectDmpElasticQuery extends ElasticInnerObjectQuery<InnerObjectDmpElasticQuery, NestedDmpElasticEntity, UUID> {
|
|
private String like;
|
|
private Collection<UUID> ids;
|
|
private Collection<UUID> excludedIds;
|
|
private Collection<DmpStatus> statuses;
|
|
private Collection<DmpVersionStatus> versionStatuses;
|
|
private Collection<DmpAccessType> accessTypes;
|
|
private Collection<Integer> versions;
|
|
private Collection<UUID> groupIds;
|
|
|
|
|
|
|
|
|
|
public InnerObjectDmpElasticQuery like(String value) {
|
|
this.like = value;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery ids(UUID value) {
|
|
this.ids = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery ids(UUID... value) {
|
|
this.ids = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery ids(Collection<UUID> values) {
|
|
this.ids = values;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery excludedIds(Collection<UUID> values) {
|
|
this.excludedIds = values;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery excludedIds(UUID value) {
|
|
this.excludedIds = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery excludedIds(UUID... value) {
|
|
this.excludedIds = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery versionStatuses(DmpVersionStatus value) {
|
|
this.versionStatuses = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery versionStatuses(DmpVersionStatus... value) {
|
|
this.versionStatuses = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery versionStatuses(Collection<DmpVersionStatus> values) {
|
|
this.versionStatuses = values;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery accessTypes(DmpAccessType value) {
|
|
this.accessTypes = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery accessTypes(DmpAccessType... value) {
|
|
this.accessTypes = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery accessTypes(Collection<DmpAccessType> values) {
|
|
this.accessTypes = values;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery statuses(DmpStatus value) {
|
|
this.statuses = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery statuses(DmpStatus... value) {
|
|
this.statuses = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery statuses(Collection<DmpStatus> values) {
|
|
this.statuses = values;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery versions(Integer value) {
|
|
this.versions = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery versions(Integer... value) {
|
|
this.versions = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery versions(Collection<Integer> values) {
|
|
this.versions = values;
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery groupIds(UUID value) {
|
|
this.groupIds = List.of(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery groupIds(UUID... value) {
|
|
this.groupIds = Arrays.asList(value);
|
|
return this;
|
|
}
|
|
|
|
public InnerObjectDmpElasticQuery groupIds(Collection<UUID> values) {
|
|
this.groupIds = values;
|
|
return this;
|
|
}
|
|
|
|
private String innerPath;
|
|
|
|
@Override
|
|
public InnerObjectDmpElasticQuery innerPath(String value) {
|
|
this.innerPath = value;
|
|
return this;
|
|
}
|
|
|
|
|
|
private final QueryFactory queryFactory;
|
|
@Autowired()
|
|
public InnerObjectDmpElasticQuery(ElasticsearchTemplate elasticsearchTemplate, ElasticProperties elasticProperties, QueryFactory queryFactory) {
|
|
super(elasticsearchTemplate, elasticProperties);
|
|
this.queryFactory = queryFactory;
|
|
}
|
|
|
|
@Override
|
|
protected Class<NestedDmpElasticEntity> entityClass() {
|
|
return NestedDmpElasticEntity.class;
|
|
}
|
|
|
|
@Override
|
|
protected Boolean isFalseQuery() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
protected Query applyFilters() {
|
|
List<Query> predicates = new ArrayList<>();
|
|
|
|
if (like != null && !like.isBlank()) {
|
|
if (!like.startsWith("*")) like = "*" + like;
|
|
if (!like.endsWith("*")) like = like + "*";
|
|
ElasticFields elasticFields = new ElasticFields(this.entityClass(), List.of(this.getInnerPath()));
|
|
elasticFields.add(DmpElasticEntity._label, true);
|
|
elasticFields.add(DmpElasticEntity._descriptions, true);
|
|
predicates.add(this.like(elasticFields, List.of(like))._toQuery());
|
|
}
|
|
if (ids != null) {
|
|
predicates.add(this.containsUUID(this.elasticFieldOf(DmpElasticEntity._id).disableInfer(true), ids)._toQuery());
|
|
}
|
|
if (groupIds != null) {
|
|
predicates.add(this.containsUUID(this.elasticFieldOf(DmpElasticEntity._groupId).disableInfer(true), groupIds)._toQuery());
|
|
}
|
|
if (versions != null) {
|
|
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._version).disableInfer(true), versions.toArray(new Integer[versions.size()]))._toQuery());
|
|
}
|
|
if (excludedIds != null) {
|
|
predicates.add(this.not(this.containsUUID(this.elasticFieldOf(DmpElasticEntity._id).disableInfer(true), excludedIds)._toQuery())._toQuery());
|
|
}
|
|
if (statuses != null) {
|
|
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._status).disableInfer(true), statuses.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[statuses.size()]))._toQuery());
|
|
}
|
|
if (versionStatuses != null) {
|
|
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._versionStatus).disableInfer(true), versionStatuses.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[versionStatuses.size()]))._toQuery());
|
|
}
|
|
if (accessTypes != null) {
|
|
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._accessType).disableInfer(true), accessTypes.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[accessTypes.size()]))._toQuery());
|
|
}
|
|
|
|
if (!predicates.isEmpty()) {
|
|
return this.and(predicates);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public NestedDmpElasticEntity convert(Map<String, Object> rawData, Set<String> columns) {
|
|
NestedDmpElasticEntity mocDoc = new NestedDmpElasticEntity();
|
|
if (columns.contains(NestedDmpElasticEntity._id)) mocDoc.setId(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._id), UUID.class));
|
|
if (columns.contains(NestedDmpElasticEntity._label)) mocDoc.setLabel(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._label), String.class));
|
|
if (columns.contains(NestedDmpElasticEntity._description)) mocDoc.setDescription(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._description), String.class));
|
|
if (columns.contains(NestedDmpElasticEntity._status)) mocDoc.setStatus(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._status), DmpStatus.class));
|
|
if (columns.contains(NestedDmpElasticEntity._versionStatus)) mocDoc.setVersionStatus(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._versionStatus), DmpVersionStatus.class));
|
|
if (columns.contains(NestedDmpElasticEntity._version)) mocDoc.setVersion(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._version), Short.class));
|
|
if (columns.contains(NestedDmpElasticEntity._groupId)) mocDoc.setGroupId(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._groupId), UUID.class));
|
|
if (columns.contains(NestedDmpElasticEntity._accessType)) mocDoc.setAccessType(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._accessType), DmpAccessType.class));
|
|
if (columns.contains(NestedDmpElasticEntity._finalizedAt)) mocDoc.setFinalizedAt(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDmpElasticEntity._finalizedAt), Date.class));
|
|
mocDoc.setCollaborators(this.convertNested(rawData, columns, this.queryFactory.query(NestedCollaboratorElasticQuery.class), NestedDmpElasticEntity._collaborators, null));
|
|
mocDoc.setReferences(this.convertNested(rawData, columns, this.queryFactory.query(NestedReferenceElasticQuery.class), NestedDmpElasticEntity._references, null));
|
|
return mocDoc;
|
|
}
|
|
|
|
@Override
|
|
protected ElasticField fieldNameOf(FieldResolver item) {
|
|
if (item.match(NestedDmpElasticEntity._id)) return this.elasticFieldOf(NestedDmpElasticEntity._id);
|
|
else if (item.match(NestedDmpElasticEntity._label)) return this.elasticFieldOf(NestedDmpElasticEntity._label);
|
|
else if (item.match(NestedDmpElasticEntity._description)) return this.elasticFieldOf(NestedDmpElasticEntity._description);
|
|
else if (item.match(NestedDmpElasticEntity._status)) return this.elasticFieldOf(NestedDmpElasticEntity._status);
|
|
else if (item.match(NestedDmpElasticEntity._versionStatus)) return this.elasticFieldOf(NestedDmpElasticEntity._versionStatus);
|
|
else if (item.match(NestedDmpElasticEntity._version)) return this.elasticFieldOf(NestedDmpElasticEntity._version);
|
|
else if (item.match(NestedDmpElasticEntity._groupId)) return this.elasticFieldOf(NestedDmpElasticEntity._groupId);
|
|
else if (item.match(NestedDmpElasticEntity._finalizedAt)) return this.elasticFieldOf(NestedDmpElasticEntity._finalizedAt);
|
|
else if (item.match(NestedDmpElasticEntity._accessType)) return this.elasticFieldOf(NestedDmpElasticEntity._accessType);
|
|
else if (item.prefix(NestedDmpElasticEntity._collaborators)) return this.queryFactory.query(NestedCollaboratorElasticQuery.class).nestedPath(NestedDmpElasticEntity._collaborators).fieldNameOf(this.extractPrefixed(item, NestedDmpElasticEntity._collaborators));
|
|
else if (item.prefix(NestedDmpElasticEntity._references)) return this.queryFactory.query(NestedReferenceElasticQuery.class).nestedPath(NestedDmpElasticEntity._references).fieldNameOf(this.extractPrefixed(item, NestedDmpElasticEntity._references));
|
|
else return null;
|
|
}
|
|
|
|
@Override
|
|
protected String getInnerPath() {
|
|
return this.innerPath;
|
|
}
|
|
|
|
@Override
|
|
protected UUID toKey(String key) {
|
|
return UUID.fromString(key);
|
|
}
|
|
|
|
@Override
|
|
protected ElasticField getKeyField() {
|
|
return this.elasticFieldOf(NestedDmpElasticEntity._id);
|
|
}
|
|
|
|
@Override
|
|
protected ElasticNestedQuery<?, ?, ?> nestedQueryOf(FieldResolver item) {
|
|
if (item.prefix(NestedDmpElasticEntity._collaborators)) return this.queryFactory.query(NestedCollaboratorElasticQuery.class).nestedPath(NestedDmpElasticEntity._collaborators);
|
|
else if (item.prefix(NestedDmpElasticEntity._references)) return this.queryFactory.query(NestedReferenceElasticQuery.class).nestedPath(NestedDmpElasticEntity._references);
|
|
else return null;
|
|
}
|
|
}
|