← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16974: support lazy load of offline data-set associations, finished

 

------------------------------------------------------------
revno: 16974
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-10-07 10:59:22 +0700
message:
  support lazy load of offline data-set associations, finished
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetMetaDataAction.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js


--
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-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetMetaDataAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetMetaDataAction.java	2014-09-29 19:53:06 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetMetaDataAction.java	2014-10-07 03:59:22 +0000
@@ -29,7 +29,6 @@
  */
 
 import com.opensymphony.xwork2.Action;
-
 import org.hisp.dhis.acl.AclService;
 import org.hisp.dhis.common.ListMap;
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
@@ -124,7 +123,7 @@
 
     @Autowired
     private ConfigurationService configurationService;
-    
+
     // -------------------------------------------------------------------------
     // Output
     // -------------------------------------------------------------------------
@@ -245,16 +244,23 @@
         expressionService.substituteExpressions( indicators, null );
 
         OrganisationUnitLevel offlineOrgUnitLevel = configurationService.getConfiguration().getOfflineOrganisationUnitLevel();
-        
+
         Integer level = offlineOrgUnitLevel != null ? offlineOrgUnitLevel.getLevel() : null;
-        
-        OrganisationUnitDataSetAssociationSet organisationUnitSet = organisationUnitService.getOrganisationUnitDataSetAssociationSet( null ); //TODO change null > "level"
+
+        OrganisationUnitDataSetAssociationSet organisationUnitSet = organisationUnitService.getOrganisationUnitDataSetAssociationSet( level );
 
         dataSetAssociationSets = organisationUnitSet.getDataSetAssociationSets();
 
         organisationUnitAssociationSetMap = organisationUnitSet.getOrganisationUnitAssociationSetMap();
 
-        dataSets = new ArrayList<>( dataSetService.getDataSetsByUidNoAcl( organisationUnitSet.getDistinctDataSets() ) );
+        if ( currentUserService.currentUserIsSuper() )
+        {
+            dataSets = new ArrayList<>( dataSetService.getAllDataSets() );
+        }
+        else
+        {
+            dataSets = new ArrayList<>( currentUserService.getCurrentUser().getUserCredentials().getAllDataSets() );
+        }
 
         Set<DataElementCategoryCombo> categoryComboSet = new HashSet<>();
         Set<DataElementCategory> categorySet = new HashSet<>();

=== 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	2014-10-06 09:35:51 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2014-10-07 03:59:22 +0000
@@ -815,72 +815,75 @@
     $( '#selectedOrganisationUnit' ).val( organisationUnitName );
     $( '#currentOrganisationUnit' ).html( organisationUnitName );
 
-    var dataSetList = getSortedDataSetList();
-
-    $( '#selectedDataSetId' ).removeAttr( 'disabled' );
-
-    var dataSetId = $( '#selectedDataSetId' ).val();
-    var periodId = $( '#selectedPeriodId').val();
-
-    clearListById( 'selectedDataSetId' );
-    addOptionById( 'selectedDataSetId', '-1', '[ ' + i18n_select_data_set + ' ]' );
-
-    var dataSetValid = false;
-    var multiDataSetValid = false;
-
-    $.safeEach( dataSetList, function( idx, item ) 
-    {
-        addOptionById( 'selectedDataSetId', item.id, item.name );
-
-        if ( dataSetId == item.id )
-        {
-            dataSetValid = true;
-        }
-    } );
-
-    if ( children )
-    {
-        var childrenDataSets = getSortedDataSetListForOrgUnits( children );
-
-        if ( childrenDataSets && childrenDataSets.length > 0 )
-        {
-            $( '#selectedDataSetId' ).append( '<optgroup label="' + i18n_childrens_forms + '">' );
-
-            $.safeEach( childrenDataSets, function( idx, item )
-            {
-                if ( dataSetId == item.id && dhis2.de.multiOrganisationUnit )
+    dhis2.de.getOrFetchDataSetList().then(function(data) {
+        var dataSetList = data;
+
+        $( '#selectedDataSetId' ).removeAttr( 'disabled' );
+
+        var dataSetId = $( '#selectedDataSetId' ).val();
+        var periodId = $( '#selectedPeriodId').val();
+
+        clearListById( 'selectedDataSetId' );
+        addOptionById( 'selectedDataSetId', '-1', '[ ' + i18n_select_data_set + ' ]' );
+
+        var dataSetValid = false;
+        var multiDataSetValid = false;
+
+        $.safeEach( dataSetList, function( idx, item )
+        {
+            addOptionById( 'selectedDataSetId', item.id, item.name );
+
+            if ( dataSetId == item.id )
+            {
+                dataSetValid = true;
+            }
+        } );
+
+        if ( children )
+        {
+            var childrenDataSets = getSortedDataSetListForOrgUnits( children );
+
+            if ( childrenDataSets && childrenDataSets.length > 0 )
+            {
+                $( '#selectedDataSetId' ).append( '<optgroup label="' + i18n_childrens_forms + '">' );
+
+                $.safeEach( childrenDataSets, function( idx, item )
                 {
-                    multiDataSetValid = true;
-                }
-
-                $( '<option />' ).attr( 'data-multiorg', true ).attr( 'value', item.id).html( item.name ).appendTo( '#selectedDataSetId' );
-            } );
-
-            $( '#selectDataSetId' ).append( '</optgroup>' );
-        }
-    }
-
-    if ( !dhis2.de.multiOrganisationUnit && dataSetValid && dataSetId ) {
-        $( '#selectedDataSetId' ).val( dataSetId ); // Restore selected data set
-
-        if ( dhis2.de.inputSelected() && dhis2.de.dataEntryFormIsLoaded ) {
-            resetSectionFilters();
-            showLoader();
-            loadDataValues();
-        }
-    } 
-    else if ( dhis2.de.multiOrganisationUnit && multiDataSetValid && dataSetId ) {
-        $( '#selectedDataSetId' ).val( dataSetId ); // Restore selected data set
-        dataSetSelected();
-    }
-    else {
-    	dhis2.de.multiOrganisationUnit = false;
-        dhis2.de.currentDataSetId = null;
-
-        clearSectionFilters();
-        clearPeriod();
-        dhis2.de.clearAttributes();
-    }
+                    if ( dataSetId == item.id && dhis2.de.multiOrganisationUnit )
+                    {
+                        multiDataSetValid = true;
+                    }
+
+                    $( '<option />' ).attr( 'data-multiorg', true ).attr( 'value', item.id).html( item.name ).appendTo( '#selectedDataSetId' );
+                } );
+
+                $( '#selectDataSetId' ).append( '</optgroup>' );
+            }
+        }
+
+        if ( !dhis2.de.multiOrganisationUnit && dataSetValid && dataSetId ) {
+            $( '#selectedDataSetId' ).val( dataSetId ); // Restore selected data set
+
+            if ( dhis2.de.inputSelected() && dhis2.de.dataEntryFormIsLoaded ) {
+                resetSectionFilters();
+                showLoader();
+                loadDataValues();
+            }
+        }
+        else if ( dhis2.de.multiOrganisationUnit && multiDataSetValid && dataSetId ) {
+            $( '#selectedDataSetId' ).val( dataSetId ); // Restore selected data set
+            dataSetSelected();
+        }
+        else {
+        	dhis2.de.multiOrganisationUnit = false;
+            dhis2.de.currentDataSetId = null;
+
+            clearSectionFilters();
+            clearPeriod();
+            dhis2.de.clearAttributes();
+        }
+    });
+
 }
 
 /**
@@ -893,41 +896,17 @@
 {
     var def = $.Deferred();
 
-    var dataSetFields = 'id,name,periodType,categoryCombo[id],allowFuturePeriods,skipOffline,version,dataSetType' +
-        ',expiryDays,fieldCombinationRequired,validCompleteOnly,renderAsTabs,renderHorizontally';
-
     $.ajax({
         type: 'GET',
         url: '../api/organisationUnits/' + ou,
         data: {
-            fields: 'dataSets[' + dataSetFields + '],children[id,name,dataSets[' + dataSetFields + ']]'
+            fields: 'id,dataSets[id],children[id,dataSets[id]]'
         }
     }).done(function(data) {
-        var dataSets = [];
-
-        data.dataSets.forEach(function( item ) {
-            dataSets.push(item.id);
-            dhis2.de.dataSets[item.id] = {
-                name: item.name,
-                periodType: item.periodType,
-                categoryCombo: item.categoryCombo.id,
-                allowFuturePeriods: item.allowFuturePeriods,
-                skipOffline: item.skipOffline,
-                version: item.version,
-                type: item.dataSetType,
-                expiryDays: item.expiryDays,
-                fieldCombinationRequired: item.fieldCombinationRequired,
-                validCompleteOnly: item.validCompleteOnly,
-                renderAsTabs: item.renderAsTabs,
-                renderHorizontally: item.renderHorizontally
-            }
-        });
-
-        dhis2.de.dataSetAssociationSets[Object.keys(dhis2.de.dataSetAssociationSets).length] = dataSets;
-        dhis2.de.organisationUnitAssociationSetMap[ou] = Object.keys(dhis2.de.dataSetAssociationSets).length -1;
+        dhis2.de._updateDataSets(data);
 
         data.children.forEach(function( item ) {
-            console.log(item);
+            dhis2.de._updateDataSets(item);
         });
 
         def.resolve(data);
@@ -937,6 +916,48 @@
 };
 
 /**
+ * Internal method that will go through all data-sets on the object and add them to
+ * {@see dhis2.de.dataSetAssociationSets} and {@see dhis2.de.organisationUnitAssociationSetMap}.
+ *
+ * @param {Object} ou Object that matches the format id,dataSets[id].
+ * @private
+ */
+dhis2.de._updateDataSets = function( ou ) {
+    var dataSets = [];
+
+    ou.dataSets.forEach(function( item ) {
+        dataSets.push(item.id);
+    });
+
+    dhis2.de.dataSetAssociationSets[Object.keys(dhis2.de.dataSetAssociationSets).length] = dataSets;
+    dhis2.de.organisationUnitAssociationSetMap[ou.id] = Object.keys(dhis2.de.dataSetAssociationSets).length - 1;
+};
+
+/**
+ * Get a list of sorted data-sets for a orgUnit, if data-set list is empty, it will try and fetch
+ * data-sets from the server.
+ *
+ * @param {String} [ou] Organisation unit to fetch data-sets for
+ * @returns {$.Deferred}
+ */
+dhis2.de.getOrFetchDataSetList = function( ou ) {
+    var def = $.Deferred();
+
+    var dataSets = getSortedDataSetList(ou);
+    ou = ou || dhis2.de.getCurrentOrganisationUnit();
+
+    if( dataSets.length > 0 ) {
+        def.resolve(dataSets);
+    } else {
+        dhis2.de.fetchDataSets(ou).then(function() {
+            def.resolve(getSortedDataSetList(ou));
+        });
+    }
+
+    return def.promise();
+};
+
+/**
  * Returns an array containing associative array elements with id and name
  * properties. The array is sorted on the element name property.
  */