← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7675: Generified rewind relative period functions.

 

Merge authors:
  Jan Henrik Øverland (janhenrik-overland)
------------------------------------------------------------
revno: 7675 [merge]
committer: Jan Henrik Overland <janhenrik.overland@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-07-23 18:32:49 +0200
message:
  Generified rewind relative period functions.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartPluginController.java
  dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/css/style.css
  dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/scripts/app.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-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java	2012-07-02 19:27:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -180,4 +180,16 @@
     {
         return ISO_FORMAT;
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.MONTH, (rewindedPeriods * -2) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -188,4 +188,16 @@
     {
         return ISO_FORMAT;
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.DAY_OF_YEAR, (rewindedPeriods * -1) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java	2012-07-09 19:56:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java	2012-07-23 16:26:34 +0000
@@ -178,4 +178,16 @@
         cal.set( Calendar.DAY_OF_MONTH, 31 );
         return createPeriod( cal );
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.YEAR, (rewindedPeriods * -1) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -186,4 +186,16 @@
     {
         return ISO_FORMAT;
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.MONTH, (rewindedPeriods * -1) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2012-07-09 15:34:16 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2012-07-23 16:26:34 +0000
@@ -187,6 +187,13 @@
      */
     public abstract int getFrequencyOrder();
 
+    /**
+     * Returns a new date rewinded from now.
+     *
+     * @return the Date.
+     */    
+    public abstract Date getRewindedDate( Date date, Integer rewindedPeriods );
+
     // -------------------------------------------------------------------------
     // Calendar support
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -226,4 +226,16 @@
             }
         }
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.MONTH, (rewindedPeriods * -3) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2012-07-18 14:27:53 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2012-07-23 16:26:34 +0000
@@ -337,6 +337,63 @@
     }
 
     /**
+     * Gets a list of Periods rewinded from current date.
+     */
+    public List<Period> getRewindedRelativePeriods()
+    {
+        return getRewindedRelativePeriods( null );
+    }
+
+    /**
+     * Gets the PeriodType with the highest frequency from a list of Periods.
+     */
+    public PeriodType getHighestFrequencyPeriodType( List<Period> periods )
+    {        
+        if ( periods != null )
+        {            
+            PeriodType periodType = periods.get( 0 ).getPeriodType();
+            
+            int lowestFrequencyOrder = periodType.getFrequencyOrder();
+            
+            for ( Period period : periods )
+            {
+                if ( period.getPeriodType().getFrequencyOrder() < lowestFrequencyOrder )
+                {
+                    lowestFrequencyOrder = period.getPeriodType().getFrequencyOrder();
+                    
+                    periodType = period.getPeriodType();
+                }
+            }
+            
+            return periodType;
+        }
+        
+        return null;
+    }
+
+    /**
+     * Gets a list of Periods rewinded from current date.
+     */
+    public List<Period> getRewindedRelativePeriods( Integer rewindedPeriods )
+    {
+        List<Period> periods = getRelativePeriods();
+        
+        PeriodType periodType = getHighestFrequencyPeriodType( periods );
+        
+        Date rewindedDate = periodType.getRewindedDate( null, rewindedPeriods );
+        
+        return getRelativePeriods( rewindedDate, null, false );
+    }
+
+    /**
+     * Gets a list of Periods based on the given input and the state of this
+     * RelativePeriods. The current date is set to todays date minus one month.
+     *
+     * @param format the i18n format.
+     * @return a list of relative Periods.
+     */
+
+    /**
      * Gets a list of Periods relative to current date.
      */
     public List<Period> getRelativePeriods()
@@ -367,8 +424,8 @@
      * @return a list of relative Periods.
      */
     public List<Period> getRelativePeriods( Date date, I18nFormat format, boolean dynamicNames )
-    {
-        date = date == null ? subtractMonth( 1, new Date() ) : date;
+    {        
+        date = date == null ? subtractMonth( 1, new Date() ) : subtractMonth( 1, date );
         
         List<Period> periods = new ArrayList<Period>();
 
@@ -471,144 +528,6 @@
         
         return periods;
     }
-
-    /**
-     * Gets a list of Periods based on the given input and the state of this
-     * RelativePeriods.
-     *
-     * @param date the date representing now. If null the current date will be
-     *        used and an interval based on the period type will be subtracted 
-     *        from the date.
-     * @param format the i18n format.
-     * @return a list of relative Periods.
-     */
-    public List<Period> getRelativePeriods( I18nFormat format, boolean dynamicNames, Integer rewindedPeriods )
-    {
-        rewindedPeriods = rewindedPeriods == null ? 0 : rewindedPeriods;
-    
-        Date date = subtractMonth( 1, new Date() );
-        
-        List<Period> periods = new ArrayList<Period>();
-        
-        boolean rewind = true;
-
-        if ( isReportingMonth() )
-        {
-            periods.add( getRelativePeriod( new MonthlyPeriodType(), REPORTING_MONTH, rewind ? subtractMonth( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isReportingBimonth() )
-        {
-            periods.add( getRelativePeriod( new BiMonthlyPeriodType(), REPORTING_BIMONTH, date, dynamicNames, format ) );
-        }
-
-        if ( isReportingQuarter() )
-        {
-            periods.add( getRelativePeriod( new QuarterlyPeriodType(), REPORTING_QUARTER, rewind ? subtractQuarters( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isLastSixMonth() )
-        {
-            periods.add( getRelativePeriod( new SixMonthlyPeriodType(), LAST_SIXMONTH, rewind ? subtractSixmonths( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isMonthsThisYear() )
-        {
-            periods.addAll( getRelativePeriodList( new MonthlyPeriodType(), MONTHS_THIS_YEAR, date, dynamicNames, format ) );
-        }
-
-        if ( isQuartersThisYear() )
-        {
-            periods.addAll( getRelativePeriodList( new QuarterlyPeriodType(), QUARTERS_THIS_YEAR, date, dynamicNames, format ) );
-        }
-
-        if ( isThisYear() )
-        {
-            periods.add( getRelativePeriod( new YearlyPeriodType(), THIS_YEAR, rewind ? subtractYears( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isLast5Years() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new YearlyPeriodType(), LAST_5_YEARS, rewind ? subtractYears( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isLast12Months() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new MonthlyPeriodType(), MONTHS_LAST_12, rewind ? subtractMonth( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isLast6BiMonths() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new BiMonthlyPeriodType(), BIMONTHS_LAST_6, date, dynamicNames, format ) );
-        }
-
-        if ( isLast4Quarters() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new QuarterlyPeriodType(), QUARTERS_THIS_YEAR, rewind ? subtractQuarters( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isLast2SixMonths() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new SixMonthlyPeriodType(), SIXMONHTS_LAST_2, rewind ? subtractSixmonths( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-
-        if ( isThisFinancialYear() )
-        {
-            periods.add( getRelativePeriod( new FinancialJulyPeriodType(), THIS_FINANCIAL_YEAR, date, dynamicNames, format ) );
-        }
-
-        if ( isLast5FinancialYears() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new FinancialJulyPeriodType(), LAST_5_FINANCIAL_YEARS, date, dynamicNames, format ) );
-        }
-
-        if ( isLast52Weeks() )
-        {
-            periods.addAll( getRollingRelativePeriodList( new WeeklyPeriodType(), WEEKS_LAST_52, date, dynamicNames, format ) );
-        }
-        
-        date = subtractMonth( MONTHS_IN_YEAR, date );
-
-        if ( isMonthsLastYear() )
-        {
-            periods.addAll( getRelativePeriodList( new MonthlyPeriodType(), MONTHS_LAST_YEAR, date, dynamicNames, format ) );
-        }
-
-        if ( isQuartersLastYear() )
-        {
-            periods.addAll( getRelativePeriodList( new QuarterlyPeriodType(), QUARTERS_LAST_YEAR, date, dynamicNames, format ) );
-        }
-
-        if ( isLastYear() )
-        {
-            periods.add( getRelativePeriod( new YearlyPeriodType(), LAST_YEAR, rewind ? subtractYears( rewindedPeriods, date ) : date, dynamicNames, format ) );
-            
-            rewind = false;
-        }
-        
-        if ( isLastFinancialYear() )
-        {
-            periods.add( getRelativePeriod( new FinancialJulyPeriodType(), LAST_FINANCIAL_YEAR, date, dynamicNames, format ) );
-        }
-        
-        return periods;
-    }
     
     /**
      * Returns periods for the last 6 months based on the given period types.
@@ -776,51 +695,6 @@
         return cal.getTime();
     }
 
-    /**
-     * Returns a date.
-     *
-     * @param months the number of months to subtract from the current date.
-     * @param date the date representing now, ignored if null.
-     * @return a date.
-     */
-    private Date subtractQuarters( int quarters, Date date )
-    {
-        Calendar cal = PeriodType.createCalendarInstance( date );
-        cal.add( Calendar.MONTH, (quarters * 3 * -1) );
-
-        return cal.getTime();
-    }
-
-    /**
-     * Returns a date.
-     *
-     * @param months the number of months to subtract from the current date.
-     * @param date the date representing now, ignored if null.
-     * @return a date.
-     */
-    private Date subtractSixmonths( int sixmonths, Date date )
-    {
-        Calendar cal = PeriodType.createCalendarInstance( date );
-        cal.add( Calendar.MONTH, (sixmonths * 6 * -1) );
-
-        return cal.getTime();
-    }
-
-    /**
-     * Returns a date.
-     *
-     * @param months the number of months to subtract from the current date.
-     * @param date the date representing now, ignored if null.
-     * @return a date.
-     */
-    private Date subtractYears( int years, Date date )
-    {
-        Calendar cal = PeriodType.createCalendarInstance( date );
-        cal.add( Calendar.YEAR, (years * -1) );
-
-        return cal.getTime();
-    }
-
     // -------------------------------------------------------------------------
     // Getters & setters
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -222,4 +222,16 @@
             }
         }
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.MONTH, (rewindedPeriods * -6) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -199,4 +199,16 @@
     {
         return null; // TODO
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.YEAR, (rewindedPeriods * -2) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -258,4 +258,16 @@
     {
         return ISO_FORMAT;
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.DAY_OF_YEAR, (rewindedPeriods * -7) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java	2012-05-04 16:36:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java	2012-07-23 16:26:34 +0000
@@ -188,4 +188,16 @@
     {
         return ISO_FORMAT;
     }
+    
+    @Override
+    public Date getRewindedDate( Date date, Integer rewindedPeriods )
+    {
+        date = date != null ? date : new Date();        
+        rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
+
+        Calendar cal = createCalendarInstance( date );        
+        cal.add( Calendar.YEAR, (rewindedPeriods * -1) );
+
+        return cal.getTime();
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartPluginController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartPluginController.java	2012-07-18 14:27:53 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartPluginController.java	2012-07-23 16:26:34 +0000
@@ -133,7 +133,8 @@
         // Periods
         // ---------------------------------------------------------------------
         
-        List<Period> periods = relativePeriods.getRelativePeriods( null, false, rewind ? 1 : 0 );
+        List<Period> periods = rewind ? relativePeriods.getRewindedRelativePeriods() :
+            relativePeriods.getRelativePeriods();
 
         if ( p != null && p.size() > 0 )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/css/style.css'
--- dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/css/style.css	2012-07-18 14:27:53 +0000
+++ dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/css/style.css	2012-07-23 16:26:34 +0000
@@ -534,12 +534,17 @@
 }
 
 /* Label heading periods */
-.dv-label-period-heading {
+.dv-label-period-heading, .dv-label-period-heading-options {
     font-size: 11px;
     font-weight: bold;
     color: #333;
     line-height: 20px;
 }
+.dv-label-period-heading-options {
+    background-color: #e1e1e1;
+    padding: 1px 5px;
+    border-radius: 2px
+}
 
 /* Grid datatable columns */
 .x-grid-header-ct {

=== modified file 'dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/scripts/app.js'
--- dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/scripts/app.js	2012-07-18 14:27:53 +0000
+++ dhis-2/dhis-web/dhis-web-visualizer/src/main/webapp/dhis-web-visualizer/app/scripts/app.js	2012-07-23 11:46:49 +0000
@@ -1600,6 +1600,7 @@
 						}
 						
 						DV.c.relativeperiod.rp = f.relativePeriods;
+						DV.c.relativeperiod.rewind = f.rewindRelativePeriods;
 						
 						if (f.periods) {
 							for (var i = 0; i < f.periods.length; i++) {
@@ -3299,8 +3300,7 @@
 															{
 																xtype: 'label',
 																text: 'Options',
-																cls: 'dv-label-period-heading',
-																style: 'color:#666'
+																cls: 'dv-label-period-heading-options'
 															},
 															{
 																xtype: 'checkbox',