From 66885da0355c5cfead40a5add7d5f3e6c5125ae3 Mon Sep 17 00:00:00 2001 From: Lyn Elisa Goltz Date: Sat, 28 Jan 2017 17:19:44 +0100 Subject: [PATCH] #235 - ignore filter if property is null --- .../persistence/feature/FeatureLayer.java | 26 +++++----- .../persistence/feature/FilterBuilder.java | 48 ++++++++++++++----- .../feature/FilterBuilderTest.java | 30 ++++++++++-- 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FeatureLayer.java b/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FeatureLayer.java index fe3896d5e3..ecc41a530c 100644 --- a/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FeatureLayer.java +++ b/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FeatureLayer.java @@ -40,17 +40,6 @@ ----------------------------------------------------------------------------*/ package org.deegree.layer.persistence.feature; -import static org.deegree.filter.Filters.addBBoxConstraint; -import static org.deegree.layer.persistence.feature.FilterBuilder.buildFilterForMap; -import static org.deegree.style.utils.Styles.getStyleFilters; -import static org.slf4j.LoggerFactory.getLogger; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.xml.namespace.QName; - import org.deegree.commons.ows.exception.OWSException; import org.deegree.feature.persistence.FeatureStore; import org.deegree.feature.persistence.query.Query; @@ -69,6 +58,16 @@ import org.deegree.style.utils.Styles; import org.slf4j.Logger; +import javax.xml.namespace.QName; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.deegree.filter.Filters.addBBoxConstraint; +import static org.deegree.layer.persistence.feature.FilterBuilder.buildFilterForMap; +import static org.deegree.style.utils.Styles.getStyleFilters; +import static org.slf4j.LoggerFactory.getLogger; + /** * * @author Andreas Schmitz @@ -129,13 +128,16 @@ public FeatureLayerData mapQuery( final LayerQuery query, List headers ) geomProp = null; } + QName ftName = featureType == null ? style.getFeatureType() : featureType; if ( ftName != null && featureStore.getSchema().getFeatureType( ftName ) == null ) { LOG.warn( "FeatureType '" + ftName + "' is not known to the FeatureStore." ); return null; } - filter = Filters.repair( filter, AppSchemas.collectProperyNames( featureStore.getSchema(), ftName ) ); + Set propertyNames = AppSchemas.collectProperyNames( featureStore.getSchema(), ftName ); + filter = FilterBuilder.appendRequestFilter( filter, query, propertyNames ); + filter = Filters.repair( filter, propertyNames ); QueryBuilder builder = new QueryBuilder( featureStore, filter, ftName, bbox, query, geomProp, sortBy, getMetadata().getName() ); diff --git a/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FilterBuilder.java b/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FilterBuilder.java index 7cc9cd213b..b831bb8af6 100644 --- a/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FilterBuilder.java +++ b/deegree-layers/deegree-layers-feature/src/main/java/org/deegree/layer/persistence/feature/FilterBuilder.java @@ -41,13 +41,6 @@ Occam Labs UG (haftungsbeschränkt) ----------------------------------------------------------------------------*/ package org.deegree.layer.persistence.feature; -import static org.deegree.feature.types.property.GeometryPropertyType.CoordinateDimension.DIM_2; -import static org.deegree.feature.types.property.GeometryPropertyType.CoordinateDimension.DIM_2_OR_3; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - import org.deegree.commons.ows.exception.OWSException; import org.deegree.commons.tom.gml.property.PropertyType; import org.deegree.commons.tom.primitive.PrimitiveValue; @@ -72,6 +65,14 @@ Occam Labs UG (haftungsbeschränkt) import org.deegree.style.utils.Styles; import javax.xml.namespace.QName; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import static org.deegree.feature.types.property.GeometryPropertyType.CoordinateDimension.DIM_2; +import static org.deegree.feature.types.property.GeometryPropertyType.CoordinateDimension.DIM_2_OR_3; /** * Responsible for building feature layer filters. @@ -89,7 +90,6 @@ static OperatorFilter buildFilterForMap( OperatorFilter filter, Style style, Lay filter = Filters.and( filter, Styles.getStyleFilters( style, query.getScale() ) ); filter = Filters.and( filter, query.getFilter() ); filter = Filters.and( filter, dimFilterBuilder.getDimensionFilter( query.getDimensions(), headers ) ); - filter = Filters.and( filter, buildRequestFilter( query ) ); return filter; } @@ -133,12 +133,17 @@ private static LinkedList findOperators( FeatureType ft, Envelope clic return list; } - static OperatorFilter buildRequestFilter( LayerQuery layerQuery ) { + static OperatorFilter appendRequestFilter( OperatorFilter filter, LayerQuery query, Set propertyNames ) { + OperatorFilter requestFilter = buildRequestFilter( query, propertyNames ); + return Filters.and( filter, requestFilter ); + } + + static OperatorFilter buildRequestFilter( LayerQuery layerQuery, Set propertyNames ) { Pair> requestFilter = layerQuery.requestFilter(); if ( requestFilter == null ) return null; - List operators = createOperators( requestFilter ); + List operators = createOperatorsIfPropertyIsKnown( requestFilter, propertyNames ); if ( operators.isEmpty() ) return null; if ( operators.size() == 1 ) @@ -146,9 +151,18 @@ static OperatorFilter buildRequestFilter( LayerQuery layerQuery ) { return new OperatorFilter( new Or( operators.toArray( new Operator[operators.size()] ) ) ); } - private static List createOperators( Pair> requestFilter ) { - List operators = new ArrayList(); + private static List createOperatorsIfPropertyIsKnown( Pair> requestFilter, + Set propertyNames ) { String filterProperty = requestFilter.getFirst(); + if ( propertyIsknown( filterProperty, propertyNames ) ) { + return createOperatorsIfPropertyIsKnown( requestFilter, filterProperty ); + } + return Collections.emptyList(); + } + + private static List createOperatorsIfPropertyIsKnown( Pair> requestFilter, + String filterProperty ) { + List operators = new ArrayList(); List filterValues = requestFilter.getSecond(); for ( String filterValue : filterValues ) { Expression filterPropertyExpression = new ValueReference( new QName( filterProperty ) ); @@ -161,4 +175,12 @@ private static List createOperators( Pair propertyNames ) { + for ( QName propertyName : propertyNames ) { + if ( filterProperty.equals( propertyName.getLocalPart() ) ) + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/deegree-layers/deegree-layers-feature/src/test/java/org/deegree/layer/persistence/feature/FilterBuilderTest.java b/deegree-layers/deegree-layers-feature/src/test/java/org/deegree/layer/persistence/feature/FilterBuilderTest.java index bb4446195f..89516aaf82 100644 --- a/deegree-layers/deegree-layers-feature/src/test/java/org/deegree/layer/persistence/feature/FilterBuilderTest.java +++ b/deegree-layers/deegree-layers-feature/src/test/java/org/deegree/layer/persistence/feature/FilterBuilderTest.java @@ -8,9 +8,11 @@ import org.junit.Test; import org.mockito.Mockito; +import javax.xml.namespace.QName; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Set; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; @@ -28,7 +30,8 @@ public void testBuildRequestFilter() { List filterValues = Collections.singletonList( "one" ); LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues ); - OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery ); + Set propertyNames = createPropertyNames( filterProperty ); + OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames ); assertThat( operatorFilter.getOperator(), instanceOf( PropertyIsEqualTo.class ) ); } @@ -38,7 +41,8 @@ public void testBuildRequestFilter_MultipleValues() { String filterProperty = "type"; List filterValues = Arrays.asList( new String[] { "one", "two" } ); LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues ); - OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery ); + Set propertyNames = createPropertyNames( filterProperty ); + OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames ); assertThat( operatorFilter.getOperator(), instanceOf( Or.class ) ); } @@ -48,7 +52,8 @@ public void testBuildRequestFilter_EmptyValues() { String filterProperty = "type"; List filterValues = Collections.emptyList(); LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues ); - OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery ); + Set propertyNames = createPropertyNames( filterProperty ); + OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames ); assertThat( operatorFilter, is( nullValue() ) ); } @@ -56,7 +61,20 @@ public void testBuildRequestFilter_EmptyValues() { @Test public void testBuildRequestFilter_NullRequestFilter() { LayerQuery layerQuery = mockLayerQuery( null ); - OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery ); + Set propertyNames = createPropertyNames( "abc" ); + OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames ); + + assertThat( operatorFilter, is( nullValue() ) ); + } + + @Test + public void testBuildRequestFilter_PropertyNotKnown() { + String filterProperty = "type"; + List filterValues = Collections.singletonList( "one" ); + + LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues ); + Set propertyNames = createPropertyNames( "anothertype" ); + OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames ); assertThat( operatorFilter, is( nullValue() ) ); } @@ -73,4 +91,8 @@ private LayerQuery mockLayerQuery( Pair> requestFilter ) { return layerQueryMock; } + private Set createPropertyNames( String filterProperty ) { + return Collections.singleton( new QName( filterProperty ) ); + } + } \ No newline at end of file