package eu.eudat.elastic.query; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import eu.eudat.commons.enums.DmpUserRole; import eu.eudat.elastic.data.DmpElasticEntity; import eu.eudat.elastic.data.nested.NestedCollaboratorElasticEntity; import gr.cite.tools.data.query.FieldResolver; 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.*; import java.util.stream.Collectors; @Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class NestedCollaboratorElasticQuery extends ElasticNestedQuery { private Collection ids; public NestedCollaboratorElasticQuery ids(UUID value) { this.ids = List.of(value); return this; } public NestedCollaboratorElasticQuery ids(UUID... value) { this.ids = Arrays.asList(value); return this; } public NestedCollaboratorElasticQuery ids(Collection values) { this.ids = values; return this; } private String nestedPath; @Override public NestedCollaboratorElasticQuery nestedPath(String value) { this.nestedPath = value; return this; } public NestedCollaboratorElasticQuery( ElasticsearchTemplate elasticsearchRestTemplate, ElasticProperties elasticProperties ) { super(elasticsearchRestTemplate, elasticProperties); } @Override protected Class entityClass() { return NestedCollaboratorElasticEntity.class; } @Override protected Boolean isFalseQuery() { return this.isEmpty(this.ids); } @Override protected Query applyAuthZ() { return null; } @Override protected Query applyFilters() { List predicates = new ArrayList<>(); if (ids != null) { predicates.add(this.containsUUID(this.elasticFieldOf(NestedCollaboratorElasticEntity._id), ids)._toQuery()); } if (!predicates.isEmpty()) { return this.and(predicates); } else { return null; } } @Override public NestedCollaboratorElasticEntity convert(Map rawData, Set columns) { NestedCollaboratorElasticEntity mocDoc = new NestedCollaboratorElasticEntity(); if (columns.contains(NestedCollaboratorElasticEntity._id)) mocDoc.setId(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedCollaboratorElasticEntity._id), UUID.class)); if (columns.contains(NestedCollaboratorElasticEntity._name)) mocDoc.setName(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedCollaboratorElasticEntity._name), String.class)); if (columns.contains(NestedCollaboratorElasticEntity._role)) mocDoc.setRole(FieldBasedMapper.shallowSafeConversion(rawData.get(NestedCollaboratorElasticEntity._role), DmpUserRole.class)); return mocDoc; } @Override protected ElasticField fieldNameOf(FieldResolver item) { if (item.match(NestedCollaboratorElasticEntity._id)) return this.elasticFieldOf(NestedCollaboratorElasticEntity._id).disableInfer(true); else if (item.match(NestedCollaboratorElasticEntity._name)) return this.elasticFieldOf(NestedCollaboratorElasticEntity._name).disableInfer(true); else if (item.match(NestedCollaboratorElasticEntity._role)) return this.elasticFieldOf(NestedCollaboratorElasticEntity._role).disableInfer(true); 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(NestedCollaboratorElasticEntity._id); } @Override protected ElasticNestedQuery nestedQueryOf(FieldResolver item) { return null; } }