← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9301: Made period store and identifiable object store

 

------------------------------------------------------------
revno: 9301
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-12-13 13:48:11 +0100
message:
  Made period store and identifiable object store
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.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/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.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/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/period/PeriodStoreTest.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/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2012-10-26 15:53:06 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2012-12-13 12:48:11 +0000
@@ -34,6 +34,7 @@
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * @author Lars Helge Overland
@@ -68,6 +69,8 @@
     
     void delete( IdentifiableObject object );
 
+    <T extends IdentifiableObject> Set<Integer> convertToId( Class<T> clazz, Collection<String> uids );
+    
     <T extends IdentifiableObject> Map<String, T> getIdMap( Class<T> clazz, IdentifiableProperty property );
 
     <T extends NameableObject> Map<String, T> getIdMap( Class<T> clazz, NameableProperty property );

=== 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	2012-09-23 14:24:14 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodStore.java	2012-12-13 12:48:11 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.GenericStore;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 
@@ -40,6 +41,7 @@
  * @version $Id: PeriodStore.java 5983 2008-10-17 17:42:44Z larshelg $
  */
 public interface PeriodStore
+    extends GenericStore<Period>
 {
     String ID = PeriodStore.class.getName();
 
@@ -56,21 +58,6 @@
     int addPeriod( Period period );
 
     /**
-     * Deletes a Period.
-     *
-     * @param period the Period to delete.
-     */
-    void deletePeriod( Period period );
-
-    /**
-     * Returns a Period.
-     *
-     * @param id the id of the Period to return.
-     * @return the Period with the given id, or null if no match.
-     */
-    Period getPeriod( int id );
-
-    /**
      * Returns a Period.
      *
      * @param startDate  the start date of the Period.
@@ -81,13 +68,6 @@
     Period getPeriod( Date startDate, Date endDate, PeriodType periodType );
 
     /**
-     * Returns all persisted Periods.
-     *
-     * @return all persisted Periods.
-     */
-    Collection<Period> getAllPeriods();
-
-    /**
      * Returns a Period.
      *
      * @param startDate  the start date of the Period.

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-10-26 15:53:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-12-13 12:48:11 +0000
@@ -273,6 +273,26 @@
 
         return (Collection<T>) store.getByLastUpdatedSorted( lastUpdated );
     }
+
+    @Override
+    public <T extends IdentifiableObject> Set<Integer> convertToId( Class<T> clazz, Collection<String> uids )
+    {
+        GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+        
+        Set<Integer> ids = new HashSet<Integer>();
+        
+        for ( String uid : uids )
+        {
+            IdentifiableObject object = store.getByUid( uid );
+            
+            if ( object != null )
+            {
+                ids.add( object.getId() );
+            }
+        }
+        
+        return ids;
+    }
     
     @Override
     @SuppressWarnings( "unchecked" )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java	2012-12-13 08:06:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java	2012-12-13 12:48:11 +0000
@@ -43,8 +43,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
-import com.sun.org.apache.regexp.internal.recompile;
-
 /**
  * @author Lars Helge Overland
  */

=== 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	2012-09-23 14:24:14 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.java	2012-12-13 12:48:11 +0000
@@ -76,12 +76,12 @@
 
     public void deletePeriod( Period period )
     {
-        periodStore.deletePeriod( period );
+        periodStore.delete( period );
     }
 
     public Period getPeriod( int id )
     {
-        return periodStore.getPeriod( id );
+        return periodStore.get( id );
     }
 
     public Period getPeriod( Date startDate, Date endDate, PeriodType periodType )
@@ -91,7 +91,7 @@
 
     public Collection<Period> getAllPeriods()
     {
-        return periodStore.getAllPeriods();
+        return periodStore.getAll();
     }
 
     public Collection<Period> getPeriods( final Collection<Integer> identifiers )

=== 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	2012-09-23 14:24:14 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/hibernate/HibernatePeriodStore.java	2012-12-13 12:48:11 +0000
@@ -34,8 +34,8 @@
 
 import org.hibernate.Criteria;
 import org.hibernate.Session;
-import org.hibernate.SessionFactory;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -51,20 +51,10 @@
  * @version $Id: HibernatePeriodStore.java 5983 2008-10-17 17:42:44Z larshelg $
  */
 public class HibernatePeriodStore
+    extends HibernateIdentifiableObjectStore<Period>
     implements PeriodStore
 {
     // -------------------------------------------------------------------------
-    // Dependencies
-    // -------------------------------------------------------------------------
-
-    private SessionFactory sessionFactory;
-
-    public void setSessionFactory( SessionFactory sessionFactory )
-    {
-        this.sessionFactory = sessionFactory;
-    }
-    
-    // -------------------------------------------------------------------------
     // Period
     // -------------------------------------------------------------------------
 
@@ -72,52 +62,23 @@
     {
         period.setPeriodType( reloadPeriodType( period.getPeriodType() ) );
 
-        Session session = sessionFactory.getCurrentSession();
-
-        return (Integer) session.save( period );
-    }
-
-    public void deletePeriod( Period period )
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        session.delete( period );
-    }
-
-    public Period getPeriod( int id )
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        return (Period) session.get( Period.class, id );
+        return save( period );
     }
 
     public Period getPeriod( Date startDate, Date endDate, PeriodType periodType )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "startDate", startDate ) );
         criteria.add( Restrictions.eq( "endDate", endDate ) );
         criteria.add( Restrictions.eq( "periodType", reloadPeriodType( periodType ) ) );
-        criteria.setCacheable( true );
 
         return (Period) criteria.uniqueResult();
     }
 
     @SuppressWarnings( "unchecked" )
-    public Collection<Period> getAllPeriods()
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        return session.createCriteria( Period.class ).list();
-    }
-
-    @SuppressWarnings( "unchecked" )
     public Collection<Period> getPeriodsBetweenDates( Date startDate, Date endDate )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.ge( "startDate", startDate ) );
         criteria.add( Restrictions.le( "endDate", endDate ) );
         criteria.setCacheable( true );
@@ -128,13 +89,10 @@
     @SuppressWarnings( "unchecked" )
     public Collection<Period> getPeriodsBetweenDates( PeriodType periodType, Date startDate, Date endDate )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "periodType", reloadPeriodType( periodType ) ) );
         criteria.add( Restrictions.ge( "startDate", startDate ) );
         criteria.add( Restrictions.le( "endDate", endDate ) );
-        criteria.setCacheable( true );
 
         return criteria.list();
     }
@@ -144,19 +102,16 @@
     {
         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();
+        return getQuery( hql ).setDate( "startDate", startDate ).setDate( "endDate", endDate ).list();
     }
     
     @SuppressWarnings( "unchecked" )
     public Collection<Period> getIntersectingPeriodsByPeriodType( PeriodType periodType, Date startDate, Date endDate )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "periodType", reloadPeriodType( periodType ) ) );
         criteria.add( Restrictions.ge( "endDate", startDate ) );
         criteria.add( Restrictions.le( "startDate", endDate ) );
-        criteria.setCacheable( true );
 
         return criteria.list();
     }
@@ -164,12 +119,9 @@
     @SuppressWarnings( "unchecked" )
     public Collection<Period> getIntersectingPeriods( Date startDate, Date endDate )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.ge( "endDate", startDate ) );
         criteria.add( Restrictions.le( "startDate", endDate ) );
-        criteria.setCacheable( true );
         
         return criteria.list();
     }
@@ -177,11 +129,8 @@
     @SuppressWarnings( "unchecked" )
     public Collection<Period> getPeriodsByPeriodType( PeriodType periodType )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "periodType", reloadPeriodType( periodType ) ) );
-        criteria.setCacheable( true );
 
         return criteria.list();
     }
@@ -216,13 +165,10 @@
 
     public Period getPeriodFromDates( Date startDate, Date endDate, PeriodType periodType )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( Period.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "startDate", startDate ) );
         criteria.add( Restrictions.eq( "endDate", endDate ) );
         criteria.add( Restrictions.eq( "periodType", periodType ) );
-        criteria.setCacheable( true );
 
         return (Period) criteria.uniqueResult();
     }
@@ -256,7 +202,7 @@
     }
 
     // -------------------------------------------------------------------------
-    // PeriodType
+    // PeriodType (do not use generic store which is linked to Period)
     // -------------------------------------------------------------------------
 
     public int addPeriodType( PeriodType periodType )
@@ -318,7 +264,7 @@
     }
 
     // -------------------------------------------------------------------------
-    // RelativePeriods
+    // RelativePeriods (do not use generic store which is linked to Period)
     // -------------------------------------------------------------------------
 
     public void deleteRelativePeriods( RelativePeriods relativePeriods )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2012-12-10 13:14:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2012-12-13 12:48:11 +0000
@@ -82,7 +82,9 @@
   </bean>
 
   <bean id="org.hisp.dhis.period.PeriodStore" class="org.hisp.dhis.period.hibernate.HibernatePeriodStore">
+    <property name="clazz" value="org.hisp.dhis.period.Period" />
     <property name="sessionFactory" ref="sessionFactory" />
+    <property name="cacheable" value="true" />
   </bean>
 
   <bean id="org.hisp.dhis.dataset.DataSetStore" class="org.hisp.dhis.dataset.hibernate.HibernateDataSetStore">

=== 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	2012-11-20 17:04:08 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/period/PeriodStoreTest.java	2012-12-13 12:48:11 +0000
@@ -82,8 +82,6 @@
         dataValueStore = (DataValueStore) getBean( DataValueStore.ID );
 
         optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
-        
-        // categoryService.addDataElementCategoryOptionCombo( optionCombo );
     }
 
     // -------------------------------------------------------------------------
@@ -118,21 +116,21 @@
             // Expected.
         }
 
-        periodA = periodStore.getPeriod( idA );
+        periodA = periodStore.get( idA );
         assertNotNull( periodA );
         assertEquals( idA, periodA.getId() );
         assertEquals( periodTypeA, periodA.getPeriodType() );
         assertEquals( getDay( 0 ), periodA.getStartDate() );
         assertEquals( getDay( 1 ), periodA.getEndDate() );
 
-        periodB = periodStore.getPeriod( idB );
+        periodB = periodStore.get( idB );
         assertNotNull( periodB );
         assertEquals( idB, periodB.getId() );
         assertEquals( periodTypeA, periodB.getPeriodType() );
         assertEquals( getDay( 1 ), periodB.getStartDate() );
         assertEquals( getDay( 2 ), periodB.getEndDate() );
 
-        periodC = periodStore.getPeriod( idC );
+        periodC = periodStore.get( idC );
         assertNotNull( periodC );
         assertEquals( idC, periodC.getId() );
         assertEquals( periodTypeB, periodC.getPeriodType() );
@@ -158,34 +156,34 @@
         int idC = periodStore.addPeriod( periodC );
         int idD = periodStore.addPeriod( periodD );
 
-        assertNotNull( periodStore.getPeriod( idA ) );
-        assertNotNull( periodStore.getPeriod( idB ) );
-        assertNotNull( periodStore.getPeriod( idC ) );
-        assertNotNull( periodStore.getPeriod( idD ) );
-
-        periodStore.deletePeriod( periodA );
-        assertNull( periodStore.getPeriod( idA ) );
-        assertNotNull( periodStore.getPeriod( idB ) );
-        assertNotNull( periodStore.getPeriod( idC ) );
-        assertNotNull( periodStore.getPeriod( idD ) );
-
-        periodStore.deletePeriod( periodB );
-        assertNull( periodStore.getPeriod( idA ) );
-        assertNull( periodStore.getPeriod( idB ) );
-        assertNotNull( periodStore.getPeriod( idC ) );
-        assertNotNull( periodStore.getPeriod( idD ) );
-
-        periodStore.deletePeriod( periodC );
-        assertNull( periodStore.getPeriod( idA ) );
-        assertNull( periodStore.getPeriod( idB ) );
-        assertNull( periodStore.getPeriod( idC ) );
-        assertNotNull( periodStore.getPeriod( idD ) );
-
-        periodStore.deletePeriod( periodD );
-        assertNull( periodStore.getPeriod( idA ) );
-        assertNull( periodStore.getPeriod( idB ) );
-        assertNull( periodStore.getPeriod( idC ) );
-        assertNull( periodStore.getPeriod( idD ) );
+        assertNotNull( periodStore.get( idA ) );
+        assertNotNull( periodStore.get( idB ) );
+        assertNotNull( periodStore.get( idC ) );
+        assertNotNull( periodStore.get( idD ) );
+
+        periodStore.delete( periodA );
+        assertNull( periodStore.get( idA ) );
+        assertNotNull( periodStore.get( idB ) );
+        assertNotNull( periodStore.get( idC ) );
+        assertNotNull( periodStore.get( idD ) );
+
+        periodStore.delete( periodB );
+        assertNull( periodStore.get( idA ) );
+        assertNull( periodStore.get( idB ) );
+        assertNotNull( periodStore.get( idC ) );
+        assertNotNull( periodStore.get( idD ) );
+
+        periodStore.delete( periodC );
+        assertNull( periodStore.get( idA ) );
+        assertNull( periodStore.get( idB ) );
+        assertNull( periodStore.get( idC ) );
+        assertNotNull( periodStore.get( idD ) );
+
+        periodStore.delete( periodD );
+        assertNull( periodStore.get( idA ) );
+        assertNull( periodStore.get( idB ) );
+        assertNull( periodStore.get( idC ) );
+        assertNull( periodStore.get( idD ) );
     }
 
     @Test
@@ -264,7 +262,7 @@
         periodStore.addPeriod( periodB );
         periodStore.addPeriod( periodC );
         
-        Collection<Period> periods = periodStore.getAllPeriods();
+        Collection<Period> periods = periodStore.getAll();
         
         assertNotNull( periods );
         assertEquals( 3, periods.size() );