← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2149: Changed in reportService and DefaultReportService for Upward Reports

 

------------------------------------------------------------
revno: 2149
committer: neeraj
branch nick: trunk
timestamp: Fri 2010-09-17 14:45:35 +0530
message:
  Changed in reportService and DefaultReportService for Upward Reports
modified:
  local/in/dhis-in-api/src/main/java/org/hisp/dhis/reports/ReportService.java
  local/in/dhis-in-services/dhis-in-service-reports/src/main/java/org/hisp/dhis/reports/DefaultReportService.java


--
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 'local/in/dhis-in-api/src/main/java/org/hisp/dhis/reports/ReportService.java'
--- local/in/dhis-in-api/src/main/java/org/hisp/dhis/reports/ReportService.java	2010-08-28 10:15:38 +0000
+++ local/in/dhis-in-api/src/main/java/org/hisp/dhis/reports/ReportService.java	2010-09-17 09:15:35 +0000
@@ -88,23 +88,27 @@
     // ReportService Design 
     // -------------------------------------------------------------------------
 
-    public List<Period> getMonthlyPeriods( Date start, Date end );
-
-    public PeriodType getPeriodTypeObject( String periodTypeName );
-
-    public Period getPeriodByMonth( int month, int year, PeriodType periodType );
-
-    public List<OrganisationUnit> getAllChildren( OrganisationUnit selecteOU );
-
-    public PeriodType getDataElementPeriodType( DataElement de );
+    List<Period> getMonthlyPeriods( Date start, Date end );
+
+    PeriodType getPeriodTypeObject( String periodTypeName );
+
+    Period getPeriodByMonth( int month, int year, PeriodType periodType );
+
+    List<OrganisationUnit> getAllChildren( OrganisationUnit selecteOU );
+
+    PeriodType getDataElementPeriodType( DataElement de );
     
     // -------------------------------------------------------------------------
     // ReportService for Report Result Action
     // -------------------------------------------------------------------------
     
    // public List<String> getDECodes( String fileName );
-    public String getResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit , String reportModelTB );
-    public String getIndividualResultDataValue( String formula, Date startDate, Date endDate,OrganisationUnit organisationUnit , String reportModelTB );
-    public String getBooleanDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit , String reportModelTB );
+    String getResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit , String reportModelTB );
+    String getIndividualResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit, String reportModelTB );
+    String getResultIndicatorValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit );
+    String getIndividualResultIndicatorValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit );
+    String getBooleanDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit , String reportModelTB );
+    List<Calendar> getStartingEndingPeriods( String deType , Period selectedPeriod );
+    Period getPreviousPeriod( Period selectedPeriod );
 
 }

=== modified file 'local/in/dhis-in-services/dhis-in-service-reports/src/main/java/org/hisp/dhis/reports/DefaultReportService.java'
--- local/in/dhis-in-services/dhis-in-service-reports/src/main/java/org/hisp/dhis/reports/DefaultReportService.java	2010-08-28 10:15:38 +0000
+++ local/in/dhis-in-services/dhis-in-service-reports/src/main/java/org/hisp/dhis/reports/DefaultReportService.java	2010-09-17 09:15:35 +0000
@@ -25,6 +25,8 @@
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.indicator.IndicatorService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.Period;
@@ -70,6 +72,13 @@
     {
         this.jdbcTemplate = jdbcTemplate;
     }
+    
+    private IndicatorService indicatorService;
+
+    public void setIndicatorService( IndicatorService indicatorService )
+    {
+        this.indicatorService = indicatorService;
+    }
 
     private PeriodService periodService;
 
@@ -187,16 +196,7 @@
     // -------------------------------------------------------------------------
     // for Report Result Action input/otput
     // -------------------------------------------------------------------------
-/*    
-    private List<String> serviceType;
-    private List<String> deCodeType;
-    
-    serviceType = new ArrayList<String>();
-    deCodeType = new ArrayList<String>();
-    
-    
-*/   
-    
+
     
    // private String reportModelTB;
 
@@ -541,87 +541,15 @@
 
     } // getDataElementPeriodType end
     
-// // getDECodes Function  
-
-/*
-    public List<String> getDECodes( String fileName )
-    {
-        List<String> deCodes = new ArrayList<String>();
-        String path = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator + raFolderName
-            + File.separator + fileName;
-        try
-        {
-            String newpath = System.getenv( "DHIS2_HOME" );
-            if ( newpath != null )
-            {
-                path = newpath + File.separator + raFolderName + File.separator + fileName;
-            }
-        }
-        catch ( NullPointerException npe )
-        {
-            // do nothing, but we might be using this somewhere without
-            // USER_HOME set, which will throw a NPE
-        }
-
-        try
-        {
-            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
-            Document doc = docBuilder.parse( new File( path ) );
-            if ( doc == null )
-            {
-                // System.out.println( "There is no DECodes related XML file in
-                // the user home" );
-                return null;
-            }
-
-            NodeList listOfDECodes = doc.getElementsByTagName( "de-code" );
-            int totalDEcodes = listOfDECodes.getLength();
-
-            for ( int s = 0; s < totalDEcodes; s++ )
-            {
-                Element deCodeElement = (Element) listOfDECodes.item( s );
-                NodeList textDECodeList = deCodeElement.getChildNodes();
-                deCodes.add( ((Node) textDECodeList.item( 0 )).getNodeValue().trim() );
-                serviceType.add( deCodeElement.getAttribute( "stype" ) );
-                deCodeType.add( deCodeElement.getAttribute( "type" ) );
-                sheetList.add( new Integer( deCodeElement.getAttribute( "sheetno" ) ) );
-                rowList.add( new Integer( deCodeElement.getAttribute( "rowno" ) ) );
-                colList.add( new Integer( deCodeElement.getAttribute( "colno" ) ) );
-
-            }// end of for loop with s var
-        }// try block end
-        catch ( SAXParseException err )
-        {
-            System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() );
-            System.out.println( " " + err.getMessage() );
-        }
-        catch ( SAXException e )
-        {
-            Exception x = e.getException();
-            ((x == null) ? e : x).printStackTrace();
-        }
-        catch ( Throwable t )
-        {
-            t.printStackTrace();
-        }
-        return deCodes;
-    }// getDECodes end
-*/    
-    // functoin getResultDataValue start
     public String getResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit , String reportModelTB )
     {
         int deFlag1 = 0;
-//      int deFlag2 = 0;
+        int deFlag2 = 0;
         int isAggregated = 0;
-        isAggregated = 0;
 
         try
         {
             // 6b1b7b5b7b5b7
-            // System.out.println( "expression : " + formula + " ***** " +
-            // String.valueOf( startDate ) + " **** "
-            // + String.valueOf( endDate ) );
             Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
 
             Matcher matcher = pattern.matcher( formula );
@@ -658,22 +586,22 @@
                     if ( aggregatedValue == null )
                     {
                         replaceString = NULL_REPLACEMENT;
-                        //deFlag2 = 0;
+                        deFlag2 = 0;
                     }
                     else
                     {
                         replaceString = String.valueOf( aggregatedValue );
 
-                       // deFlag2 = 1;
+                        deFlag2 = 1;
 
-                        isAggregated = 0;
+                        isAggregated = 1;
                     }
 
                 }
                 else
                 {
                     deFlag1 = 1;
-                    //deFlag2 = 0;
+                    deFlag2 = 0;
                     PeriodType dePeriodType = getDataElementPeriodType( dataElement );
                     List<Period> periodList = new ArrayList<Period>( periodService.getIntersectingPeriodsByPeriodType(
                         dePeriodType, startDate, endDate ) );
@@ -766,7 +694,7 @@
             }
             else
             {
-                //deFlag2 = 0;
+                deFlag2 = 0;
                 resultValue = buffer.toString();
             }
 
@@ -794,8 +722,8 @@
     public String getIndividualResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit , String reportModelTB )
     {
         int deFlag1 = 0;
-       // int deFlag2 = 0;
-       // int isAggregated = 0;
+        int deFlag2 = 0;
+    //    int isAggregated = 0;
        
         try
         {
@@ -863,13 +791,13 @@
 
                         replaceString = String.valueOf( aggregatedValue );
 
-                       // deFlag2 = 1;
+                        deFlag2 = 1;
                     }
                 }
                 else
                 {
                     deFlag1 = 1;
-                   // deFlag2 = 0;
+                    deFlag2 = 0;
                     PeriodType dePeriodType = getDataElementPeriodType( dataElement );
                     List<Period> periodList = new ArrayList<Period>( periodService.getIntersectingPeriodsByPeriodType(
                         dePeriodType, startDate, endDate ) );
@@ -962,7 +890,7 @@
             }
             else
             {
-                //deFlag2 = 0;
+                deFlag2 = 0;
                 resultValue = buffer.toString();
             }
 
@@ -1130,4 +1058,310 @@
     }
     
  // functoin getBooleanDataValue end
+    
+    //function getStartingEndingPeriods starts
+    
+    public List<Calendar> getStartingEndingPeriods( String deType , Period selectedPeriod)
+    {
+
+        List<Calendar> calendarList = new ArrayList<Calendar>();
+
+        Calendar tempStartDate = Calendar.getInstance();
+        Calendar tempEndDate = Calendar.getInstance();
+
+        Period previousPeriod = new Period();
+        previousPeriod = getPreviousPeriod( selectedPeriod );
+
+        if ( deType.equalsIgnoreCase( "ccmcy" ) )
+        {
+            tempStartDate.setTime( selectedPeriod.getStartDate() );
+            if ( tempStartDate.get( Calendar.MONTH ) < Calendar.APRIL )
+            {
+                tempStartDate.roll( Calendar.YEAR, -1 );
+            }
+            tempStartDate.set( Calendar.MONTH, Calendar.APRIL );
+            tempEndDate.setTime( selectedPeriod.getEndDate() );
+            // System.out.println("CCMCY : "+ String.valueOf(
+            // tempStartDate.getTime()) +" ------ "+String.valueOf(
+            // tempEndDate.getTime()));
+        } else
+        {
+            if ( deType.equalsIgnoreCase( "cpmcy" ) )
+            {
+                tempStartDate.setTime( previousPeriod.getStartDate() );
+                if ( tempStartDate.get( Calendar.MONTH ) < Calendar.APRIL )
+                {
+                    tempStartDate.roll( Calendar.YEAR, -1 );
+                }
+                tempStartDate.set( Calendar.MONTH, Calendar.APRIL );
+                tempEndDate.setTime( previousPeriod.getEndDate() );
+            } else
+            {
+                if ( deType.equalsIgnoreCase( "cmpy" ) )
+                {
+                    tempStartDate.setTime( selectedPeriod.getStartDate() );
+                    tempEndDate.setTime( selectedPeriod.getEndDate() );
+
+                    tempStartDate.roll( Calendar.YEAR, -1 );
+                    tempEndDate.roll( Calendar.YEAR, -1 );
+                } else
+                {
+                    if ( deType.equalsIgnoreCase( "ccmpy" ) )
+                    {
+                        tempStartDate.setTime( selectedPeriod.getStartDate() );
+                        tempEndDate.setTime( selectedPeriod.getEndDate() );
+
+                        tempStartDate.roll( Calendar.YEAR, -1 );
+                        tempEndDate.roll( Calendar.YEAR, -1 );
+
+                        if ( tempStartDate.get( Calendar.MONTH ) < Calendar.APRIL )
+                        {
+                            tempStartDate.roll( Calendar.YEAR, -1 );
+                        }
+                        tempStartDate.set( Calendar.MONTH, Calendar.APRIL );
+
+                    } else
+                    {
+                        if ( deType.equalsIgnoreCase( "pmcy" ) )
+                        {
+                            tempStartDate.setTime( previousPeriod.getStartDate() );
+                            tempEndDate.setTime( previousPeriod.getEndDate() );
+
+                        } else
+                        {
+
+                            tempStartDate.setTime( selectedPeriod.getStartDate() );
+                            tempEndDate.setTime( selectedPeriod.getEndDate() );
+                        }
+                    }
+                }
+            }
+        }
+
+        // System.out.print(deType+" -- ");
+        calendarList.add( tempStartDate );
+        calendarList.add( tempEndDate );
+
+        return calendarList;
+    }
+  //function getStartingEndingPeriods end
+    
+    //function getPreviousPeriod starts
+    
+    public Period getPreviousPeriod( Period selectedPeriod )
+    {
+        Period period = new Period();
+        Calendar tempDate = Calendar.getInstance();
+        tempDate.setTime( selectedPeriod.getStartDate() );
+        if ( tempDate.get( Calendar.MONTH ) == Calendar.JANUARY )
+        {
+            tempDate.set( Calendar.MONTH, Calendar.DECEMBER );
+            tempDate.roll( Calendar.YEAR, -1 );
+
+        } else
+        {
+            tempDate.roll( Calendar.MONTH, -1 );
+        }
+        PeriodType periodType = getPeriodTypeObject( "monthly" );
+        period = getPeriodByMonth( tempDate.get( Calendar.MONTH ), tempDate.get( Calendar.YEAR ), periodType );
+
+        return period;
+    }
+    
+  //function getPreviousPeriod end
+    
+    public String getResultIndicatorValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit )
+        {
+    		int deFlag1 = 0;
+    		int deFlag2 = 0;
+    		
+            try
+            {
+
+                Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
+
+                Matcher matcher = pattern.matcher( formula );
+                StringBuffer buffer = new StringBuffer();
+
+                while ( matcher.find() )
+                {
+                    String replaceString = matcher.group();
+
+                    replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
+
+                    replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
+
+                    int indicatorId = Integer.parseInt( replaceString );
+
+                    Indicator indicator = indicatorService.getIndicator( indicatorId );
+
+                    if ( indicator == null )
+                    {
+                        replaceString = "";
+                        matcher.appendReplacement( buffer, replaceString );
+                        continue;
+
+                    }
+
+                    Double aggregatedValue = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate,
+                        organisationUnit );
+
+                    if ( aggregatedValue == null )
+                    {
+                        replaceString = NULL_REPLACEMENT;
+                    } else
+                    {
+                        replaceString = String.valueOf( aggregatedValue );
+                        deFlag2 = 1;
+                    }
+                    matcher.appendReplacement( buffer, replaceString );
+                }
+
+                matcher.appendTail( buffer );
+
+                String resultValue = "";
+                if ( deFlag1 == 0 )
+                {
+                    double d = 0.0;
+                    try
+                    {
+                        d = MathUtils.calculateExpression( buffer.toString() );
+                    } catch ( Exception e )
+                    {
+                        d = 0.0;
+                    }
+                    if ( d == -1 )
+                    {
+                        d = 0.0;
+                    } else
+                    {
+                        d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 );
+                        resultValue = "" + d;
+                    }
+
+                    if ( deFlag2 == 0 )
+                    {
+                        resultValue = " ";
+                    }
+                } else
+                {
+                    resultValue = buffer.toString();
+                    deFlag2 = 0;
+                }
+                return resultValue;
+            } catch ( NumberFormatException ex )
+            {
+                throw new RuntimeException( "Illegal DataElement id", ex );
+            }
+        }
+    public String getIndividualResultIndicatorValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit )
+        {
+    		int deFlag1 = 0;
+    		int deFlag2 = 0;
+            try
+            {
+                Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
+
+                Matcher matcher = pattern.matcher( formula );
+                StringBuffer buffer = new StringBuffer();
+
+                while ( matcher.find() )
+                {
+                    String replaceString = matcher.group();
+
+                    replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
+
+                    replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
+
+                    int indicatorId = Integer.parseInt( replaceString );
+
+                    Indicator indicator = indicatorService.getIndicator( indicatorId );
+
+                    if ( indicator == null )
+                    {
+                        replaceString = "";
+                        matcher.appendReplacement( buffer, replaceString );
+                        continue;
+
+                    }
+
+                    String numeratorExp = indicator.getNumerator();
+                    String denominatorExp = indicator.getDenominator();
+                    int indicatorFactor = indicator.getIndicatorType().getFactor();
+                    String reportModelTB = null;
+					String numeratorVal = getIndividualResultDataValue( numeratorExp, startDate, endDate, organisationUnit, reportModelTB  );
+                    String denominatorVal = getIndividualResultDataValue( denominatorExp, startDate, endDate, organisationUnit, reportModelTB );
+
+                    double numeratorValue;
+                    try
+                    {
+                        numeratorValue = Double.parseDouble( numeratorVal );
+                    } catch ( Exception e )
+                    {
+                        numeratorValue = 0.0;
+                    }
+
+                    double denominatorValue;
+                    try
+                    {
+                        denominatorValue = Double.parseDouble( denominatorVal );
+                    } catch ( Exception e )
+                    {
+                        denominatorValue = 1.0;
+                    }
+
+                    double aggregatedValue;
+                    try
+                    {
+                        aggregatedValue = ( numeratorValue / denominatorValue ) * indicatorFactor;
+                    } catch ( Exception e )
+                    {
+                        System.out.println( "Exception while calculating Indicator value for Indicaotr " + indicator.getName() );
+                        aggregatedValue = 0.0;
+                    }
+
+                    replaceString = String.valueOf( aggregatedValue );
+                    deFlag2 = 1;
+
+                    matcher.appendReplacement( buffer, replaceString );
+                }
+
+                matcher.appendTail( buffer );
+
+                String resultValue = "";
+                if ( deFlag1 == 0 )
+                {
+                    double d = 0.0;
+                    try
+                    {
+                        d = MathUtils.calculateExpression( buffer.toString() );
+                    } catch ( Exception e )
+                    {
+                        d = 0.0;
+                    }
+                    if ( d == -1 )
+                    {
+                        d = 0.0;
+                    } else
+                    {
+                        d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 );
+                        resultValue = "" + d;
+                    }
+
+                    if ( deFlag2 == 0 )
+                    {
+                        resultValue = " ";
+                    }
+                } else
+                {
+                    deFlag2 = 0;
+                    resultValue = buffer.toString();
+                }
+                return resultValue;
+            } catch ( NumberFormatException ex )
+            {
+                throw new RuntimeException( "Illegal DataElement id", ex );
+            }
+        }
+    
 }