← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21280: support idManager.getObjects by attribute/value

 

------------------------------------------------------------
revno: 21280
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-12-03 11:18:07 +0700
message:
  support idManager.getObjects by attribute/value
modified:
  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


--
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/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2015-12-03 03:12:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2015-12-03 04:18:07 +0000
@@ -125,7 +125,9 @@
 
     <T extends IdentifiableObject> List<T> getObjects( Class<T> clazz, Collection<Integer> identifiers );
 
-    <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String id );
+    <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String value );
+
+    <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String aid, String value );
 
     IdentifiableObject getObject( String uid, String simpleClassName );
 

=== 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-12-03 03:12:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-12-03 04:18:07 +0000
@@ -803,34 +803,51 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String id )
+    public <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String value )
+    {
+        return getObject( clazz, property, null, value );
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String aid, String value )
     {
         GenericIdentifiableObjectStore<T> store = (GenericIdentifiableObjectStore<T>) getIdentifiableObjectStore( clazz );
-
-        if ( id != null )
-        {
-            if ( property == null || IdentifiableProperty.ID.equals( property ) )
-            {
-                if ( Integer.valueOf( id ) > 0 )
+        Attribute attribute = null;
+
+        if ( aid != null )
+        {
+            attribute = get( Attribute.class, aid );
+        }
+
+        if ( value != null )
+        {
+            if ( IdentifiableProperty.UID == property )
+            {
+                return store.getByUid( value );
+            }
+            else if ( IdentifiableProperty.CODE == property )
+            {
+                return store.getByCode( value );
+            }
+            else if ( IdentifiableProperty.NAME == property )
+            {
+                return store.getByName( value );
+            }
+            else if ( IdentifiableProperty.ATTRIBUTE == property )
+            {
+                return store.getByAttributeValue( attribute, value );
+            }
+            else if ( property == null || IdentifiableProperty.ID == property )
+            {
+                if ( Integer.valueOf( value ) > 0 )
                 {
-                    return store.get( Integer.valueOf( id ) );
+                    return store.get( Integer.valueOf( value ) );
                 }
             }
-            else if ( IdentifiableProperty.UID.equals( property ) )
-            {
-                return store.getByUid( id );
-            }
             else if ( IdentifiableProperty.UUID.equals( property ) && OrganisationUnit.class.isAssignableFrom( clazz ) )
             {
-                return (T) organisationUnitService.getOrganisationUnitByUuid( id );
-            }
-            else if ( IdentifiableProperty.CODE.equals( property ) )
-            {
-                return store.getByCode( id );
-            }
-            else if ( IdentifiableProperty.NAME.equals( property ) )
-            {
-                return store.getByName( id );
+                return (T) organisationUnitService.getOrganisationUnitByUuid( value );
             }
 
             throw new InvalidIdentifierReferenceException( "Invalid identifiable property / class combination: " + property );

=== 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-12-03 03:12:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2015-12-03 04:18:07 +0000
@@ -43,6 +43,7 @@
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.hibernate.HibernateGenericStore;
 import org.hisp.dhis.hibernate.exception.ReadAccessDeniedException;
+import org.springframework.util.StringUtils;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -176,7 +177,7 @@
     @SuppressWarnings( "unchecked" )
     public T getByAttributeValue( Attribute attribute, String value )
     {
-        if ( !attribute.isUnique() )
+        if ( attribute == null || StringUtils.isEmpty( value ) || !attribute.isUnique() )
         {
             return null;
         }