← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14285: scope validation for enrollment (unique only, orgUnit, program, orgUnit+program)

 

------------------------------------------------------------
revno: 14285
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-03-18 17:13:20 +0100
message:
  scope validation for enrollment (unique only, orgUnit, program, orgUnit+program)
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-18 13:20:03 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-03-18 16:13:20 +0000
@@ -255,7 +255,8 @@
 
         if ( !enrollments.getEnrollments().isEmpty() )
         {
-            importSummary = new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance()
+            importSummary.setStatus( ImportStatus.ERROR );
+            importSummary.setDescription( "TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance()
                 + " already have an active enrollment in program " + program.getUid() );
             importSummary.getImportCount().incrementIgnored();
 
@@ -281,8 +282,11 @@
 
         if ( programInstance == null )
         {
-            return new ImportSummary( ImportStatus.ERROR, "Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance()
+            importSummary.setStatus( ImportStatus.ERROR );
+            importSummary.setDescription( "Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance()
                 + " into program " + enrollment.getProgram() );
+
+            return importSummary;
         }
 
         updateAttributeValues( enrollment );
@@ -438,6 +442,8 @@
             attributeValueMap.put( attribute.getAttribute(), attribute.getValue() );
         }
 
+        TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance( enrollment.getTrackedEntityInstance() );
+
         for ( TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet() )
         {
             Boolean mandatory = mandatoryMap.get( trackedEntityAttribute );
@@ -449,6 +455,17 @@
                 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() ) ) );
+
+                System.err.println( "instances: " + instances );
+
+                importConflicts.addAll( checkScope( enrollment, instance, trackedEntityAttribute, instances ) );
+            }
+
             attributeValueMap.remove( trackedEntityAttribute.getUid() );
         }
 
@@ -461,6 +478,82 @@
         return importConflicts;
     }
 
+    private List<ImportConflict> checkScope( Enrollment enrollment, TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute attribute, List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances )
+    {
+        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;
+        }
+
+        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;
+                }
+            }
+
+        }
+        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;
+                }
+            }
+        }
+        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;
+                    }
+                }
+            }
+        }
+        else
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Non-unique attribute value for attribute " +
+                attribute.getUid() ) );
+        }
+
+        return importConflicts;
+    }
+
     private void updateAttributeValues( Enrollment enrollment )
     {
         org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(

=== 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-18 15:42:08 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-18 16:13:20 +0000
@@ -299,11 +299,11 @@
                 continue;
             }
 
-            List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>( trackedEntityAttributeValueService.getTrackedEntityInstance(
-                entityAttribute, attribute.getValue() ) );
-
             if ( entityAttribute.isUnique() )
             {
+                List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>( trackedEntityAttributeValueService.getTrackedEntityInstance(
+                    entityAttribute, attribute.getValue() ) );
+
                 importConflicts.addAll( checkScope( trackedEntityInstance, entityAttribute, instances ) );
             }
         }
@@ -314,7 +314,7 @@
     private List<ImportConflict> checkScope( TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute attribute, List<org.hisp.dhis.trackedentity.TrackedEntityInstance> instances )
     {
         List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
-        org.hisp.dhis.trackedentity.TrackedEntityInstance instance = entityInstanceService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntity() );
+        org.hisp.dhis.trackedentity.TrackedEntityInstance instance = entityInstanceService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() );
 
         if ( instances.isEmpty() || (instances.size() == 1 && instances.contains( instance )) )
         {