← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1950: Improved performance in method getDataSetsBySource by using a query rather than java manipulation...

 

------------------------------------------------------------
revno: 1950
committer: Lars <larshelg@larshelg-laptop>
branch nick: trunk
timestamp: Thu 2010-06-03 00:52:41 +0200
message:
  Improved performance in method getDataSetsBySource by using a query rather than java manipulation (in Jason's spirit)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetStoreTest.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/dataset/DataSetStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java	2010-06-02 22:52:41 +0000
@@ -113,6 +113,8 @@
      */
     Collection<DataSet> getDataSetsByPeriodType( PeriodType periodType );
 
+    Collection<DataSet> getDataSetsBySource( Source source );
+    
     // -------------------------------------------------------------------------
     // FrequencyOverrideAssociation
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java	2010-05-28 09:12:03 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java	2010-06-02 22:52:41 +0000
@@ -149,17 +149,7 @@
 
     public Collection<DataSet> getDataSetsBySource( Source source )
     {
-        Set<DataSet> dataSets = new HashSet<DataSet>();
-
-        for ( DataSet dataSet : getAllDataSets() )
-        {
-            if ( dataSet.getSources().contains( source ) )
-            {
-                dataSets.add( dataSet );
-            }
-        }
-
-        return dataSets;
+        return i18n( i18nService, dataSetStore.getDataSetsBySource( source ) );
     }
 
     public Collection<DataSet> getDataSetsBySources( Collection<? extends Source> sources )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java	2010-05-18 15:42:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java	2010-06-02 22:52:41 +0000
@@ -30,6 +30,7 @@
 import java.util.Collection;
 
 import org.hibernate.Criteria;
+import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.criterion.Restrictions;
@@ -152,6 +153,17 @@
         
         return criteria.list();
     }
+
+    @SuppressWarnings( "unchecked" )
+    public Collection<DataSet> getDataSetsBySource( Source source )
+    {
+        String hql = "from DataSet d where :source in elements(d.sources)";
+        
+        Query query = sessionFactory.getCurrentSession().createQuery( hql );
+        query.setEntity( "source", source );
+        
+        return query.list();
+    }
     
     // -------------------------------------------------------------------------
     // FrequencyOverrideAssociation

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetStoreTest.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetStoreTest.java	2010-06-02 22:52:41 +0000
@@ -36,6 +36,8 @@
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.mock.MockSource;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.PeriodStore;
 import org.hisp.dhis.period.PeriodType;
@@ -70,6 +72,8 @@
 
         sourceStore = (SourceStore) getBean( SourceStore.ID );
         
+        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
+        
         periodType = PeriodType.getAvailablePeriodTypes().iterator().next();
     }
 
@@ -89,6 +93,43 @@
     // -------------------------------------------------------------------------
 
     @Test
+    public void testGetDataSetsBySource()
+    {
+        OrganisationUnit unitA = createOrganisationUnit( 'A' );
+        OrganisationUnit unitB = createOrganisationUnit( 'B' );  
+        OrganisationUnit unitC = createOrganisationUnit( 'C' );  
+        organisationUnitService.addOrganisationUnit( unitA );
+        organisationUnitService.addOrganisationUnit( unitB );
+        organisationUnitService.addOrganisationUnit( unitC );
+
+        DataSet dataSetA = createDataSet( 'A', periodType );
+        DataSet dataSetB = createDataSet( 'B', periodType );
+        DataSet dataSetC = createDataSet( 'C', periodType );
+        dataSetA.getSources().add( unitA );
+        dataSetA.getSources().add( unitB );
+        dataSetB.getSources().add( unitA );
+        
+        dataSetStore.addDataSet( dataSetA );
+        dataSetStore.addDataSet( dataSetB );
+        dataSetStore.addDataSet( dataSetC );
+        
+        Collection<DataSet> dataSets = dataSetStore.getDataSetsBySource( unitA );
+
+        assertEquals( 2, dataSets.size() );
+        assertTrue( dataSets.contains( dataSetA ) );
+        assertTrue( dataSets.contains( dataSetB ) );
+
+        dataSets = dataSetStore.getDataSetsBySource( unitB );
+        
+        assertEquals( 1, dataSets.size() );
+        assertTrue( dataSets.contains( dataSetA ) );
+
+        dataSets = dataSetStore.getDataSetsBySource( unitC );
+        
+        assertEquals( 0, dataSets.size() );
+    }
+    
+    @Test
     public void testAddDataSet()
     {
         DataSet dataSetA = createDataSet( 'A', periodType );