← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21788: minor, adds put/remove all on Preheat

 

------------------------------------------------------------
revno: 21788
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-01-21 13:00:57 +0700
message:
  minor, adds put/remove all on Preheat
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.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/preheat/Preheat.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-01-21 05:06:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-01-21 06:00:57 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.common.IdentifiableObject;
 
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -44,6 +45,22 @@
     {
     }
 
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> T get( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, String key )
+    {
+        if ( !containsKey( identifier, klass, key ) )
+        {
+            return null;
+        }
+
+        return (T) map.get( identifier ).get( klass ).get( key );
+    }
+
+    public boolean containsKey( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, String key )
+    {
+        return !(isEmpty() || isEmpty( identifier ) || isEmpty( identifier, klass )) && map.get( identifier ).get( klass ).containsKey( key );
+    }
+
     public boolean isEmpty()
     {
         return map.isEmpty();
@@ -78,19 +95,33 @@
         return this;
     }
 
-    @SuppressWarnings( "unchecked" )
-    public <T extends IdentifiableObject> T get( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, String key )
-    {
-        if ( !containsKey( identifier, klass, key ) )
-        {
-            return null;
-        }
-
-        return (T) map.get( identifier ).get( klass ).get( key );
-    }
-
-    public boolean containsKey( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, String key )
-    {
-        return !(isEmpty() || isEmpty( identifier ) || isEmpty( identifier, klass )) && map.get( identifier ).get( klass ).containsKey( key );
+    public <T extends IdentifiableObject> Preheat put( PreheatIdentifier identifier, Collection<T> objects )
+    {
+        for ( T object : objects )
+        {
+            put( identifier, object );
+        }
+
+        return this;
+    }
+
+    public Preheat remove( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, String key )
+    {
+        if ( containsKey( identifier, klass, key ) )
+        {
+            map.get( identifier ).get( klass ).remove( key );
+        }
+
+        return this;
+    }
+
+    public Preheat remove( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, Collection<String> keys )
+    {
+        for ( String key : keys )
+        {
+            remove( identifier, klass, key );
+        }
+
+        return this;
     }
 }

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.java	2016-01-21 05:06:57 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.java	2016-01-21 06:00:57 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import org.hisp.dhis.dataelement.DataElement;
 import org.junit.Test;
 
@@ -110,4 +111,87 @@
         assertEquals( de2.getCode(), preheat.get( PreheatIdentifier.CODE, DataElement.class, de2.getCode() ).getCode() );
         assertEquals( de3.getCode(), preheat.get( PreheatIdentifier.CODE, DataElement.class, de3.getCode() ).getCode() );
     }
+
+    @Test
+    public void testPutCollectionUid()
+    {
+        Preheat preheat = new Preheat();
+
+        DataElement de1 = new DataElement( "dataElementA" );
+        DataElement de2 = new DataElement( "dataElementB" );
+        DataElement de3 = new DataElement( "dataElementC" );
+
+        de1.setAutoFields();
+        de2.setAutoFields();
+        de3.setAutoFields();
+
+        preheat.put( PreheatIdentifier.UID, Lists.newArrayList( de1, de2, de3 ) );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) );
+        assertTrue( preheat.isEmpty( PreheatIdentifier.CODE ) );
+
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) );
+
+        assertEquals( de1.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de1.getUid() ).getUid() );
+        assertEquals( de2.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de2.getUid() ).getUid() );
+        assertEquals( de3.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de3.getUid() ).getUid() );
+    }
+
+    @Test
+    public void testPutCollectionRemoveOneUid()
+    {
+        Preheat preheat = new Preheat();
+
+        DataElement de1 = new DataElement( "dataElementA" );
+        DataElement de2 = new DataElement( "dataElementB" );
+        DataElement de3 = new DataElement( "dataElementC" );
+
+        de1.setAutoFields();
+        de2.setAutoFields();
+        de3.setAutoFields();
+
+        preheat.put( PreheatIdentifier.UID, Lists.newArrayList( de1, de2, de3 ) );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) );
+        assertTrue( preheat.isEmpty( PreheatIdentifier.CODE ) );
+
+        preheat.remove( PreheatIdentifier.UID, DataElement.class, de2.getUid() );
+
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) );
+
+        assertEquals( de1.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de1.getUid() ).getUid() );
+        assertEquals( de3.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de3.getUid() ).getUid() );
+    }
+
+    @Test
+    public void testPutCollectionRemoveAllUid()
+    {
+        Preheat preheat = new Preheat();
+
+        DataElement de1 = new DataElement( "dataElementA" );
+        DataElement de2 = new DataElement( "dataElementB" );
+        DataElement de3 = new DataElement( "dataElementC" );
+
+        de1.setAutoFields();
+        de2.setAutoFields();
+        de3.setAutoFields();
+
+        preheat.put( PreheatIdentifier.UID, Lists.newArrayList( de1, de2, de3 ) );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) );
+        assertTrue( preheat.isEmpty( PreheatIdentifier.CODE ) );
+
+        preheat.remove( PreheatIdentifier.UID, DataElement.class, Lists.newArrayList( de1.getUid(), de2.getUid(), de3.getUid() ) );
+
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) );
+    }
 }