← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21138: add uniqueness checks for add/update attribute value

 

------------------------------------------------------------
revno: 21138
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-11-20 12:42:56 +0700
message:
  add uniqueness checks for add/update attribute value
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.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/attribute/AttributeService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java	2015-11-19 07:29:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java	2015-11-20 05:42:56 +0000
@@ -242,14 +242,14 @@
      *
      * @param attributeValue the attribute value.
      */
-    <T extends IdentifiableObject> void addAttributeValue( T object, AttributeValue attributeValue );
+    <T extends IdentifiableObject> void addAttributeValue( T object, AttributeValue attributeValue ) throws NonUniqueAttributeValueException;
 
     /**
      * Updates an attribute value.
      *
      * @param attributeValue the attribute value.
      */
-    <T extends IdentifiableObject> void updateAttributeValue( T object, AttributeValue attributeValue );
+    <T extends IdentifiableObject> void updateAttributeValue( T object, AttributeValue attributeValue ) throws NonUniqueAttributeValueException;
 
     /**
      * Deletes an attribute value.

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java	2015-11-20 05:42:56 +0000
@@ -0,0 +1,40 @@
+package org.hisp.dhis.attribute;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class NonUniqueAttributeValueException extends Exception
+{
+    public NonUniqueAttributeValueException( String message )
+    {
+        super( message );
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java	2015-11-19 07:29:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java	2015-11-20 05:42:56 +0000
@@ -29,8 +29,8 @@
  */
 
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.i18n.I18nService;
-import org.hisp.dhis.schema.SchemaService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -72,7 +72,7 @@
     }
 
     @Autowired
-    private SchemaService schemaService;
+    private IdentifiableObjectManager manager;
 
     // -------------------------------------------------------------------------
     // Attribute implementation
@@ -251,7 +251,7 @@
 
 
     @Override
-    public <T extends IdentifiableObject> void addAttributeValue( T object, AttributeValue attributeValue )
+    public <T extends IdentifiableObject> void addAttributeValue( T object, AttributeValue attributeValue ) throws NonUniqueAttributeValueException
     {
         if ( object == null || attributeValue == null || attributeValue.getAttribute() == null ||
             !attributeValue.getAttribute().getSupportedClasses().contains( object.getClass() ) )
@@ -259,13 +259,23 @@
             return;
         }
 
+        if ( attributeValue.getAttribute().isUnique() )
+        {
+            List<AttributeValue> values = manager.getAttributeValueByAttributeAndValue( object.getClass(), attributeValue.getAttribute(), attributeValue.getValue() );
+
+            if ( !values.isEmpty() )
+            {
+                throw new NonUniqueAttributeValueException( "Value " + attributeValue.getValue() + " already exists for attribute." );
+            }
+        }
+
         attributeValue.setAutoFields();
         attributeValueStore.save( attributeValue );
         object.getAttributeValues().add( attributeValue );
     }
 
     @Override
-    public <T extends IdentifiableObject> void updateAttributeValue( T object, AttributeValue attributeValue )
+    public <T extends IdentifiableObject> void updateAttributeValue( T object, AttributeValue attributeValue ) throws NonUniqueAttributeValueException
     {
         if ( object == null || attributeValue == null || attributeValue.getAttribute() == null ||
             !attributeValue.getAttribute().getSupportedClasses().contains( object.getClass() ) )
@@ -273,6 +283,16 @@
             return;
         }
 
+        if ( attributeValue.getAttribute().isUnique() )
+        {
+            List<AttributeValue> values = manager.getAttributeValueByAttributeAndValue( object.getClass(), attributeValue.getAttribute(), attributeValue.getValue() );
+
+            if ( values.size() > 1 || (values.size() == 1 && !object.getAttributeValues().contains( values.get( 0 ) )) )
+            {
+                throw new NonUniqueAttributeValueException( "Value " + attributeValue.getValue() + " already exists for attribute." );
+            }
+        }
+
         attributeValue.setAutoFields();
         attributeValueStore.update( attributeValue );
         object.getAttributeValues().add( attributeValue );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java	2015-11-18 05:35:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java	2015-11-20 05:42:56 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.dataelement.DataElement;
 import org.junit.Test;
@@ -45,11 +46,14 @@
     @Autowired
     private AttributeService attributeService;
 
+    @Autowired
+    private IdentifiableObjectManager manager;
+
     private AttributeValue avA;
     private AttributeValue avB;
 
     @Override
-    protected void setUpTest()
+    protected void setUpTest() throws NonUniqueAttributeValueException
     {
         avA = new AttributeValue( "value 1" );
         avB = new AttributeValue( "value 2" );
@@ -83,7 +87,7 @@
     }
 
     @Test
-    public void testUpdateAttributeValue()
+    public void testUpdateAttributeValue() throws NonUniqueAttributeValueException
     {
         avA.setValue( "updated value 1" );
         avB.setValue( "updated value 2" );
@@ -129,4 +133,80 @@
         assertNotNull( avA );
         assertNotNull( avB );
     }
+
+    @Test
+    public void testAddNonUniqueAttributeValue() throws NonUniqueAttributeValueException
+    {
+        Attribute attribute = new Attribute( "ID", ValueType.TEXT );
+        attribute.setUnique( true );
+        attribute.setDataElementAttribute( true );
+
+        attributeService.addAttribute( attribute );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+
+        manager.save( dataElementA );
+        manager.save( dataElementB );
+
+        AttributeValue attributeValueA = new AttributeValue( "A", attribute );
+        attributeService.addAttributeValue( dataElementA, attributeValueA );
+        manager.update( dataElementA );
+
+        AttributeValue attributeValueB = new AttributeValue( "B", attribute );
+        attributeService.addAttributeValue( dataElementB, attributeValueB );
+        manager.update( dataElementB );
+    }
+
+    @Test( expected = NonUniqueAttributeValueException.class )
+    public void testAddUniqueAttributeValue() throws NonUniqueAttributeValueException
+    {
+        Attribute attribute = new Attribute( "ID", ValueType.TEXT );
+        attribute.setUnique( true );
+        attribute.setDataElementAttribute( true );
+
+        attributeService.addAttribute( attribute );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+
+        manager.save( dataElementA );
+        manager.save( dataElementB );
+
+        AttributeValue attributeValueA = new AttributeValue( "A", attribute );
+        attributeService.addAttributeValue( dataElementA, attributeValueA );
+        manager.update( dataElementA );
+
+        AttributeValue attributeValueB = new AttributeValue( "A", attribute );
+        attributeService.addAttributeValue( dataElementB, attributeValueB );
+        manager.update( dataElementB );
+    }
+
+    @Test( expected = NonUniqueAttributeValueException.class )
+    public void testUpdateNonUniqueAttributeValue() throws NonUniqueAttributeValueException
+    {
+        Attribute attribute = new Attribute( "ID", ValueType.TEXT );
+        attribute.setUnique( true );
+        attribute.setDataElementAttribute( true );
+
+        attributeService.addAttribute( attribute );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+
+        manager.save( dataElementA );
+        manager.save( dataElementB );
+
+        AttributeValue attributeValueA = new AttributeValue( "A", attribute );
+        attributeService.addAttributeValue( dataElementA, attributeValueA );
+        manager.update( dataElementA );
+
+        AttributeValue attributeValueB = new AttributeValue( "B", attribute );
+        attributeService.addAttributeValue( dataElementB, attributeValueB );
+        manager.update( dataElementB );
+
+        attributeValueB.setValue( "A" );
+        attributeService.updateAttributeValue( dataElementB, attributeValueB );
+        manager.update( dataElementB );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-11-19 07:29:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-11-20 05:42:56 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeService;
 import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.attribute.NonUniqueAttributeValueException;
 import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
@@ -419,7 +420,7 @@
     }
 
     @Test
-    public void testDataElementFromAttribute()
+    public void testDataElementFromAttribute() throws NonUniqueAttributeValueException
     {
         Attribute attribute = new Attribute( "test", ValueType.TEXT );
         attribute.setDataElementAttribute( true );
@@ -441,7 +442,7 @@
     }
 
     @Test
-    public void testAttributeValueFromAttribute()
+    public void testAttributeValueFromAttribute() throws NonUniqueAttributeValueException
     {
         Attribute attribute = new Attribute( "test", ValueType.TEXT );
         attribute.setDataElementAttribute( true );
@@ -472,7 +473,7 @@
     }
 
     @Test
-    public void testAttributeValueFromAttributeAndValue()
+    public void testAttributeValueFromAttributeAndValue() throws NonUniqueAttributeValueException
     {
         Attribute attribute = new Attribute( "test", ValueType.TEXT );
         attribute.setDataElementAttribute( true );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2015-11-18 05:35:33 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2015-11-20 05:42:56 +0000
@@ -37,6 +37,7 @@
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeService;
 import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.attribute.NonUniqueAttributeValueException;
 import org.hisp.dhis.common.BaseAnalyticalObject;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.DataDimensionItem;
@@ -1194,7 +1195,14 @@
 
                 for ( AttributeValue attributeValue : attributeValues )
                 {
-                    attributeService.addAttributeValue( object, attributeValue );
+                    try
+                    {
+                        attributeService.addAttributeValue( object, attributeValue );
+                    }
+                    catch ( NonUniqueAttributeValueException ex )
+                    {
+                        log.info( ex.getMessage() );
+                    }
                 }
 
                 ReflectionUtils.invokeSetterMethod( "attributeValues", object, attributeValues );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.java	2015-11-18 05:35:33 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.java	2015-11-20 05:42:56 +0000
@@ -32,6 +32,7 @@
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeService;
 import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.attribute.NonUniqueAttributeValueException;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.springframework.util.StringUtils;
 
@@ -87,7 +88,14 @@
                     else
                     {
                         attributeValueItem.setValue( attributeValue.getValue() );
-                        attributeService.updateAttributeValue( object, attributeValueItem );
+                        try
+                        {
+                            attributeService.updateAttributeValue( object, attributeValueItem );
+                        }
+                        catch ( NonUniqueAttributeValueException ignored ) // ignore for now
+                        {
+                        }
+
                         attributeValue = null;
                     }
                 }
@@ -95,7 +103,14 @@
 
             if ( attributeValue != null && attributeValue.getValue() != null && !attributeValue.getValue().isEmpty() )
             {
-                attributeService.addAttributeValue( object, attributeValue );
+                try
+                {
+                    attributeService.addAttributeValue( object, attributeValue );
+                }
+                catch ( NonUniqueAttributeValueException ignored ) // ignore for now
+                {
+                }
+
                 attributeValues.add( attributeValue );
             }
         }