← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1605: Cannot generate calculated data element in Excel report.

 

------------------------------------------------------------
revno: 1605
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: trunk
timestamp: Wed 2010-03-10 14:12:56 +0700
message:
  Cannot generate calculated data element in Excel report.
modified:
  dhis-2/dhis-web/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/export/action/GenerateReportSupport.java
  dhis-2/dhis-web/dhis-web-excel-reporting/src/main/resources/META-INF/dhis/beans.xml


--
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-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/export/action/GenerateReportSupport.java'
--- dhis-2/dhis-web/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/export/action/GenerateReportSupport.java	2010-01-06 08:24:42 +0000
+++ dhis-2/dhis-web/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/export/action/GenerateReportSupport.java	2010-03-10 07:12:56 +0000
@@ -36,7 +36,10 @@
 import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
+import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -53,11 +56,14 @@
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.hisp.dhis.aggregation.AggregationService;
+import org.hisp.dhis.dataelement.CalculatedDataElement;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.datamart.DataMartStore;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.indicator.IndicatorService;
@@ -83,8 +89,8 @@
 /**
  * @author Dang Duy Hieu
  * @author Tran Thanh Tri
- * @version $Id$
  * @since 2009-09-18
+ * @version $Id: GenerateReportSupport.java 6216 2010-03-10 13:40:42Z Chau Thu Tran$
  */
 public abstract class GenerateReportSupport
     implements Action
@@ -142,6 +148,8 @@
 
     protected OrganisationUnitSelectionManager organisationUnitSelectionManager;
 
+    protected DataValueService dataValueService;
+
     // -------------------------------------------
     // Input & Output
     // -------------------------------------------
@@ -179,6 +187,11 @@
         this.indicatorService = indicatorService;
     }
 
+    public void setDataValueService( DataValueService dataValueService )
+    {
+        this.dataValueService = dataValueService;
+    }
+
     public void setCurrentUserService( CurrentUserService currentUserService )
     {
         this.currentUserService = currentUserService;
@@ -276,6 +289,8 @@
 
     protected Date endSixMonthly;
 
+    private Period period;
+
     // ------------------------------------------
     // Excel format
     // ------------------------------------------
@@ -380,6 +395,9 @@
 
     protected void installPeriod( Period period )
     {
+
+        this.period = period;
+
         Calendar calendar = Calendar.getInstance();
 
         // Monthly period
@@ -447,7 +465,7 @@
         this.initExcelFormat();
 
         this.installDefaultExcelFormat();
-        
+
         this.initFormulaEvaluating();
 
         ExcelUtils.writeValueByPOI( reportExcel.getOrganisationRow(), reportExcel.getOrganisationColumn(),
@@ -631,23 +649,78 @@
                 DataElementCategoryOptionCombo optionCombo = categoryService
                     .getDataElementCategoryOptionCombo( optionComboId );
 
-                double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo,
-                    startDate, endDate, organisationUnit );
-
-                if ( aggregatedValue == AggregationService.NO_VALUES_REGISTERED )
-                {
-                    replaceString = NULL_REPLACEMENT;
-                }
-                else
-                {
-                    replaceString = String.valueOf( aggregatedValue );
-                }
-
-                matcher.appendReplacement( buffer, replaceString );
+                if ( !(dataElement instanceof CalculatedDataElement) )
+                {
+
+                    double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo,
+                        startDate, endDate, organisationUnit );
+
+                    if ( aggregatedValue == AggregationService.NO_VALUES_REGISTERED )
+                    {
+                        replaceString = NULL_REPLACEMENT;
+                    }
+                    else
+                    {
+                        replaceString = String.valueOf( aggregatedValue );
+                    }
+
+                    matcher.appendReplacement( buffer, replaceString );
+
+                    matcher.appendTail( buffer );
+
+                }
+
+                else if ( dataElement instanceof CalculatedDataElement )
+                {
+                    CalculatedDataElement calculatedDataElement = (CalculatedDataElement) dataElement;
+
+                    Collection<DataElement> dataElements = dataElement.getDataSets().iterator().next()
+                        .getDataElements();
+
+                    Collection<DataValue> dataValues = dataValueService.getDataValues( organisationUnit, period,
+                        dataElements );
+
+                    Map<Integer, DataValue> dataValueMap = new HashMap<Integer, DataValue>( dataValues.size() );
+
+                    for ( DataValue dataValue : dataValues )
+                    {
+                        dataValueMap.put( dataValue.getDataElement().getId(), dataValue );
+                    }
+
+                    int factor = 0;
+
+                    int value = 0;
+
+                    Map<String, Integer> factorMap = dataElementService.getOperandFactors( calculatedDataElement );
+
+                    Collection<String> operandIds = dataElementService.getOperandIds( calculatedDataElement );
+
+                    for ( String operandId : operandIds )
+                    {
+                        factor = factorMap.get( operandId );
+
+                        dataElementIdString = operandId.substring( 0, operandId.indexOf( SEPARATOR ) );
+                        optionComboIdString = operandId.substring( operandId.indexOf( SEPARATOR ) + 1, operandId
+                            .length() );
+
+                        DataElement element = dataElementService.getDataElement( Integer.parseInt( dataElementIdString ) );
+                        optionCombo = categoryService.getDataElementCategoryOptionCombo( Integer
+                            .parseInt( optionComboIdString ) );
+
+                        DataValue dataValue = dataValueService.getDataValue( organisationUnit, element, period, optionCombo );
+
+                        if ( dataValue != null )
+                        {
+                            value += Integer.parseInt( dataValue.getValue() ) * factor;
+                        }
+                    }
+                    
+                    buffer.append( value );
+                    
+                    break;
+                }
             }
-
-            matcher.appendTail( buffer );
-
+            
             return buffer.toString();
         }
         catch ( NumberFormatException ex )

=== modified file 'dhis-2/dhis-web/dhis-web-excel-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-excel-reporting/src/main/resources/META-INF/dhis/beans.xml	2010-03-05 08:27:33 +0000
+++ dhis-2/dhis-web/dhis-web-excel-reporting/src/main/resources/META-INF/dhis/beans.xml	2010-03-10 07:12:56 +0000
@@ -455,6 +455,7 @@
 			ref="org.hisp.dhis.reportexcel.preview.manager.InitializePOIStylesManager" />
 		<property name="organisationUnitService"
 			ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+		<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
 	</bean>
 
 	<bean
@@ -480,6 +481,7 @@
 			ref="org.hisp.dhis.reportexcel.period.db.PeriodDatabaseService" />
 		<property name="initPOIStylesManager"
 			ref="org.hisp.dhis.reportexcel.preview.manager.InitializePOIStylesManager" />
+		<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
 	</bean>
 
 	<bean
@@ -505,6 +507,7 @@
 			ref="org.hisp.dhis.reportexcel.period.db.PeriodDatabaseService" />
 		<property name="initPOIStylesManager"
 			ref="org.hisp.dhis.reportexcel.preview.manager.InitializePOIStylesManager" />
+		<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
 	</bean>
 
 
@@ -531,6 +534,7 @@
 			ref="org.hisp.dhis.reportexcel.period.db.PeriodDatabaseService" />
 		<property name="initPOIStylesManager"
 			ref="org.hisp.dhis.reportexcel.preview.manager.InitializePOIStylesManager" />
+		<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
 	</bean>
 
 	<!-- DATA ENTRY STATUS BEAN -->
@@ -1332,7 +1336,7 @@
 	<bean
 		id="org.hisp.dhis.reportexcel.filemanager.action.ValidateUploadExcelTemplate"
 		class="org.hisp.dhis.reportexcel.filemanager.action.ValidateUploadExcelTemplate"
-		scope="prototype">		
+		scope="prototype">
 		<property name="reportLocationManager"
 			ref="org.hisp.dhis.reportexcel.ReportLocationManager" />
 	</bean>