← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6883: Implemented support for weekly periods in data mart and scheduled tasks

 

------------------------------------------------------------
revno: 6883
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-05-08 20:53:35 +0200
message:
  Implemented support for weekly periods in data mart and scheduled tasks
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm
  dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/tablecreator/action/SaveTableAction.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataMart.js
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/viewDataMartForm.vm


--
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/RelativePeriods.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2012-05-08 16:00:25 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2012-05-08 18:53:35 +0000
@@ -139,7 +139,14 @@
         "financial_year_minus_2",
         "financial_year_minus_1",
         "financial_year_this" };
-
+    
+    public static final String[] WEEKS_LAST_52 = {
+        "w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "w10", 
+        "w11", "w12", "w13", "w14", "w15", "w16", "w17", "w18", "w19", "w20",
+        "w21", "w22", "w23", "w24", "w25", "w26", "w27", "w28", "w29", "w30", 
+        "w31", "w32", "w33", "w34", "w35", "w36", "w37", "w38", "w39", "w40",
+        "w41", "w42", "w43", "w44", "w45", "w46", "w47", "w48", "w49", "w50", "w51", "w52" };
+    
     private static final int MONTHS_IN_YEAR = 12;
 
     private boolean reportingMonth = false; // TODO rename to lastMonth
@@ -177,6 +184,8 @@
     private boolean lastFinancialYear = false;
     
     private boolean last5FinancialYears = false;
+    
+    private boolean last52Weeks = false;
 
     // -------------------------------------------------------------------------
     // Constructors
@@ -206,7 +215,7 @@
                             boolean monthsThisYear, boolean quartersThisYear, boolean thisYear,
                             boolean monthsLastYear, boolean quartersLastYear, boolean lastYear, boolean last5Years,
                             boolean last12Months, boolean last6BiMonths, boolean last4Quarters, boolean last2SixMonths,
-                            boolean thisFinancialYear, boolean lastFinancialYear, boolean last5FinancialYears )
+                            boolean thisFinancialYear, boolean lastFinancialYear, boolean last5FinancialYears, boolean last52Weeks )
     {
         this.reportingMonth = reportingMonth;
         this.reportingBimonth = reportingBimonth;
@@ -226,6 +235,7 @@
         this.thisFinancialYear = thisFinancialYear;
         this.lastFinancialYear = lastFinancialYear;
         this.last5FinancialYears = last5FinancialYears;
+        this.last52Weeks = last52Weeks;
     }
 
     // -------------------------------------------------------------------------
@@ -255,6 +265,7 @@
         this.thisFinancialYear = false;
         this.lastFinancialYear = false;
         this.last5FinancialYears = false;
+        this.last52Weeks = false;
 
         return this;
     }
@@ -266,6 +277,11 @@
      */
     public PeriodType getPeriodType()
     {
+        if ( isLast52Weeks() )
+        {
+            return PeriodType.getPeriodTypeByName( WeeklyPeriodType.NAME );
+        }
+        
         if ( isReportingMonth() || isLast12Months() )
         {
             return PeriodType.getPeriodTypeByName( MonthlyPeriodType.NAME );
@@ -316,7 +332,7 @@
      */
     public String getReportingPeriodName( I18nFormat format )
     {
-        Period period = getPeriodType().createPeriod( getDate( 1, new Date() ) );
+        Period period = getPeriodType().createPeriod( subtractMonth( 1, new Date() ) );
         return format.formatPeriod( period );
     }
 
@@ -352,7 +368,7 @@
      */
     public List<Period> getRelativePeriods( Date date, I18nFormat format, boolean dynamicNames )
     {
-        date = date == null ? getDate( 1, new Date() ) : date;
+        date = date == null ? subtractMonth( 1, new Date() ) : date;
         
         List<Period> periods = new ArrayList<Period>();
 
@@ -426,7 +442,12 @@
             periods.addAll( getRollingRelativePeriodList( new FinancialJulyPeriodType(), LAST_5_FINANCIAL_YEARS, date, dynamicNames, format ) );
         }
 
-        date = getDate( MONTHS_IN_YEAR, date );
+        if ( isLast52Weeks() )
+        {
+            periods.addAll( getRollingRelativePeriodList( new WeeklyPeriodType(), WEEKS_LAST_52, date, dynamicNames, format ) );
+        }
+        
+        date = subtractMonth( MONTHS_IN_YEAR, date );
 
         if ( isMonthsLastYear() )
         {
@@ -461,8 +482,10 @@
     {
         List<Period> periods = new ArrayList<Period>();
         
-        Date date = getDate( 1, new Date() );
+        Date date = subtractMonth( 1, new Date() );
+        Date weekDate = subtractWeeks( 1, new Date() );
 
+        periods.addAll( periodTypes.contains( WeeklyPeriodType.NAME ) ? new WeeklyPeriodType().generateRollingPeriods( weekDate ).subList( 26, 52 ) : NO );
         periods.addAll( periodTypes.contains( MonthlyPeriodType.NAME ) ? new MonthlyPeriodType().generateRollingPeriods( date ).subList( 6, 12 ) : NO );
         periods.addAll( periodTypes.contains( BiMonthlyPeriodType.NAME ) ? new BiMonthlyPeriodType().generateRollingPeriods( date ).subList( 3, 6 ) : NO );
         periods.addAll( periodTypes.contains( QuarterlyPeriodType.NAME ) ? new QuarterlyPeriodType().generateRollingPeriods( date ).subList( 2, 4 ) : NO );
@@ -483,8 +506,10 @@
     {
         List<Period> periods = new ArrayList<Period>();
 
-        Date date = getDate( 1, new Date() );
+        Date date = subtractMonth( 1, new Date() );
+        Date weekDate = subtractWeeks( 1, new Date() );
 
+        periods.addAll( periodTypes.contains( WeeklyPeriodType.NAME ) ? new WeeklyPeriodType().generateRollingPeriods( weekDate ).subList( 0, 26 ) : NO );
         periods.addAll( periodTypes.contains( MonthlyPeriodType.NAME ) ? new MonthlyPeriodType().generateRollingPeriods( date ).subList( 0, 6 ) : NO );
         periods.addAll( periodTypes.contains( BiMonthlyPeriodType.NAME ) ? new BiMonthlyPeriodType().generateRollingPeriods( date ).subList( 0, 3 ) : NO );
         periods.addAll( periodTypes.contains( QuarterlyPeriodType.NAME ) ? new QuarterlyPeriodType().generateRollingPeriods( date ).subList( 0, 2 ) : NO );
@@ -587,10 +612,10 @@
      * Returns a date.
      *
      * @param months the number of months to subtract from the current date.
-     * @param date   the date representing now, ignored if null.
+     * @param date the date representing now, ignored if null.
      * @return a date.
      */
-    private Date getDate( int months, Date date )
+    private Date subtractMonth( int months, Date date )
     {
         Calendar cal = PeriodType.createCalendarInstance( date );
         cal.add( Calendar.MONTH, (months * -1) );
@@ -598,6 +623,21 @@
         return cal.getTime();
     }
 
+    /**
+     * Returns a date.
+     *
+     * @param months the number of weeks to subtract from the current date.
+     * @param date the date representing now, ignored if null.
+     * @return a date.
+     */
+    public Date subtractWeeks( int weeks, Date date )
+    {
+        Calendar cal = PeriodType.createCalendarInstance( date );
+        cal.add( Calendar.DAY_OF_YEAR, (weeks * -7) );
+
+        return cal.getTime();
+    }
+
     // -------------------------------------------------------------------------
     // Getters & setters
     // -------------------------------------------------------------------------
@@ -835,6 +875,19 @@
         return this;
     }
 
+    @JsonProperty
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
+    public boolean isLast52Weeks()
+    {
+        return last52Weeks;
+    }
+
+    public RelativePeriods setLast52Weeks( boolean last52Weeks )
+    {
+        this.last52Weeks = last52Weeks;
+        return this;
+    }
+
     // -------------------------------------------------------------------------
     // Equals, hashCode, and toString
     // -------------------------------------------------------------------------
@@ -864,6 +917,7 @@
         result = prime * result + (thisFinancialYear ? 1 : 0);
         result = prime * result + (lastFinancialYear ? 1 : 0);
         result = prime * result + (last5FinancialYears ? 1 : 0);
+        result = prime * result + (last52Weeks ? 1 : 0);
 
         return result;
     }
@@ -978,6 +1032,11 @@
             return false;
         }
 
+        if ( !last52Weeks == other.last52Weeks )
+        {
+            return false;
+        }
+
         return true;
     }
 }

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java	2012-05-08 15:55:59 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java	2012-05-08 18:53:35 +0000
@@ -60,7 +60,7 @@
     @Test
     public void getRelativePeriods()
     {        
-        RelativePeriods periods = new RelativePeriods( true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true );
+        RelativePeriods periods = new RelativePeriods( true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true );
         
         List<Period> relatives = periods.getRelativePeriods( getDate( 2001, 1, 1 ), i18nFormat, false );
         
@@ -126,7 +126,12 @@
         assertTrue( relatives.contains( new Period( new FinancialJulyPeriodType(), getDate( 1999, 7, 1 ), getDate( 2000, 6, 30 ) ) ) );
         assertTrue( relatives.contains( new Period( new FinancialJulyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2001, 6, 30 ) ) ) );
         
-        assertEquals( 74, relatives.size() );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2000, 1, 10 ), getDate( 2000, 1, 16 ) ) ) );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2000, 1, 17 ), getDate( 2000, 1, 23 ) ) ) );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2000, 1, 24 ), getDate( 2000, 1, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2001, 1, 1 ), getDate( 2001, 1, 7 ) ) ) );
+        
+        assertEquals( 126, relatives.size() );
     }
     
     @Test
@@ -185,6 +190,18 @@
     }
     
     @Test
+    public void testGetLast52Weeks()
+    {
+        List<Period> relatives = new RelativePeriods().setLast52Weeks( true ).getRelativePeriods( getDate( 2001, 1, 1 ), null, false );
+
+        assertEquals( 52, relatives.size() );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2000, 1, 10 ), getDate( 2000, 1, 16 ) ) ) );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2000, 1, 17 ), getDate( 2000, 1, 23 ) ) ) );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2000, 1, 24 ), getDate( 2000, 1, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new WeeklyPeriodType(), getDate( 2001, 1, 1 ), getDate( 2001, 1, 7 ) ) ) );        
+    }
+    
+    @Test
     public void testGetMonthsThisYear()
     {
         List<Period> relatives = new RelativePeriods().setMonthsThisYear( true ).getRelativePeriods( getDate( 2001, 4, 1 ), null, false );
@@ -200,8 +217,7 @@
         assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2001, 9, 1 ), getDate( 2001, 9, 30 ) ) ) );
         assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2001, 10, 1 ), getDate( 2001, 10, 31 ) ) ) );
         assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2001, 11, 1 ), getDate( 2001, 11, 30 ) ) ) );
-        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2001, 12, 1 ), getDate( 2001, 12, 31 ) ) ) );
-        
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2001, 12, 1 ), getDate( 2001, 12, 31 ) ) ) );        
     }
 
     @Test
@@ -245,5 +261,16 @@
         periods = new RelativePeriods().getLast6To12Months( periodTypes );
 
         assertEquals( 14, periods.size() );
+        
+        periodTypes.clear();
+        periodTypes.add( WeeklyPeriodType.NAME );
+        
+        periods = new RelativePeriods().getLast6Months( periodTypes );
+
+        assertEquals( 26, periods.size() );
+        
+        periods = new RelativePeriods().getLast6To12Months( periodTypes );
+
+        assertEquals( 26, periods.size() );   
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm	2012-05-08 15:55:59 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm	2012-05-08 18:53:35 +0000
@@ -14,6 +14,7 @@
 </tr>
 <tr>
 	<td>
+		<label for="weekly">$i18n.getString( "Weekly" )</label><input type="checkbox" id="weekly" class="scheduling" name="scheduledPeriodTypes" value="Weekly"#if( $scheduledPeriodTypes.contains( "Weekly" ) ) checked="checked"#end>&nbsp;
 		<label for="monthly">$i18n.getString( "Monthly" )</label><input type="checkbox" id="monthly" class="scheduling" name="scheduledPeriodTypes" value="Monthly"#if( $scheduledPeriodTypes.contains( "Monthly" ) ) checked="checked"#end>&nbsp;
 		<label for="biMonthly">$i18n.getString( "BiMonthly" )</label><input type="checkbox" id="biMonthly" class="scheduling" name="scheduledPeriodTypes" value="BiMonthly"#if( $scheduledPeriodTypes.contains( "BiMonthly" ) ) checked="checked"#end>&nbsp;
 		<label for="quarterly">$i18n.getString( "Quarterly" )</label><input type="checkbox" id="quarterly" class="scheduling" name="scheduledPeriodTypes" value="Quarterly"#if( $scheduledPeriodTypes.contains( "Quarterly" ) ) checked="checked"#end><br><br>

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/tablecreator/action/SaveTableAction.java'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/tablecreator/action/SaveTableAction.java	2012-05-03 16:08:40 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/tablecreator/action/SaveTableAction.java	2012-05-08 18:53:35 +0000
@@ -439,7 +439,7 @@
             monthsThisYear, quartersThisYear, thisYear, 
             monthsLastYear, quartersLastYear, lastYear, 
             last5Years, last12Months, false, last4Quarters, last2SixMonths,
-            thisFinancialYear, lastFinancialYear, last5FinancialYears );
+            thisFinancialYear, lastFinancialYear, last5FinancialYears, false );
         
         ReportParams reportParams = new ReportParams();
         

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataMart.js'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataMart.js	2012-04-11 13:49:03 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataMart.js	2012-05-08 18:53:35 +0000
@@ -13,8 +13,12 @@
 	
 	var url = 'startExport.action?startDate=' + startDate + '&endDate=' + endDate;
 	
-	$( 'input[name="periodType"]').each( function() {
-		url += "&periodType=" + $( this ).val();
+	$( 'input[name="periodType"]').each( function() 
+	{
+		if ( $( this ).is( ':checked' ) )
+		{
+			url += "&periodType=" + $( this ).val();
+		}
 	} );
 	
 	$.get( url, pingNotificationsTimeout );

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/viewDataMartForm.vm'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/viewDataMartForm.vm	2012-05-08 15:55:59 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/viewDataMartForm.vm	2012-05-08 18:53:35 +0000
@@ -1,12 +1,13 @@
 <h3>$i18n.getString( "data_mart_management" ) #openHelp( "data_mart" )</h3>
 
-<div id="critiera" class="inputCriteria" style="width:420px;height:170px;">
+<div id="critiera" class="inputCriteria" style="width:430px;height:170px;">
 <table>
 <col width="100">
-<col width="270">
+<col width="290">
 <tr>
 	<td>$i18n.getString( "period_types" )</td>
-	<td><label for="monthly">$i18n.getString( "Monthly" )</label><input type="checkbox" id="monthly" name="periodType" value="Monthly" checked="checked">&nbsp;
+	<td><label for="weekly">$i18n.getString( "Weekly" )</label><input type="checkbox" id="weekly" name="periodType" value="Weekly">&nbsp;
+		<label for="monthly">$i18n.getString( "Monthly" )</label><input type="checkbox" id="monthly" name="periodType" value="Monthly" checked="checked">&nbsp;
 		<label for="biMonthly">$i18n.getString( "BiMonthly" )</label><input type="checkbox" id="biMonthly" name="periodType" value="BiMonthly">&nbsp;
 		<label for="quarterly">$i18n.getString( "Quarterly" )</label><input type="checkbox" id="quarterly" name="periodType" value="Quarterly" checked="checked"><br>
 		<label for="sixMonthly">$i18n.getString( "SixMonthly" )</label><input type="checkbox" id="sixMonthly" name="periodType" value="SixMonthly" checked="checked">&nbsp;