← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17484: bugfix for partial ouwt, clear and reload all partial orgunits when a new orgunit is added, this ...

 

------------------------------------------------------------
revno: 17484
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-11-16 18:32:57 +0700
message:
  bugfix for partial ouwt, clear and reload all partial orgunits when a new orgunit is added, this is necessary to not have a stale local cache
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.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-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2014-11-07 23:27:55 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2014-11-16 11:32:57 +0000
@@ -188,6 +188,7 @@
     };
 
     this.setOrganisationUnits = function( ous ) {
+        organisationUnits = [];
         $.extend( organisationUnits, ous );
         ous = ous ? _.values( ous ) : [];
 
@@ -204,6 +205,20 @@
         return def.promise();
     };
 
+    this.clearOrganisationUnits = function() {
+      organisationUnits = [];
+
+      var def = $.Deferred();
+
+      dhis2.ou.store.removeAll(OU_KEY).always(function() {
+        dhis2.ou.store.removeAll(OU_PARTIAL_KEY).always(function() {
+          def.resolve();
+        });
+      });
+
+      return def.promise();
+    };
+
     this.getOrganisationUnit = function( id ) {
         var def = $.Deferred();
 
@@ -351,11 +366,12 @@
                     selection.setVersion( data.version );
                     selection.setUsername( data.username );
 
-                    selection.setOrganisationUnits( data.organisationUnits ).done(function() {
-                        sync_and_reload();
-                        $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
+                    selection.clearOrganisationUnits().always(function() {
+                      selection.setOrganisationUnits( data.organisationUnits ).done(function() {
+                          sync_and_reload();
+                          $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
+                      });
                     });
-
                 } ).fail( function() {
                     sync_and_reload();
                     $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
@@ -675,6 +691,14 @@
 // -----------------------------------------------------------------------------
 
 function Subtree() {
+    this.ajaxGetChildren = function( parentId ) {
+        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parentId);
+    };
+
+    this.ajaxGetLeaf = function( parentId ) {
+        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?leafId=' + parentId);
+    };
+
     this.toggle = function( unitId ) {
         var children = $( "#" + getTagId( unitId ) ).find( "ul" );
         var ou = organisationUnits[unitId];
@@ -697,15 +721,25 @@
         $( "#" + getTagId( ou ) + " > a" ).addClass( "selected" );
     };
 
-    var expandTreeAtOrgUnits = function( ous ) {
+    var expandTreeAtOrgUnits = function( ous, select ) {
         $.each( ous, function( i, item ) {
-            expandTreeAtOrgUnit( item );
+            expandTreeAtOrgUnit( item, select );
         } );
     };
 
-    var expandTreeAtOrgUnit = function( ou ) {
-        if( organisationUnits[ou] == null ) {
-            return;
+    var expandTreeAtOrgUnit = function( ou, select ) {
+        select = select || false;
+
+      if( organisationUnits[ou] == null ) {
+          subtree.ajaxGetLeaf(ou).done(function( data ) {
+            $.extend( organisationUnits, data.organisationUnits);
+
+            if(organisationUnits[ou]) {
+              expandTreeAtOrgUnit(ou, select);
+            }
+          });
+
+          return;
         }
 
         var ouEl = organisationUnits[ou];
@@ -746,6 +780,10 @@
             var expand = organisationUnits[item];
             processExpand( expand );
         } );
+
+        if( select ) {
+          selectOrgUnit(ou);
+        }
     };
 
     this.reloadTree = function() {
@@ -756,7 +794,7 @@
         var selected = selection.getSelected();
 
         expandTreeAtOrgUnits( roots );
-        expandTreeAtOrgUnits( selected );
+        expandTreeAtOrgUnits( selected, true );
 
         selectOrgUnits( selected );
     };
@@ -788,10 +826,6 @@
         }
     }
 
-    this.ajaxGetChildren = function( parentId ) {
-        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parentId);
-    };
-
     this.getChildren = function( parentId ) {
         var def = $.Deferred();
         var p = def.promise();

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java	2014-10-16 06:17:19 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java	2014-11-16 11:32:57 +0000
@@ -28,12 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
-
+import com.opensymphony.xwork2.Action;
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
 import org.hisp.dhis.configuration.ConfigurationService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -44,7 +39,11 @@
 import org.hisp.dhis.version.Version;
 import org.hisp.dhis.version.VersionService;
 
-import com.opensymphony.xwork2.Action;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
 
 /**
  * @author mortenoh
@@ -135,6 +134,13 @@
         this.parentId = parentId;
     }
 
+    private String leafId;
+
+    public void setLeafId( String leafId )
+    {
+        this.leafId = leafId;
+    }
+
     private String byName;
 
     public void setByName( String byName )
@@ -157,6 +163,10 @@
     public String execute()
         throws Exception
     {
+        version = getVersionString();
+
+        username = currentUserService.getCurrentUsername();
+
         if ( byName != null )
         {
             List<OrganisationUnit> organisationUnitByName = organisationUnitService.getOrganisationUnitByName( byName );
@@ -181,6 +191,25 @@
             }
         }
 
+        if ( leafId != null )
+        {
+            OrganisationUnit leaf = organisationUnitService.getOrganisationUnit( leafId );
+
+            if ( leaf != null )
+            {
+                organisationUnits.add( leaf );
+                organisationUnits.addAll( leaf.getChildren() );
+
+                for ( OrganisationUnit organisationUnit : leaf.getAncestors() )
+                {
+                    organisationUnits.add( organisationUnit );
+                    organisationUnits.addAll( organisationUnit.getChildren() );
+                }
+            }
+
+            return "partial";
+        }
+
         if ( parentId != null )
         {
             OrganisationUnit parent = organisationUnitService.getOrganisationUnit( parentId );
@@ -213,9 +242,9 @@
             OrganisationUnitLevel offlineOrgUnitLevel = configurationService.getConfiguration().getOfflineOrganisationUnitLevel();
 
             List<OrganisationUnitLevel> orgUnitLevels = organisationUnitService.getOrganisationUnitLevels();
-            
-            final Integer maxLevels = ( offlineOrgUnitLevel != null && !orgUnitLevels.isEmpty() ) ? offlineOrgUnitLevel.getLevel() : null;
-            
+
+            final Integer maxLevels = (offlineOrgUnitLevel != null && !orgUnitLevels.isEmpty()) ? offlineOrgUnitLevel.getLevel() : null;
+
             for ( OrganisationUnit unit : userOrganisationUnits )
             {
                 organisationUnits.addAll( organisationUnitService.getOrganisationUnitWithChildren( unit.getId(), maxLevels ) );
@@ -232,10 +261,6 @@
 
         Collections.sort( rootOrganisationUnits, IdentifiableObjectNameComparator.INSTANCE );
 
-        version = getVersionString();
-
-        username = currentUserService.getCurrentUsername();
-
         return SUCCESS;
     }