------------------------------------------------------------ revno: 21127 committer: Morten Olav Hansen <morte...@gmail.com> branch nick: dhis2 timestamp: Thu 2015-11-19 13:24:20 +0700 message: attribute value store, add isAttributeValueUnique, checks for value uniqueness among -all- supported types of a attribute modified: dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValueStore.java dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/hibernate/HibernateAttributeValueStore.java dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueStoreTest.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/AttributeValueStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValueStore.java 2015-11-19 05:41:44 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValueStore.java 2015-11-19 06:24:20 +0000 @@ -29,6 +29,7 @@ */ import org.hisp.dhis.common.GenericStore; +import org.hisp.dhis.common.IdentifiableObject; import java.util.List; @@ -41,4 +42,13 @@ List<AttributeValue> getAllByAttribute( Attribute attribute ); List<AttributeValue> getAllByAttributeAndValue( Attribute attribute, String value ); + + /** + * Is attribute value unique, the value must either not exist, or just exist in given object. + * + * @param object Object + * @param attributeValue AV to check for + * @return true/false depending on uniqueness of AV + */ + <T extends IdentifiableObject> boolean isAttributeValueUnique( T object, AttributeValue attributeValue ); } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/hibernate/HibernateAttributeValueStore.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/hibernate/HibernateAttributeValueStore.java 2015-11-19 05:41:44 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/hibernate/HibernateAttributeValueStore.java 2015-11-19 06:24:20 +0000 @@ -32,6 +32,7 @@ import org.hisp.dhis.attribute.Attribute; import org.hisp.dhis.attribute.AttributeValue; import org.hisp.dhis.attribute.AttributeValueStore; +import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.hibernate.HibernateGenericStore; import java.util.List; @@ -59,4 +60,11 @@ .add( Restrictions.eq( "value", value ) ) .list(); } + + @Override + public <T extends IdentifiableObject> boolean isAttributeValueUnique( T object, AttributeValue attributeValue ) + { + List<AttributeValue> values = getAllByAttributeAndValue( attributeValue.getAttribute(), attributeValue.getValue() ); + return values.isEmpty() || (values.size() == 1 && object != null && object.getAttributeValues().contains( values.get( 0 ) )); + } } === modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueStoreTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueStoreTest.java 2015-11-19 05:41:44 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueStoreTest.java 2015-11-19 06:24:20 +0000 @@ -29,12 +29,13 @@ */ 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; import org.springframework.beans.factory.annotation.Autowired; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; /** * @author Morten Olav Hansen <morte...@gmail.com> @@ -48,6 +49,9 @@ @Autowired private AttributeStore attributeStore; + @Autowired + private IdentifiableObjectManager manager; + private AttributeValue avA; private AttributeValue avB; @@ -103,4 +107,18 @@ assertEquals( 1, attributeValueStore.getAllByAttributeAndValue( atA, "value 1" ).size() ); assertEquals( 1, attributeValueStore.getAllByAttributeAndValue( atA, "value 2" ).size() ); } + + @Test + public void testIsAttributeValueUnique() + { + DataElement dataElementA = createDataElement( 'A' ); + dataElementA.getAttributeValues().add( avA ); + DataElement dataElementB = createDataElement( 'B' ); + + manager.save( dataElementA ); + manager.save( dataElementB ); + + assertTrue( attributeValueStore.isAttributeValueUnique( dataElementA, avA ) ); + assertFalse( attributeValueStore.isAttributeValueUnique( dataElementB, avA ) ); + } }
_______________________________________________ Mailing list: https://launchpad.net/~dhis2-devs Post to : dhis2-devs@lists.launchpad.net Unsubscribe : https://launchpad.net/~dhis2-devs More help : https://help.launchpad.net/ListHelp