Skip to content

Commit

Permalink
deegree#228 - create filter from request parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoltz committed Jan 19, 2017
1 parent 94ac052 commit 2d6f7e3
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 5 deletions.
8 changes: 8 additions & 0 deletions deegree-layers/deegree-layers-feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
<artifactId>deegree-featurestore-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>

</project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,24 @@ Occam Labs UG (haftungsbeschränkt)
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;
import org.deegree.commons.utils.Pair;
import org.deegree.feature.types.FeatureType;
import org.deegree.feature.types.property.GeometryPropertyType;
import org.deegree.filter.Expression;
import org.deegree.filter.Filters;
import org.deegree.filter.MatchAction;
import org.deegree.filter.Operator;
import org.deegree.filter.OperatorFilter;
import org.deegree.filter.comparison.ComparisonOperator;
import org.deegree.filter.comparison.PropertyIsEqualTo;
import org.deegree.filter.expression.Literal;
import org.deegree.filter.expression.ValueReference;
import org.deegree.filter.logical.And;
import org.deegree.filter.logical.Or;
Expand All @@ -63,24 +71,28 @@ Occam Labs UG (haftungsbeschränkt)
import org.deegree.style.se.unevaluated.Style;
import org.deegree.style.utils.Styles;

import javax.xml.namespace.QName;

/**
* Responsible for building feature layer filters.
*
*
* @author <a href="mailto:schmitz@occamlabs.de">Andreas Schmitz</a>
* @author last edited by: $Author: stranger $
*
* @version $Revision: $, $Date: $
*/
class FilterBuilder {

static OperatorFilter buildFilterForMap(OperatorFilter filter, Style style, LayerQuery query, DimensionFilterBuilder dimFilterBuilder, List<String> headers) throws OWSException{
static OperatorFilter buildFilterForMap( OperatorFilter filter, Style style, LayerQuery query,
DimensionFilterBuilder dimFilterBuilder, List<String> headers )
throws OWSException {
style = style.filter( query.getScale() );
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;
}

static OperatorFilter buildFilter( Operator operator, FeatureType ft, Envelope clickBox ) {
if ( ft == null ) {
if ( operator == null ) {
Expand Down Expand Up @@ -113,11 +125,40 @@ private static LinkedList<Operator> findOperators( FeatureType ft, Envelope clic
LinkedList<Operator> list = new LinkedList<Operator>();
for ( PropertyType pt : ft.getPropertyDeclarations() ) {
if ( pt instanceof GeometryPropertyType
&& ( ( (GeometryPropertyType) pt ).getCoordinateDimension() == DIM_2 || ( (GeometryPropertyType) pt ).getCoordinateDimension() == DIM_2_OR_3 ) ) {
&& ( ( (GeometryPropertyType) pt ).getCoordinateDimension() == DIM_2 ||
( (GeometryPropertyType) pt ).getCoordinateDimension() == DIM_2_OR_3 ) ) {
list.add( new Intersects( new ValueReference( pt.getName() ), clickBox ) );
}
}
return list;
}

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

List<ComparisonOperator> operators = createOperators( requestFilter );
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>();
String filterProperty = requestFilter.getFirst();
List<String> filterValues = requestFilter.getSecond();
for ( String filterValue : filterValues ) {
Expression filterPropertyExpression = new ValueReference( new QName( filterProperty ) );
Expression filterValueExpression = new Literal<PrimitiveValue>( filterValue );
PropertyIsEqualTo isEqualTo = new PropertyIsEqualTo( filterPropertyExpression,
filterValueExpression, false,
MatchAction.ALL );
operators.add( isEqualTo );
}
return operators;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.deegree.layer.persistence.feature;

import org.deegree.commons.utils.Pair;
import org.deegree.filter.OperatorFilter;
import org.deegree.filter.comparison.PropertyIsEqualTo;
import org.deegree.filter.logical.Or;
import org.deegree.layer.LayerQuery;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

/**
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public class FilterBuilderTest {

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

LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );

assertThat( operatorFilter.getOperator(), instanceOf( PropertyIsEqualTo.class ) );
}

@Test
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 );

assertThat( operatorFilter.getOperator(), instanceOf( Or.class ) );
}

@Test
public void testBuildRequestFilter_EmptyValues() {
String filterProperty = "type";
List<String> filterValues = Collections.emptyList();
LayerQuery layerQuery = mockLayerQuery( filterProperty, filterValues );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );

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

@Test
public void testBuildRequestFilter_NullRequestFilter() {
LayerQuery layerQuery = mockLayerQuery( null );
OperatorFilter operatorFilter = FilterBuilder.buildRequestFilter( layerQuery );

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

private LayerQuery mockLayerQuery( String filterProperty, List<String> filterValues ) {
Pair<String, List<String>> requestFilter = new Pair<String, List<String>>( filterProperty, filterValues );

return mockLayerQuery( requestFilter );
}

private LayerQuery mockLayerQuery( Pair<String, List<String>> requestFilter ) {
LayerQuery layerQueryMock = Mockito.mock( LayerQuery.class );
Mockito.when( layerQueryMock.requestFilter() ).thenReturn( requestFilter );
return layerQueryMock;
}

}

0 comments on commit 2d6f7e3

Please sign in to comment.