← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19699: Analytics, user org unit. Using all org units linked to user instead of just one.

 

------------------------------------------------------------
revno: 19699
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-08-03 12:21:51 +0200
message:
  Analytics, user org unit. Using all org units linked to user instead of just one.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.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-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2015-07-13 12:34:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2015-08-03 10:21:51 +0000
@@ -28,13 +28,22 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
-import com.google.common.base.Joiner;
+import static org.hisp.dhis.common.NameableObjectUtils.getDisplayProperty;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.attribute.AttributeValue;
 import org.hisp.dhis.common.BaseIdentifiableObject;
@@ -56,22 +65,13 @@
 import org.hisp.dhis.schema.annotation.PropertyRange;
 import org.hisp.dhis.user.User;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import static org.hisp.dhis.common.NameableObjectUtils.getDisplayProperty;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import com.google.common.base.Joiner;
 
 /**
  * @author Kristian Nordal
@@ -98,8 +98,6 @@
 
     private static final List<String> FEATURETYPES = Arrays.asList( FEATURETYPE_NONE, FEATURETYPE_MULTIPOLYGON, FEATURETYPE_POLYGON, FEATURETYPE_POINT );
 
-    private static final Comparator<IdentifiableObject> COMPARATOR = new IdentifiableObjectNameComparator();
-
     private static final Pattern JSON_POINT_PATTERN = Pattern.compile( "(\\[.*?\\])" );
     private static final Pattern JSON_COORDINATE_PATTERN = Pattern.compile( "(\\[{3}.*?\\]{3})" );
     private static final Pattern COORDINATE_PATTERN = Pattern.compile( "([\\-0-9.]+,[\\-0-9.]+)" );
@@ -315,11 +313,35 @@
     {
         List<OrganisationUnit> sortedChildren = new ArrayList<>( children );
 
-        Collections.sort( sortedChildren, COMPARATOR );
+        Collections.sort( sortedChildren, IdentifiableObjectNameComparator.INSTANCE );
 
         return sortedChildren;
     }
 
+    public static List<OrganisationUnit> getSortedChildren( Collection<OrganisationUnit> units )
+    {
+        List<OrganisationUnit> children = new ArrayList<OrganisationUnit>();
+        
+        for ( OrganisationUnit unit : units )
+        {
+            children.addAll( unit.getSortedChildren() );
+        }
+        
+        return children;
+    }
+
+    public static List<OrganisationUnit> getSortedGrandChildren( Collection<OrganisationUnit> units )
+    {
+        List<OrganisationUnit> children = new ArrayList<OrganisationUnit>();
+        
+        for ( OrganisationUnit unit : units )
+        {
+            children.addAll( unit.getSortedGrandChildren() );
+        }
+        
+        return children;
+    }
+
     public Set<OrganisationUnit> getGrandChildren()
     {
         Set<OrganisationUnit> grandChildren = new HashSet<>();
@@ -788,7 +810,7 @@
 
         return map;
     }
-
+    
     public boolean hasLevel()
     {
         return level > 0;

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2015-07-15 16:04:35 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2015-08-03 10:21:51 +0000
@@ -33,6 +33,7 @@
 import static org.hisp.dhis.analytics.AnalyticsTableManager.COMPLETENESS_TARGET_TABLE_NAME;
 import static org.hisp.dhis.analytics.AnalyticsTableManager.ORGUNIT_TARGET_TABLE_NAME;
 import static org.hisp.dhis.analytics.DataQueryParams.COMPLETENESS_DIMENSION_TYPES;
+import static org.hisp.dhis.analytics.DataQueryParams.CO_INDEX;
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO;
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_CATEGORYOPTIONCOMBO;
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_DATA_X;
@@ -41,7 +42,6 @@
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT;
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_PERIOD;
 import static org.hisp.dhis.analytics.DataQueryParams.DX_INDEX;
-import static org.hisp.dhis.analytics.DataQueryParams.CO_INDEX;
 import static org.hisp.dhis.analytics.DataQueryParams.KEY_DE_GROUP;
 import static org.hisp.dhis.common.DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID;
 import static org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID;
@@ -1216,6 +1216,7 @@
         if ( ORGUNIT_DIM_ID.equals( dimension ) )
         {
             User user = currentUserService.getCurrentUser();
+            Set<OrganisationUnit> userOrgUnits = user != null ? user.getOrganisationUnits() : new HashSet<OrganisationUnit>();
 
             List<NameableObject> ous = new UniqueArrayList<>();
             List<Integer> levels = new UniqueArrayList<>();
@@ -1223,17 +1224,17 @@
 
             for ( String ou : items )
             {
-                if ( KEY_USER_ORGUNIT.equals( ou ) && user != null && user.hasOrganisationUnit() )
-                {
-                    ous.add( user.getOrganisationUnit() );
-                }
-                else if ( KEY_USER_ORGUNIT_CHILDREN.equals( ou ) && user != null && user.hasOrganisationUnit() )
-                {
-                    ous.addAll( user.getOrganisationUnit().getSortedChildren() );
-                }
-                else if ( KEY_USER_ORGUNIT_GRANDCHILDREN.equals( ou ) && user != null && user.hasOrganisationUnit() )
-                {
-                    ous.addAll( user.getOrganisationUnit().getSortedGrandChildren() );
+                if ( KEY_USER_ORGUNIT.equals( ou ) && !userOrgUnits.isEmpty() )
+                {
+                    ous.addAll( userOrgUnits );
+                }
+                else if ( KEY_USER_ORGUNIT_CHILDREN.equals( ou ) && !userOrgUnits.isEmpty() )
+                {
+                    ous.addAll( OrganisationUnit.getSortedChildren( userOrgUnits ) );
+                }
+                else if ( KEY_USER_ORGUNIT_GRANDCHILDREN.equals( ou ) && !userOrgUnits.isEmpty() )
+                {
+                    ous.addAll( OrganisationUnit.getSortedGrandChildren( userOrgUnits ) );
                 }
                 else if ( ou != null && ou.startsWith( KEY_LEVEL ) )
                 {