dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #41221
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20988: support for in-memory sorting + tests
------------------------------------------------------------
revno: 20988
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-11-09 14:41:17 +0700
message:
support for in-memory sorting + tests
modified:
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java 2015-11-09 05:05:13 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java 2015-11-09 07:41:17 +0000
@@ -31,9 +31,9 @@
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.PagerUtils;
import org.hisp.dhis.schema.Property;
-import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.system.util.ReflectionUtils;
+import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -47,6 +47,7 @@
{
validateQuery( query );
List<T> list = runQuery( query );
+ list = runSorter( query, list );
return PagerUtils.pageCollection( list, query.getFirstResult(), query.getMaxResults() );
}
@@ -77,17 +78,34 @@
private List<T> runQuery( Query query )
{
return query.getObjects().stream()
- .filter( object -> test( query.getSchema(), (T) object, query.getCriterions() ) )
+ .filter( object -> test( query, (T) object ) )
.map( object -> (T) object )
.collect( Collectors.toList() );
}
- private boolean test( Schema schema, T object, List<Criterion> criterions )
- {
- for ( Criterion criterion : criterions )
+ private List<T> runSorter( Query query, List<T> objects )
+ {
+ List<T> sorted = new ArrayList<>( objects );
+
+ sorted.sort( ( o1, o2 ) -> {
+ for ( Order order : query.getOrders() )
+ {
+ int result = order.compare( o1, o2 );
+ if ( result != 0 ) return result;
+ }
+
+ return 0;
+ } );
+
+ return sorted;
+ }
+
+ private boolean test( Query query, T object )
+ {
+ for ( Criterion criterion : query.getCriterions() )
{
Restriction restriction = (Restriction) criterion;
- Object value = getValue( schema, object, restriction.getPath() );
+ Object value = getValue( query, object, restriction.getPath() );
if ( !restriction.getOperator().test( value ) )
{
@@ -98,10 +116,9 @@
return true;
}
- private Object getValue( Schema schema, Object object, String path )
+ private Object getValue( Query query, Object object, String path )
{
- Property property = schema.getProperty( path );
-
+ Property property = query.getSchema().getProperty( path );
return ReflectionUtils.invokeMethod( object, property.getGetterMethod() );
}
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java 2015-11-05 03:31:11 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java 2015-11-09 07:41:17 +0000
@@ -30,7 +30,9 @@
import com.google.common.base.MoreObjects;
import org.hisp.dhis.schema.Property;
+import org.hisp.dhis.system.util.ReflectionUtils;
+import java.util.Date;
import java.util.Objects;
/**
@@ -71,6 +73,35 @@
return property;
}
+ public int compare( Object lside, Object rside )
+ {
+ Object o1 = ReflectionUtils.invokeMethod( lside, property.getGetterMethod() );
+ Object o2 = ReflectionUtils.invokeMethod( rside, property.getGetterMethod() );
+
+ if ( String.class.isInstance( o1 ) && String.class.isInstance( o2 ) )
+ {
+ return ascending ? ((String) o1).compareTo( (String) o2 ) : ((String) o2).compareTo( (String) o1 );
+ }
+ else if ( Integer.class.isInstance( o1 ) && Integer.class.isInstance( o2 ) )
+ {
+ return ascending ? ((Integer) o1).compareTo( (Integer) o2 ) : ((Integer) o2).compareTo( (Integer) o1 );
+ }
+ else if ( Float.class.isInstance( o1 ) && Float.class.isInstance( o2 ) )
+ {
+ return ascending ? ((Float) o1).compareTo( (Float) o2 ) : ((Float) o2).compareTo( (Float) o1 );
+ }
+ else if ( Double.class.isInstance( o1 ) && Double.class.isInstance( o2 ) )
+ {
+ return ascending ? ((Double) o1).compareTo( (Double) o2 ) : ((Double) o2).compareTo( (Double) o1 );
+ }
+ else if ( Date.class.isInstance( o1 ) && Date.class.isInstance( o2 ) )
+ {
+ return ascending ? ((Date) o1).compareTo( (Date) o2 ) : ((Date) o2).compareTo( (Date) o1 );
+ }
+
+ return 0;
+ }
+
public static Order asc( Property property )
{
return new Order( property, true );
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java 2015-11-09 07:08:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java 2015-11-09 07:41:17 +0000
@@ -406,7 +406,6 @@
}
@Test
- @Ignore
public void sortNameDesc()
{
Schema schema = schemaService.getDynamicSchema( DataElement.class );
@@ -427,7 +426,6 @@
}
@Test
- @Ignore
public void sortNameAsc()
{
Schema schema = schemaService.getDynamicSchema( DataElement.class );
@@ -448,7 +446,6 @@
}
@Test
- @Ignore
public void sortCreatedDesc()
{
Schema schema = schemaService.getDynamicSchema( DataElement.class );
@@ -469,7 +466,6 @@
}
@Test
- @Ignore
public void sortCreatedAsc()
{
Schema schema = schemaService.getDynamicSchema( DataElement.class );