=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/SetMap.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/SetMap.java 2013-10-15 12:45:25 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/SetMap.java 2014-03-17 18:16:56 +0000 @@ -30,7 +30,6 @@ import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Set; /** @@ -49,12 +48,11 @@ super( setMap ); } - public List putValue( T key, V value ) + public Set putValue( T key, V value ) { Set set = this.get( key ); set = set == null ? new HashSet() : set; set.add( value ); - super.put( key, set ); - return null; + return super.put( key, set ); } } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java 2014-03-17 16:54:54 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java 2014-03-17 18:16:56 +0000 @@ -34,6 +34,7 @@ import java.util.Set; import org.hisp.dhis.common.QueryItem; +import org.hisp.dhis.common.SetMap; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.Program; @@ -77,19 +78,18 @@ // ------------------------------------------------------------------------- /** - * Indicates whether this params has any items with filter. + * Returns a mapping between level and organisation units. */ - public boolean hasItemsWithFilter() - { - for ( QueryItem item : items ) + public SetMap getLevelOrgUnitMap() + { + SetMap setMap = new SetMap(); + + for ( OrganisationUnit ou : organisationUnits ) { - if ( item.hasFilter() ) - { - return true; - } + setMap.putValue( ou.getLevel(), ou ); } - return false; + return setMap; } /** @@ -113,7 +113,7 @@ */ public boolean isOrganisationUnitMode( String mode ) { - return organisationUnitMode != null && organisationUnitMode.equals( mode ); + return organisationUnitMode != null && organisationUnitMode.equalsIgnoreCase( mode ); } /** === modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java' --- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java 2014-03-17 17:34:56 +0000 +++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java 2014-03-17 18:16:56 +0000 @@ -194,6 +194,8 @@ throw new IllegalQueryException( "Organisation unit does not exist: " + orgUnit ); } + organisationUnit.setLevel( organisationUnitService.getLevelOfOrganisationUnit( organisationUnit.getId() ) ); + params.getOrganisationUnits().add( organisationUnit ); } === modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java' --- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java 2014-03-17 17:39:08 +0000 +++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java 2014-03-17 18:16:56 +0000 @@ -62,6 +62,7 @@ import org.hisp.dhis.common.DimensionalObject; import org.hisp.dhis.common.Grid; import org.hisp.dhis.common.QueryItem; +import org.hisp.dhis.common.SetMap; import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore; import org.hisp.dhis.i18n.I18nFormat; import org.hisp.dhis.jdbc.StatementBuilder; @@ -141,7 +142,7 @@ sql += col + ".value as " + col + ", "; } - sql = sql.substring( 0, sql.length() - 2 ); // Remove last comma + sql = sql.substring( 0, sql.length() - 2 ) + " "; // Remove last comma // --------------------------------------------------------------------- // From, join and restriction clause @@ -165,21 +166,32 @@ if ( item.hasFilter() ) { - sql += "and " + col + ".value " + item.getSqlOperator() + " " + item.getSqlFilter( filter ); + sql += "and " + col + ".value " + item.getSqlOperator() + " " + item.getSqlFilter( filter ) + " "; } } - if ( !params.isOrganisationUnitMode( DimensionalObject.OU_MODE_SELECTED ) ) + if ( params.isOrganisationUnitMode( DimensionalObject.OU_MODE_DESCENDANTS ) ) { - sql += "left join _orgunitstructure ous using tei.organisationunitid=ous.organisationunitid "; + sql += "left join _orgunitstructure ous on tei.organisationunitid = ous.organisationunitid "; } if ( params.hasTrackedEntity() ) { sql += hlp.whereAnd() + " tei.trackedentityid = " + params.getTrackedEntity().getId(); } - - if ( params.isOrganisationUnitMode( DimensionalObject.OU_MODE_SELECTED ) ) + + if ( params.isOrganisationUnitMode( DimensionalObject.OU_MODE_DESCENDANTS ) ) + { + SetMap levelOuMap = params.getLevelOrgUnitMap(); + + for ( Integer level : levelOuMap.keySet() ) + { + sql += hlp.whereAnd() + " ous.idlevel" + level + " in (" + getCommaDelimitedString( getIdentifiers( levelOuMap.get( level ) ) ) + ") or "; + } + + sql = sql.substring( 0, sql.length() - 3 ); // Reomove last or + } + else // SELECTED { sql += hlp.whereAnd() + " tei.organisationunitid in (" + getCommaDelimitedString( getIdentifiers( params.getOrganisationUnits() ) ) + ") "; } === modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java 2014-03-16 23:07:04 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java 2014-03-17 18:16:56 +0000 @@ -42,8 +42,7 @@ Map map = this.get( key ); map = map == null ? new HashMap() : map; map.put( valueKey, value ); - this.put( key, map ); - return null; + return this.put( key, map ); } public void putEntries( T key, Map m )