← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4537: Implement identifiers mapped to objects rather than to primary ids.

 

------------------------------------------------------------
revno: 4537
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: dhis2
timestamp: Wed 2011-09-07 12:42:48 +0100
message:
  Implement identifiers mapped to objects rather than to primary ids.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractIdentifiableObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractNameableObject.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/AbstractNameableObjectTest.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/AbstractIdentifiableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractIdentifiableObject.java	2011-09-06 14:01:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractIdentifiableObject.java	2011-09-07 11:42:48 +0000
@@ -121,4 +121,23 @@
 
         return map;
     }
+
+    /**
+     * Get a map of uuids to iidentifiable objects
+     *
+     * @param objects the IdentifiableObjects to put in the map
+     * @return the map
+     */
+    public static Map<String, IdentifiableObject> getUUIDMapObjects( Collection<? extends AbstractIdentifiableObject> objects )
+    {
+        Map<String, IdentifiableObject> map = new HashMap<String, IdentifiableObject>();
+        for ( IdentifiableObject object : objects )
+        {
+            String uuid = object.getUuid();
+
+            map.put( uuid, object );
+        }
+
+        return map;
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractNameableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractNameableObject.java	2011-09-06 14:01:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractNameableObject.java	2011-09-07 11:42:48 +0000
@@ -148,4 +148,25 @@
         }
         return map;
     }
+
+    public static Map<String, NameableObject> getCodeMappedObjects( Collection<? extends AbstractNameableObject> objects )
+    {
+        Map<String, NameableObject> map = new HashMap<String, NameableObject>();
+        for ( NameableObject object : objects )
+        {
+            String code = object.getCode();
+
+            // NOTE: its really not good that duplicate codes are possible
+            // Best we can do here is severe log and remove the item
+            if ( map.containsKey( code ) )
+            {
+                log.warn( object.getClass() + ": Duplicate code " + code );
+                map.remove( code );
+            } else
+            {
+                map.put( code, object );
+            }
+        }
+        return map;
+    }
 }

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/AbstractNameableObjectTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/AbstractNameableObjectTest.java	2011-09-06 14:01:26 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/AbstractNameableObjectTest.java	2011-09-07 11:42:48 +0000
@@ -69,4 +69,35 @@
 
     }
 
+    @Test
+    public void testCodeMappedObjects()
+    {
+
+        List<DataElement> dataElements = new LinkedList<DataElement>();
+        for (int i=0; i<5; ++i)
+        {
+            DataElement de = new DataElement();
+            de.setId( i);
+            de.setUrl( "http://junk.org/"+i);
+            de.setCode("code"+i);
+            dataElements.add( de );
+        }
+
+        Map<String, NameableObject> codeMap = DataElement.getCodeMappedObjects( dataElements );
+        DataElement de = (DataElement) codeMap.get( "code2");
+        int id = de.getId();
+        assertEquals(2, id);
+        assertEquals("http://junk.org/2";, de.getUrl());
+
+        DataElement dup = new DataElement();
+        dup.setId(6);
+        dup.setCode("code2");
+        dataElements.add( dup );
+
+        codeMap = DataElement.getCodeMappedObjects( dataElements );
+        DataElement de2 = (DataElement) codeMap.get( "code2");
+        assertNull(de2);
+
+    }
+
 }