← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10673: wip, offline username optionset in anonymous reporting

 

------------------------------------------------------------
revno: 10673
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-04-24 13:11:42 +0700
message:
  wip, offline username optionset in anonymous reporting
added:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetUsernamesAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonUsers.vm
modified:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetOptionSetAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.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-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetOptionSetAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetOptionSetAction.java	2013-04-18 07:03:12 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetOptionSetAction.java	2013-04-24 06:11:42 +0000
@@ -30,7 +30,6 @@
 import com.opensymphony.xwork2.Action;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
-import org.hisp.dhis.option.OptionService;
 import org.hisp.dhis.option.OptionSet;
 import org.springframework.beans.factory.annotation.Autowired;
 

=== added file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetUsernamesAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetUsernamesAction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/GetUsernamesAction.java	2013-04-24 06:11:42 +0000
@@ -0,0 +1,76 @@
+package org.hisp.dhis.caseentry.action;
+
+/*
+ * Copyright (c) 2004-2013, 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 com.opensymphony.xwork2.Action;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class GetUsernamesAction implements Action
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    @Autowired
+    private UserService userService;
+
+    // -------------------------------------------------------------------------
+    // Input & Output
+    // -------------------------------------------------------------------------
+
+    private SortedSet<String> usernames = new TreeSet<String>();
+
+    public Set<String> getUsernames()
+    {
+        return usernames;
+    }
+
+    // -------------------------------------------------------------------------
+    // Action Impl
+    // -------------------------------------------------------------------------
+
+    @Override
+    public String execute() throws Exception
+    {
+        for ( User user : userService.getAllUsers() )
+        {
+            usernames.add( user.getUsername() );
+        }
+
+        return SUCCESS;
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-04-18 11:15:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-04-24 06:11:42 +0000
@@ -1137,4 +1137,8 @@
   <bean id="org.hisp.dhis.caseentry.action.GetOptionSetAction"
     class="org.hisp.dhis.caseentry.action.GetOptionSetAction" scope="prototype">
   </bean>
+
+  <bean id="org.hisp.dhis.caseentry.action.GetUsernamesAction"
+    class="org.hisp.dhis.caseentry.action.GetUsernamesAction" scope="prototype">
+  </bean>
 </beans>

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2013-04-18 06:05:48 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2013-04-24 06:11:42 +0000
@@ -175,6 +175,10 @@
       <result name="success" type="velocity-json">/dhis-web-caseentry/jsonOptionSet.vm</result>
     </action>
 
+    <action name="getUsernames" class="org.hisp.dhis.caseentry.action.GetUsernamesAction">
+      <result name="success" type="velocity-json">/dhis-web-caseentry/jsonUsers.vm</result>
+    </action>
+
     <action name="removeCurrentEncounter"
       class="org.hisp.dhis.caseentry.action.caseentry.RemoveCurrentEncounterAction">
       <result name="success" type="velocity-json">

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-04-22 06:06:16 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-04-24 06:11:42 +0000
@@ -3,6 +3,7 @@
 var PROGRAMS_STORE = 'anonymousPrograms';
 var PROGRAM_STAGES_STORE = 'anonymousProgramStages';
 var OPTION_SET_STORE = 'optionSets';
+var USERNAME_STORE = 'usernames';
 var OFFLINE_DATA_STORE = 'anonymousOfflineData';
 
 function initalizeProgramStages() {
@@ -17,8 +18,6 @@
             var keys = _.keys( data.metaData.programs );
             var objs = _.values( data.metaData.programs );
 
-            loadOptionSets( data.metaData.optionSets );
-
             DAO.programs.addAll( keys, objs, function ( store ) {
                 var deferred = $.Deferred();
                 var promise = deferred.promise();
@@ -31,16 +30,21 @@
                     });
                 });
 
+                promise = promise.pipe(function() {
+                    loadOptionSets( data.metaData.optionSets, true );
+                });
+
                 deferred.resolve();
 
                 selection.setListenerFunction( organisationUnitSelected );
                 $( document ).trigger('dhis2.anonymous.programsInitialized');
             } );
         } ).fail( function () {
+            DAO.optionSets = new dhis2.storage.Store( {name: OPTION_SET_STORE, adapter: 'dom-ss'}, function() {} );
+            DAO.usernames = new dhis2.storage.Store( {name: USERNAME_STORE, adapter: 'dom-ss'}, function() {} );
+
             selection.setListenerFunction( organisationUnitSelected );
             $( document ).trigger('dhis2.anonymous.programsInitialized');
-
-            DAO.optionSets = new dhis2.storage.Store( {name: OPTION_SET_STORE, adapter: 'dom-ss'}, function() {} );
         } );
     } );
 }
@@ -144,10 +148,10 @@
     } );
 
     $( "#orgUnitTree" ).one( "ouwtLoaded", function () {
-        $( document ).one('dhis2.anonymous.programStagesInitialized', initializePrograms);
-        $( document ).one('dhis2.anonymous.programsInitialized', showOfflineEvents);
+        $( document ).one( 'dhis2.anonymous.programStagesInitialized', initializePrograms );
+        $( document ).one( 'dhis2.anonymous.programsInitialized', showOfflineEvents );
         $( document ).one( 'dhis2.anonymous.checkOfflineEvents', checkOfflineData );
-        $( document ).one( 'dhis2.anonymous.checkOfflineData', function() {
+        $( document ).one( 'dhis2.anonymous.checkOfflineData', function () {
             dhis2.availability.startAvailabilityCheck();
             selection.responseReceived();
         } );
@@ -997,21 +1001,35 @@
     });
 }
 
-function loadOptionSets(uids, success ) {
+function loadOptionSets(uids, usernames, success ) {
     DAO.optionSets = new dhis2.storage.Store( {name: OPTION_SET_STORE, adapter: 'dom-ss'}, function ( store ) {
-        var deferred = $.Deferred();
-        var promise = deferred.promise();
-
-        _.each( uids, function(item, idx) {
-            promise = promise.pipe($.ajax({
-                url: 'getOptionSet.action?dataElementUid=' + item,
-                dataType: 'json',
-                success: function(json) {
-                    DAO.optionSets.add(item, json);
-                }
-            }));
+        DAO.usernames = new dhis2.storage.Store( {name: USERNAME_STORE, adapter: 'dom-ss'}, function ( store ) {
+            var deferred = $.Deferred();
+            var promise = deferred.promise();
+
+            _.each( uids, function(item, idx) {
+                promise = promise.pipe( $.ajax( {
+                    url: 'getOptionSet.action?dataElementUid=' + item,
+                    dataType: 'json',
+                    success: function ( json ) {
+                        DAO.optionSets.add( item, json );
+                        if ( success && typeof success == 'function' ) success( json );
+                    }
+                } ) );
+            });
+
+            if ( usernames ) {
+                promise = promise.pipe( $.ajax( {
+                    url: 'getUsernames.action',
+                    dataType: 'json',
+                    success: function ( json ) {
+                        DAO.usernames.add( 'usernames', json.usernames );
+                        if ( success && typeof success == 'function' ) success( json );
+                    }
+                } ) );
+            }
+
+            deferred.resolve();
         });
-
-        deferred.resolve();
     } );
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.js	2013-04-22 05:48:12 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.js	2013-04-24 06:11:42 +0000
@@ -1,3 +1,6 @@
+
+var MAX_DROPDOWN_DISPLAYED = 30;
+
 //------------------------------------------------------------------------------
 // Save value
 //------------------------------------------------------------------------------
@@ -903,8 +906,6 @@
 		});
 }
 
-var MAX_OPTIONS_DISPLAYED = 30;
-
 function searchOptionSet( uid, query, success ) {
     if(window.DAO !== undefined && window.DAO.optionSets !== undefined ) {
         DAO.optionSets.fetch(uid, function(store, arr) {
@@ -913,7 +914,7 @@
                 var options = [];
 
                 if(query == null || query == "") {
-                    options = obj.optionSet.options.slice(0, MAX_OPTIONS_DISPLAYED-1);
+                    options = obj.optionSet.options.slice(0, MAX_DROPDOWN_DISPLAYED-1);
                 } else {
                     query = query.toLowerCase();
 
@@ -1032,19 +1033,54 @@
 }
 
 function searchUsername( query, success ) {
-    $.ajax({
+    if(window.DAO !== undefined && window.DAO.usernames !== undefined ) {
+        DAO.usernames.fetch('usernames', function(store, arr) {
+            if ( arr.length > 0 ) {
+                var obj = arr[0];
+                var usernames = [];
+
+                if(query == null || query == "") {
+                    delete obj['key'];
+                    usernames = obj.slice(0, MAX_DROPDOWN_DISPLAYED-1);
+                } else {
+                    query = query.toLowerCase();
+
+                    _.each(obj, function(item, idx) {
+                        if ( item.toLowerCase().indexOf( query ) != -1 ) {
+                            usernames.push(item);
+                        }
+                    });
+                }
+
+                success( $.map( usernames, function ( item ) {
+                    return {
+                        label: item,
+                        id: item
+                    };
+                } ) );
+            } else {
+                getUsername( query, success );
+            }
+        } );
+    } else {
+        getUsername( query, success );
+    }
+}
+
+function getUsername( query, success ) {
+    return $.ajax( {
         url: "getUsernameList.action?query=" + query,
         dataType: "json",
         cache: true,
-        success: function(data) {
-            success($.map(data.usernames, function(item) {
+        success: function ( data ) {
+            success( $.map( data.usernames, function ( item ) {
                 return {
                     label: item.u,
                     id: item.u
                 };
-            }));
+            } ) );
         }
-    });
+    } );
 }
 
 function autocompletedUsernameField( idField )

=== added file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonUsers.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonUsers.vm	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonUsers.vm	2013-04-24 06:11:42 +0000
@@ -0,0 +1,1 @@
+{ "usernames": [#foreach( $username in $usernames )"$!encoder.jsonEncode("$username")"#if( $velocityCount < $usernames.size() ),#end #end]}
\ No newline at end of file