← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17516: Data entry. Implemented validation/etag caching for the dataset associations response.

 

------------------------------------------------------------
revno: 17516
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-11-17 23:18:59 +0100
message:
  Data entry. Implemented validation/etag caching for the dataset associations response.
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.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-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2014-10-28 10:29:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2014-11-17 22:18:59 +0000
@@ -354,7 +354,7 @@
      * the "If-None-Match" header value, generates an ETag based on the given 
      * collection of IdentifiableObjects and compares them for equality. If this
      * evaluates to true, it will set status code 304 Not Modified on the response
-     * and remove all elements from the given list. It will also set the ETag header
+     * and remove all elements from the given list. It will set the ETag header
      * on the response in any case.
      * 
      * @param request the HttpServletRequest.
@@ -380,4 +380,34 @@
         
         return false;
     }
+    
+    /**
+     * This method looks up the ETag sent in the request from the "If-None-Match" 
+     * header value and compares it to the given tag. If they match, it will set 
+     * status code 304 Not Modified on the response. It will set the ETag header
+     * on the response in any case. It will wrap the given tag in quotes.
+     * 
+     * @param request the HttpServletRequest.
+     * @param response the HttpServletResponse.
+     * @param tag the tag to compare.
+     * @return true if the given tag match the request tag and the response is
+     *         considered not modified, false if not.
+     */
+    public static boolean isNotModified( HttpServletRequest request, HttpServletResponse response, String tag )
+    {
+        tag = tag != null ? ( QUOTE + tag + QUOTE ) : null;
+        
+        String inputTag = request.getHeader( HEADER_IF_NONE_MATCH );
+
+        response.setHeader( HEADER_ETAG, tag );
+        
+        if ( inputTag != null && inputTag.equals( tag ) )
+        {
+            response.setStatus( HttpServletResponse.SC_NOT_MODIFIED );
+            
+            return true;
+        }
+        
+        return false;
+    }
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java	2014-11-17 17:38:08 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java	2014-11-17 22:18:59 +0000
@@ -28,14 +28,23 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.struts2.ServletActionContext;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.configuration.ConfigurationService;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitDataSetAssociationSet;
 import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import com.opensymphony.xwork2.Action;
@@ -51,19 +60,22 @@
     
     @Autowired
     private ConfigurationService configurationService;
+    
+    @Autowired
+    private IdentifiableObjectManager identifiableObjectManager;
 
     // -------------------------------------------------------------------------
     // Output
     // -------------------------------------------------------------------------
 
-    private List<Set<String>> dataSetAssociationSets;
+    private List<Set<String>> dataSetAssociationSets = new ArrayList<>();
 
     public List<Set<String>> getDataSetAssociationSets()
     {
         return dataSetAssociationSets;
     }
 
-    private Map<String, Integer> organisationUnitAssociationSetMap;
+    private Map<String, Integer> organisationUnitAssociationSetMap = new HashMap<>();
 
     public Map<String, Integer> getOrganisationUnitAssociationSetMap()
     {
@@ -77,6 +89,15 @@
     @Override
     public String execute()
     {
+        Date lastUpdated = DateUtils.max( identifiableObjectManager.getLastUpdated( DataSet.class ), 
+            identifiableObjectManager.getLastUpdated( OrganisationUnit.class ) );
+        String tag = lastUpdated != null ? DateUtils.LONG_DATE_FORMAT.format( lastUpdated ) : null;
+        
+        if ( ContextUtils.isNotModified( ServletActionContext.getRequest(), ServletActionContext.getResponse(), tag ) )
+        {
+            return SUCCESS;
+        }
+        
         OrganisationUnitLevel offlineOrgUnitLevel = configurationService.getConfiguration().getOfflineOrganisationUnitLevel();
 
         Integer level = offlineOrgUnitLevel != null ? offlineOrgUnitLevel.getLevel() : null;

=== 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-11-17 21:59:40 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2014-11-17 22:18:59 +0000
@@ -162,8 +162,10 @@
  */
 $( document ).ready( function()
 {
+    /**
+     * Cache false necessary to prevent IE from caching by default.
+     */
     $.ajaxSetup( {
-        type: 'POST',
         cache: false
     } );
 
@@ -291,6 +293,7 @@
 	$.ajax( {
     	url: 'getDataSetAssociations.action',
     	dataType: 'json',
+    	cache: true,
     	success: function( json )
 	    {
 	        sessionStorage[dhis2.de.cst.dataSetAssociations] = JSON.stringify( json.dataSetAssociations );