← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12112: wip, events with registration

 

------------------------------------------------------------
revno: 12112
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-09-17 13:13:16 +0200
message:
  wip, events with registration
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.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/program/ProgramStageInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2013-09-10 04:51:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2013-09-17 11:13:16 +0000
@@ -73,6 +73,8 @@
 
     Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
 
+    Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances, boolean completed );
+
     Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate );
 
     Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate, Boolean completed );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2013-08-26 13:01:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2013-09-17 11:13:16 +0000
@@ -67,6 +67,8 @@
 
     Collection<ProgramStageInstance> get( Collection<ProgramInstance> programInstances );
 
+    Collection<ProgramStageInstance> get( Collection<ProgramInstance> programInstances, boolean completed );
+
     /**
      * Get all {@link ProgramStageInstance program stage instances} for unit.
      *

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java	2013-09-16 13:13:14 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java	2013-09-17 11:13:16 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dxf2.InputValidationService;
@@ -41,6 +42,8 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.patientdatavalue.PatientDataValue;
 import org.hisp.dhis.patientdatavalue.PatientDataValueService;
 import org.hisp.dhis.program.Program;
@@ -54,7 +57,9 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
@@ -96,6 +101,12 @@
     private PatientDataValueService patientDataValueService;
 
     @Autowired
+    private PatientService patientService;
+
+    @Autowired
+    private IdentifiableObjectManager manager;
+
+    @Autowired
     private InputValidationService inputValidationService;
 
     @Autowired
@@ -107,44 +118,96 @@
     private I18nFormat format;
 
     // -------------------------------------------------------------------------
-    // Implementation
+    // CREATE
     // -------------------------------------------------------------------------
 
     protected ImportSummary saveEvent( Event event, ImportOptions importOptions )
     {
-        Program program;
+        try
+        {
+            format = i18nManager.getI18nFormat();
+        }
+        catch ( I18nManagerException ex )
+        {
+            return new ImportSummary( ImportStatus.ERROR, ex.getMessage() );
+        }
 
-        if ( event.getProgram() != null )
-        {
-            program = programService.getProgram( event.getProgram() );
-        }
-        else if ( event.getProgramStage() != null )
-        {
-            ProgramStage programStage = programStageService.getProgramStage( event.getProgramStage() );
-            program = programStage.getProgram();
-        }
-        else
-        {
-            return new ImportSummary( ImportStatus.ERROR, "No Event.program or Event.programStage was provided." );
-        }
+        Program program = programService.getProgram( event.getProgram() );
+        ProgramInstance programInstance = null;
+        ProgramStage programStage = programStageService.getProgramStage( event.getProgramStage() );
+        ProgramStageInstance programStageInstance = null;
 
         if ( program == null )
         {
-            return new ImportSummary( ImportStatus.ERROR, "No valid Event.program or Event.programStage was provided." );
+            return new ImportSummary( ImportStatus.ERROR, "Event.program does not point to a valid program" );
+        }
+
+        if ( programStage == null && !program.isSingleEvent() )
+        {
+            return new ImportSummary( ImportStatus.ERROR, "Event.programStage does not point to a valid programStage, and program is multi-event program" );
         }
         else
         {
-            Collection<Program> programsByCurrentUser = programService.getProgramsByCurrentUser();
-
-            if ( !programsByCurrentUser.contains( program ) )
-            {
-                return new ImportSummary( ImportStatus.ERROR, "Current user does not have permission to access this program." );
-            }
-        }
-
-        if ( program.isRegistration() && event.getPerson() == null )
-        {
-            return new ImportSummary( ImportStatus.ERROR, "No Event.person was provided for registration based program." );
+            programStage = program.getProgramStageByStage( 1 );
+        }
+
+        Assert.notNull( programStage );
+
+        if ( verifyProgramAccess( program ) )
+        {
+            return new ImportSummary( ImportStatus.ERROR, "Current user does not have permission to access this program." );
+        }
+
+        if ( program.isRegistration() )
+        {
+            if ( event.getPerson() == null )
+            {
+                return new ImportSummary( ImportStatus.ERROR, "No Event.person was provided for registration based program." );
+            }
+
+            Patient patient = patientService.getPatient( event.getPerson() );
+
+            if ( patient == null )
+            {
+                return new ImportSummary( ImportStatus.ERROR, "Event.person does not point to a valid person." );
+            }
+
+            List<ProgramInstance> programInstances = new ArrayList<ProgramInstance>(
+                programInstanceService.getProgramInstances( patient, program, ProgramInstance.STATUS_ACTIVE ) );
+
+            if ( programInstances.isEmpty() )
+            {
+                return new ImportSummary( ImportStatus.ERROR, "Person " + patient.getUid() + " is not enrolled in program " + program.getUid() );
+            }
+            else if ( programInstances.size() > 1 )
+            {
+                return new ImportSummary( ImportStatus.ERROR,
+                    "Person " + patient.getUid() + " have multiple active enrollments into program " + program.getUid()
+                        + " please check and correct your database." );
+            }
+
+            programInstance = programInstances.get( 0 );
+
+            if ( program.isSingleEvent() )
+            {
+                List<ProgramStageInstance> programStageInstances = new ArrayList<ProgramStageInstance>(
+                    programStageInstanceService.getProgramStageInstances( programInstances, false ) );
+
+                System.err.println( "programStageInstances: " + programStageInstances );
+
+                if ( programStageInstances.isEmpty() )
+                {
+                    return new ImportSummary( ImportStatus.ERROR, "No active event exists for single event program " + program.getUid()
+                        + ", please fix your database." );
+                }
+                else if ( programStageInstances.size() > 1 )
+                {
+                    return new ImportSummary( ImportStatus.ERROR, "Multiple active events exists for single event program " + program.getUid()
+                        + ", please fix your database." );
+                }
+
+                programStageInstance = programStageInstances.get( 0 );
+            }
         }
 
         OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( event.getOrgUnit() );
@@ -153,232 +216,18 @@
         {
             return new ImportSummary( ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit." );
         }
-        else
-        {
-            boolean assignedToOrganisationUnit = false;
-
-            if ( program.getOrganisationUnits().contains( organisationUnit ) )
-            {
-                assignedToOrganisationUnit = true;
-            }
-            else
-            {
-                for ( OrganisationUnitGroup organisationUnitGroup : program.getOrganisationUnitGroups() )
-                {
-                    if ( organisationUnitGroup.getMembers().contains( organisationUnit ) )
-                    {
-                        assignedToOrganisationUnit = true;
-                        break;
-                    }
-                }
-            }
-
-            if ( !assignedToOrganisationUnit )
-            {
-                return new ImportSummary( ImportStatus.ERROR, "Program is not assigned to this organisation unit." );
-            }
-        }
-
-        if ( program.getType() == Program.SINGLE_EVENT_WITHOUT_REGISTRATION )
-        {
-            return saveSingleEventWithoutRegistration( program, organisationUnit, event, importOptions );
-        }
-        else if ( program.getType() == Program.SINGLE_EVENT_WITH_REGISTRATION )
-        {
-            return saveEventWithRegistration( program, organisationUnit, event, importOptions );
-        }
-        else if ( program.getType() == Program.MULTIPLE_EVENTS_WITH_REGISTRATION )
-        {
-            return saveEventWithRegistration( program, organisationUnit, event, importOptions );
-        }
-
-        return new ImportSummary();
-    }
-
-    private ImportSummary saveEventWithRegistration( Program program, OrganisationUnit organisationUnit, Event event, ImportOptions importOptions )
-    {
-        ImportSummary importSummary = new ImportSummary();
-        importSummary.setStatus( ImportStatus.SUCCESS );
-
-        return importSummary;
-    }
-
-    private ImportSummary saveSingleEventWithoutRegistration( Program program, OrganisationUnit organisationUnit, Event event, ImportOptions importOptions )
-    {
-        try
-        {
-            format = i18nManager.getI18nFormat();
-        }
-        catch ( I18nManagerException ex )
-        {
-            return new ImportSummary( ImportStatus.ERROR, ex.getMessage() );
-        }
-
-        Date eventDate = format.parseDate( event.getEventDate() );
-
-        if ( eventDate == null )
-        {
-            return new ImportSummary( ImportStatus.ERROR, "Event.eventDate is not in a valid format." );
-        }
-
-        ImportSummary importSummary = new ImportSummary();
-        importSummary.setStatus( ImportStatus.SUCCESS );
-
-        ProgramStageInstance programStageInstance = null;
-
-        String storedBy = getStoredBy( event, importSummary );
-
-        if ( importOptions == null || !importOptions.isDryRun() )
-        {
-            ProgramInstance programInstance = programInstanceService.getProgramInstances( program ).iterator().next();
-
-            programStageInstance = saveEventDate( programInstance, organisationUnit, eventDate,
-                event.getCompleted(), event.getCoordinate(), storedBy );
-
-            importSummary.setReference( programStageInstance.getUid() );
-        }
-
-        for ( DataValue dataValue : event.getDataValues() )
-        {
-            DataElement dataElement = dataElementService.getDataElement( dataValue.getDataElement() );
-
-            if ( dataElement == null )
-            {
-                importSummary.getConflicts().add( new ImportConflict( "dataElementId", dataValue.getDataElement() + " is not a valid dataElementId." ) );
-                importSummary.getDataValueCount().incrementIgnored();
-            }
-            else
-            {
-                if ( validateDataElement( dataElement, dataValue.getValue(), importSummary ) )
-                {
-                    String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
-
-                    if ( importOptions == null || !importOptions.isDryRun() )
-                    {
-                        saveDataValue( programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere() );
-                    }
-
-                    importSummary.getDataValueCount().incrementImported();
-                }
-            }
-        }
-
-        return importSummary;
-    }
-
-    private String getStoredBy( Event event, ImportSummary importSummary )
-    {
-        String storedBy = event.getStoredBy();
-
-        if ( storedBy == null )
-        {
-            storedBy = currentUserService.getCurrentUsername();
-        }
-        else if ( storedBy.length() >= 31 )
-        {
-            if ( importSummary != null )
-            {
-                importSummary.getConflicts().add( new ImportConflict( "storedBy", storedBy + " is more than 31 characters, using current username instead." ) );
-            }
-            storedBy = currentUserService.getCurrentUsername();
-        }
-        return storedBy;
-    }
-
-    private boolean validateDataElement( DataElement dataElement, String value, ImportSummary importSummary )
-    {
-        InputValidationService.Status status = inputValidationService.validateDataElement( dataElement, value );
-
-        if ( !status.isSuccess() )
-        {
-            importSummary.getConflicts().add( new ImportConflict( dataElement.getUid(), status.getMessage() ) );
-            importSummary.getDataValueCount().incrementIgnored();
-            return false;
-        }
-
-        return true;
-    }
-
-    private ImportSummary saveSingleEventWithRegistration( Program program, OrganisationUnit organisationUnit, Event event )
-    {
-        return new ImportSummary();
-    }
-
-    private ImportSummary saveMultipleEventsWithRegistration( Program program, OrganisationUnit organisationUnit, Event event )
-    {
-        return new ImportSummary();
-    }
-
-    private ProgramStageInstance saveEventDate( ProgramInstance programInstance, OrganisationUnit organisationUnit, Date date, Boolean completed,
-        Coordinate coordinate, String storedBy )
-    {
-        ProgramStage programStage = programInstance.getProgram().getProgramStageByStage( 1 );
-
-        ProgramStageInstance programStageInstance = new ProgramStageInstance();
-        programStageInstance.setProgramInstance( programInstance );
-        programStageInstance.setProgramStage( programStage );
-        programStageInstance.setDueDate( date );
-        programStageInstance.setExecutionDate( date );
-        programStageInstance.setOrganisationUnit( organisationUnit );
-
-        if ( programStage.getCaptureCoordinates() )
-        {
-            if ( coordinate.isValid() )
-            {
-                programStageInstance.setCoordinates( coordinate.getCoordinateString() );
-            }
-            else
-            {
-                programStageInstance.setCoordinates( null );
-            }
-        }
-
-        if ( completed != null )
-        {
-            programStageInstance.setCompleted( completed );
-            programStageInstance.setCompletedDate( new Date() );
-            programStageInstance.setCompletedUser( storedBy );
-        }
-
-        programStageInstanceService.addProgramStageInstance( programStageInstance );
-
-        return programStageInstance;
-    }
-
-    private void saveDataValue( ProgramStageInstance programStageInstance, String storedBy, DataElement dataElement, String value, Boolean providedElsewhere )
-    {
-        if ( value != null && value.trim().length() == 0 )
-        {
-            value = null;
-        }
-
-        PatientDataValue patientDataValue = patientDataValueService.getPatientDataValue( programStageInstance, dataElement );
-
-        if ( value != null )
-        {
-            if ( patientDataValue == null )
-            {
-                patientDataValue = new PatientDataValue( programStageInstance, dataElement, new Date(), value );
-                patientDataValue.setStoredBy( storedBy );
-                patientDataValue.setProvidedElsewhere( providedElsewhere );
-
-                patientDataValueService.savePatientDataValue( patientDataValue );
-            }
-            else
-            {
-                patientDataValue.setValue( value );
-                patientDataValue.setTimestamp( new Date() );
-                patientDataValue.setProvidedElsewhere( providedElsewhere );
-                patientDataValue.setStoredBy( storedBy );
-
-                patientDataValueService.updatePatientDataValue( patientDataValue );
-            }
-        }
-        else if ( patientDataValue != null )
-        {
-            patientDataValueService.deletePatientDataValue( patientDataValue );
-        }
-    }
+
+        if ( verifyProgramOrganisationUnitAssociation( program, organisationUnit ) )
+        {
+            return new ImportSummary( ImportStatus.ERROR, "Program is not assigned to this organisation unit." );
+        }
+
+        return saveEvent( program, programStage, organisationUnit, event, importOptions );
+    }
+
+    // -------------------------------------------------------------------------
+    // READ
+    // -------------------------------------------------------------------------
 
     @Override
     public Events getEvents( Program program, OrganisationUnit organisationUnit )
@@ -463,6 +312,10 @@
         return convertProgramStageInstance( programStageInstance );
     }
 
+    // -------------------------------------------------------------------------
+    // UPDATE
+    // -------------------------------------------------------------------------
+
     @Override
     public void updateEvent( Event event )
     {
@@ -523,6 +376,10 @@
         }
     }
 
+    // -------------------------------------------------------------------------
+    // DELETE
+    // -------------------------------------------------------------------------
+
     @Override
     public void deleteEvent( Event event )
     {
@@ -534,6 +391,10 @@
         }
     }
 
+    // -------------------------------------------------------------------------
+    // HELPERS
+    // -------------------------------------------------------------------------
+
     private Event convertProgramStageInstance( ProgramStageInstance programStageInstance )
     {
         if ( programStageInstance == null )
@@ -566,4 +427,194 @@
 
         return event;
     }
+
+    private boolean verifyProgramOrganisationUnitAssociation( Program program, OrganisationUnit organisationUnit )
+    {
+        boolean assignedToOrganisationUnit = false;
+
+        if ( program.getOrganisationUnits().contains( organisationUnit ) )
+        {
+            assignedToOrganisationUnit = true;
+        }
+        else
+        {
+            for ( OrganisationUnitGroup organisationUnitGroup : program.getOrganisationUnitGroups() )
+            {
+                if ( organisationUnitGroup.getMembers().contains( organisationUnit ) )
+                {
+                    assignedToOrganisationUnit = true;
+                    break;
+                }
+            }
+        }
+
+        return !assignedToOrganisationUnit;
+    }
+
+    private boolean verifyProgramAccess( Program program )
+    {
+        Collection<Program> programsByCurrentUser = programService.getProgramsByCurrentUser();
+        return !programsByCurrentUser.contains( program );
+    }
+
+    private boolean validateDataElement( DataElement dataElement, String value, ImportSummary importSummary )
+    {
+        InputValidationService.Status status = inputValidationService.validateDataElement( dataElement, value );
+
+        if ( !status.isSuccess() )
+        {
+            importSummary.getConflicts().add( new ImportConflict( dataElement.getUid(), status.getMessage() ) );
+            importSummary.getDataValueCount().incrementIgnored();
+            return false;
+        }
+
+        return true;
+    }
+
+    private String getStoredBy( Event event, ImportSummary importSummary )
+    {
+        String storedBy = event.getStoredBy();
+
+        if ( storedBy == null )
+        {
+            storedBy = currentUserService.getCurrentUsername();
+        }
+        else if ( storedBy.length() >= 31 )
+        {
+            if ( importSummary != null )
+            {
+                importSummary.getConflicts().add( new ImportConflict( "storedBy", storedBy + " is more than 31 characters, using current username instead." ) );
+            }
+            storedBy = currentUserService.getCurrentUsername();
+        }
+        return storedBy;
+    }
+
+    private void saveDataValue( ProgramStageInstance programStageInstance, String storedBy, DataElement dataElement, String value, Boolean providedElsewhere )
+    {
+        if ( value != null && value.trim().length() == 0 )
+        {
+            value = null;
+        }
+
+        PatientDataValue patientDataValue = patientDataValueService.getPatientDataValue( programStageInstance, dataElement );
+
+        if ( value != null )
+        {
+            if ( patientDataValue == null )
+            {
+                patientDataValue = new PatientDataValue( programStageInstance, dataElement, new Date(), value );
+                patientDataValue.setStoredBy( storedBy );
+                patientDataValue.setProvidedElsewhere( providedElsewhere );
+
+                patientDataValueService.savePatientDataValue( patientDataValue );
+            }
+            else
+            {
+                patientDataValue.setValue( value );
+                patientDataValue.setTimestamp( new Date() );
+                patientDataValue.setProvidedElsewhere( providedElsewhere );
+                patientDataValue.setStoredBy( storedBy );
+
+                patientDataValueService.updatePatientDataValue( patientDataValue );
+            }
+        }
+        else if ( patientDataValue != null )
+        {
+            patientDataValueService.deletePatientDataValue( patientDataValue );
+        }
+    }
+
+    private ProgramStageInstance saveEventDate( ProgramStage programStage, ProgramInstance programInstance, OrganisationUnit organisationUnit, Date date, Boolean completed,
+        Coordinate coordinate, String storedBy )
+    {
+        ProgramStageInstance programStageInstance = new ProgramStageInstance();
+        programStageInstance.setProgramInstance( programInstance );
+        programStageInstance.setProgramStage( programStage );
+        programStageInstance.setDueDate( date );
+        programStageInstance.setExecutionDate( date );
+        programStageInstance.setOrganisationUnit( organisationUnit );
+
+        if ( programStage.getCaptureCoordinates() )
+        {
+            if ( coordinate.isValid() )
+            {
+                programStageInstance.setCoordinates( coordinate.getCoordinateString() );
+            }
+            else
+            {
+                programStageInstance.setCoordinates( null );
+            }
+        }
+
+        if ( completed != null )
+        {
+            programStageInstance.setCompleted( completed );
+            programStageInstance.setCompletedDate( new Date() );
+            programStageInstance.setCompletedUser( storedBy );
+        }
+
+        programStageInstanceService.addProgramStageInstance( programStageInstance );
+
+        return programStageInstance;
+    }
+
+    private ImportSummary saveEvent( Program program, ProgramStage programStage, OrganisationUnit organisationUnit, Event event, ImportOptions importOptions )
+    {
+        ImportSummary importSummary = new ImportSummary();
+        importSummary.setStatus( ImportStatus.SUCCESS );
+
+        if ( !program.isSingleEvent() )
+        {
+            return new ImportSummary( ImportStatus.ERROR, "Multi-event programs are not supported right now." );
+        }
+
+        Date eventDate = format.parseDate( event.getEventDate() );
+
+        if ( eventDate == null )
+        {
+            return new ImportSummary( ImportStatus.ERROR, "Event.eventDate is not in a valid format." );
+        }
+
+        String storedBy = getStoredBy( event, importSummary );
+
+        ProgramStageInstance programStageInstance = null;
+
+        if ( importOptions == null || !importOptions.isDryRun() )
+        {
+            ProgramInstance programInstance = programInstanceService.getProgramInstances( program ).iterator().next();
+
+            programStageInstance = saveEventDate( programStage, programInstance, organisationUnit, eventDate,
+                event.getCompleted(), event.getCoordinate(), storedBy );
+
+            importSummary.setReference( programStageInstance.getUid() );
+        }
+
+        for ( DataValue dataValue : event.getDataValues() )
+        {
+            DataElement dataElement = dataElementService.getDataElement( dataValue.getDataElement() );
+
+            if ( dataElement == null )
+            {
+                importSummary.getConflicts().add( new ImportConflict( "dataElementId", dataValue.getDataElement() + " is not a valid dataElementId." ) );
+                importSummary.getDataValueCount().incrementIgnored();
+            }
+            else
+            {
+                if ( validateDataElement( dataElement, dataValue.getValue(), importSummary ) )
+                {
+                    String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
+
+                    if ( importOptions == null || !importOptions.isDryRun() )
+                    {
+                        saveDataValue( programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere() );
+                    }
+
+                    importSummary.getDataValueCount().incrementImported();
+                }
+            }
+        }
+
+        return importSummary;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-09-10 04:51:49 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-09-17 11:13:16 +0000
@@ -206,6 +206,11 @@
         return programStageInstanceStore.get( programInstances );
     }
 
+    public Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances, boolean completed )
+    {
+        return programStageInstanceStore.get( programInstances, completed );
+    }
+
     public Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate )
     {
         return programStageInstanceStore.get( dueDate );

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-09-12 07:33:02 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-09-17 11:13:16 +0000
@@ -171,13 +171,19 @@
         return getCriteria( Restrictions.eq( "programStage", programStage ) ).list();
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public Collection<ProgramStageInstance> get( Collection<ProgramInstance> programInstances )
     {
         return getCriteria( Restrictions.in( "programInstance", programInstances ) ).list();
     }
 
     @SuppressWarnings("unchecked")
+    public Collection<ProgramStageInstance> get( Collection<ProgramInstance> programInstances, boolean completed )
+    {
+        return getCriteria( Restrictions.in( "programInstance", programInstances ), Restrictions.eq( "completed", completed ) ).list();
+    }
+
+    @SuppressWarnings("unchecked")
     public Collection<ProgramStageInstance> get( Date dueDate )
     {
         return getCriteria( Restrictions.eq( "dueDate", dueDate ) ).list();

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-09-16 13:13:14 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-09-17 11:13:16 +0000
@@ -36,6 +36,7 @@
 import org.hisp.dhis.dxf2.event.EventService;
 import org.hisp.dhis.dxf2.event.Events;
 import org.hisp.dhis.dxf2.event.ImportEventTask;
+import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
@@ -206,7 +207,10 @@
             {
                 if ( !importOptions.isDryRun() )
                 {
-                    importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    if ( !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+                    {
+                        importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    }
                 }
             }
 
@@ -216,7 +220,10 @@
 
                 if ( !importOptions.isDryRun() )
                 {
-                    response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    if ( !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+                    {
+                        response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    }
                 }
             }
 
@@ -245,7 +252,10 @@
             {
                 if ( !importOptions.isDryRun() )
                 {
-                    importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    if ( !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+                    {
+                        importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    }
                 }
             }
 
@@ -255,7 +265,10 @@
 
                 if ( !importOptions.isDryRun() )
                 {
-                    response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    if ( !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+                    {
+                        response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+                    }
                 }
             }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java	2013-09-16 13:13:14 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java	2013-09-17 11:13:16 +0000
@@ -32,12 +32,13 @@
 import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.dxf2.importsummary.ImportStatus;
+import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.person.Gender;
 import org.hisp.dhis.dxf2.person.Person;
 import org.hisp.dhis.dxf2.person.PersonService;
 import org.hisp.dhis.dxf2.person.Persons;
-import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
-import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
@@ -178,7 +179,12 @@
         {
             response.setStatus( HttpServletResponse.SC_CREATED );
             ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
-            response.setHeader( "Location", getResourcePath( request, importSummary ) );
+
+            if ( !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+            {
+                response.setHeader( "Location", getResourcePath( request, importSummary ) );
+            }
+
             JacksonUtils.toXml( response.getOutputStream(), importSummary );
         }
     }
@@ -197,7 +203,12 @@
         {
             response.setStatus( HttpServletResponse.SC_CREATED );
             ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
-            response.setHeader( "Location", getResourcePath( request, importSummary ) );
+
+            if ( !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+            {
+                response.setHeader( "Location", getResourcePath( request, importSummary ) );
+            }
+
             JacksonUtils.toJson( response.getOutputStream(), importSummary );
         }
     }
@@ -235,11 +246,6 @@
     }
 
     // -------------------------------------------------------------------------
-    // ENROLLMENT
-    // -------------------------------------------------------------------------
-
-
-    // -------------------------------------------------------------------------
     // HELPERS
     // -------------------------------------------------------------------------