← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14364: simplified scopeCheck in teiService, enrollmentService, uses TrackedEntityInstanceQueryParams for...

 

------------------------------------------------------------
revno: 14364
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-24 15:06:55 +0100
message:
  simplified scopeCheck in teiService, enrollmentService, uses TrackedEntityInstanceQueryParams for getting values based on scopes
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-03-24 10:46:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-03-24 14:06:55 +0000
@@ -29,6 +29,9 @@
  */
 
 import com.google.common.collect.Maps;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.dxf2.events.trackedentity.Attribute;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService;
@@ -44,6 +47,7 @@
 import org.hisp.dhis.program.ProgramTrackedEntityAttribute;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
+import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -79,6 +83,9 @@
     private TrackedEntityAttributeValueService trackedEntityAttributeValueService;
 
     @Autowired
+    private IdentifiableObjectManager manager;
+
+    @Autowired
     private I18nManager i18nManager;
 
     // -------------------------------------------------------------------------
@@ -461,13 +468,13 @@
                 continue;
             }
 
-
             if ( trackedEntityAttribute.isUnique() )
             {
-                List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>( trackedEntityAttributeValueService.getTrackedEntityInstance(
-                    trackedEntityAttribute, attributeValueMap.get( trackedEntityAttribute.getUid() ) ) );
+                OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, instance.getOrgUnit() );
 
-                importConflicts.addAll( checkScope( enrollment, instance, trackedEntityAttribute, instances ) );
+                importConflicts.addAll(
+                    checkScope( trackedEntityInstance, trackedEntityAttribute, attributeValueMap.get( trackedEntityAttribute.getUid() ), organisationUnit, program )
+                );
             }
 
             attributeValueMap.remove( trackedEntityAttribute.getUid() );
@@ -482,79 +489,39 @@
         return importConflicts;
     }
 
-    private List<ImportConflict> checkScope( Enrollment enrollment, TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute attribute, List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances )
+    private List<ImportConflict> checkScope( org.hisp.dhis.trackedentity.TrackedEntityInstance tei, TrackedEntityAttribute attribute, String value, OrganisationUnit organisationUnit, Program program )
     {
         List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
-        org.hisp.dhis.trackedentity.TrackedEntityInstance instance = teiService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() );
-
-        if ( instances.isEmpty() || (instances.size() == 1 && instances.contains( instance )) )
-        {
-            return importConflicts;
-        }
+
+        TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
+        params.setOrganisationUnitMode( "DESCENDANTS" );
+
+        QueryItem queryItem = new QueryItem( attribute, "eq", value, false );
+        params.getAttributes().add( queryItem );
 
         if ( attribute.getOrgunitScope() && attribute.getProgramScope() )
         {
-            for ( org.hisp.dhis.trackedentity.TrackedEntityInstance tei : instances )
-            {
-                boolean orgUnitMatch = false;
-                boolean programMatch = false;
-
-                if ( trackedEntityInstance.getOrgUnit().equals( tei.getOrganisationUnit().getUid() ) )
-                {
-                    orgUnitMatch = true;
-                }
-
-                for ( ProgramInstance programInstance : tei.getProgramInstances() )
-                {
-                    if ( enrollment.getProgram().equals( programInstance.getProgram().getUid() ) )
-                    {
-                        programMatch = true;
-                        break;
-                    }
-                }
-
-                if ( orgUnitMatch && programMatch )
-                {
-                    importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
-                        attribute.getUid() + ", with scope orgUnit+program." ) );
-                    break;
-                }
-            }
-
+            params.setProgram( program );
+            params.getOrganisationUnits().add( organisationUnit );
         }
         else if ( attribute.getOrgunitScope() )
         {
-            for ( org.hisp.dhis.trackedentity.TrackedEntityInstance tei : instances )
-            {
-                if ( trackedEntityInstance.getOrgUnit().equals( tei.getOrganisationUnit().getUid() ) )
-                {
-                    importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
-                        attribute.getUid() + ", with scope orgUnit." ) );
-                    break;
-                }
-            }
+            params.getOrganisationUnits().add( organisationUnit );
         }
         else if ( attribute.getProgramScope() )
         {
-            for ( org.hisp.dhis.trackedentity.TrackedEntityInstance tei : instances )
-            {
-                for ( ProgramInstance programInstance : tei.getProgramInstances() )
-                {
-                    if ( enrollment.getProgram().equals( programInstance.getProgram().getUid() ) )
-                    {
-                        importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
-                            attribute.getUid() + ", with scope program." ) );
-                        break;
-                    }
-                }
-            }
+            params.setProgram( program );
         }
-        else
+
+        Grid instances = teiService.getTrackedEntityInstances( params );
+
+        if ( instances.getHeight() == 0 || (instances.getHeight() == 1 && instances.getRow( 0 ).contains( tei.getUid() )) )
         {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
-                attribute.getUid() ) );
+            return importConflicts;
         }
 
+        importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value '" + value + "' for attribute " + attribute.getUid() ) );
+
         return importConflicts;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-19 15:39:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-24 14:06:55 +0000
@@ -28,7 +28,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
@@ -38,6 +40,7 @@
 import org.hisp.dhis.relationship.RelationshipType;
 import org.hisp.dhis.trackedentity.TrackedEntity;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.hisp.dhis.trackedentity.TrackedEntityService;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
@@ -59,7 +62,7 @@
     // -------------------------------------------------------------------------
 
     @Autowired
-    private org.hisp.dhis.trackedentity.TrackedEntityInstanceService trackedEntityInstanceService;
+    private org.hisp.dhis.trackedentity.TrackedEntityInstanceService teiService;
 
     @Autowired
     private TrackedEntityAttributeValueService attributeValueService;
@@ -86,7 +89,7 @@
     @Override
     public TrackedEntityInstance getTrackedEntityInstance( String uid )
     {
-        return getTrackedEntityInstance( trackedEntityInstanceService.getTrackedEntityInstance( uid ) );
+        return getTrackedEntityInstance( teiService.getTrackedEntityInstance( uid ) );
     }
 
     @Override
@@ -174,10 +177,10 @@
         }
 
         org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance( trackedEntityInstance );
-        trackedEntityInstanceService.addTrackedEntityInstance( entityInstance );
+        teiService.addTrackedEntityInstance( entityInstance );
 
         updateAttributeValues( trackedEntityInstance, entityInstance );
-        trackedEntityInstanceService.updateTrackedEntityInstance( entityInstance );
+        teiService.updateTrackedEntityInstance( entityInstance );
 
         importSummary.setReference( entityInstance.getUid() );
         importSummary.getImportCount().incrementImported();
@@ -227,11 +230,11 @@
 
         removeRelationships( entityInstance );
         removeAttributeValues( entityInstance );
-        trackedEntityInstanceService.updateTrackedEntityInstance( entityInstance );
+        teiService.updateTrackedEntityInstance( entityInstance );
 
         updateRelationships( trackedEntityInstance, entityInstance );
         updateAttributeValues( trackedEntityInstance, entityInstance );
-        trackedEntityInstanceService.updateTrackedEntityInstance( entityInstance );
+        teiService.updateTrackedEntityInstance( entityInstance );
 
         importSummary.setStatus( ImportStatus.SUCCESS );
         importSummary.setReference( entityInstance.getUid() );
@@ -247,11 +250,11 @@
     @Override
     public void deleteTrackedEntityInstance( TrackedEntityInstance trackedEntityInstance )
     {
-        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = trackedEntityInstanceService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() );
+        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = teiService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() );
 
         if ( entityInstance != null )
         {
-            trackedEntityInstanceService.deleteTrackedEntityInstance( entityInstance );
+            teiService.deleteTrackedEntityInstance( entityInstance );
         }
         else
         {
@@ -302,44 +305,42 @@
 
             if ( entityAttribute.isUnique() )
             {
-                List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>( trackedEntityAttributeValueService.getTrackedEntityInstance(
-                    entityAttribute, attribute.getValue() ) );
+                OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, trackedEntityInstance.getOrgUnit() );
+                org.hisp.dhis.trackedentity.TrackedEntityInstance tei = teiService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() );
 
-                importConflicts.addAll( checkScope( trackedEntityInstance, entityAttribute, instances ) );
+                importConflicts.addAll(
+                    checkScope( tei, entityAttribute, attribute.getValue(), organisationUnit )
+                );
             }
         }
 
         return importConflicts;
     }
 
-    private List<ImportConflict> checkScope( TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute attribute, List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances )
+    private List<ImportConflict> checkScope( org.hisp.dhis.trackedentity.TrackedEntityInstance tei, TrackedEntityAttribute attribute, String value, OrganisationUnit organisationUnit )
     {
         List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
-        org.hisp.dhis.trackedentity.TrackedEntityInstance instance = entityInstanceService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() );
-
-        if ( instances.isEmpty() || (instances.size() == 1 && instances.contains( instance )) )
-        {
-            return importConflicts;
-        }
+
+        TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
+        params.setOrganisationUnitMode( "DESCENDANTS" );
+
+        QueryItem queryItem = new QueryItem( attribute, "eq", value, false );
+        params.getAttributes().add( queryItem );
 
         if ( attribute.getOrgunitScope() )
         {
-            for ( org.hisp.dhis.trackedentity.TrackedEntityInstance tei : instances )
-            {
-                if ( trackedEntityInstance.getOrgUnit().equals( tei.getOrganisationUnit().getUid() ) )
-                {
-                    importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
-                        attribute.getUid() + ", with scope orgUnit." ) );
-                    break;
-                }
-            }
+            params.getOrganisationUnits().add( organisationUnit );
         }
-        else
+
+        Grid instances = teiService.getTrackedEntityInstances( params );
+
+        if ( instances.getHeight() == 0 || (instances.getHeight() == 1 && instances.getRow( 0 ).contains( tei.getUid() )) )
         {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
-                attribute.getUid() ) );
+            return importConflicts;
         }
 
+        importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value '" + value + "' for attribute " + attribute.getUid() ) );
+
         return importConflicts;
     }
 
@@ -422,6 +423,6 @@
             attributeValueService.deleteTrackedEntityAttributeValue( trackedEntityAttributeValue );
         }
 
-        trackedEntityInstanceService.updateTrackedEntityInstance( entityInstance );
+        teiService.updateTrackedEntityInstance( entityInstance );
     }
 }