← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21796: wip, id object reference collector

 

------------------------------------------------------------
revno: 21796
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-01-22 11:05:17 +0700
message:
  wip, id object reference collector
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.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/PreheatService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-01-21 05:06:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-01-22 04:05:17 +0000
@@ -28,6 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObject;
+
+import java.util.Collection;
+import java.util.Map;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -46,4 +51,13 @@
      * @param params PreheatParams
      */
     void validate( PreheatParams params ) throws PreheatException;
+
+    /**
+     * Scan object and collect all references (both id object and collections with id objects).
+     *
+     * @param object     Object to scan
+     * @param identifier Identifier to collect
+     * @return Maps classes to collections of identifiers
+     */
+    Map<Class<? extends IdentifiableObject>, Collection<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-01-21 09:05:12 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-01-22 04:05:17 +0000
@@ -28,14 +28,30 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.schema.Property;
+import org.hisp.dhis.schema.PropertyType;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Transactional
 public class DefaultPreheatService implements PreheatService
 {
+    @Autowired
+    private SchemaService schemaService;
+
     @Override
     public Preheat preheat( PreheatParams params )
     {
@@ -64,4 +80,36 @@
             throw new PreheatException( "Invalid preheat mode." );
         }
     }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public Map<Class<? extends IdentifiableObject>, Collection<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier )
+    {
+        Map<Class<? extends IdentifiableObject>, Collection<String>> map = new HashMap<>();
+
+        if ( object == null )
+        {
+            return map;
+        }
+
+        Schema schema = schemaService.getDynamicSchema( object.getClass() );
+        List<Property> properties = schema.getProperties().stream()
+            .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
+            .collect( Collectors.toList() );
+
+        properties.forEach( p -> {
+            if ( !p.isCollection() )
+            {
+                Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
+                if ( !map.containsKey( klass ) ) map.put( klass, new ArrayList<>() );
+            }
+            else
+            {
+                Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getItemKlass();
+                if ( !map.containsKey( klass ) ) map.put( klass, new ArrayList<>() );
+            }
+        } );
+
+        return map;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-01-21 09:05:12 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-01-22 04:05:17 +0000
@@ -30,10 +30,21 @@
 
 import com.google.common.collect.Lists;
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementGroup;
+import org.hisp.dhis.legend.LegendSet;
+import org.hisp.dhis.option.OptionSet;
+import org.hisp.dhis.user.User;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.Collection;
+import java.util.Map;
+
+import static org.junit.Assert.assertTrue;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -74,4 +85,30 @@
 
         preheatService.validate( params );
     }
+
+    @Test
+    public void testScanNoObjectsDE()
+    {
+        DataElement dataElement = new DataElement( "DataElementA" );
+        dataElement.setAutoFields();
+
+        Map<Class<? extends IdentifiableObject>, Collection<String>> references = preheatService.scanObjectForReferences( dataElement, PreheatIdentifier.UID );
+
+        assertTrue( references.containsKey( OptionSet.class ) );
+        assertTrue( references.containsKey( LegendSet.class ) );
+        assertTrue( references.containsKey( DataElementCategoryCombo.class ) );
+        assertTrue( references.containsKey( User.class ) );
+    }
+
+    @Test
+    public void testScanNoObjectsDEG()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        Map<Class<? extends IdentifiableObject>, Collection<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
+
+        assertTrue( references.containsKey( DataElement.class ) );
+        assertTrue( references.containsKey( User.class ) );
+    }
 }