← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2075: Impl method getPeriodBetweenOrSpanningPeriods

 

------------------------------------------------------------
revno: 2075
committer: Lars <larshelg@larshelg-laptop>
branch nick: trunk
timestamp: Sun 2010-07-04 12:31:31 +0200
message:
  Impl method getPeriodBetweenOrSpanningPeriods
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/hibernate/HibernatePeriodStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/period/PeriodStoreTest.java
  dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/aggregation/cache/MemoryAggregationCache.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 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodService.java	2010-06-23 17:50:25 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodService.java	2010-07-04 10:31:31 +0000
@@ -130,6 +130,8 @@
      */
     Collection<Period> getPeriodsBetweenDates( PeriodType periodType, Date startDate, Date endDate );
 
+    Collection<Period> getPeriodsBetweenOrSpanningDates( Date startDate, Date endDate );
+    
     /**
      * Returns all Intersecting Periods between the startDate and endDate based on PeriodType
      * For example if the startDate is 2007-05-01 and endDate is 2007-08-01 and periodType is Quartely

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodStore.java	2010-06-21 21:21:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodStore.java	2010-07-04 10:31:31 +0000
@@ -123,6 +123,8 @@
      */
     Collection<Period> getPeriodsBetweenDates( PeriodType periodType, Date startDate, Date endDate );
 
+    Collection<Period> getPeriodsBetweenOrSpanningDates( Date startDate, Date endDate );
+    
     /**
      * Returns all intersecting Periods between the startDate and endDate based on PeriodType
      * For example if the startDate is 2007-05-01 and endDate is 2007-08-01 and periodType is Quartely

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.java	2010-06-23 17:50:25 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.java	2010-07-04 10:31:31 +0000
@@ -111,12 +111,17 @@
     {
         return periodStore.getPeriodsBetweenDates( startDate, endDate );
     }
-
+    
     public Collection<Period> getPeriodsBetweenDates( PeriodType periodType, Date startDate, Date endDate )
     {
         return periodStore.getPeriodsBetweenDates( periodType, startDate, endDate );
     }
 
+    public Collection<Period> getPeriodsBetweenOrSpanningDates( Date startDate, Date endDate )
+    {
+        return periodStore.getPeriodsBetweenOrSpanningDates( startDate, endDate );
+    }
+
     public Collection<Period> getIntersectingPeriodsByPeriodType( PeriodType periodType, Date startDate, Date endDate )
     {
         return periodStore.getIntersectingPeriodsByPeriodType( periodType, startDate, endDate );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/hibernate/HibernatePeriodStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/hibernate/HibernatePeriodStore.java	2010-06-23 17:50:25 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/hibernate/HibernatePeriodStore.java	2010-07-04 10:31:31 +0000
@@ -133,6 +133,14 @@
     }
 
     @SuppressWarnings( "unchecked" )
+    public Collection<Period> getPeriodsBetweenOrSpanningDates( Date startDate, Date endDate )
+    {
+        String hql = "from Period p where ( p.startDate >= :startDate and p.endDate <= :endDate ) or ( p.startDate <= :startDate and p.endDate >= :endDate )";
+        
+        return sessionFactory.getCurrentSession().createQuery( hql ).setDate( "startDate", startDate ).setDate( "endDate", endDate ).list();
+    }
+    
+    @SuppressWarnings( "unchecked" )
     public Collection<Period> getIntersectingPeriodsByPeriodType( PeriodType periodType, Date startDate, Date endDate )
     {
         Session session = sessionFactory.getCurrentSession();
@@ -153,7 +161,7 @@
         Criteria criteria = session.createCriteria( Period.class );
         criteria.add( Restrictions.gt( "endDate", startDate ) );
         criteria.add( Restrictions.lt( "startDate", endDate ) );
-
+        
         return criteria.list();
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/period/PeriodStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/period/PeriodStoreTest.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/period/PeriodStoreTest.java	2010-07-04 10:31:31 +0000
@@ -322,6 +322,54 @@
     }
 
     @Test
+    public void testGetPeriodsBetweenOrSpanningDates()
+        throws Exception
+    {
+        Collection<PeriodType> periodTypes = periodStore.getAllPeriodTypes();
+        Iterator<PeriodType> it = periodTypes.iterator();
+        PeriodType periodTypeA = it.next();
+        PeriodType periodTypeB = it.next();
+
+        Period periodA = new Period( periodTypeA, getDay( 0 ), getDay( 1 ) );
+        Period periodB = new Period( periodTypeA, getDay( 1 ), getDay( 2 ) );
+        Period periodC = new Period( periodTypeB, getDay( 2 ), getDay( 3 ) );
+        Period periodD = new Period( periodTypeB, getDay( 3 ), getDay( 4 ) );
+        Period periodE = new Period( periodTypeB, getDay( 0 ), getDay( 4 ) );
+        periodStore.addPeriod( periodA );
+        periodStore.addPeriod( periodB );
+        periodStore.addPeriod( periodC );
+        periodStore.addPeriod( periodD );
+        periodStore.addPeriod( periodE );
+
+        Collection<Period> periods = periodStore.getPeriodsBetweenOrSpanningDates( getDay( 0 ), getDay( 0 ) );
+        assertNotNull( periods );
+        assertEquals( 2, periods.size() );
+        assertTrue( periods.contains( periodA ) );
+        assertTrue( periods.contains( periodE ) );
+
+        periods = periodStore.getPeriodsBetweenOrSpanningDates( getDay( 0 ), getDay( 1 ) );
+        assertNotNull( periods );
+        assertEquals( 2, periods.size() );
+        assertTrue( periods.contains( periodA ) );
+        assertTrue( periods.contains( periodE ) );
+
+        periods = periodStore.getPeriodsBetweenOrSpanningDates( getDay( 1 ), getDay( 3 ) );
+        assertNotNull( periods );
+        assertEquals( 3, periods.size() );
+        assertTrue( periods.contains( periodB ) );
+        assertTrue( periods.contains( periodC ) );
+        assertTrue( periods.contains( periodE ) );
+
+        periods = periodStore.getPeriodsBetweenOrSpanningDates( getDay( 1 ), getDay( 4 ) );
+        assertNotNull( periods );
+        assertEquals( 4, periods.size() );
+        assertTrue( periods.contains( periodB ) );
+        assertTrue( periods.contains( periodC ) );
+        assertTrue( periods.contains( periodD ) );
+        assertTrue( periods.contains( periodE ) );
+    }
+
+    @Test
     public void testGetIntersectingPeriodsByPeriodType()
         throws Exception
     {

=== modified file 'dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/aggregation/cache/MemoryAggregationCache.java'
--- dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/aggregation/cache/MemoryAggregationCache.java	2010-07-04 08:53:02 +0000
+++ dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/aggregation/cache/MemoryAggregationCache.java	2010-07-04 10:31:31 +0000
@@ -51,8 +51,6 @@
 
     private final ThreadLocal<Map<String, Collection<Integer>>> intersectingPeriodCache = new ThreadLocal<Map<String,Collection<Integer>>>();
 
-    private final ThreadLocal<Map<String, Collection<Integer>>> periodsBetweenDatesCache = new ThreadLocal<Map<String,Collection<Integer>>>();
-
     private final ThreadLocal<Map<String, Period>> periodCache = new ThreadLocal<Map<String,Period>>();
 
     private final ThreadLocal<Map<String, Integer>> organisationUnitLevelCache = new ThreadLocal<Map<String, Integer>>();
@@ -102,31 +100,7 @@
         
         return periods;
     }
-    
-    public Collection<Integer> getPeriodsBetweenDates( final Date startDate, final Date endDate )
-    {
-        final String key = startDate.toString() + SEPARATOR + endDate.toString();
-        
-        Map<String, Collection<Integer>> cache = periodsBetweenDatesCache.get();
-        
-        Collection<Integer> periods = null;
-        
-        if ( cache != null && ( periods = cache.get( key ) ) != null )
-        {
-            return periods;
-        }
-        
-        periods = ConversionUtils.getIdentifiers( Period.class, periodService.getPeriodsBetweenDates( startDate, endDate ) );
-        
-        cache = ( cache == null ) ? new HashMap<String, Collection<Integer>>() : cache;
-        
-        cache.put( key, periods );
-        
-        periodsBetweenDatesCache.set( cache );
-        
-        return periods;
-    }
-    
+        
     public Period getPeriod( final int id )
     {
         final String key = String.valueOf( id );
@@ -178,7 +152,6 @@
     public void clearCache()
     {
         intersectingPeriodCache.remove();
-        periodsBetweenDatesCache.remove();
         periodCache.remove();
         organisationUnitLevelCache.remove();
     }