← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5990: Impl new get method for lock exceptions. Reloading periods when adding or updating lock exceptions.

 

------------------------------------------------------------
revno: 5990
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-02-16 16:07:04 +0100
message:
  Impl new get method for lock exceptions. Reloading periods when adding or updating lock exceptions.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockException.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockExceptionStore.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/HibernateLockExceptionStore.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/test/java/org/hisp/dhis/dataset/DataSetServiceTest.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/DataSetService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java	2012-02-15 13:19:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java	2012-02-16 15:07:04 +0000
@@ -239,21 +239,21 @@
      * @param lockException LockException instance to add
      * @return Database ID of LockException
      */
-    public int addLockException( LockException lockException );
+    int addLockException( LockException lockException );
 
     /**
      * Update lock exception
      *
      * @param lockException LockException instance to update
      */
-    public void updateLockException( LockException lockException );
+    void updateLockException( LockException lockException );
 
     /**
      * Delete lock exception
      *
      * @param lockException LockException instance to delete
      */
-    public void deleteLockException( LockException lockException );
+    void deleteLockException( LockException lockException );
 
     /**
      * Get LockException by ID
@@ -261,21 +261,21 @@
      * @param id ID of LockException to get
      * @return LockException with given ID, or null if not found
      */
-    public LockException getLockException( int id );
+    LockException getLockException( int id );
 
     /**
      * Get number of LockExceptions in total
      *
      * @return Total count of LockExceptions
      */
-    public int getLockExceptionCount();
+    int getLockExceptionCount();
 
     /**
      * Returns all lock exceptions
      *
      * @return List of all the lock exceptions
      */
-    public Collection<LockException> getAllLockExceptions();
+    Collection<LockException> getAllLockExceptions();
 
     /**
      * Get all LockExceptions within a specific range
@@ -284,7 +284,7 @@
      * @param max   Number of results wanted
      * @return Collection of LockExceptions withing the range specified
      */
-    public Collection<LockException> getLockExceptionsBetween( int first, int max );
+    Collection<LockException> getLockExceptionsBetween( int first, int max );
 
     /**
      * Find all unique dataSet + period combinations
@@ -301,7 +301,7 @@
      * @param dataSet DataSet part of the combination
      * @param period  Period part of the combination
      */
-    public void deleteLockExceptionCombination( DataSet dataSet, Period period );
+    void deleteLockExceptionCombination( DataSet dataSet, Period period );
 
     /**
      * For a given ou + ds + period combination, is the data set locked or not
@@ -311,5 +311,15 @@
      * @param period Period instance
      * @return true or false depending on the lock status of the ou + ds + period combination
      */
-    public boolean isLocked( OrganisationUnit organisationUnit, DataSet dataSet, Period period );
+    boolean isLocked( OrganisationUnit organisationUnit, DataSet dataSet, Period period );
+    
+    /**
+     * Checks whether the system is locked for data entry for the given input.
+     * 
+     * @param dataElement the data element.
+     * @param period the period.
+     * @param organisationUnit the organisation unit.
+     * @return true or false indicating whether the system is locked.
+     */
+    boolean isLocked( DataElement dataElement, Period period, OrganisationUnit organisationUnit );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockException.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockException.java	2012-02-14 07:18:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockException.java	2012-02-16 15:07:04 +0000
@@ -45,7 +45,13 @@
 
     public LockException()
     {
-
+    }
+    
+    public LockException( Period period, OrganisationUnit organisationUnit, DataSet dataSet )
+    {
+        this.period = period;
+        this.organisationUnit = organisationUnit;
+        this.dataSet = dataSet;
     }
 
     public String getName()

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockExceptionStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockExceptionStore.java	2012-02-14 07:51:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/LockExceptionStore.java	2012-02-16 15:07:04 +0000
@@ -28,6 +28,8 @@
  */
 
 import org.hisp.dhis.common.GenericStore;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 
 import java.util.Collection;
@@ -45,4 +47,6 @@
     Collection<LockException> getCombinations();
 
     void deleteCombination( DataSet dataSet, Period period );
+    
+    long getCount( DataElement dataElement, Period period, OrganisationUnit organisationUnit );
 }

=== 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	2012-02-15 13:19:34 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java	2012-02-16 15:07:04 +0000
@@ -339,10 +339,7 @@
 
         DataSet dataSet = lockException.getDataSet();
 
-        if ( !dataSet.getLockExceptions().contains( lockException ) )
-        {
-            dataSet.getLockExceptions().add( lockException );
-        }
+        dataSet.getLockExceptions().add( lockException );
 
         return lockExceptionStore.save( lockException );
     }
@@ -362,10 +359,7 @@
 
         DataSet dataSet = lockException.getDataSet();
 
-        if ( dataSet.getLockExceptions().contains( lockException ) )
-        {
-            dataSet.getLockExceptions().remove( lockException );
-        }
+        dataSet.getLockExceptions().remove( lockException );
 
         lockExceptionStore.delete( lockException );
     }
@@ -436,4 +430,10 @@
 
         return true;
     }
+
+    @Override
+    public boolean isLocked( DataElement dataElement, Period period, OrganisationUnit organisationUnit )
+    {
+        return lockExceptionStore.getCount( dataElement, period, organisationUnit ) > 0l;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateLockExceptionStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateLockExceptionStore.java	2012-02-16 14:00:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateLockExceptionStore.java	2012-02-16 15:07:04 +0000
@@ -35,11 +35,15 @@
 import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.Session;
+import org.hibernate.criterion.Projections;
+import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.dataset.LockException;
 import org.hisp.dhis.dataset.LockExceptionStore;
 import org.hisp.dhis.hibernate.HibernateGenericStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.springframework.jdbc.core.RowCallbackHandler;
@@ -74,6 +78,22 @@
     // -------------------------------------------------------------------------
 
     @Override
+    public int save( LockException lockException )
+    {
+        lockException.setPeriod( periodService.reloadPeriod( lockException.getPeriod() ) );
+        
+        return super.save( lockException );
+    }
+    
+    @Override
+    public void update( LockException lockException )
+    {
+        lockException.setPeriod( periodService.reloadPeriod( lockException.getPeriod() ) );
+        
+        super.update( lockException );
+    }
+    
+    @Override
     public Collection<LockException> getCombinations()
     {
         final String sql = "SELECT DISTINCT datasetid, periodid FROM LockException";
@@ -125,4 +145,14 @@
 
         return criteria.list();
     }
+    
+    public long getCount( DataElement dataElement, Period period, OrganisationUnit organisationUnit )
+    {
+        Criteria criteria = getCriteria( 
+            Restrictions.eq( "period", period ),
+            Restrictions.eq( "organisationUnit", organisationUnit ),
+            Restrictions.in( "dataSet", dataElement.getDataSets() ) );
+        
+        return (Long) criteria.setProjection( Projections.rowCount() ).uniqueResult();
+    }
 }

=== 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-02-07 13:00:52 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.java	2012-02-16 15:07:04 +0000
@@ -256,6 +256,16 @@
         return periods;
     }
 
+    public Period getPeriodFromDates( Date startDate, Date endDate, PeriodType periodType )
+    {
+        return periodStore.getPeriodFromDates( startDate, endDate, periodType );
+    }
+
+    public Period reloadPeriod( Period period )
+    {
+        return periodStore.reloadForceAddPeriod( period );
+    }
+
     // -------------------------------------------------------------------------
     // PeriodType
     // -------------------------------------------------------------------------
@@ -279,14 +289,4 @@
     {
         return periodStore.getPeriodType( periodType );
     }
-
-    public Period getPeriodFromDates( Date startDate, Date endDate, PeriodType periodType )
-    {
-        return periodStore.getPeriodFromDates( startDate, endDate, periodType );
-    }
-
-    public Period reloadPeriod( Period period )
-    {
-        return periodStore.reloadForceAddPeriod( period );
-    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java	2012-02-16 15:07:04 +0000
@@ -31,13 +31,18 @@
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertNull;
 import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.assertFalse;
 
 import java.util.Collection;
 import java.util.HashSet;
 
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.MonthlyPeriodType;
+import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
 import org.junit.Test;
 
@@ -48,8 +53,6 @@
 public class DataSetServiceTest
     extends DhisSpringTest
 {
-    private DataSetService dataSetService;
-
     private PeriodType periodType;
 
     @Override
@@ -57,10 +60,12 @@
         throws Exception
     {
         dataSetService = (DataSetService) getBean( DataSetService.ID );
+        
+        dataElementService = (DataElementService) getBean( DataElementService.ID );
 
         organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
         
-        periodType = PeriodType.getAvailablePeriodTypes().iterator().next();
+        periodType = new MonthlyPeriodType();
     }
 
     // -------------------------------------------------------------------------
@@ -272,4 +277,55 @@
         assertEquals( 2, dataSetService.getSourcesAssociatedWithDataSet( dataSetA, sources ) );
         assertEquals( 2, dataSetService.getSourcesAssociatedWithDataSet( dataSetB, sources ) );
     }
+    
+    // -------------------------------------------------------------------------
+    // LockException
+    // -------------------------------------------------------------------------
+
+    @Test
+    public void testSaveGet()
+    {
+        OrganisationUnit unit = createOrganisationUnit( 'A' );
+        Period period = periodType.createPeriod();
+        DataSet dataSet = createDataSet( 'A', periodType );
+        
+        organisationUnitService.addOrganisationUnit( unit );
+        dataSetService.addDataSet( dataSet );
+        
+        LockException lockException = new LockException( period, unit, dataSet ); 
+        
+        int id = dataSetService.addLockException( lockException );
+        
+        lockException = dataSetService.getLockException( id );
+        
+        assertNotNull( lockException );
+        assertEquals( unit, lockException.getOrganisationUnit() );
+        assertEquals( period, lockException.getPeriod() );
+        assertEquals( dataSet, lockException.getDataSet() );
+    }
+    
+    @Test
+    public void testIsLocked()
+    {
+        OrganisationUnit unit = createOrganisationUnit( 'A' );
+        Period period = periodType.createPeriod();
+        DataSet dataSet = createDataSet( 'A', periodType );
+        
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+        dataElementA.getDataSets().add( dataSet );
+        dataSet.getDataElements().add( dataElementA );        
+        
+        organisationUnitService.addOrganisationUnit( unit );
+        dataElementService.addDataElement( dataElementA );
+        dataElementService.addDataElement( dataElementB );
+        dataSetService.addDataSet( dataSet );
+        
+        LockException lockException = new LockException( period, unit, dataSet ); 
+        
+        dataSetService.addLockException( lockException );
+        
+        assertTrue( dataSetService.isLocked( dataElementA, period, unit ) );
+        assertFalse( dataSetService.isLocked( dataElementB, period, unit ) );
+    }        
 }