argos/dmp-backend/core/src/main/java/eu/eudat/elastic/query/NestedDescriptionElasticQue...

115 lines
6.0 KiB
Java

package eu.eudat.elastic.query;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.convention.ConventionService;
import eu.eudat.elastic.data.nested.NestedDescriptionElasticEntity;
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.ElasticNestedQuery;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class NestedDescriptionElasticQuery extends ElasticNestedQuery<NestedDescriptionElasticQuery, NestedDescriptionElasticEntity, UUID> {
private String nestedPath;
@Override
public NestedDescriptionElasticQuery nestedPath(String value) {
this.nestedPath = value;
return this;
}
private final QueryFactory queryFactory;
private final ConventionService conventionService;
public NestedDescriptionElasticQuery(
ElasticsearchTemplate elasticsearchRestTemplate,
ElasticProperties elasticProperties,
QueryFactory queryFactory, ConventionService conventionService) {
super(elasticsearchRestTemplate, elasticProperties);
this.queryFactory = queryFactory;
this.conventionService = conventionService;
}
@Override
protected Class<NestedDescriptionElasticEntity> entityClass() {
return NestedDescriptionElasticEntity.class;
}
@Override
protected Boolean isFalseQuery() {
return false;
}
@Override
protected Query applyAuthZ() {
return null;
}
@Override
protected Query applyFilters() {
return null;
}
@Override
public NestedDescriptionElasticEntity convert(Map<String, Object> rawData, Set<String> columns) {
NestedDescriptionElasticEntity mocDoc = new NestedDescriptionElasticEntity();
if (columns.contains(NestedDescriptionElasticEntity._id)) mocDoc.setId(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDescriptionElasticEntity._id), UUID.class));
if (columns.contains(NestedDescriptionElasticEntity._label)) mocDoc.setLabel(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDescriptionElasticEntity._label), String.class));
if (columns.contains(NestedDescriptionElasticEntity._dmpId)) mocDoc.setDmpId(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDescriptionElasticEntity._dmpId), UUID.class));
if (columns.contains(NestedDescriptionElasticEntity._description)) mocDoc.setDescription(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDescriptionElasticEntity._description), String.class));
if (columns.contains(NestedDescriptionElasticEntity._status)) mocDoc.setStatus(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDescriptionElasticEntity._status), DescriptionStatus.class));
if (columns.contains(NestedDescriptionElasticEntity._finalizedAt)) mocDoc.setFinalizedAt(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedDescriptionElasticEntity._finalizedAt), Date.class));
mocDoc.setReferences(this.convertNested(rawData, columns, this.queryFactory.query(NestedReferenceElasticQuery.class), NestedDescriptionElasticEntity._references, this.getNestedPath()));
mocDoc.setTags(this.convertNested(rawData, columns, this.queryFactory.query(NestedTagElasticQuery.class), NestedDescriptionElasticEntity._tags, this.getNestedPath()));
return mocDoc;
}
@Override
protected ElasticField fieldNameOf(FieldResolver item) {
if (item.match(NestedDescriptionElasticEntity._id)) return this.elasticFieldOf(NestedDescriptionElasticEntity._id);
else if (item.match(NestedDescriptionElasticEntity._label)) return this.elasticFieldOf(NestedDescriptionElasticEntity._label);
else if (item.match(NestedDescriptionElasticEntity._dmpId)) return this.elasticFieldOf(NestedDescriptionElasticEntity._dmpId);
else if (item.match(NestedDescriptionElasticEntity._description)) return this.elasticFieldOf(NestedDescriptionElasticEntity._description);
else if (item.match(NestedDescriptionElasticEntity._status)) return this.elasticFieldOf(NestedDescriptionElasticEntity._status);
else if (item.match(NestedDescriptionElasticEntity._finalizedAt)) return this.elasticFieldOf(NestedDescriptionElasticEntity._finalizedAt);
else if (item.prefix(NestedDescriptionElasticEntity._references)) return this.queryFactory.query(NestedReferenceElasticQuery.class).nestedPath(this.conventionService.asIndexer(this.getNestedPath(), NestedDescriptionElasticEntity._references)).fieldNameOf(this.extractPrefixed(item, NestedDescriptionElasticEntity._references));
else if (item.prefix(NestedDescriptionElasticEntity._tags)) return this.queryFactory.query(NestedTagElasticQuery.class).nestedPath(this.conventionService.asIndexer(this.getNestedPath(), NestedDescriptionElasticEntity._tags)).fieldNameOf(this.extractPrefixed(item, NestedDescriptionElasticEntity._tags));
else return null;
}
@Override
protected String getNestedPath() {
return this.nestedPath;
}
@Override
protected UUID toKey(String key) {
return UUID.fromString(key);
}
@Override
protected ElasticField getKeyField() {
return this.elasticFieldOf(NestedDescriptionElasticEntity._id);
}
@Override
protected ElasticNestedQuery<?, ?, ?> nestedQueryOf(FieldResolver item) {
if (item.prefix(NestedDescriptionElasticEntity._references)) return this.queryFactory.query(NestedReferenceElasticQuery.class).nestedPath(this.conventionService.asIndexer(this.getNestedPath(), NestedDescriptionElasticEntity._references));
else if (item.prefix(NestedDescriptionElasticEntity._tags)) return this.queryFactory.query(NestedTagElasticQuery.class).nestedPath(this.conventionService.asIndexer(this.getNestedPath(), NestedDescriptionElasticEntity._tags));
else return null;
}
}