=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Criteria.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Criteria.java 2015-11-05 03:31:11 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Criteria.java 2015-11-05 05:04:18 +0000 @@ -70,12 +70,6 @@ continue; } - if ( (restriction.getOperator().getMax() != null && restriction.getParameters().size() > restriction.getOperator().getMax()) - || (restriction.getOperator().getMin() != null && restriction.getParameters().size() < restriction.getOperator().getMin()) ) - { - continue; - } - this.criterions.add( restriction ); } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java 2015-11-04 03:03:17 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java 2015-11-05 05:04:18 +0000 @@ -39,7 +39,6 @@ import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -212,7 +211,6 @@ } } - // TODO verify parameters length private Criterion getHibernateCriterion( Schema schema, Restriction restriction ) { if ( restriction == null || restriction.getOperator() == null ) @@ -222,67 +220,7 @@ Property property = schema.getProperty( restriction.getPath() ); - List parameters = new ArrayList<>(); - - for ( Object parameter : restriction.getParameters() ) - { - parameters.add( QueryUtils.getValue( property.getKlass(), parameter ) ); - } - - switch ( restriction.getOperator() ) - { - case EQ: - { - return Restrictions.eq( property.getFieldName(), parameters.get( 0 ) ); - } - case NE: - { - return Restrictions.ne( property.getFieldName(), parameters.get( 0 ) ); - } - case GT: - { - return Restrictions.gt( property.getFieldName(), parameters.get( 0 ) ); - } - case LT: - { - return Restrictions.lt( property.getFieldName(), parameters.get( 0 ) ); - } - case GE: - { - return Restrictions.ge( property.getFieldName(), parameters.get( 0 ) ); - } - case LE: - { - return Restrictions.le( property.getFieldName(), parameters.get( 0 ) ); - } - case BETWEEN: - { - return Restrictions.between( property.getFieldName(), parameters.get( 0 ), parameters.get( 1 ) ); - } - case LIKE: - { - return Restrictions.like( property.getFieldName(), parameters.get( 0 ) ); - } - case ILIKE: - { - return Restrictions.ilike( property.getFieldName(), parameters.get( 0 ) ); - } - case IN: - { - if ( !Collection.class.isInstance( parameters.get( 0 ) ) || ((Collection) parameters.get( 0 )).isEmpty() ) - { - return null; - } - - return Restrictions.in( property.getFieldName(), (Collection) parameters.get( 0 ) ); - } - case NULL: - { - return Restrictions.isNull( property.getFieldName() ); - } - } - - return null; + return restriction.getOperator().getHibernateCriterion( property.getFieldName() ); } public org.hibernate.criterion.Order getHibernateOrder( Order order ) === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java 2015-11-04 03:03:17 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java 2015-11-05 05:04:18 +0000 @@ -210,11 +210,11 @@ } case "like": { - return Restrictions.like( split[0], "%" + value + "%" ); + return Restrictions.like( split[0], value ); } case "ilike": { - return Restrictions.ilike( split[0], "%" + value + "%" ); + return Restrictions.ilike( split[0], value ); } case "in": { === removed file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Operator.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Operator.java 2015-11-05 03:31:11 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Operator.java 1970-01-01 00:00:00 +0000 @@ -1,117 +0,0 @@ -package org.hisp.dhis.query; - -/* - * Copyright (c) 2004-2015, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import org.hisp.dhis.schema.Klass; - -import java.util.Date; - -/** - * @author Morten Olav Hansen - */ -public enum Operator -{ - EQ( Typed.from( String.class, Boolean.class, Number.class, Date.class ), 1 ), - NE( Typed.from( String.class, Boolean.class, Number.class, Date.class ), 1 ), - GT( Typed.from( String.class, Boolean.class, Number.class, Date.class ), 1 ), - LT( Typed.from( String.class, Boolean.class, Number.class, Date.class ), 1 ), - GE( Typed.from( String.class, Boolean.class, Number.class, Date.class ), 1 ), - LE( Typed.from( String.class, Boolean.class, Number.class, Date.class ), 1 ), - NULL( Typed.from( String.class, Boolean.class, Number.class, Date.class ) ), - BETWEEN( Typed.from( String.class, Number.class, Date.class ), 2 ), - LIKE( Typed.from( String.class ), 1 ), - ILIKE( Typed.from( String.class ), 1 ), - IN( 1, Integer.MAX_VALUE ); - - Integer min; - - Integer max; - - // default is to allow all types - Typed typed = Typed.from(); - - Operator() - { - this.min = null; - this.max = null; - } - - Operator( Typed typed ) - { - this.typed = typed; - this.min = null; - this.max = null; - } - - Operator( int value ) - { - this.min = value; - this.max = value; - } - - Operator( Typed typed, int value ) - { - this.typed = typed; - this.min = value; - this.max = value; - } - - Operator( int min, int max ) - { - this.min = min; - this.max = max; - } - - Operator( Typed typed, int min, int max ) - { - this.typed = typed; - this.min = min; - this.max = max; - } - - public Integer getMin() - { - return min; - } - - public Integer getMax() - { - return max; - } - - public boolean isValid( Klass klass ) - { - return typed.isValid( klass ); - } - - public boolean isValid( Class klass ) - { - return typed.isValid( klass ); - } -} === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java 2015-11-05 03:38:05 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java 2015-11-05 05:04:18 +0000 @@ -33,10 +33,8 @@ import com.google.common.collect.Lists; import org.hisp.dhis.system.util.DateUtils; -import java.util.ArrayList; import java.util.Collection; import java.util.Date; -import java.util.List; /** * @author Morten Olav Hansen === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Restriction.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Restriction.java 2015-11-05 03:31:11 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Restriction.java 2015-11-05 05:04:18 +0000 @@ -29,9 +29,7 @@ */ import com.google.common.base.MoreObjects; - -import java.util.ArrayList; -import java.util.List; +import org.hisp.dhis.query.operators.Operator; /** * @author Morten Olav Hansen @@ -48,20 +46,10 @@ */ private Operator operator; - /** - * 0..* parameters as required by operator. - */ - private List parameters = new ArrayList<>(); - - public Restriction( String path, Operator operator, Object... parameters ) + public Restriction( String path, Operator operator ) { this.path = path; this.operator = operator; - - for ( Object object : parameters ) - { - addParameter( object ); - } } public String getPath() @@ -69,55 +57,17 @@ return path; } - public void setPath( String path ) - { - this.path = path; - } - public Operator getOperator() { return operator; } - public void setOperator( Operator operator ) - { - this.operator = operator; - } - - public List getParameters() - { - return parameters; - } - - public Object getParameter( int idx ) - { - if ( parameters.size() < idx ) - { - return null; - } - - return parameters.get( idx ); - } - - public void setParameters( List parameters ) - { - this.parameters = parameters; - } - - // Builder - public Restriction addParameter( Object parameter ) - { - this.parameters.add( ( parameter ) ); - return this; - } - @Override public String toString() { return MoreObjects.toStringHelper( this ) .add( "path", path ) .add( "operator", operator ) - .add( "parameters", parameters ) .toString(); } } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Restrictions.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Restrictions.java 2015-11-05 03:31:11 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Restrictions.java 2015-11-05 05:04:18 +0000 @@ -28,6 +28,18 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hisp.dhis.query.operators.BetweenOperator; +import org.hisp.dhis.query.operators.ContainsOperator; +import org.hisp.dhis.query.operators.EqualOperator; +import org.hisp.dhis.query.operators.GreaterEqualOperator; +import org.hisp.dhis.query.operators.GreaterThanOperator; +import org.hisp.dhis.query.operators.InOperator; +import org.hisp.dhis.query.operators.LessEqualOperator; +import org.hisp.dhis.query.operators.LessThanOperator; +import org.hisp.dhis.query.operators.NotEqualOperator; +import org.hisp.dhis.query.operators.NotNullOperator; +import org.hisp.dhis.query.operators.NullOperator; + import java.util.Collection; /** @@ -37,63 +49,63 @@ { public static Restriction eq( String path, Object value ) { - return new Restriction( path, Operator.EQ, value ); + return new Restriction( path, new EqualOperator( value ) ); } public static Restriction ne( String path, Object value ) { - return new Restriction( path, Operator.NE, value ); + return new Restriction( path, new NotEqualOperator( value ) ); } public static Restriction gt( String path, Object value ) { - return new Restriction( path, Operator.GT, value ); + return new Restriction( path, new GreaterThanOperator( value ) ); } public static Restriction lt( String path, Object value ) { - return new Restriction( path, Operator.LT, value ); + return new Restriction( path, new LessThanOperator( value ) ); } public static Restriction ge( String path, Object value ) { - return new Restriction( path, Operator.GE, value ); + return new Restriction( path, new GreaterEqualOperator( value ) ); } public static Restriction le( String path, Object value ) { - return new Restriction( path, Operator.LE, value ); + return new Restriction( path, new LessEqualOperator( value ) ); } public static Restriction between( String path, Object lside, Object rside ) { - return new Restriction( path, Operator.BETWEEN, lside, rside ); + return new Restriction( path, new BetweenOperator( lside, rside ) ); } // Map like to ilike for the moment, since like in the web-api is actually ilike.. public static Restriction like( String path, Object value ) { - return new Restriction( path, Operator.ILIKE, value ); + return new Restriction( path, new ContainsOperator( value, false ) ); } public static Restriction ilike( String path, Object value ) { - return new Restriction( path, Operator.ILIKE, value ); - } - - public static Restriction in( String path, Object... values ) - { - return new Restriction( path, Operator.IN, values ); + return new Restriction( path, new ContainsOperator( value, false ) ); } public static Restriction in( String path, Collection values ) { - return new Restriction( path, Operator.IN, values ); + return new Restriction( path, new InOperator( values ) ); } public static Restriction isNull( String path ) { - return new Restriction( path, Operator.NULL ); + return new Restriction( path, new NullOperator() ); + } + + public static Restriction isNotNull( String path ) + { + return new Restriction( path, new NotNullOperator() ); } private Restrictions() === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java 2015-11-05 03:55:41 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java 2015-11-05 05:04:18 +0000 @@ -48,7 +48,7 @@ @Override public Criterion getHibernateCriterion( String propertyName ) { - return Restrictions.in( propertyName, getValue( Collection.class, args ) ); + return Restrictions.in( propertyName, getValue( Collection.class, args.get( 0 ) ) ); } @Override === modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryTest.java 2015-11-05 03:31:11 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryTest.java 2015-11-05 05:04:18 +0000 @@ -35,7 +35,7 @@ import java.util.Date; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Morten Olav Hansen @@ -80,41 +80,4 @@ assertEquals( 3, query.getCriterions().size() ); } - - @Test - public void invalidRestrictionParameters() - { - Query query = Query.from( createSchema() ); - query.add( new Restriction( "id", Operator.EQ ) ); - query.add( new Restriction( "id", Operator.EQ, 1, 2 ) ); - query.add( new Restriction( "name", Operator.EQ ) ); - query.add( new Restriction( "name", Operator.EQ, 1, 2 ) ); - query.add( new Restriction( "code", Operator.EQ ) ); - query.add( new Restriction( "code", Operator.EQ, 1, 2, 3, 4, 5 ) ); - query.add( new Restriction( "lastUpdated", Operator.BETWEEN, new Date() ) ); - query.add( new Restriction( "lastUpdated", Operator.BETWEEN, new Date(), 1, 2, 3, 4 ) ); - - assertEquals( 0, query.getCriterions().size() ); - } - - @Test - public void operatorValid() - { - Schema schema = createSchema(); - - assertTrue( Operator.EQ.isValid( schema.getProperty( "id" ) ) ); - assertTrue( Operator.GE.isValid( schema.getProperty( "id" ) ) ); - assertTrue( Operator.LT.isValid( schema.getProperty( "id" ) ) ); - assertTrue( Operator.GE.isValid( schema.getProperty( "id" ) ) ); - assertTrue( Operator.LE.isValid( schema.getProperty( "id" ) ) ); - assertTrue( Operator.BETWEEN.isValid( schema.getProperty( "id" ) ) ); - assertTrue( Operator.LIKE.isValid( schema.getProperty( "id" ) ) ); - - assertTrue( Operator.EQ.isValid( schema.getProperty( "int" ) ) ); - assertTrue( Operator.EQ.isValid( schema.getProperty( "long" ) ) ); - assertTrue( Operator.EQ.isValid( schema.getProperty( "float" ) ) ); - assertTrue( Operator.EQ.isValid( schema.getProperty( "double" ) ) ); - - assertFalse( Operator.LIKE.isValid( schema.getProperty( "lastUpdated" ) ) ); - } }