Commit 2bd926a6 authored by Noel Alonso's avatar Noel Alonso
Browse files

Crea base común para reutilizar en observations

parent 9312b82b
Loading
Loading
Loading
Loading
+113 −0
Original line number Diff line number Diff line
package es.redmic.es.geodata.tracking.common.repository;

/*-
 * #%L
 * ElasticSearch
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BaseAggregationBuilder;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.springframework.beans.factory.annotation.Value;

import es.redmic.es.common.queryFactory.geodata.GeoDataQueryUtils;
import es.redmic.es.common.queryFactory.geodata.TrackingQueryUtils;
import es.redmic.models.es.common.query.dto.AggsPropertiesDTO;
import es.redmic.models.es.common.query.dto.GeoDataQueryDTO;
import es.redmic.models.es.common.query.dto.SimpleQueryDTO;
import es.redmic.models.es.common.request.dto.CategoryPathInfo;
import es.redmic.models.es.geojson.common.model.GeoPointData;
import es.redmic.models.es.geojson.common.model.GeoSearchWrapper;

public abstract class TrackingBaseESRepository extends ClusterTrackingESRepository<GeoPointData> {

	@Value("${controller.mapping.TAXONS}")
	private String TAXONS_TARGET;

	@Value("${controller.mapping.ANIMAL}")
	private String ANIMAL_TARGET;

	@Value("${controller.mapping.DEVICE}")
	private String DEVICE_TARGET;

	@Value("${controller.mapping.PLATFORM}")
	private String PLATFORM_TARGET;

	protected TrackingBaseESRepository() {
		super();
		setInternalQuery(TrackingQueryUtils.INTERNAL_QUERY);
	}

	@Override
	protected <TQueryDTO extends SimpleQueryDTO> List<BaseAggregationBuilder> getAggs(TQueryDTO elasticQueryDTO) {

		GeoDataQueryDTO geoDataQueryDTO = (GeoDataQueryDTO) elasticQueryDTO;

		List<AggsPropertiesDTO> aggs = geoDataQueryDTO.getAggs();

		if (aggs != null && !aggs.isEmpty() && aggs.get(0).getField().equals("elements")) {
			List<BaseAggregationBuilder> aggsBuilder = new ArrayList<>();

			aggsBuilder.add(AggregationBuilders.terms("animal").field("properties.collect.animal.id")
					.order(BucketOrder.key(true)).size(MAX_SIZE));
			aggsBuilder.add(AggregationBuilders.terms("platform").field("properties.inTrack.platform.id")
					.order(BucketOrder.key(true)).size(MAX_SIZE));

			return aggsBuilder;
		} else
			return super.getAggs(elasticQueryDTO);
	}

	/*
	 * Devuelve todos los puntos de un track para un elemento dado
	 */
	public GeoSearchWrapper<?, ?> find(String parentId, String uuid, GeoDataQueryDTO queryDTO) {

		QueryBuilder serviceQuery = GeoDataQueryUtils.getHierarchicalQuery(queryDTO, parentId);

		BoolQueryBuilder builder = QueryBuilders.boolQuery()
				.filter(QueryBuilders.boolQuery()
						.must(QueryBuilders.boolQuery().should(QueryBuilders.termQuery(PLATFORM_PATH, uuid))
								.should(QueryBuilders.termQuery(ANIMAL_PATH, uuid))));

		SearchResponse result = searchRequest(queryDTO, builder.must(serviceQuery));

		return searchResponseToWrapper(result, getSourceType(GeoSearchWrapper.class));
	}

	@Override
	public HashMap<String, CategoryPathInfo> getCategoriesPaths() {

		// TODO: usar el diccionario de dto a model cuando esté implementado
		HashMap<String, CategoryPathInfo> categoriesPaths = new HashMap<>();
		categoriesPaths.put("properties.taxon.id", new CategoryPathInfo("properties.collect.taxon.path", TAXONS_TARGET));
		categoriesPaths.put("properties.animal.id", new CategoryPathInfo("properties.collect.animal.id", ANIMAL_TARGET));
		categoriesPaths.put("properties.device.id", new CategoryPathInfo("properties.inTrack.device.id", DEVICE_TARGET));
		categoriesPaths.put("properties.platform.id", new CategoryPathInfo("properties.inTrack.platform.id", PLATFORM_TARGET));

		return categoriesPaths;
	}
}
+1 −85
Original line number Diff line number Diff line
@@ -20,96 +20,12 @@ package es.redmic.es.geodata.tracking.common.repository;
 * #L%
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BaseAggregationBuilder;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import es.redmic.es.common.queryFactory.geodata.GeoDataQueryUtils;
import es.redmic.es.common.queryFactory.geodata.TrackingQueryUtils;
import es.redmic.models.es.common.query.dto.AggsPropertiesDTO;
import es.redmic.models.es.common.query.dto.GeoDataQueryDTO;
import es.redmic.models.es.common.query.dto.SimpleQueryDTO;
import es.redmic.models.es.common.request.dto.CategoryPathInfo;
import es.redmic.models.es.geojson.common.model.GeoPointData;
import es.redmic.models.es.geojson.common.model.GeoSearchWrapper;

@Repository
public class TrackingESRepository extends ClusterTrackingESRepository<GeoPointData> {

	@Value("${controller.mapping.TAXONS}")
	private String TAXONS_TARGET;

	@Value("${controller.mapping.ANIMAL}")
	private String ANIMAL_TARGET;

	@Value("${controller.mapping.DEVICE}")
	private String DEVICE_TARGET;

	@Value("${controller.mapping.PLATFORM}")
	private String PLATFORM_TARGET;
public class TrackingESRepository extends TrackingBaseESRepository {

	public TrackingESRepository() {
		super();
		setInternalQuery(TrackingQueryUtils.INTERNAL_QUERY);
	}

	@Override
	protected <TQueryDTO extends SimpleQueryDTO> List<BaseAggregationBuilder> getAggs(TQueryDTO elasticQueryDTO) {

		GeoDataQueryDTO geoDataQueryDTO = (GeoDataQueryDTO) elasticQueryDTO;

		List<AggsPropertiesDTO> aggs = geoDataQueryDTO.getAggs();

		if (aggs != null && !aggs.isEmpty() && aggs.get(0).getField().equals("elements")) {
			List<BaseAggregationBuilder> aggsBuilder = new ArrayList<>();

			aggsBuilder.add(AggregationBuilders.terms("animal").field("properties.collect.animal.id")
					.order(BucketOrder.key(true)).size(MAX_SIZE));
			aggsBuilder.add(AggregationBuilders.terms("platform").field("properties.inTrack.platform.id")
					.order(BucketOrder.key(true)).size(MAX_SIZE));

			return aggsBuilder;
		} else
			return super.getAggs(elasticQueryDTO);
	}

	/*
	 * Devuelve todos los puntos de un track para un elemento dado
	 */
	public GeoSearchWrapper<?, ?> find(String parentId, String uuid, GeoDataQueryDTO queryDTO) {

		QueryBuilder serviceQuery = GeoDataQueryUtils.getHierarchicalQuery(queryDTO, parentId);

		BoolQueryBuilder builder = QueryBuilders.boolQuery()
				.filter(QueryBuilders.boolQuery()
						.must(QueryBuilders.boolQuery().should(QueryBuilders.termQuery(PLATFORM_PATH, uuid))
								.should(QueryBuilders.termQuery(ANIMAL_PATH, uuid))));

		SearchResponse result = searchRequest(queryDTO, builder.must(serviceQuery));

		return searchResponseToWrapper(result, getSourceType(GeoSearchWrapper.class));
	}

	@Override
	public HashMap<String, CategoryPathInfo> getCategoriesPaths() {

		// TODO: usar el diccionario de dto a model cuando esté implementado
		HashMap<String, CategoryPathInfo> categoriesPaths = new HashMap<>();
		categoriesPaths.put("properties.taxon.id", new CategoryPathInfo("properties.collect.taxon.path", TAXONS_TARGET));
		categoriesPaths.put("properties.animal.id", new CategoryPathInfo("properties.collect.animal.id", ANIMAL_TARGET));
		categoriesPaths.put("properties.device.id", new CategoryPathInfo("properties.inTrack.device.id", DEVICE_TARGET));
		categoriesPaths.put("properties.platform.id", new CategoryPathInfo("properties.inTrack.platform.id", PLATFORM_TARGET));

		return categoriesPaths;
	}
}
+142 −0
Original line number Diff line number Diff line
package es.redmic.es.geodata.tracking.common.service;

/*-
 * #%L
 * ElasticSearch
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.springframework.beans.factory.annotation.Autowired;

import es.redmic.es.administrative.service.PlatformESService;
import es.redmic.es.administrative.taxonomy.service.AnimalESService;
import es.redmic.es.config.OrikaScanBeanESItfc;
import es.redmic.es.geodata.common.service.RGeoDataESService;
import es.redmic.es.geodata.tracking.common.repository.TrackingBaseESRepository;
import es.redmic.models.es.common.dto.AggregationsDTO;
import es.redmic.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.common.dto.UuidDTO;
import es.redmic.models.es.common.query.dto.AggsPropertiesDTO;
import es.redmic.models.es.common.query.dto.GeoDataQueryDTO;
import es.redmic.models.es.geojson.common.dto.GeoJSONFeatureCollectionDTO;
import es.redmic.models.es.geojson.common.model.GeoPointData;
import es.redmic.models.es.geojson.common.model.GeoSearchWrapper;
import es.redmic.models.es.geojson.tracking.common.ElementListDTO;
import es.redmic.models.es.geojson.tracking.common.ElementTrackingDTO;
import ma.glasnost.orika.MappingContext;

public abstract class TrackingBaseESService extends RGeoDataESService<ElementTrackingDTO, GeoPointData> {

	@Autowired
	protected OrikaScanBeanESItfc orikaMapper;

	TrackingBaseESRepository repository;

	@Autowired
	AnimalESService animalESService;

	@Autowired
	PlatformESService platformESService;

	@Autowired
	protected TrackingBaseESService(TrackingBaseESRepository repository) {
		super(repository);
		this.repository = repository;
	}

	/*
	 * Devuelve el elemento buscado independientemente de si es animal o plataforma
	 */

	public UuidDTO getElement(String elementUuid) {

		UuidDTO animal = animalESService.findByUuid(elementUuid);

		if (animal != null)
			return orikaMapper.getMapperFacade().map(animal, UuidDTO.class);

		return orikaMapper.getMapperFacade().map(platformESService.findByUuid(elementUuid), UuidDTO.class);
	}

	/*
	 * Obtiene animales y plataformas relacionada con el track de una actividad
	 * dada.
	 */

	public JSONCollectionDTO getElementsByActivity(String activityId, GeoDataQueryDTO query) {

		query.setSize(0);
		// añade identificador para que se cree la agregación correspondinte en
		// el repositorio
		query.addAgg(new AggsPropertiesDTO("elements"));

		GeoSearchWrapper<?, ?> response = repository.find(query, activityId);

		// Obtenemos List de data en las agregaciones
		ElementListDTO dtoOut = orikaMapper.getMapperFacade().convert(response.getAggregations(), ElementListDTO.class,
				null, null);

		JSONCollectionDTO collection = new JSONCollectionDTO();
		collection.setData(dtoOut);
		collection.setTotal(dtoOut.size());
		return collection;
	}

	public GeoJSONFeatureCollectionDTO getTrackingPointsInLineStringCluster(String activityId, GeoDataQueryDTO queryDTO,
			String uuid) {

		GeoJSONFeatureCollectionDTO clusterCollection = repository.getTrackingPointsInLineStringCluster(activityId,
				queryDTO, uuid);

		return clusterCollection;
	}

	public GeoJSONFeatureCollectionDTO getTrackingPointsInLineStringCluster(String activityId, GeoDataQueryDTO queryDTO) {

		GeoJSONFeatureCollectionDTO clusterCollection = repository.getTrackingPointsInLineStringCluster(activityId,
				queryDTO);

		return clusterCollection;
	}

	/*
	 * Devuelve todos los puntos de un track para un elemento dado
	 */
	public GeoJSONFeatureCollectionDTO find(String activityId, String uuid, GeoDataQueryDTO queryDTO) {

		GeoSearchWrapper<?, ?> result = repository.find(activityId, uuid, queryDTO);

		if (result.getTotal() == 0)
			return new GeoJSONFeatureCollectionDTO();

		GeoJSONFeatureCollectionDTO featureCollection = orikaMapper.getMapperFacade().map(result.getHits(),
				GeoJSONFeatureCollectionDTO.class, getMappingContext());
		if (result.getAggregations() != null)
			featureCollection
					.set_aggs(orikaMapper.getMapperFacade().map(result.getAggregations(), AggregationsDTO.class));
		return featureCollection;
	}

	@Override
	protected MappingContext getMappingContext() {

		MappingContext context = orikaMapper.getMappingContext();
		context.setProperty("targetTypeDto", ElementTrackingDTO.class);

		return context;
	}
}
+1 −110
Original line number Diff line number Diff line
@@ -23,122 +23,13 @@ package es.redmic.es.geodata.tracking.common.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import es.redmic.es.administrative.service.PlatformESService;
import es.redmic.es.administrative.taxonomy.service.AnimalESService;
import es.redmic.es.config.OrikaScanBeanESItfc;
import es.redmic.es.geodata.common.service.RGeoDataESService;
import es.redmic.es.geodata.tracking.common.repository.TrackingESRepository;
import es.redmic.models.es.common.dto.AggregationsDTO;
import es.redmic.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.common.dto.UuidDTO;
import es.redmic.models.es.common.query.dto.AggsPropertiesDTO;
import es.redmic.models.es.common.query.dto.GeoDataQueryDTO;
import es.redmic.models.es.geojson.common.dto.GeoJSONFeatureCollectionDTO;
import es.redmic.models.es.geojson.common.model.GeoPointData;
import es.redmic.models.es.geojson.common.model.GeoSearchWrapper;
import es.redmic.models.es.geojson.tracking.common.ElementListDTO;
import es.redmic.models.es.geojson.tracking.common.ElementTrackingDTO;
import ma.glasnost.orika.MappingContext;

@Service
public class TrackingESService extends RGeoDataESService<ElementTrackingDTO, GeoPointData> {

	@Autowired
	protected OrikaScanBeanESItfc orikaMapper;

	TrackingESRepository repository;

	@Autowired
	AnimalESService animalESService;

	@Autowired
	PlatformESService platformESService;
public class TrackingESService extends TrackingBaseESService {

	@Autowired
	public TrackingESService(TrackingESRepository repository) {
		super(repository);
		this.repository = repository;
	}

	/*
	 * Devuelve el elemento buscado independientemente de si es animal o plataforma
	 */

	public UuidDTO getElement(String elementUuid) {

		UuidDTO animal = animalESService.findByUuid(elementUuid);

		if (animal != null)
			return orikaMapper.getMapperFacade().map(animal, UuidDTO.class);

		return orikaMapper.getMapperFacade().map(platformESService.findByUuid(elementUuid), UuidDTO.class);
	}

	/*
	 * Obtiene animales y plataformas relacionada con el track de una actividad
	 * dada.
	 */

	public JSONCollectionDTO getElementsByActivity(String activityId, GeoDataQueryDTO query) {

		query.setSize(0);
		// añade identificador para que se cree la agregación correspondinte en
		// el repositorio
		query.addAgg(new AggsPropertiesDTO("elements"));

		GeoSearchWrapper<?, ?> response = repository.find(query, activityId);

		// Obtenemos List de data en las agregaciones
		ElementListDTO dtoOut = orikaMapper.getMapperFacade().convert(response.getAggregations(), ElementListDTO.class,
				null, null);

		JSONCollectionDTO collection = new JSONCollectionDTO();
		collection.setData(dtoOut);
		collection.setTotal(dtoOut.size());
		return collection;
	}

	public GeoJSONFeatureCollectionDTO getTrackingPointsInLineStringCluster(String activityId, GeoDataQueryDTO queryDTO,
			String uuid) {

		GeoJSONFeatureCollectionDTO clusterCollection = repository.getTrackingPointsInLineStringCluster(activityId,
				queryDTO, uuid);

		return clusterCollection;
	}

	public GeoJSONFeatureCollectionDTO getTrackingPointsInLineStringCluster(String activityId, GeoDataQueryDTO queryDTO) {

		GeoJSONFeatureCollectionDTO clusterCollection = repository.getTrackingPointsInLineStringCluster(activityId,
				queryDTO);

		return clusterCollection;
	}

	/*
	 * Devuelve todos los puntos de un track para un elemento dado
	 */
	public GeoJSONFeatureCollectionDTO find(String activityId, String uuid, GeoDataQueryDTO queryDTO) {

		GeoSearchWrapper<?, ?> result = repository.find(activityId, uuid, queryDTO);

		if (result.getTotal() == 0)
			return new GeoJSONFeatureCollectionDTO();

		GeoJSONFeatureCollectionDTO featureCollection = orikaMapper.getMapperFacade().map(result.getHits(),
				GeoJSONFeatureCollectionDTO.class, getMappingContext());
		if (result.getAggregations() != null)
			featureCollection
					.set_aggs(orikaMapper.getMapperFacade().map(result.getAggregations(), AggregationsDTO.class));
		return featureCollection;
	}

	@Override
	protected MappingContext getMappingContext() {

		MappingContext context = orikaMapper.getMappingContext();
		context.setProperty("targetTypeDto", ElementTrackingDTO.class);

		return context;
	}
}