← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4205: Data entry: loading organisation unit - dataset associations at initial page load. No server roun...

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 4205 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2011-07-25 17:49:11 +0200
message:
  Data entry: loading organisation unit - dataset associations at initial page load. No server round-trip required for the 'select orgunit - show data sets' step.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDataSetAssociationSet.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/hibernate/HibernateIndicatorStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/PageInitAction.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm


--
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	2011-07-13 18:16:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2011-07-25 08:52:10 +0000
@@ -104,14 +104,14 @@
     
     private String phoneNumber;
 
+    private Boolean hasPatients;
+    
     private transient int level;
     
     private transient boolean currentParent;
     
     private transient String type;
     
-    private Boolean hasPatients;
-    
     // -------------------------------------------------------------------------
     // Constructors
     // -------------------------------------------------------------------------
@@ -720,5 +720,4 @@
     {
         this.type = type;
     }
-
 }

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDataSetAssociationSet.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDataSetAssociationSet.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDataSetAssociationSet.java	2011-07-25 09:10:33 +0000
@@ -0,0 +1,68 @@
+package org.hisp.dhis.organisationunit;
+
+/*
+ * Copyright (c) 2004-2010, 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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class OrganisationUnitDataSetAssociationSet
+{
+    private List<Set<Integer>> dataSetAssociationSets = new ArrayList<Set<Integer>>();
+    
+    private Map<Integer, Integer> organisationUnitAssociationSetMap = new HashMap<Integer, Integer>();
+
+    public OrganisationUnitDataSetAssociationSet()
+    {
+    }
+
+    public List<Set<Integer>> getDataSetAssociationSets()
+    {
+        return dataSetAssociationSets;
+    }
+
+    public void setDataSetAssociationSets( List<Set<Integer>> dataSetAssociationSets )
+    {
+        this.dataSetAssociationSets = dataSetAssociationSets;
+    }
+
+    public Map<Integer, Integer> getOrganisationUnitAssociationSetMap()
+    {
+        return organisationUnitAssociationSetMap;
+    }
+
+    public void setOrganisationUnitAssociationSetMap( Map<Integer, Integer> organisationUnitAssociationSetMap )
+    {
+        this.organisationUnitAssociationSetMap = organisationUnitAssociationSetMap;
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2011-04-26 17:29:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2011-07-22 20:43:53 +0000
@@ -240,6 +240,8 @@
      * @return a collection of organisation units.
      */
     Collection<OrganisationUnit> getOrganisationUnitsByNameAndGroups( String name, Collection<OrganisationUnitGroup> groups, OrganisationUnit parent, boolean limit );
+
+    OrganisationUnitDataSetAssociationSet getOrganisationUnitDataSetAssociationSet();
     
     // -------------------------------------------------------------------------
     // OrganisationUnitHierarchy

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2011-04-23 18:52:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2011-07-25 08:52:10 +0000
@@ -28,6 +28,8 @@
  */
 
 import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
 
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
 
@@ -91,6 +93,8 @@
      */
     void updateOrganisationUnitParent( int organisationUnitId, int parentId );
     
+    Map<Integer, Set<Integer>> getOrganisationUnitDataSetAssocationMap();
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitLevel
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java	2011-07-14 07:22:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java	2011-07-22 20:43:53 +0000
@@ -89,6 +89,24 @@
     }
     
     /**
+     * Indicates whether this user credentials is a super user, implying that the
+     * ALL authority is present in at least one of the user authority groups of
+     * this user credentials.
+     */
+    public boolean isSuper()
+    {
+        for ( UserAuthorityGroup group : userAuthorityGroups )
+        {
+            if ( group.getAuthorities().contains( UserAuthorityGroup.AUTHORITY_ALL ) )
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+    
+    /**
      * Returns a set of the aggregated data sets for all user authority groups
      * of this user credentials.
      */

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java	2011-07-22 09:17:04 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java	2011-07-22 20:43:53 +0000
@@ -38,7 +38,6 @@
 import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
@@ -165,16 +164,9 @@
         return (DataElement) criteria.uniqueResult();
     }
 
-    @SuppressWarnings( "unchecked" )
     public Collection<DataElement> getAllDataElements()
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.setCacheable( true );
-        criteria.addOrder( Order.asc( "name" ) );
-
-        return criteria.list();
+        return getAll();
     }
 
     @SuppressWarnings( "unchecked" )
@@ -252,11 +244,9 @@
     @SuppressWarnings( "unchecked" )
     public Collection<DataElement> getDataElementsWithGroupSets()
     {
-        final String sql = "from DataElement d where d.groupSets.size > 0";
-
-        Query query = sessionFactory.getCurrentSession().createQuery( sql );
-
-        return query.list();
+        String hql = "from DataElement d where d.groupSets.size > 0";
+
+        return getQuery( hql ).list();
     }
 
     public void setZeroIsSignificantForDataElements( Collection<Integer> dataElementIds )
@@ -283,9 +273,7 @@
     @SuppressWarnings( "unchecked" )
     public Collection<DataElement> getDataElementsByZeroIsSignificant( boolean zeroIsSignificant )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "zeroIsSignificant", zeroIsSignificant ) );
         criteria.add( Restrictions.eq( "type", DataElement.VALUE_TYPE_INT ) );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java	2011-07-21 20:11:07 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java	2011-07-22 20:43:53 +0000
@@ -271,12 +271,6 @@
                     return "Category option combo with id: " + optionComboId + " does not exist";
                 }
 
-                String dataElementValueType = dataElement.getDetailedNumberType();
-
-                // -------------------------------------------------------------
-                // Insert data value for data element in output code for boolean
-                // -------------------------------------------------------------
-
                 if ( dataElement.getType().equals( DataElement.VALUE_TYPE_BOOL ) )
                 {
                     inputHtml = inputHtml.replace( "input", "select" );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java	2011-04-22 17:02:27 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java	2011-07-22 20:43:53 +0000
@@ -158,12 +158,9 @@
         return (DataSet) criteria.uniqueResult();
     }
 
-    @SuppressWarnings( "unchecked" )
     public Collection<DataSet> getAllDataSets()
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        return session.createCriteria( DataSet.class ).list();
+        return getAll();
     }
 
     @SuppressWarnings( "unchecked" )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/hibernate/HibernateIndicatorStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/hibernate/HibernateIndicatorStore.java	2011-07-14 10:31:16 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/hibernate/HibernateIndicatorStore.java	2011-07-22 20:43:53 +0000
@@ -147,7 +147,7 @@
     {
         final String hql = "from Indicator d where d.groupSets.size > 0";
 
-        return sessionFactory.getCurrentSession().createQuery( hql ).list();
+        return getQuery( hql ).list();
     }
 
     @SuppressWarnings( "unchecked" )
@@ -155,7 +155,7 @@
     {
         final String hql = "from Indicator d where d.groups.size = 0";
 
-        return sessionFactory.getCurrentSession().createQuery( hql ).list();
+        return getQuery( hql ).list();
     }
 
     @SuppressWarnings( "unchecked" )
@@ -163,7 +163,7 @@
     {
         final String hql = "from Indicator d where d.dataSets.size > 0";
 
-        return sessionFactory.getCurrentSession().createQuery( hql ).list();
+        return getQuery( hql ).list();
     }
 
     public int getIndicatorCount()

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2011-04-30 09:28:32 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2011-07-25 09:10:33 +0000
@@ -40,13 +40,16 @@
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.hierarchy.HierarchyViolationException;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitLevelComparator;
 import org.hisp.dhis.system.util.AuditLogUtil;
+import org.hisp.dhis.system.util.ConversionUtils;
 import org.hisp.dhis.system.util.Filter;
 import org.hisp.dhis.system.util.FilterUtils;
 import org.hisp.dhis.system.util.UUIdUtils;
 import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -418,6 +421,47 @@
         return limit && intersection != null && intersection.size() > MAX_LIMIT ? intersection.subList( 0, MAX_LIMIT ) : intersection;   
     }
     
+    public OrganisationUnitDataSetAssociationSet getOrganisationUnitDataSetAssociationSet()
+    {
+        //TODO hierarchy ?
+        
+        Map<Integer, Set<Integer>> associationSet = organisationUnitStore.getOrganisationUnitDataSetAssocationMap();
+        
+        filterOrganisationUnitSortedDataSets( associationSet );
+        
+        OrganisationUnitDataSetAssociationSet set = new OrganisationUnitDataSetAssociationSet();
+        
+        for ( Map.Entry<Integer, Set<Integer>> entry : associationSet.entrySet() )
+        {
+            int index = set.getDataSetAssociationSets().indexOf( entry.getValue() );
+            
+            if ( index == -1 ) // Association set does not exist, add new
+            {
+                index = set.getDataSetAssociationSets().size();
+                set.getDataSetAssociationSets().add( entry.getValue() );
+            }
+            
+            set.getOrganisationUnitAssociationSetMap().put( entry.getKey(), index );
+        }
+        
+        return set;
+    }
+    
+    private void filterOrganisationUnitSortedDataSets( Map<Integer, Set<Integer>> associationMap )
+    {
+        User currentUser = currentUserService.getCurrentUser();
+        
+        if ( currentUser != null && !currentUser.getUserCredentials().isSuper() )
+        {
+            Collection<Integer> userDataSets = ConversionUtils.getIdentifiers( DataSet.class, currentUser.getUserCredentials().getAllDataSets() );
+            
+            for ( Set<Integer> dataSets : associationMap.values() )
+            {
+                dataSets.retainAll( userDataSets );
+            }
+        }
+    }
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitHierarchy
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2011-05-11 01:04:56 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2011-07-25 08:52:10 +0000
@@ -27,8 +27,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
 import org.amplecode.quick.StatementHolder;
 import org.amplecode.quick.StatementManager;
@@ -47,6 +52,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitStore;
 import org.hisp.dhis.system.objectmapper.OrganisationUnitRelationshipRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowCallbackHandler;
 
 /**
  * @author Kristian Nordal
@@ -156,6 +162,34 @@
 
         return query.list();
     }
+        
+    public Map<Integer, Set<Integer>> getOrganisationUnitDataSetAssocationMap()
+    {
+        final String sql = "select datasetid, sourceid from datasetsource";
+        
+        final Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
+        
+        jdbcTemplate.query( sql, new RowCallbackHandler()
+        {
+            public void processRow( ResultSet rs ) throws SQLException
+            {
+                int dataSetId = rs.getInt( 1 );
+                int organisationUnitId = rs.getInt( 2 );
+                
+                Set<Integer> dataSets = map.get( organisationUnitId );
+                
+                if ( dataSets == null )
+                {
+                    dataSets = new HashSet<Integer>();
+                    map.put( organisationUnitId, dataSets );
+                }
+                
+                dataSets.add( dataSetId );
+            }
+        } );
+        
+        return map;
+    }
 
     // -------------------------------------------------------------------------
     // OrganisationUnitHierarchy

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/PageInitAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/PageInitAction.java	2011-07-21 20:33:57 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/PageInitAction.java	2011-07-25 08:52:10 +0000
@@ -28,6 +28,9 @@
  */
 
 import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
@@ -36,6 +39,8 @@
 import org.hisp.dhis.expression.ExpressionService;
 import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.indicator.IndicatorService;
+import org.hisp.dhis.organisationunit.OrganisationUnitDataSetAssociationSet;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
 
 import com.opensymphony.xwork2.Action;
@@ -84,6 +89,13 @@
     {
         this.selectionManager = selectionManager;
     }
+    
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
 
     // -------------------------------------------------------------------------
     // Output
@@ -116,6 +128,20 @@
     {
         return dataSets;
     }
+    
+    private List<Set<Integer>> dataSetAssociationSets;
+    
+    public List<Set<Integer>> getDataSetAssociationSets()
+    {
+        return dataSetAssociationSets;
+    }
+
+    private Map<Integer, Integer> organisationUnitAssociationSetMap;
+
+    public Map<Integer, Integer> getOrganisationUnitAssociationSetMap()
+    {
+        return organisationUnitAssociationSetMap;
+    }
 
     // -------------------------------------------------------------------------
     // Action implementation
@@ -133,12 +159,18 @@
         
         dataSets = dataSetService.getAllDataSets();
         
+        OrganisationUnitDataSetAssociationSet organisationUnitSet = organisationUnitService.getOrganisationUnitDataSetAssociationSet();
+        
+        dataSetAssociationSets = organisationUnitSet.getDataSetAssociationSets();
+        
+        organisationUnitAssociationSetMap = organisationUnitSet.getOrganisationUnitAssociationSetMap();
+        
         for ( Indicator indicator : indicators )
         {
             indicator.setExplodedNumerator( expressionService.explodeExpression( indicator.getNumerator() ) );
             indicator.setExplodedDenominator( expressionService.explodeExpression( indicator.getDenominator() ) );
         }
-        
+
         return SUCCESS;
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml	2011-07-21 14:11:46 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml	2011-07-22 20:54:42 +0000
@@ -18,6 +18,7 @@
 	<property name="expressionService" ref="org.hisp.dhis.expression.ExpressionService" />
 	<property name="dataSetService" ref="org.hisp.dhis.dataset.DataSetService" />
     <property name="selectionManager" ref="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager" />
+	<property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
   </bean>
 
   <bean id="org.hisp.dhis.de.action.GetDataValuesForDataSetAction" class="org.hisp.dhis.de.action.GetDataValuesForDataSetAction"

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-07-22 09:17:04 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-07-25 15:46:47 +0000
@@ -10,6 +10,12 @@
 // Array with associative arrays for each data set, populated in select.vm
 var dataSets = [];
 
+// Associative array with identifier and array of assigned data sets, populated in select.vm
+var dataSetAssociationSets = [];
+
+// Associate array with mapping between organisation unit identifier and data set association set identifier, populated in select.vm
+var organisationUnitAssociationSetMap = [];
+
 // Array with keys on form {dataelementid}-{optioncomboid}-min/max with min/max values
 var currentMinMaxValueMap = [];
 
@@ -116,6 +122,35 @@
 // OrganisationUnit Selection
 // -----------------------------------------------------------------------------
 
+/**
+ * Returns an array containing associative array elements with id and name 
+ * properties. The array is sorted on the element name property.
+ */
+function getSortedDataSetList()
+{
+	var associationSet = organisationUnitAssociationSetMap[currentOrganisationUnitId];
+	var orgUnitDataSets = dataSetAssociationSets[associationSet];
+	
+	var dataSetList = [];
+	
+	for ( i in orgUnitDataSets )
+	{
+		var dataSetId = orgUnitDataSets[i];
+		var dataSetName = dataSets[dataSetId].name;
+		
+		var row = [];
+		row['id'] = dataSetId;
+		row['name'] = dataSetName;
+		dataSetList[i] = row;		
+	}
+	
+	dataSetList.sort( function( a, b ) {
+		return a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
+	} );
+	
+	return dataSetList;	
+}
+
 function organisationUnitSelected( orgUnits, orgUnitNames )
 {
 	currentOrganisationUnitId = orgUnits[0];
@@ -128,35 +163,41 @@
 
     var url = 'loadDataSets.action';
 
+	$( '#selectedOrganisationUnit' ).val( organisationUnitName );
+	$( '#currentOrganisationUnit' ).html( organisationUnitName );
+
     clearListById( 'selectedDataSetId' );
 
-    $.getJSON( url, { dataSetId:dataSetId },  function( json )
-    {
-        $( '#selectedOrganisationUnit' ).val( organisationUnitName );
-        $( '#currentOrganisationUnit' ).html( organisationUnitName );
-
-        addOptionById( 'selectedDataSetId', '-1', '[ ' + i18n_select_data_set + ' ]' );
-
-        for ( i in json.dataSets )
-        {
-            addOptionById( 'selectedDataSetId', json.dataSets[i].id, json.dataSets[i].name );
-        }
-
-        if ( json.dataSetValid && dataSetId != null )
-        {
-            $( '#selectedDataSetId' ).val( dataSetId );
-
-            if ( periodId && periodId != -1 && dataEntryFormIsLoaded ) //TODO if period valid
-            {
-                showLoader();
-                loadDataValues();
-            }
-        } 
-        else
-        {
-            clearPeriod();
-        }
-    } );
+	addOptionById( 'selectedDataSetId', '-1', '[ ' + i18n_select_data_set + ' ]' );
+	
+	var dataSetList = getSortedDataSetList();
+	
+	var dataSetValid = false;
+	
+	for ( i in dataSetList )
+    {
+        addOptionById( 'selectedDataSetId', dataSetList[i].id, dataSetList[i].name );
+        
+        if ( dataSetId == dataSetList[i].id )
+        {
+        	dataSetValid = true;
+        }
+    }
+
+	if ( dataSetValid && dataSetId != null )
+	{
+		$( '#selectedDataSetId' ).val( dataSetId );
+
+        if ( periodId && periodId != -1 && dataEntryFormIsLoaded ) //TODO if period valid
+        {
+            showLoader();
+            loadDataValues();
+        }
+    } 
+    else
+    {
+        clearPeriod();
+    }
 }
 
 selection.setListenerFunction( organisationUnitSelected );

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm	2011-07-21 19:49:23 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm	2011-07-25 08:52:10 +0000
@@ -22,26 +22,46 @@
 significantZeros = [
 #set( $size1 = $significantZeros.size() )
 #foreach( $dataElement in $significantZeros )
-${dataElement.id}#if( $velocityCount < $size1 ),#end #end ];
+${dataElement.id}#if( $velocityCount < $size1 ),#end 
+#end ];
 
 dataElements = {
 #set( $size2 = $dataElements.size() )
 #foreach( $dataElement in $dataElements )
-"${dataElement.id}": {
-"name":"${dataElement.name}", "type":"$encoder.jsonEncode( ${dataElement.getDetailedNumberType()} )"
+"${dataElement.id}":{"name":"${dataElement.name}","type":"$encoder.jsonEncode( ${dataElement.getDetailedNumberType()} )" 
 }#if( $velocityCount < $size2 ),#end
 #end };
 
+indicatorFormulas = {
+#set( $size3 = $indicators.size() )
 #foreach( $indicator in $indicators )
-indicatorFormulas['${indicator.id}'] = '($!{indicator.explodedNumerator})/($!{indicator.explodedDenominator})*($!{indicator.indicatorType.factor})';
-#end
+"${indicator.id}":"($!{indicator.explodedNumerator})/($!{indicator.explodedDenominator})*($!{indicator.indicatorType.factor})"
+#if( $velocityCount < $size3 ),#end
+#end };
 
 dataSets = {
-#set( $size3 = $dataSets.size() )
+#set( $size4 = $dataSets.size() )
 #foreach( $dataSet in $dataSets )
-"${dataSet.id}": {
-"name":"$encoder.jsonEncode( ${dataSet.name} )", "periodType":"$encoder.jsonEncode( ${dataSet.periodType.name} )" 
-}#if( $velocityCount < $size3 ),#end
+"${dataSet.id}":{"name":"$encoder.jsonEncode( ${dataSet.name} )","periodType":"$encoder.jsonEncode( ${dataSet.periodType.name} )" 
+}#if( $velocityCount < $size4 ),#end
+#end };
+
+dataSetAssociationSets = {
+#set( $size5 = $dataSetAssociationSets.size() )
+#set( $index = 0 )
+#foreach( $associationSet in $dataSetAssociationSets )
+"${index}": [
+#set( $index = $index + 1 )
+#set( $size6 = $associationSet.size() )
+#foreach( $id in $associationSet )
+${id}#if( $velocityCount < $size6 ),#end
+#end ]#if( $velocityCount < $size5 ),#end
+#end };
+
+organisationUnitAssociationSetMap = {
+#set( $size7 = $organisationUnitAssociationSetMap.size() )
+#foreach( $orgUnit in $organisationUnitAssociationSetMap.keySet() )
+"${orgUnit}":"$organisationUnitAssociationSetMap.get( ${orgUnit} )"#if( $velocityCount < $size7 ),#end
 #end };
 
 </script>