← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21278: add new method to generic stores / id object manager, getByAttributeValue(att, value) gets object...

 

------------------------------------------------------------
revno: 21278
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-12-03 10:12:51 +0700
message:
  add new method to generic stores / id object manager, getByAttributeValue(att, value) gets object by its -unique- attribute/value
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.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/GenericIdentifiableObjectStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java	2015-10-13 22:07:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java	2015-12-03 03:12:51 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.attribute.Attribute;
+
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -79,6 +81,8 @@
      */
     T getByCode( String code );
 
+    T getByAttributeValue( Attribute attribute, String value );
+
     /**
      * Retrieves a List of all objects (sorted on name).
      *
@@ -113,7 +117,7 @@
 
     /**
      * Return the number of objects where the name is equal the given name.
-     * <p/>
+     * <p>
      * This count is _unfiltered_ (no ACL!), so this is not the same as
      * getAllEqName().size().
      *
@@ -150,7 +154,7 @@
      * @return a List of objects.
      */
     List<T> getAllLikeName( Set<String> words, int first, int max );
-    
+
     /**
      * The returned list is ordered by the last updated property descending.
      *
@@ -191,7 +195,7 @@
      * @return a list of objects.
      */
     List<T> getById( Collection<Integer> ids );
-    
+
     /**
      * Retrieves a list of objects referenced by the given collection of uids.
      *
@@ -266,10 +270,10 @@
      * @return All objects equal or newer than given date.
      */
     List<T> getAllGeLastUpdatedOrderedName( Date lastUpdated );
-    
+
     /**
      * Returns the date of the last updated object.
-     * 
+     *
      * @return a Date / time stamp.
      */
     Date getLastUpdated();

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2015-11-23 06:05:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2015-12-03 03:12:51 +0000
@@ -67,6 +67,8 @@
 
     <T extends IdentifiableObject> T getByName( Class<T> clazz, String name );
 
+    <T extends IdentifiableObject> T getByAttributeValue( Class<T> clazz, Attribute attribute, String value );
+
     <T extends IdentifiableObject> T search( Class<T> clazz, String query );
 
     <T extends IdentifiableObject> List<T> filter( Class<T> clazz, String query );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-11-26 16:52:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-12-03 03:12:51 +0000
@@ -240,6 +240,20 @@
     }
 
     @Override
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> T getByAttributeValue( Class<T> clazz, Attribute attribute, String value )
+    {
+        GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+
+        if ( store == null )
+        {
+            return null;
+        }
+
+        return (T) store.getByAttributeValue( attribute, value );
+    }
+
+    @Override
     public <T extends IdentifiableObject> T search( Class<T> clazz, String query )
     {
         T object = get( clazz, query );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2015-10-13 22:07:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2015-12-03 03:12:51 +0000
@@ -28,19 +28,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.Set;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.criterion.Conjunction;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.common.AuditLogUtil;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.GenericDimensionalObjectStore;
@@ -48,6 +44,12 @@
 import org.hisp.dhis.hibernate.HibernateGenericStore;
 import org.hisp.dhis.hibernate.exception.ReadAccessDeniedException;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
+
 /**
  * @author bobj
  */
@@ -172,6 +174,22 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
+    public T getByAttributeValue( Attribute attribute, String value )
+    {
+        if ( !attribute.isUnique() )
+        {
+            return null;
+        }
+
+        Criteria criteria = getSharingCriteria();
+        criteria.createAlias( "attributeValues", "av" );
+        criteria.add( Restrictions.eq( "av.value", value ) );
+
+        return (T) criteria.uniqueResult();
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
     public List<T> getAllEqName( String name )
     {
         return getSharingCriteria()

=== 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-23 07:21:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-12-03 03:12:51 +0000
@@ -505,4 +505,43 @@
         values = dataElementStore.getAttributeValueByAttributeAndValue( attribute, "ANOTHER VALUE" );
         assertEquals( 1, values.size() );
     }
+
+    @Test
+    public void testDataElementByAttributeValue() throws NonUniqueAttributeValueException
+    {
+        Attribute attribute = new Attribute( "cid", ValueType.TEXT );
+        attribute.setDataElementAttribute( true );
+        attribute.setUnique( true );
+        attributeService.addAttribute( attribute );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+        DataElement dataElementC = createDataElement( 'C' );
+
+        dataElementStore.save( dataElementA );
+        dataElementStore.save( dataElementB );
+        dataElementStore.save( dataElementC );
+
+        AttributeValue attributeValueA = new AttributeValue( "CID1", attribute );
+        AttributeValue attributeValueB = new AttributeValue( "CID2", attribute );
+        AttributeValue attributeValueC = new AttributeValue( "CID3", attribute );
+
+        attributeService.addAttributeValue( dataElementA, attributeValueA );
+        attributeService.addAttributeValue( dataElementB, attributeValueB );
+        attributeService.addAttributeValue( dataElementC, attributeValueC );
+
+        dataElementStore.update( dataElementA );
+        dataElementStore.update( dataElementB );
+        dataElementStore.update( dataElementC );
+
+        assertNotNull( dataElementStore.getByAttributeValue( attribute, "CID1" ) );
+        assertNotNull( dataElementStore.getByAttributeValue( attribute, "CID2" ) );
+        assertNotNull( dataElementStore.getByAttributeValue( attribute, "CID3" ) );
+        assertNull( dataElementStore.getByAttributeValue( attribute, "CID4" ) );
+        assertNull( dataElementStore.getByAttributeValue( attribute, "CID5" ) );
+
+        assertEquals( "DataElementA", dataElementStore.getByAttributeValue( attribute, "CID1" ).getName() );
+        assertEquals( "DataElementB", dataElementStore.getByAttributeValue( attribute, "CID2" ).getName() );
+        assertEquals( "DataElementC", dataElementStore.getByAttributeValue( attribute, "CID3" ).getName() );
+    }
 }