← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4312: added methods for checking if dhis2 is offline / online (but not logged in) / online (and logged in)

 

------------------------------------------------------------
revno: 4312
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-08-16 14:16:33 +0200
message:
  added methods for checking if dhis2 is offline / online (but not logged in) / online (and logged in)
added:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonPing.vm
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.availability.js
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetPingAction.java
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm
  dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/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
=== added file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonPing.vm'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonPing.vm	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonPing.vm	2011-08-16 12:16:33 +0000
@@ -0,0 +1,3 @@
+{
+  "loggedIn": $loggedIn
+}

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js	2011-08-08 18:21:36 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js	2011-08-16 12:16:33 +0000
@@ -8,23 +8,6 @@
 var _loading_circle_html = "<img src='../images/ajax-loader-circle.gif'>";
 
 /**
- * Determines whether the DHIS server can be accessed, ie. if the server
- * is running (in the context of a remote server also if the network is up).
- */
-function serverIsAccessible()
-{
-	$.ajax( {
-		url: "../dhis-web-commons-stream/ping.action",
-		success: function( data, textStatus, jqXHR ) {
-			return true;
-		},
-		error: function( jqXHR, textStatus, errorThrown ) {
-			return false;
-		}
-	} );
-}
-
-/**
  * Go back using the document.referrer.
  * 
  * @param defaultUrl if there is not document.referrer, use this url

=== added file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.availability.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.availability.js	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.availability.js	2011-08-16 12:16:33 +0000
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+
+dhis2.util.namespace( 'dhis2.availability' );
+
+dhis2.availability._isAvailable = undefined;
+dhis2.availability._isLoggedIn = -1;
+dhis2.availability._availableTimeoutHandler = undefined;
+
+/**
+ * Start availability check, will trigger dhis2.online / dhis2.offline events
+ * when availability changes.
+ * 
+ * @param checkInterval How often to check for availability, default is 1000.
+ */
+dhis2.availability.startAvailabilityCheck = function( checkInterval )
+{
+    if ( checkInterval === undefined )
+    {
+        checkInterval = 1000;
+    }
+
+    function _checkAvailability()
+    {
+        $.ajax( {
+            url : "../dhis-web-commons-stream/ping.action",
+            success : function( data, textStatus, jqXHR )
+            {
+                dhis2.availability._isAvailable = true;
+                var loggedIn = data.loggedIn ? true : false;
+
+                if ( loggedIn != dhis2.availability._isLoggedIn )
+                {
+                    dhis2.availability._isLoggedIn = loggedIn;
+                    $( document ).trigger( "dhis2.online", [ loggedIn ]);
+                }
+            },
+            error : function( jqXHR, textStatus, errorThrown )
+            {
+                if ( dhis2.availability._isAvailable )
+                {
+                    dhis2.availability._isAvailable = false;
+                    $( document ).trigger( "dhis2.offline" );
+                }
+            }
+        } ).complete( function()
+        {
+            _availableTimeoutHandler = setTimeout( _checkAvailability, checkInterval );
+        } );
+    }
+
+    _availableTimeoutHandler = setTimeout( _checkAvailability, checkInterval );
+}
+
+/**
+ * Stop checking for availability.
+ */
+dhis2.availability.stopAvailabilityCheck = function()
+{
+    clearTimeout( _availableTimeoutHandler );
+}

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm	2011-08-09 09:48:09 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm	2011-08-16 12:16:33 +0000
@@ -52,6 +52,7 @@
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.array.js"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.select.js"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.comparator.js"></script>
+    <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.availability.js"></script>
 
     <script type="text/javascript" src="../dhis-web-commons/css/${stylesheetDirectory}/dom.js"></script>
 		

=== added file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetPingAction.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetPingAction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetPingAction.java	2011-08-16 12:16:33 +0000
@@ -0,0 +1,82 @@
+package org.hisp.dhis.commons.action;
+
+/*
+ * 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 org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
+
+import com.opensymphony.xwork2.Action;
+
+/**
+ * @author mortenoh
+ */
+public class GetPingAction
+    implements Action
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private CurrentUserService currentUserService;
+
+    public void setCurrentUserService( CurrentUserService currentUserService )
+    {
+        this.currentUserService = currentUserService;
+    }
+
+    // -------------------------------------------------------------------------
+    // Input & Output
+    // -------------------------------------------------------------------------
+
+    private Boolean loggedIn;
+
+    public Boolean isLoggedIn()
+    {
+        return loggedIn;
+    }
+
+    // -------------------------------------------------------------------------
+    // Action implementation
+    // -------------------------------------------------------------------------
+
+    public String execute()
+    {
+        User user = currentUserService.getCurrentUser();
+
+        if ( user == null )
+        {
+            loggedIn = false;
+        }
+        else
+        {
+            loggedIn = true;
+        }
+
+        return SUCCESS;
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml	2011-08-12 13:23:47 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml	2011-08-16 12:16:33 +0000
@@ -617,4 +617,8 @@
     </property>
   </bean>
 
+  <bean id="org.hisp.dhis.commons.action.GetPingAction" class="org.hisp.dhis.commons.action.GetPingAction">
+    <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
+  </bean>
+
 </beans>

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml'
--- dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml	2011-08-09 09:48:09 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml	2011-08-16 12:16:33 +0000
@@ -728,12 +728,6 @@
       <param name="onExceptionReturn">plainTextError</param>
     </action>
 
-    <action name="ping" class="org.hisp.dhis.commons.action.NoAction">
-      <result name="success" type="velocity-json">
-        /dhis-web-commons/ajax/jsonResponseSuccess.vm
-      </result>
-    </action>
-
   </package>
 
   <!-- Stream -->
@@ -743,10 +737,12 @@
     <action name="loadDocument" class="org.hisp.dhis.commons.action.LoadDocumentAction">
       <result name="success" type="outputStreamResult" />
     </action>
-	
-	<action name="ping" class="org.hisp.dhis.commons.action.NoAction">
-		<result name="success" type="outputStreamResult" />
-	</action>
+
+    <action name="ping" class="org.hisp.dhis.commons.action.GetPingAction">
+      <result name="success" type="velocity-json">
+        /dhis-web-commons/ajax/jsonPing.vm
+      </result>
+    </action>
 
   </package>
 

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2011-08-15 13:48:42 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2011-08-16 12:16:33 +0000
@@ -208,6 +208,17 @@
     $("#orgUnitTree").one("ouwtLoaded", function() {
         saveDataValuesInLocalStorage();
     });
+
+    dhis2.availability.startAvailabilityCheck();
+
+    $(document).bind("dhis2.online", function(event, loggedIn) {
+        console.log("dhis2 is online")
+        console.log("loggedIn: " + loggedIn)
+    })
+
+    $(document).bind("dhis2.offline", function() {
+        console.log("dhis2 is offline")
+    })
 })
 
 function saveDataValuesInLocalStorage() {
@@ -278,8 +289,9 @@
 
     function handleError( jqXHR, textStatus, errorThrown )
     {
-        markValue( COLOR_RED );
-        window.alert( i18n_saving_value_failed_status_code + '\n\n' + textStatus );
+        setHeaderMessage( "Unable to contact server. Data will be stored locally." )
+//        markValue( COLOR_RED );
+//        window.alert( i18n_saving_value_failed_status_code + '\n\n' + textStatus );
     }
 
     function markValue( color )