← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12094: (person api) validate person when updating

 

------------------------------------------------------------
revno: 12094
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-09-16 09:42:02 +0200
message:
  (person api) validate person when updating
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java	2013-09-13 13:39:22 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java	2013-09-16 07:42:02 +0000
@@ -333,7 +333,7 @@
         Patient patient = getPatient( person );
         patientService.savePatient( patient );
 
-        addAttributes( patient, person );
+        addAttributes( person, patient );
         patientService.updatePatient( patient );
 
         importSummary.setStatus( ImportStatus.SUCCESS );
@@ -390,10 +390,29 @@
     @Override
     public ImportSummary updatePerson( Person person )
     {
-        ImportSummary importSummary = new ImportSummary();
-
         System.err.println( "UPDATE: " + person );
-        Patient patient = getPatient( person );
+
+        ImportSummary importSummary = new ImportSummary();
+        importSummary.setDataValueCount( null );
+
+        Patient patient = patientService.getPatient( person.getPerson() );
+
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        importConflicts.addAll( checkForRequiredIdentifiers( person ) );
+        importConflicts.addAll( checkForRequiredAttributes( person ) );
+
+        importSummary.setConflicts( importConflicts );
+
+        if ( !importConflicts.isEmpty() )
+        {
+            importSummary.setStatus( ImportStatus.ERROR );
+            importSummary.getImportCount().incrementIgnored();
+            return importSummary;
+        }
+
+        importSummary.setStatus( ImportStatus.SUCCESS );
+        importSummary.setReference( patient.getUid() );
+        importSummary.getImportCount().incrementImported();
 
         return importSummary;
     }
@@ -427,6 +446,7 @@
         List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
         Collection<PatientIdentifierType> patientIdentifierTypes = manager.getAll( PatientIdentifierType.class );
         Map<String, String> cacheMap = new HashMap<String, String>();
+        Patient patient = manager.get( Patient.class, person.getPerson() );
 
         for ( Identifier identifier : person.getIdentifiers() )
         {
@@ -447,13 +467,21 @@
                 }
             }
 
-            Collection<PatientIdentifier> patientIdentifiers = patientIdentifierService.getAll(
-                patientIdentifierType, cacheMap.get( patientIdentifierType.getUid() ) );
+            List<PatientIdentifier> patientIdentifiers = new ArrayList<PatientIdentifier>( patientIdentifierService.getAll(
+                patientIdentifierType, cacheMap.get( patientIdentifierType.getUid() ) ) );
 
             if ( !patientIdentifiers.isEmpty() )
             {
-                importConflicts.add(
-                    new ImportConflict( "Identifier.value", "Value already exists for identifier type " + patientIdentifierType.getUid() ) );
+                // if .size() > 1, there is something wrong with the db.. but we for-loop for now
+                for ( PatientIdentifier patientIdentifier : patientIdentifiers )
+                {
+                    if ( !patientIdentifier.getPatient().equals( patient ) )
+                    {
+                        importConflicts.add(
+                            new ImportConflict( "Identifier.value", "Value already exists for patient " + patientIdentifier.getPatient().getUid()
+                                + " with identifier type " + patientIdentifierType.getUid() ) );
+                    }
+                }
             }
         }
 
@@ -489,7 +517,7 @@
         return importConflicts;
     }
 
-    private void addAttributes( Patient patient, Person person )
+    private void addAttributes( Person person, Patient patient )
     {
         for ( Attribute attribute : person.getAttributes() )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java	2013-09-13 14:17:50 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java	2013-09-16 07:42:02 +0000
@@ -106,12 +106,14 @@
 
             for ( Person person : persons.getPersons() )
             {
+                person.setPerson( null );
                 importSummaries.addImportSummary( savePerson( person ) );
             }
         }
         catch ( Exception ex )
         {
             Person person = fromXml( input, Person.class );
+            person.setPerson( null );
             importSummaries.addImportSummary( savePerson( person ) );
         }
 
@@ -130,12 +132,14 @@
 
             for ( Person person : persons.getPersons() )
             {
+                person.setPerson( null );
                 importSummaries.addImportSummary( savePerson( person ) );
             }
         }
         catch ( Exception ex )
         {
             Person person = fromJson( input, Person.class );
+            person.setPerson( null );
             importSummaries.addImportSummary( savePerson( person ) );
         }
 

=== 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 07:06:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java	2013-09-16 07:42:02 +0000
@@ -202,11 +202,6 @@
         }
     }
 
-    public String getResourcePath( HttpServletRequest request, ImportSummary importSummary )
-    {
-        return ContextUtils.getContextPath( request ) + "/api/" + "persons" + "/" + importSummary.getReference();
-    }
-
     // -------------------------------------------------------------------------
     // UPDATE
     // -------------------------------------------------------------------------
@@ -265,4 +260,9 @@
 
         return program;
     }
+
+    private String getResourcePath( HttpServletRequest request, ImportSummary importSummary )
+    {
+        return ContextUtils.getContextPath( request ) + "/api/" + "persons" + "/" + importSummary.getReference();
+    }
 }