← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4597: Minor refactor - code should be with identifiers rather than names

 

------------------------------------------------------------
revno: 4597
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: dhis2
timestamp: Thu 2011-09-15 15:47:18 +0100
message:
  Minor refactor - code should be with identifiers rather than names
removed:
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/AbstractNameableObjectTest.java
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/main/java/org/hisp/dhis/common/IdentifiableObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroup.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-07 11:42:48 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractIdentifiableObject.java	2011-09-15 14:47:18 +0000
@@ -4,6 +4,8 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /*
  * Copyright (c) 2004-2010, University of Oslo
@@ -43,6 +45,8 @@
      */
     private static final long serialVersionUID = 5532508099213570673L;
 
+    private final static Log log = LogFactory.getLog( AbstractNameableObject.class );
+
     /**
      * The database internal identifier for this Object.
      */
@@ -54,6 +58,11 @@
     protected String uuid;
 
     /**
+     * The unique code for this Object.
+     */
+    protected String code;
+
+    /**
      * The name of this Object. Required and unique.
      */
     protected String name;
@@ -92,6 +101,17 @@
     }
 
     @Override
+    public String getCode()
+    {
+        return code;
+    }
+
+    public void setCode( String code )
+    {
+        this.code = code;
+    }
+
+    @Override
     public String getName()
     {
         return name;
@@ -123,21 +143,33 @@
     }
 
     /**
-     * Get a map of uuids to iidentifiable objects
+     * Get a map of codes to internal identifiers
      *
-     * @param objects the IdentifiableObjects to put in the map
+     * @param objects the NameableObjects to put in the map
      * @return the map
      */
-    public static Map<String, IdentifiableObject> getUUIDMapObjects( Collection<? extends AbstractIdentifiableObject> objects )
+    public static Map<String, Integer> getCodeMap( Collection<? extends AbstractNameableObject> objects )
     {
-        Map<String, IdentifiableObject> map = new HashMap<String, IdentifiableObject>();
-        for ( IdentifiableObject object : objects )
+        Map<String, Integer> map = new HashMap<String, Integer>();
+        for ( NameableObject object : objects )
         {
-            String uuid = object.getUuid();
-
-            map.put( uuid, object );
+            String code = object.getCode();
+            int internalId = object.getId();
+
+            if (code == null) continue;
+
+            // 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, internalId );
+            }
         }
-
         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-11 16:28:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AbstractNameableObject.java	2011-09-15 14:47:18 +0000
@@ -1,12 +1,5 @@
 package org.hisp.dhis.common;
 
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
 /*
  * Copyright (c) 2004-2010, University of Oslo
  * All rights reserved.
@@ -45,8 +38,6 @@
      */
     private static final long serialVersionUID = 714136796552146362L;
 
-    private final static Log log = LogFactory.getLog( AbstractNameableObject.class );
-
     /**
      * An alternative name of this Object. Optional but unique.
      */
@@ -58,11 +49,6 @@
     protected String shortName;
 
     /**
-     * An code representing this Object. Optional but unique.
-     */
-    protected String code;
-
-    /**
      * Description of this Object.
      */
     protected String description;
@@ -101,16 +87,6 @@
         this.shortName = shortName;
     }
 
-    public String getCode()
-    {
-        return code;
-    }
-
-    public void setCode( String code )
-    {
-        this.code = code;
-    }
-
     public String getDescription()
     {
         return description;
@@ -121,54 +97,4 @@
         this.description = description;
     }
 
-    /**
-     * Get a map of codes to internal identifiers
-     *
-     * @param objects the NameableObjects to put in the map
-     * @return the map
-     */
-    public static Map<String, Integer> getCodeMap( Collection<? extends AbstractNameableObject> objects )
-    {
-        Map<String, Integer> map = new HashMap<String, Integer>();
-        for ( NameableObject object : objects )
-        {
-            String code = object.getCode();
-            int internalId = object.getId();
-
-            if (code == null) continue;
-            
-            // 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, internalId );
-            }
-        }
-        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/main/java/org/hisp/dhis/common/IdentifiableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObject.java	2011-04-01 11:00:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObject.java	2011-09-15 14:47:18 +0000
@@ -35,4 +35,6 @@
     public abstract String getUuid();
 
     public abstract String getName();
+
+    public abstract String getCode();
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObject.java	2011-09-06 11:53:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObject.java	2011-09-15 14:47:18 +0000
@@ -33,7 +33,5 @@
 
     public String getShortName();
 
-    public String getCode();
-
     public String getDescription();
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroup.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroup.java	2011-05-24 10:18:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroup.java	2011-09-15 14:47:18 +0000
@@ -118,4 +118,10 @@
     {
         throw new NotImplementedException();
     }
+
+    @Override
+    public String getCode()
+    {
+        throw new NotImplementedException();
+    }
 }

=== removed 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-07 11:42:48 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/AbstractNameableObjectTest.java	1970-01-01 00:00:00 +0000
@@ -1,103 +0,0 @@
-package org.hisp.dhis.common;
-
-/*
- * Copyright (c) 2004-2005, University of Oslo
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright notice, this
- *   list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright notice,
- *   this list of conditions and the following disclaimer in the documentation
- *   and/or other materials provided with the distribution.
- * * Neither the name of the <ORGANIZATION> nor the names of its contributors may
- *   be used to endorse or promote products derived from this software without
- *   specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import java.util.Map;
-import java.util.LinkedList;
-import java.util.List;
-import org.hisp.dhis.dataelement.DataElement;
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- *
- * @author bobj
- * @version created 06-Sep-2011
- */
-public class AbstractNameableObjectTest {
-
-    @Test
-    public void testCodeMap()
-    {
-        List<DataElement> dataElements = new LinkedList<DataElement>();
-        for (int i=0; i<5; ++i)
-        {
-            DataElement de = new DataElement();
-            de.setId( i);
-            de.setCode("code"+i);
-            dataElements.add( de );
-        }
-
-        Map<String,Integer> codeMap = DataElement.getCodeMap( dataElements );
-        int id = codeMap.get( "code2");
-        assertEquals(2, id);
-
-        DataElement dup = new DataElement();
-        dup.setId(6);
-        dup.setCode("code2");
-        dataElements.add( dup );
-
-        codeMap = DataElement.getCodeMap( dataElements );
-        Integer i = codeMap.get( "code2");
-        assertNull(i);
-
-    }
-
-    @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);
-
-    }
-
-}