Skip to content

Commit

Permalink
deegree#235 - ignore filter if property is null
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoltz committed Dec 20, 2019
1 parent 23de8f2 commit 873b1a2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
Expand Down Expand Up @@ -129,13 +128,16 @@ public FeatureLayerData mapQuery( final LayerQuery query, List<String> 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<QName> 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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -133,22 +133,36 @@ private static LinkedList<Operator> findOperators( FeatureType ft, Envelope clic
return list;
}

static OperatorFilter buildRequestFilter( LayerQuery layerQuery ) {
static OperatorFilter appendRequestFilter( OperatorFilter filter, LayerQuery query, Set<QName> propertyNames ) {
OperatorFilter requestFilter = buildRequestFilter( query, propertyNames );
return Filters.and( filter, requestFilter );
}

static OperatorFilter buildRequestFilter( LayerQuery layerQuery, Set<QName> propertyNames ) {
Pair<String, List<String>> requestFilter = layerQuery.requestFilter();
if ( requestFilter == null )
return null;

List<ComparisonOperator> operators = createOperators( requestFilter );
List<ComparisonOperator> operators = createOperatorsIfPropertyIsKnown( requestFilter, propertyNames );
if ( operators.isEmpty() )
return null;
if ( operators.size() == 1 )
return new OperatorFilter( operators.get( 0 ) );
return new OperatorFilter( new Or( operators.toArray( new Operator[operators.size()] ) ) );
}

private static List<ComparisonOperator> createOperators( Pair<String, List<String>> requestFilter ) {
List<ComparisonOperator> operators = new ArrayList<ComparisonOperator>();
private static List<ComparisonOperator> createOperatorsIfPropertyIsKnown( Pair<String, List<String>> requestFilter,
Set<QName> propertyNames ) {
String filterProperty = requestFilter.getFirst();
if ( propertyIsknown( filterProperty, propertyNames ) ) {
return createOperatorsIfPropertyIsKnown( requestFilter, filterProperty );
}
return Collections.emptyList();
}

private static List<ComparisonOperator> createOperatorsIfPropertyIsKnown( Pair<String, List<String>> requestFilter,
String filterProperty ) {
List<ComparisonOperator> operators = new ArrayList<ComparisonOperator>();
List<String> filterValues = requestFilter.getSecond();
for ( String filterValue : filterValues ) {
Expression filterPropertyExpression = new ValueReference( new QName( filterProperty ) );
Expand All @@ -161,4 +175,12 @@ private static List<ComparisonOperator> createOperators( Pair<String, List<Strin
return operators;
}

}
private static boolean propertyIsknown( String filterProperty, Set<QName> propertyNames ) {
for ( QName propertyName : propertyNames ) {
if ( filterProperty.equals( propertyName.getLocalPart() ) )
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +30,8 @@ public void testBuildRequestFilter() {
List<String> filterValues = Collections.singletonList( "one" );

LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );
Set<QName> propertyNames = createPropertyNames( filterProperty );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames );

assertThat( operatorFilter.getOperator(), instanceOf( PropertyIsEqualTo.class ) );
}
Expand All @@ -38,7 +41,8 @@ public void testBuildRequestFilter_MultipleValues() {
String filterProperty = "type";
List<String> filterValues = Arrays.asList( new String[] { "one", "two" } );
LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );
Set<QName> propertyNames = createPropertyNames( filterProperty );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames );

assertThat( operatorFilter.getOperator(), instanceOf( Or.class ) );
}
Expand All @@ -48,15 +52,29 @@ public void testBuildRequestFilter_EmptyValues() {
String filterProperty = "type";
List<String> filterValues = Collections.emptyList();
LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );
Set<QName> propertyNames = createPropertyNames( filterProperty );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames );

assertThat( operatorFilter, is( nullValue() ) );
}

@Test
public void testBuildRequestFilter_NullRequestFilter() {
LayerQuery layerQuery = mockLayerQuery( null );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );
Set<QName> propertyNames = createPropertyNames( "abc" );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames );

assertThat( operatorFilter, is( nullValue() ) );
}

@Test
public void testBuildRequestFilter_PropertyNotKnown() {
String filterProperty = "type";
List<String> filterValues = Collections.singletonList( "one" );

LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues );
Set<QName> propertyNames = createPropertyNames( "anothertype" );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery, propertyNames );

assertThat( operatorFilter, is( nullValue() ) );
}
Expand All @@ -73,4 +91,8 @@ private LayerQuery mockLayerQuery( Pair<String, List<String>> requestFilter ) {
return layerQueryMock;
}

private Set<QName> createPropertyNames( String filterProperty ) {
return Collections.singleton( new QName( filterProperty ) );
}

}

0 comments on commit 873b1a2

Please sign in to comment.