← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21673: Added method to IdentifiableObjectManager to get collection by code

 

------------------------------------------------------------
revno: 21673
committer: bobjolliffe <bobjolliffe@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-01-08 16:32:09 +0000
message:
  Added method to IdentifiableObjectManager to get collection by code
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/test/java/org/hisp/dhis/common/IdentifiableObjectManagerTest.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	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2016-01-08 16:32:09 +0000
@@ -65,6 +65,8 @@
 
     <T extends IdentifiableObject> T getByCode( Class<T> clazz, String code );
 
+    <T extends IdentifiableObject> List<T> getByCode( Class<T> clazz, Collection<String> codes );
+
     <T extends IdentifiableObject> T getByName( Class<T> clazz, String name );
 
     <T extends IdentifiableObject> T getByUniqueAttributeValue( Class<T> clazz, Attribute attribute, String value );

=== 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	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2016-01-08 16:32:09 +0000
@@ -383,6 +383,21 @@
 
         return (List<T>) store.getByUid( uids );
     }
+    
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> List<T> getByCode(Class<T> clazz, Collection<String> codes) 
+    {
+        GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+
+        if ( store == null )
+        {
+            return new ArrayList<>();
+        }
+
+        return (List<T>) store.getByCode( codes );
+    }
+
 
     @Override
     @SuppressWarnings( "unchecked" )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/IdentifiableObjectManagerTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/IdentifiableObjectManagerTest.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/IdentifiableObjectManagerTest.java	2016-01-08 16:32:09 +0000
@@ -615,6 +615,38 @@
 
         assertEquals( expected, actual );
     }
+
+    @Test
+    public void getByCodeTest()
+    {
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+        DataElement dataElementC = createDataElement( 'C' );
+        DataElement dataElementD = createDataElement( 'D' );
+
+        dataElementA.setCode("DE_A");
+        dataElementB.setCode("DE_B");
+        dataElementC.setCode("DE_C");
+        dataElementD.setCode("DE_D");
+        
+        identifiableObjectManager.save( dataElementA );
+        identifiableObjectManager.save( dataElementB );
+        identifiableObjectManager.save( dataElementC );
+        identifiableObjectManager.save( dataElementD );
+
+        List<DataElement> ab = identifiableObjectManager.getByCode( DataElement.class, Arrays.asList( dataElementA.getCode(), dataElementB.getCode() ) );
+        List<DataElement> cd = identifiableObjectManager.getByCode( DataElement.class, Arrays.asList( dataElementC.getCode(), dataElementD.getCode() ) );
+
+        assertTrue( ab.contains( dataElementA ) );
+        assertTrue( ab.contains( dataElementB ) );
+        assertFalse( ab.contains( dataElementC ) );
+        assertFalse( ab.contains( dataElementD ) );
+
+        assertFalse( cd.contains( dataElementA ) );
+        assertFalse( cd.contains( dataElementB ) );
+        assertTrue( cd.contains( dataElementC ) );
+        assertTrue( cd.contains( dataElementD ) );
+    }
     
     @Test
     public void testGetObjects()