← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20111: IdentifiableObjectManager, centralized code

 

------------------------------------------------------------
revno: 20111
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2015-09-13 19:13:09 +0200
message:
  IdentifiableObjectManager, centralized code
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.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/IdentifiableObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2015-06-25 18:50:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2015-09-13 17:13:09 +0000
@@ -39,11 +39,13 @@
 import java.util.ListIterator;
 import java.util.Map;
 
+import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.calendar.Calendar;
 import org.hisp.dhis.calendar.DateTimeUnit;
 import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryOption;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
 
@@ -398,4 +400,60 @@
 
         return map;
     }
+
+    /**
+     * Returns a map of identifiable properties and objects.
+     * 
+     * @param objects the objects.
+     * @param property the identifiable property.
+     * @return a map.
+     */
+    @SuppressWarnings( "unchecked" )
+    public static <T extends IdentifiableObject> Map<String, T> getMap( List<T> objects, IdentifiableProperty property )
+    {
+        Map<String, T> map = new HashMap<>();
+        
+        for ( T object : objects )
+        {
+            if ( IdentifiableProperty.ID.equals( property ) )
+            {
+                if ( object.getId() > 0 )
+                {
+                    map.put( String.valueOf( object.getId() ), object );
+                }
+            }
+            else if ( IdentifiableProperty.UID.equals( property ) )
+            {
+                if ( object.getUid() != null )
+                {
+                    map.put( object.getUid(), object );
+                }
+            }
+            else if ( IdentifiableProperty.CODE.equals( property ) )
+            {
+                if ( object.getCode() != null )
+                {
+                    map.put( object.getCode(), object );
+                }
+            }
+            else if ( IdentifiableProperty.NAME.equals( property ) )
+            {
+                if ( object.getName() != null )
+                {
+                    map.put( object.getName(), object );
+                }
+            }
+            else if ( IdentifiableProperty.UUID.equals( property ) && OrganisationUnit.class.isAssignableFrom( object.getClass() ) )
+            {
+                OrganisationUnit organisationUnit = (OrganisationUnit) object;
+
+                if ( !StringUtils.isEmpty( organisationUnit.getUuid() ) )
+                {
+                    map.put( organisationUnit.getUuid(), (T) organisationUnit );
+                }
+            }
+        }
+        
+        return map;
+    }
 }

=== 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-06-25 18:50:24 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-09-13 17:13:09 +0000
@@ -48,7 +48,6 @@
 import org.hisp.dhis.user.UserCredentials;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.StringUtils;
 
 /**
  * Note that it is required for nameable object stores to have concrete implementation
@@ -674,40 +673,8 @@
         }
 
         List<T> objects = store.getAll();
-
-        for ( T object : objects )
-        {
-            if ( IdentifiableProperty.ID.equals( property ) )
-            {
-                if ( object.getId() > 0 )
-                {
-                    map.put( String.valueOf( object.getId() ), object );
-                }
-            }
-            else if ( IdentifiableProperty.UID.equals( property ) )
-            {
-                if ( object.getUid() != null )
-                {
-                    map.put( object.getUid(), object );
-                }
-            }
-            else if ( IdentifiableProperty.CODE.equals( property ) )
-            {
-                if ( object.getCode() != null )
-                {
-                    map.put( object.getCode(), object );
-                }
-            }
-            else if ( IdentifiableProperty.NAME.equals( property ) )
-            {
-                if ( object.getName() != null )
-                {
-                    map.put( object.getName(), object );
-                }
-            }
-        }
-
-        return map;
+        
+        return IdentifiableObjectUtils.getMap( objects, property );
     }
 
     @Override
@@ -725,48 +692,7 @@
 
         List<T> objects = store.getAllNoAcl();
 
-        for ( T object : objects )
-        {
-            if ( IdentifiableProperty.ID.equals( property ) )
-            {
-                if ( object.getId() > 0 )
-                {
-                    map.put( String.valueOf( object.getId() ), object );
-                }
-            }
-            else if ( IdentifiableProperty.UID.equals( property ) )
-            {
-                if ( object.getUid() != null )
-                {
-                    map.put( object.getUid(), object );
-                }
-            }
-            else if ( IdentifiableProperty.CODE.equals( property ) )
-            {
-                if ( object.getCode() != null )
-                {
-                    map.put( object.getCode(), object );
-                }
-            }
-            else if ( IdentifiableProperty.NAME.equals( property ) )
-            {
-                if ( object.getName() != null )
-                {
-                    map.put( object.getName(), object );
-                }
-            }
-            else if ( IdentifiableProperty.UUID.equals( property ) && OrganisationUnit.class.isAssignableFrom( clazz ) )
-            {
-                OrganisationUnit organisationUnit = (OrganisationUnit) object;
-
-                if ( !StringUtils.isEmpty( organisationUnit.getUuid() ) )
-                {
-                    map.put( organisationUnit.getUuid(), (T) organisationUnit );
-                }
-            }
-        }
-
-        return map;
+        return IdentifiableObjectUtils.getMap( objects, property );
     }
 
     @Override