← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20589: IdentifiableObjectManager, added method getObjects. Using it in GmlImportService to fetch org units.

 

------------------------------------------------------------
revno: 20589
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-10-08 16:29:41 +0200
message:
  IdentifiableObjectManager, added method getObjects. Using it in GmlImportService to fetch org units.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.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/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  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/organisationunit/OrganisationUnitServiceTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/gml/DefaultGmlImportService.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	2015-09-16 14:49:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2015-10-08 14:29:41 +0000
@@ -117,6 +117,8 @@
 
     <T extends NameableObject> Map<String, T> getIdMapNoAcl( Class<T> clazz, NameableProperty property );
 
+    <T extends IdentifiableObject> List<T> getObjects( Class<T> clazz, IdentifiableProperty property, Collection<String> identifiers );
+    
     <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String id );
 
     IdentifiableObject getObject( String uid, String simpleClassName );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2015-09-17 12:29:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2015-10-08 14:29:41 +0000
@@ -181,22 +181,6 @@
     List<OrganisationUnit> getOrganisationUnitByNameIgnoreCase( String name );
 
     /**
-     * Returns all OrganisationUnits exactly matching the given names.
-     *
-     * @param names names of OrganisationUnits to return.
-     * @return the OrganisationUnits matching the given names.
-     */
-    List<OrganisationUnit> getOrganisationUnitsByNames( Collection<String> names );
-
-    /**
-     * Returns all OrganisationUnits matching the given codes.
-     *
-     * @param codes codes of OrganisationUnits to return.
-     * @return the OrganisationUnits matching the given codes.
-     */
-    List<OrganisationUnit> getOrganisationUnitsByCodes( Collection<String> codes );
-
-    /**
      * Returns all root OrganisationUnits. A root OrganisationUnit is an
      * OrganisationUnit with no parent/the parent set to null.
      *

=== 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-09-13 17:13:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-10-08 14:29:41 +0000
@@ -745,6 +745,33 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> List<T> getObjects( Class<T> clazz, IdentifiableProperty property, Collection<String> identifiers )
+    {
+        GenericIdentifiableObjectStore<T> store = (GenericIdentifiableObjectStore<T>) getIdentifiableObjectStore( clazz );
+
+        if ( identifiers != null && !identifiers.isEmpty() )
+        {
+            if ( property == null || IdentifiableProperty.UID.equals( property ) )
+            {
+                return store.getByUid( identifiers );
+            }
+            else if ( IdentifiableProperty.CODE.equals( property ) )
+            {
+                return store.getByCode( identifiers );
+            }
+            else if ( IdentifiableProperty.NAME.equals( property ) )
+            {
+                return store.getByName( identifiers );
+            }
+            
+            throw new IllegalArgumentException( "Invalid identifiable property / class combination: " + property );
+        }
+
+        return null;
+    }
+    
+    @Override
+    @SuppressWarnings( "unchecked" )
     public <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String id )
     {
         GenericIdentifiableObjectStore<T> store = (GenericIdentifiableObjectStore<T>) getIdentifiableObjectStore( clazz );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-10-08 14:12:50 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-10-08 14:29:41 +0000
@@ -254,18 +254,6 @@
     }
 
     @Override
-    public List<OrganisationUnit> getOrganisationUnitsByNames( Collection<String> names )
-    {
-        return i18n( i18nService, organisationUnitStore.getByName( names ) );
-    }
-
-    @Override
-    public List<OrganisationUnit> getOrganisationUnitsByCodes( Collection<String> codes )
-    {
-        return i18n( i18nService, organisationUnitStore.getByCode( codes ) );
-    }
-
-    @Override
     public List<OrganisationUnit> getRootOrganisationUnits()
     {
         return i18n( i18nService, organisationUnitStore.getRootOrganisationUnits() );

=== 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	2015-07-14 09:34:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/IdentifiableObjectManagerTest.java	2015-10-08 14:29:41 +0000
@@ -615,4 +615,24 @@
 
         assertEquals( expected, actual );
     }
+    
+    @Test
+    public void testGetObjects()
+    {
+        OrganisationUnit unit1 = createOrganisationUnit( 'A' );
+        OrganisationUnit unit2 = createOrganisationUnit( 'B' );
+        OrganisationUnit unit3 = createOrganisationUnit( 'C' );
+        
+        identifiableObjectManager.save( unit1 );
+        identifiableObjectManager.save( unit2 );
+        identifiableObjectManager.save( unit3 );
+        
+        Set<String> codes = Sets.newHashSet( unit2.getCode(), unit3.getCode() );
+        
+        List<OrganisationUnit> units = identifiableObjectManager.getObjects( OrganisationUnit.class, IdentifiableProperty.CODE, codes );
+        
+        assertEquals( 2, units.size() );
+        assertTrue( units.contains( unit2 ) );
+        assertTrue( units.contains( unit3 ) );
+    }    
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2015-10-08 14:12:50 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2015-10-08 14:29:41 +0000
@@ -162,26 +162,6 @@
     }
     
     @Test
-    public void testGetOrganisationUnitByCode()
-    {
-        OrganisationUnit unit1 = createOrganisationUnit( 'A' );
-        OrganisationUnit unit2 = createOrganisationUnit( 'B' );
-        OrganisationUnit unit3 = createOrganisationUnit( 'C' );
-        
-        organisationUnitService.addOrganisationUnit( unit1 );
-        organisationUnitService.addOrganisationUnit( unit2 );
-        organisationUnitService.addOrganisationUnit( unit3 );
-        
-        Set<String> codes = Sets.newHashSet( unit2.getCode(), unit3.getCode() );
-        
-        List<OrganisationUnit> units = organisationUnitService.getOrganisationUnitsByCodes( codes );
-        
-        assertEquals( 2, units.size() );
-        assertTrue( units.contains( unit2 ) );
-        assertTrue( units.contains( unit3 ) );
-    }
-    
-    @Test
     public void testGetOrganisationUnitLevel()
     {
         OrganisationUnit unitA = createOrganisationUnit( 'A' );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/gml/DefaultGmlImportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/gml/DefaultGmlImportService.java	2015-09-17 04:29:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/gml/DefaultGmlImportService.java	2015-10-08 14:29:41 +0000
@@ -28,15 +28,27 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.base.Strings;
-import com.google.common.collect.Iterators;
-import com.google.common.collect.Maps;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.xerces.impl.io.MalformedByteSequenceException;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.IdentifiableProperty;
 import org.hisp.dhis.common.MergeStrategy;
 import org.hisp.dhis.dxf2.common.ImportOptions;
@@ -46,7 +58,6 @@
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.organisationunit.FeatureType;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.system.notification.NotificationLevel;
 import org.hisp.dhis.system.notification.Notifier;
@@ -56,19 +67,9 @@
 import org.springframework.web.util.HtmlUtils;
 import org.xml.sax.SAXParseException;
 
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import com.google.common.base.Strings;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Maps;
 
 /**
  * Import geospatial data from GML documents and merge into OrganisationUnits.
@@ -111,9 +112,6 @@
     private RenderService renderService;
 
     @Autowired
-    private OrganisationUnitService organisationUnitService;
-
-    @Autowired
     private IdentifiableObjectManager idObjectManager;
 
     @Autowired
@@ -266,6 +264,7 @@
         for ( OrganisationUnit orgUnit : sourceList ) // Identifier Matching priority: uid, code, name
         {
             // Only matches if UID is actually in DB as an empty UID on input will be replaced by auto-generated value
+            
             if ( !Strings.isNullOrEmpty( orgUnit.getUid() ) && idObjectManager.exists( OrganisationUnit.class, orgUnit.getUid() ) )
             {
                 uidMap.put( orgUnit.getUid(), orgUnit );
@@ -281,19 +280,11 @@
         }
     }
 
-    private Map<String, OrganisationUnit> getMatchingPersistedOrgUnits( Collection<String> identifiers, final IdentifiableProperty idProperty )
+    private Map<String, OrganisationUnit> getMatchingPersistedOrgUnits( Collection<String> identifiers, final IdentifiableProperty property )
     {
-        Collection<OrganisationUnit> orgUnits =
-            idProperty == IdentifiableProperty.UID ? organisationUnitService.getOrganisationUnitsByUid( identifiers ) :
-                idProperty == IdentifiableProperty.CODE ? organisationUnitService.getOrganisationUnitsByCodes( identifiers ) :
-                    idProperty == IdentifiableProperty.NAME ? organisationUnitService.getOrganisationUnitsByNames( identifiers ) :
-                        new HashSet<>();
+        List<OrganisationUnit> orgUnits = idObjectManager.getObjects( OrganisationUnit.class, property, identifiers );
 
-        return Maps.uniqueIndex( orgUnits,
-            organisationUnit -> idProperty == IdentifiableProperty.UID ? organisationUnit.getUid() :
-                idProperty == IdentifiableProperty.CODE ? organisationUnit.getCode() :
-                    idProperty == IdentifiableProperty.NAME ? organisationUnit.getName() : null
-        );
+        return IdentifiableObjectUtils.getMap( orgUnits, property );
     }
 
     private void mergeNonGeoData( OrganisationUnit source, OrganisationUnit target )