← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2939: Fix bug: NPE when user does not click an orgunit in data analysis modules.

 

------------------------------------------------------------
revno: 2939
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-03-01 11:17:29 +0700
message:
  Fix bug: NPE when user does not click an orgunit in data analysis modules.
added:
  dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/ValidationRunAnalysisAction.java
modified:
  dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/GetAnalysisAction.java
  dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/org/hisp/dhis/validationrule/i18n_module.properties
  dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/dataAnalysisForm.vm
  dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/javascript/dataAnalysis.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-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/GetAnalysisAction.java'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/GetAnalysisAction.java	2011-02-10 23:09:13 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/GetAnalysisAction.java	2011-03-01 04:17:29 +0000
@@ -42,6 +42,7 @@
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.oust.manager.SelectionTreeManager;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 
@@ -58,7 +59,7 @@
     implements Action
 {
     private static final Log log = LogFactory.getLog( GetAnalysisAction.class );
-    
+
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -70,11 +71,11 @@
         this.serviceProvider = serviceProvider;
     }
 
-    private OrganisationUnitService organisationUnitService;
+    private SelectionTreeManager selectionTreeManager;
 
-    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
     {
-        this.organisationUnitService = organisationUnitService;
+        this.selectionTreeManager = selectionTreeManager;
     }
 
     private PeriodService periodService;
@@ -129,37 +130,37 @@
     }
 
     private Collection<String> dataSets;
-    
+
     public void setDataSets( Collection<String> dataSets )
     {
         this.dataSets = dataSets;
     }
 
-    private Integer organisationUnit;
-    
-    public void setOrganisationUnit( Integer organisationUnit )
-    {
-        this.organisationUnit = organisationUnit;
-    }
-
     private Double standardDeviation;
 
     public void setStandardDeviation( Double standardDeviation )
     {
         this.standardDeviation = standardDeviation;
     }
-    
+
+    private String message;
+
+    public String getMessage()
+    {
+        return message;
+    }
+
     // -------------------------------------------------------------------------
     // Output
     // -------------------------------------------------------------------------
-    
+
     private Collection<DeflatedDataValue> dataValues = new ArrayList<DeflatedDataValue>();
 
     public Collection<DeflatedDataValue> getDataValues()
     {
         return dataValues;
     }
-    
+
     private boolean maxExceeded;
 
     public boolean isMaxExceeded()
@@ -175,9 +176,9 @@
     {
         Set<DataElement> dataElements = new HashSet<DataElement>();
         Collection<Period> periods = null;
-        OrganisationUnit unit = null;
-        
-        if ( fromDate != null && toDate != null && dataSets != null && organisationUnit != null )
+        OrganisationUnit unit = selectionTreeManager.getReloadedSelectedOrganisationUnit();;
+
+        if ( fromDate != null && toDate != null && dataSets != null )
         {
             periods = periodService.getPeriodsBetweenDates( format.parseDate( fromDate ), format.parseDate( toDate ) );
 
@@ -185,22 +186,22 @@
             {
                 dataElements.addAll( dataSetService.getDataSet( Integer.parseInt( id ) ).getDataElements() );
             }
-        
-            unit = organisationUnitService.getOrganisationUnit( organisationUnit );
-            
-            log.info( "From date: " + fromDate + ", To date: " + toDate + ", Organisation unit: " + unit + ", Std dev: " + standardDeviation + ", Key: " + key );
+
+
+            log.info( "From date: " + fromDate + ", To date: " + toDate + ", Organisation unit: " + unit
+                + ", Std dev: " + standardDeviation + ", Key: " + key );
             log.info( "Nr of data elements: " + dataElements.size() + " Nr of periods: " + periods.size() );
         }
-        
+
         DataAnalysisService service = serviceProvider.provide( key );
 
         if ( service != null ) // Follow-up analysis has no input params
-        {      
+        {
             dataValues = service.analyse( unit, dataElements, periods, standardDeviation );
-            
+
             maxExceeded = dataValues.size() > DataAnalysisService.MAX_OUTLIERS;
         }
-        
+
         return SUCCESS;
     }
 }

=== added file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/ValidationRunAnalysisAction.java'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/ValidationRunAnalysisAction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/dataanalysis/ValidationRunAnalysisAction.java	2011-03-01 04:17:29 +0000
@@ -0,0 +1,146 @@
+/*
+ * 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.
+ */
+
+package org.hisp.dhis.validationrule.action.dataanalysis;
+
+import java.util.Date;
+
+import org.hisp.dhis.i18n.I18n;
+import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.oust.manager.SelectionTreeManager;
+
+import com.opensymphony.xwork2.ActionSupport;
+
+/**
+ * @author Chau Thu Tran
+ * @version $Id ValidationRunAnalysisAction.java Mar 1, 2011 9:54:31 AM $
+ */
+public class ValidationRunAnalysisAction
+    extends ActionSupport
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private SelectionTreeManager selectionTreeManager;
+
+    public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
+    {
+        this.selectionTreeManager = selectionTreeManager;
+    }
+
+    private I18nFormat format;
+
+    public void setFormat( I18nFormat format )
+    {
+        this.format = format;
+    }
+
+    // -------------------------------------------------------------------------
+    // Input
+    // -------------------------------------------------------------------------
+
+    private I18n i18n;
+
+    public void setI18n( I18n i18n )
+    {
+        this.i18n = i18n;
+    }
+    
+    private String fromDate;
+
+    public void setFromDate( String fromDate )
+    {
+        this.fromDate = fromDate;
+    }
+
+    private String toDate;
+
+    public void setToDate( String toDate )
+    {
+        this.toDate = toDate;
+    }
+
+    // -------------------------------------------------------------------------
+    // Output
+    // -------------------------------------------------------------------------
+
+    private String message;
+
+    public String getMessage()
+    {
+        return message;
+    }
+
+    // -------------------------------------------------------------------------
+    // Action implementation
+    // -------------------------------------------------------------------------
+
+    public String execute()
+        throws Exception
+    {
+        OrganisationUnit selectedOrganisationUnit = selectionTreeManager.getReloadedSelectedOrganisationUnit();
+
+        if ( selectedOrganisationUnit == null )
+        {
+            message = i18n.getString( "specify_organisationunit" );
+
+            return INPUT;
+        }
+
+        Date sDate = format.parseDate( fromDate.trim() );
+
+        if ( sDate == null )
+        {
+            message = i18n.getString( "enter_a_valid_start_date" );
+
+            return INPUT;
+        }
+
+        Date eDate = format.parseDate( toDate.trim() );
+
+        if ( eDate == null )
+        {
+            message = i18n.getString( "enter_a_valid_ending_date" );
+
+            return INPUT;
+        }
+
+        if ( eDate.before( sDate ) )
+        {
+            message = i18n.getString( "end_date_cannot_be_before_start_date" );
+
+            return INPUT;
+        }
+
+        message = i18n.getString( "everything_is_ok" );
+
+        return SUCCESS;
+    }
+
+}

=== modified file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/META-INF/dhis/beans.xml	2011-01-25 19:26:03 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/META-INF/dhis/beans.xml	2011-03-01 04:17:29 +0000
@@ -248,8 +248,8 @@
     scope="prototype">
 	<property name="serviceProvider"
 	  ref="dataAnalysisServiceProvider"/>
-    <property name="organisationUnitService"
-      ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+    <property name="selectionTreeManager"
+      ref="org.hisp.dhis.oust.manager.SelectionTreeManager" />
     <property name="periodService" 
       ref="org.hisp.dhis.period.PeriodService" />
     <property name="dataSetService" 
@@ -288,6 +288,13 @@
       ref="org.hisp.dhis.dataelement.DataElementCategoryService"/>
   </bean>	
 
+ <bean id="org.hisp.dhis.validationrule.action.dataanalysis.ValidationRunAnalysisAction"
+    class="org.hisp.dhis.validationrule.action.dataanalysis.ValidationRunAnalysisAction" 
+    scope="prototype">
+    <property name="selectionTreeManager">
+      <ref bean="org.hisp.dhis.oust.manager.SelectionTreeManager"/>
+    </property>
+  </bean>
   <!-- PDF -->
 
   <bean id="org.hisp.dhis.validationrule.action.pdf.ExportToPdfAction" 

=== modified file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/org/hisp/dhis/validationrule/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/org/hisp/dhis/validationrule/i18n_module.properties	2011-02-10 23:09:13 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/org/hisp/dhis/validationrule/i18n_module.properties	2011-03-01 04:17:29 +0000
@@ -146,4 +146,5 @@
 get_report_as_xls = Download as Excel
 get_report_as_csv = Download as CSV
 get_report_as_pdf = Download as PDF
-max_values_exceeded = More than 500 values found, please fix the violations or narrow the search to see all
\ No newline at end of file
+max_values_exceeded = More than 500 values found, please fix the violations or narrow the search to see all
+specify_dataset = Please specify data sets
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/struts.xml	2011-01-22 21:02:10 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/resources/struts.xml	2011-03-01 04:17:29 +0000
@@ -205,6 +205,12 @@
 	  <result name="success" type="velocity-xml">/dhis-web-validationrule/responseSuccess.vm</result>
 	</action>
 	
+	 <action name="validateRunAnalysis" class="org.hisp.dhis.validationrule.action.dataanalysis.ValidationRunAnalysisAction" >
+      <result name="success" type="velocity-xml">/dhis-web-validationrule/responseSuccess.vm</result>
+      <result name="error" type="velocity-xml">/dhis-web-validationrule/responseError.vm</result>
+      <result name="input" type="velocity-xml">/dhis-web-validationrule/responseInput.vm</result>
+    </action>
+    
 	<!-- PDF -->
 
 	<action name="exportToPdf" class="org.hisp.dhis.validationrule.action.pdf.ExportToPdfAction">

=== modified file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/dataAnalysisForm.vm'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/dataAnalysisForm.vm	2010-07-04 23:55:46 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/dataAnalysisForm.vm	2011-03-01 04:17:29 +0000
@@ -107,7 +107,7 @@
     </tr>
 	<tr>
 		<td>
-			<input type="button" onclick="analyseData()" value="$i18n.getString( 'start' )" style="width:120px" />
+			<input type="button" onclick="validateRunAnalyseData()" value="$i18n.getString( 'start' )" style="width:120px" />
 		</td>
 	</tr>
 </table>
@@ -116,6 +116,9 @@
 
 <script type="text/javascript">
   var i18n_analysing_please_wait = '$encoder.jsEscape( $i18n.getString( "analysing_please_wait" ) , "'")';
+  var i18n_specify_dataset = '$encoder.jsEscape( $i18n.getString( "specify_dataset" ) , "'")';
+  var i18n_specify_a_start_date = '$encoder.jsEscape( $i18n.getString( "specify_a_start_date" ) , "'")';
+  var i18n_specify_an_ending_date = '$encoder.jsEscape( $i18n.getString( "specify_an_ending_date" ) , "'")';
   
   jQuery(document).ready(function(){
 		datePickerInRange( 'fromDate' , 'toDate' );	

=== modified file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/javascript/dataAnalysis.js'
--- dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/javascript/dataAnalysis.js	2010-12-29 21:48:41 +0000
+++ dhis-2/dhis-web/dhis-web-validationrule/src/main/webapp/dhis-web-validationrule/javascript/dataAnalysis.js	2011-03-01 04:17:29 +0000
@@ -6,51 +6,88 @@
 	selectedOrganisationUnit = organisationUnits[0];
 }
 
-function analyseData()
+function validateRunAnalyseData()
 {
-	if ( analysisFormIsValid() == true )
+	if( analyseDataInvalid() )
 	{
-		setWaitMessage( i18n_analysing_please_wait );
-		
-		var url = "getAnalysis.action" +
-			"?key=" + $( "#key" ).val() +
-			"&toDate=" + $( "#toDate" ).val() + 
-			"&fromDate=" + $( "#fromDate" ).val() +
-			"&organisationUnit=" + selectedOrganisationUnit +
-			"&" + getParamString( "dataSets", "dataSets" );
-			
-		if ( byId( "standardDeviation" ) != null )
-		{
-			url += "&standardDeviation=" + $( "#standardDeviation" ).val();
-		}
-		
-		$.get( url, function( data ) {
-			$( "div#analysisInput" ).hide();
-			$( "div#analysisResult" ).show();
-			$( "div#analysisResult" ).html( data );
-		} );
+		$.post("validateRunAnalysis.action",
+		{
+			fromDate: getFieldValue( 'fromDate' ),
+			toDate: getFieldValue( 'toDate' )
+		},
+		function (data)
+		{
+			runValidationCompleted(data);
+		},'xml');
 	}
 }
 
-function analysisFormIsValid()
-{	
+function analyseDataInvalid ()
+{
+	if( $('#fromDate').val().length == 0 )
+	{
+		setMessage(i18n_specify_a_start_date);
+		return false;
+	}
+	
+	if( $('#toDate').val().length == 0 )
+	{
+		setMessage(i18n_specify_an_ending_date);
+		return false;
+	}
+	
 	var dataSets = document.getElementById( "dataSets" );
 	
 	if ( dataSets.options.length == 0 )
 	{
-		setMessage( "Please select at least one data set" );
-		return false;
-	}
-	
-	if ( selectedOrganisationUnit == null )
-	{
-		setMessage( "Please select an organisation unit" );
+		setMessage( i18n_specify_dataset );
 		return false;
 	}
 	
 	return true;
 }
 
+function runValidationCompleted( messageElement )
+{ 
+	var type = messageElement.firstChild.getAttribute( 'type' );
+	var message = messageElement.firstChild.firstChild.nodeValue;
+
+	if ( type == 'success' )
+	{
+		analyseData();
+	}
+	else if ( type == 'error' )
+	{
+		window.alert( i18n_validation_failed + ':' + '\n' + message );
+	}
+	else if ( type == 'input' )
+	{
+		setMessage( message );
+	} 
+}
+
+function analyseData()
+{
+	setWaitMessage( i18n_analysing_please_wait );
+	
+	var url = "getAnalysis.action" +
+		"?key=" + $( "#key" ).val() +
+		"&toDate=" + $( "#toDate" ).val() + 
+		"&fromDate=" + $( "#fromDate" ).val() +
+		"&" + getParamString( "dataSets", "dataSets" );
+		
+	if ( byId( "standardDeviation" ) != null )
+	{
+		url += "&standardDeviation=" + $( "#standardDeviation" ).val();
+	}
+	
+	$.get( url, function( data ) {
+		$( "div#analysisInput" ).hide();
+		$( "div#analysisResult" ).show();
+		$( "div#analysisResult" ).html( data );
+	} );
+}
+
 function getFollowUpAnalysis()
 {
 	setWaitMessage( i18n_analysing_please_wait );