← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22272: wip, method for checking for property uniqueness in preater, stored internally as map klass -> p-...

 

------------------------------------------------------------
revno: 22272
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2016-03-13 15:51:26 +0700
message:
  wip, method for checking for property uniqueness in preater, stored internally as map klass -> p-name -> value -> uid
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-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.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-03-10 06:06:01 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-03-13 08:51:26 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.feedback.ErrorReport;
 import org.hisp.dhis.feedback.ObjectErrorReport;
 
 import java.util.Collection;
@@ -100,6 +101,22 @@
     List<PreheatErrorReport> checkReferences( IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier );
 
     /**
+     * Check for properties that are unique.
+     *
+     * @param objects Object to check
+     * @param preheat Preheat Cache to use
+     */
+    List<ObjectErrorReport> checkUniqueness( List<IdentifiableObject> objects, Preheat preheat );
+
+    /**
+     * Check for properties that are unique.
+     *
+     * @param object  Object to check
+     * @param preheat Preheat Cache to use
+     */
+    List<ErrorReport> checkUniqueness( IdentifiableObject object, Preheat preheat );
+
+    /**
      * Connects id object references on a given object using a given identifier + a preheated Preheat cache.
      *
      * @param object     Object to connect to

=== 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-03-10 06:06:01 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-03-13 08:51:26 +0000
@@ -36,6 +36,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.feedback.ErrorCode;
+import org.hisp.dhis.feedback.ErrorReport;
 import org.hisp.dhis.feedback.ObjectErrorReport;
 import org.hisp.dhis.query.Query;
 import org.hisp.dhis.query.QueryService;
@@ -222,6 +223,7 @@
 
         Map<Class<? extends IdentifiableObject>, Set<String>> uidMap = map.get( PreheatIdentifier.UID );
         Map<Class<? extends IdentifiableObject>, Set<String>> codeMap = map.get( PreheatIdentifier.CODE );
+        Map<Class<? extends IdentifiableObject>, Map<String, Map<Object, String>>> uniqueMap = new HashMap<>();
 
         if ( objects.isEmpty() )
         {
@@ -252,7 +254,7 @@
         for ( Class<? extends IdentifiableObject> objectClass : scanObjects.keySet() )
         {
             Schema schema = schemaService.getDynamicSchema( objectClass );
-            List<Property> properties = schema.getProperties().stream()
+            List<Property> identifiableProperties = schema.getProperties().stream()
                 .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
                 .collect( Collectors.toList() );
 
@@ -263,7 +265,7 @@
 
             for ( IdentifiableObject object : identifiableObjects )
             {
-                properties.forEach( p -> {
+                identifiableProperties.forEach( p -> {
                     if ( !p.isCollection() )
                     {
                         Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
@@ -302,7 +304,6 @@
                             if ( !StringUtils.isEmpty( code ) ) codeMap.get( klass ).add( code );
                         }
                     }
-
                 } );
 
                 if ( ValidationRule.class.isInstance( object ) )
@@ -369,6 +370,34 @@
                 if ( uidMap.get( UserGroup.class ).isEmpty() ) uidMap.remove( UserGroup.class );
                 if ( codeMap.get( UserGroup.class ).isEmpty() ) codeMap.remove( UserGroup.class );
             }
+
+            uniqueMap.put( objectClass, handleUniqueProperties( schema, identifiableObjects ) );
+        }
+
+        System.err.println( "uniqueMap: " + uniqueMap );
+
+        return map;
+    }
+
+    private Map<String, Map<Object, String>> handleUniqueProperties( Schema schema, List<IdentifiableObject> objects )
+    {
+        List<Property> uniqueProperties = schema.getProperties().stream()
+            .filter( p -> p.isPersisted() && p.isOwner() && p.isUnique() )
+            .collect( Collectors.toList() );
+
+        Map<String, Map<Object, String>> map = new HashMap<>();
+
+        for ( IdentifiableObject object : objects )
+        {
+            uniqueProperties.forEach( property -> {
+                if ( !map.containsKey( property.getName() ) ) map.put( property.getName(), new HashMap<>() );
+                Object value = ReflectionUtils.invokeMethod( object, property.getGetterMethod() );
+
+                if ( value != null )
+                {
+                    map.get( property.getName() ).put( value, object.getUid() );
+                }
+            } );
         }
 
         return map;
@@ -571,6 +600,45 @@
     }
 
     @Override
+    public List<ObjectErrorReport> checkUniqueness( List<IdentifiableObject> objects, Preheat preheat )
+    {
+        List<ObjectErrorReport> objectErrorReports = new ArrayList<>();
+
+        if ( objects.isEmpty() )
+        {
+            return objectErrorReports;
+        }
+
+        for ( int i = 0; i < objects.size(); i++ )
+        {
+            IdentifiableObject object = objects.get( i );
+            List<ErrorReport> errorReports = checkUniqueness( object, preheat );
+
+            if ( errorReports.isEmpty() ) continue;
+
+            ObjectErrorReport objectErrorReport = new ObjectErrorReport( object.getClass(), i );
+            objectErrorReport.addErrorReports( errorReports );
+            objectErrorReports.add( objectErrorReport );
+
+        }
+
+        return objectErrorReports;
+    }
+
+    @Override
+    public List<ErrorReport> checkUniqueness( IdentifiableObject object, Preheat preheat )
+    {
+        List<ErrorReport> errorReports = new ArrayList<>();
+
+        if ( object == null )
+        {
+            return errorReports;
+        }
+
+        return errorReports;
+    }
+
+    @Override
     @SuppressWarnings( "unchecked" )
     public void connectReferences( Object object, Preheat preheat, PreheatIdentifier identifier )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-03-13 04:20:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-03-13 08:51:26 +0000
@@ -111,6 +111,13 @@
             preheatParams.setReferences( preheatService.collectReferences( params.getObjects() ) );
         }
 
+        for ( Class<? extends IdentifiableObject> klass : params.getObjects().keySet() )
+        {
+            params.getObjects().get( klass ).stream()
+                .filter( identifiableObject -> StringUtils.isEmpty( identifiableObject.getUid() ) )
+                .forEach( identifiableObject -> ((BaseIdentifiableObject) identifiableObject).setUid( CodeGenerator.generateCode() ) );
+        }
+
         ObjectBundle bundle = new ObjectBundle( params, preheatService.preheat( preheatParams ), params.getObjects() );
         bundle.setObjectReferences( preheatService.collectObjectReferences( params.getObjects() ) );
 
@@ -128,11 +135,6 @@
             {
                 if ( Preheat.isDefault( identifiableObject ) ) continue;
 
-                if ( StringUtils.isEmpty( identifiableObject.getUid() ) )
-                {
-                    ((BaseIdentifiableObject) identifiableObject).setUid( CodeGenerator.generateCode() );
-                }
-
                 if ( !StringUtils.isEmpty( identifiableObject.getUid() ) )
                 {
                     map.get( PreheatIdentifier.UID ).get( klass ).put( identifiableObject.getUid(), identifiableObject );
@@ -162,6 +164,7 @@
                 objectBundleValidation.addObjectErrorReports( validateBySchemas( klass, bundle.getObjectMap().get( klass ), bundle ) );
                 objectBundleValidation.addObjectErrorReports( preheatService.checkReferences( bundle.getObjectMap().get( klass ),
                     bundle.getPreheat(), bundle.getPreheatIdentifier() ) );
+                objectBundleValidation.addObjectErrorReports( preheatService.checkUniqueness( bundle.getObjectMap().get( klass ), bundle.getPreheat() ) );
             }
 
             if ( bundle.getImportMode().isCreate() )
@@ -170,6 +173,7 @@
                 objectBundleValidation.addObjectErrorReports( validateBySchemas( klass, bundle.getObjects( klass, false ), bundle ) );
                 objectBundleValidation.addObjectErrorReports( preheatService.checkReferences( bundle.getObjectMap().get( klass ),
                     bundle.getPreheat(), bundle.getPreheatIdentifier() ) );
+                objectBundleValidation.addObjectErrorReports( preheatService.checkUniqueness( bundle.getObjectMap().get( klass ), bundle.getPreheat() ) );
             }
 
             if ( bundle.getImportMode().isUpdate() )
@@ -178,6 +182,7 @@
                 objectBundleValidation.addObjectErrorReports( validateBySchemas( klass, bundle.getObjects( klass, true ), bundle ) );
                 objectBundleValidation.addObjectErrorReports( preheatService.checkReferences( bundle.getObjectMap().get( klass ),
                     bundle.getPreheat(), bundle.getPreheatIdentifier() ) );
+                objectBundleValidation.addObjectErrorReports( preheatService.checkUniqueness( bundle.getObjectMap().get( klass ), bundle.getPreheat() ) );
             }
 
             if ( bundle.getImportMode().isDelete() )