← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21102: Add method for getting AttributeValue from a class given a Attribute

 

------------------------------------------------------------
revno: 21102
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-11-18 10:02:19 +0700
message:
  Add method for getting AttributeValue from a class given a Attribute
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.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/GenericStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2015-11-17 07:00:29 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2015-11-18 03:02:19 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.hisp.dhis.attribute.Attribute;
+import org.hisp.dhis.attribute.AttributeValue;
 
 import java.util.List;
 
@@ -133,4 +134,6 @@
      * @return Object if object.attributeValues.attribute=attribute exists
      */
     T getByAttribute( Attribute attribute );
+
+    AttributeValue getAttributeValueByAttribute( Attribute attribute );
 }

=== 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-17 07:00:29 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-11-18 03:02:19 +0000
@@ -439,4 +439,26 @@
         DataElement dataElement = dataElementStore.getByAttribute( attribute );
         assertEquals( dataElement.getUid(), dataElementA.getUid() );
     }
+
+    @Test
+    public void testAttributeValueFromAttribute()
+    {
+        Attribute attribute = new Attribute( "test", ValueType.TEXT );
+        attribute.setDataElementAttribute( true );
+        attributeService.addAttribute( attribute );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+        dataElementStore.save( dataElementA );
+        dataElementStore.save( dataElementB );
+
+        AttributeValue attributeValue = new AttributeValue( "SOME VALUE", attribute );
+        attributeService.addAttributeValue( attributeValue );
+
+        dataElementA.getAttributeValues().add( attributeValue );
+        dataElementStore.update( dataElementA );
+
+        AttributeValue value = dataElementStore.getAttributeValueByAttribute( attribute );
+        assertEquals( value.getValue(), attributeValue.getValue() );
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2015-11-17 07:09:05 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2015-11-18 03:02:19 +0000
@@ -43,6 +43,7 @@
 import org.hibernate.criterion.Restrictions;
 import org.hibernate.criterion.Subqueries;
 import org.hisp.dhis.attribute.Attribute;
+import org.hisp.dhis.attribute.AttributeValue;
 import org.hisp.dhis.common.AuditLogUtil;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.GenericStore;
@@ -541,6 +542,22 @@
         return (T) criteria.uniqueResult();
     }
 
+    @Override
+    public AttributeValue getAttributeValueByAttribute( Attribute attribute )
+    {
+        Schema schema = schemaService.getDynamicSchema( getClazz() );
+
+        if ( schema == null || !schema.havePersistedProperty( "attributeValues" ) )
+        {
+            return null;
+        }
+
+        String hql = "select av from " + getClazz().getSimpleName() + "  as e " +
+            "inner join e.attributeValues av inner join av.attribute at where at = :attribute )";
+
+        return (AttributeValue) getQuery( hql ).setEntity( "attribute", attribute ).uniqueResult();
+    }
+
     //----------------------------------------------------------------------------------------------------------------
     // Helpers
     //----------------------------------------------------------------------------------------------------------------