← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5993: Implemented server side check for data locking

 

------------------------------------------------------------
revno: 5993
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-02-16 18:57:30 +0100
message:
  Implemented server side check for data locking
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.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/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml


--
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/dataelement/DataElement.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java	2012-02-03 08:35:56 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java	2012-02-16 17:57:30 +0000
@@ -49,6 +49,8 @@
 import java.util.List;
 import java.util.Set;
 
+import static org.hisp.dhis.dataset.DataSet.NO_EXPIRY;
+
 /**
  * A DataElement is a definition (meta-information about) of the entities that
  * are captured in the system. An example from public health care is a
@@ -362,6 +364,25 @@
     {
         return formName != null && !formName.isEmpty() ? formName : getDisplayName();
     }
+    
+    /**
+     * Returns the minimum number of expiry days from the data sets of this data
+     * element.
+     */
+    public int getExpiryDays()
+    {
+        int expiryDays = Integer.MAX_VALUE;
+        
+        for ( DataSet dataSet : dataSets )
+        {
+            if ( dataSet.getExpiryDays() != NO_EXPIRY && dataSet.getExpiryDays() < expiryDays )
+            {
+                expiryDays = dataSet.getExpiryDays();
+            }
+        }
+        
+        return expiryDays == Integer.MAX_VALUE ? NO_EXPIRY : expiryDays;
+    }
 
     public String toJSON()
     {

=== 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-16 15:07:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java	2012-02-16 17:57:30 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.period.PeriodType;
 
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -321,5 +322,5 @@
      * @param organisationUnit the organisation unit.
      * @return true or false indicating whether the system is locked.
      */
-    boolean isLocked( DataElement dataElement, Period period, OrganisationUnit organisationUnit );
+    boolean isLocked( DataElement dataElement, Period period, OrganisationUnit organisationUnit, Date now );
 }

=== 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-16 15:31:42 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java	2012-02-16 17:57:30 +0000
@@ -432,8 +432,12 @@
     }
 
     @Override
-    public boolean isLocked( DataElement dataElement, Period period, OrganisationUnit organisationUnit )
+    public boolean isLocked( DataElement dataElement, Period period, OrganisationUnit organisationUnit, Date now )
     {
-        return lockExceptionStore.getCount( dataElement, period, organisationUnit ) > 0l;
+        int expiryDays = dataElement.getExpiryDays();
+        
+        boolean expired = expiryDays != DataSet.NO_EXPIRY && new DateTime( period.getEndDate() ).plusDays( expiryDays ).isBefore( new DateTime( now ) );
+        
+        return expired && lockExceptionStore.getCount( dataElement, period, organisationUnit ) == 0l;
     }
 }

=== 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	2012-02-16 15:07:04 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java	2012-02-16 17:57:30 +0000
@@ -28,10 +28,10 @@
  */
 
 import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
 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;
@@ -43,6 +43,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
 import org.junit.Test;
 
@@ -65,6 +66,8 @@
 
         organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
         
+        periodService = (PeriodService) getBean( PeriodService.ID );
+        
         periodType = new MonthlyPeriodType();
     }
 
@@ -308,24 +311,47 @@
     public void testIsLocked()
     {
         OrganisationUnit unit = createOrganisationUnit( 'A' );
-        Period period = periodType.createPeriod();
-        DataSet dataSet = createDataSet( 'A', periodType );
+        Period period = createPeriod( periodType, getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) );
+        DataSet dataSetA = createDataSet( 'A', periodType );
+        DataSet dataSetB = createDataSet( 'B', periodType );
+        dataSetA.setExpiryDays( 20 );
+        dataSetB.setExpiryDays( 10 );
         
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
-        dataElementA.getDataSets().add( dataSet );
-        dataSet.getDataElements().add( dataElementA );        
+        dataElementA.getDataSets().add( dataSetA );
+        dataElementA.getDataSets().add( dataSetB );
+        dataSetA.getDataElements().add( dataElementA );
+        dataSetB.getDataElements().add( dataElementA );
         
         organisationUnitService.addOrganisationUnit( unit );
+        periodService.addPeriod( period );
         dataElementService.addDataElement( dataElementA );
         dataElementService.addDataElement( dataElementB );
-        dataSetService.addDataSet( dataSet );
-        
-        LockException lockException = new LockException( period, unit, dataSet ); 
-        
+        dataSetService.addDataSet( dataSetA );
+        dataSetService.addDataSet( dataSetB );
+
+        // ---------------------------------------------------------------------
+        // Expiry days
+        // ---------------------------------------------------------------------
+
+        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 1 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 5 ) ) );
+        assertTrue( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 15 ) ) );
+        assertTrue( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementB, period, unit, getDate( 2000, 4, 25 ) ) );
+        
+        // ---------------------------------------------------------------------
+        // Lock exception
+        // ---------------------------------------------------------------------
+
+        LockException lockException = new LockException( period, unit, dataSetA );        
         dataSetService.addLockException( lockException );
-        
-        assertTrue( dataSetService.isLocked( dataElementA, period, unit ) );
-        assertFalse( dataSetService.isLocked( dataElementB, period, unit ) );
+
+        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 1 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 5 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 15 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementB, period, unit, getDate( 2000, 4, 25 ) ) );      
     }        
 }

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java	2012-02-08 11:51:34 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java	2012-02-16 17:57:30 +0000
@@ -35,6 +35,7 @@
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.datavalue.DataValueService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -91,6 +92,13 @@
     {
         this.organisationUnitService = organisationUnitService;
     }
+    
+    private DataSetService dataSetService;
+
+    public void setDataSetService( DataSetService dataSetService )
+    {
+        this.dataSetService = dataSetService;
+    }
 
     // -------------------------------------------------------------------------
     // Input
@@ -163,6 +171,8 @@
 
         DataElementCategoryOptionCombo optionCombo = categoryService.getDataElementCategoryOptionCombo( optionComboId );
 
+        Date now = new Date();
+        
         if ( storedBy == null )
         {
             storedBy = "[unknown]";
@@ -179,6 +189,15 @@
         }
 
         // ---------------------------------------------------------------------
+        // Check locked status
+        // ---------------------------------------------------------------------
+
+        if ( dataSetService.isLocked( dataElement, period, organisationUnit, new Date() ) )
+        {
+            return logError( "Entry locked for combination: " + dataElement + ", " + period + ", " + organisationUnit );
+        }
+        
+        // ---------------------------------------------------------------------
         // Update data
         // ---------------------------------------------------------------------
 
@@ -188,15 +207,14 @@
         {
             if ( value != null )
             {
-                dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(), null,
-                    optionCombo );
+                dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, now, null, optionCombo );
                 dataValueService.addDataValue( dataValue );
             }
         }
         else
         {
             dataValue.setValue( value );
-            dataValue.setTimestamp( new Date() );
+            dataValue.setTimestamp( now );
             dataValue.setStoredBy( storedBy );
 
             dataValueService.updateDataValue( dataValue );

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml	2012-02-16 12:58:37 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml	2012-02-16 17:57:30 +0000
@@ -51,6 +51,7 @@
     <property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
     <property name="categoryService" ref="org.hisp.dhis.dataelement.DataElementCategoryService" />
     <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+    <property name="dataSetService" ref="org.hisp.dhis.dataset.DataSetService" />
   </bean>
 
   <bean id="org.hisp.dhis.de.action.SaveCommentAction" class="org.hisp.dhis.de.action.SaveCommentAction"