← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12101: more update person fixes

 

------------------------------------------------------------
revno: 12101
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-09-16 13:56:40 +0200
message:
  more update person fixes
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.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-16 10:10:25 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java	2013-09-16 11:56:40 +0000
@@ -274,33 +274,7 @@
         }
 
         patient.setRegistrationDate( person.getDateOfRegistration() );
-
-        for ( Identifier identifier : person.getIdentifiers() )
-        {
-            if ( identifier.getType() == null )
-            {
-                PatientIdentifier patientIdentifier = new PatientIdentifier();
-                patientIdentifier.setIdentifier( identifier.getValue().trim() );
-                patientIdentifier.setIdentifierType( null );
-                patientIdentifier.setPatient( patient );
-
-                patient.getIdentifiers().add( patientIdentifier );
-
-                continue;
-            }
-
-            PatientIdentifierType type = manager.get( PatientIdentifierType.class, identifier.getType() );
-
-            if ( type != null )
-            {
-                PatientIdentifier patientIdentifier = new PatientIdentifier();
-                patientIdentifier.setIdentifier( identifier.getValue().trim() );
-                patientIdentifier.setIdentifierType( type );
-                patientIdentifier.setPatient( patient );
-
-                patient.getIdentifiers().add( patientIdentifier );
-            }
-        }
+        updateIdentifiers( person, patient );
 
         return patient;
     }
@@ -328,12 +302,12 @@
             return importSummary;
         }
 
-        addSystemIdentifier( person );
+        updateSystemIdentifier( person );
 
         Patient patient = getPatient( person );
         patientService.savePatient( patient );
 
-        addAttributes( person, patient );
+        updateAttributeValues( person, patient );
         patientService.updatePatient( patient );
 
         importSummary.setStatus( ImportStatus.SUCCESS );
@@ -343,46 +317,6 @@
         return importSummary;
     }
 
-    private void addSystemIdentifier( Person person )
-    {
-        Date birthDate = person.getDateOfBirth() != null ? person.getDateOfBirth().getDate() : null;
-        String gender = person.getGender() != null ? person.getGender().getValue() : null;
-
-        if ( birthDate == null || gender == null )
-        {
-            birthDate = new Date();
-            gender = "F";
-        }
-
-        Iterator<Identifier> iterator = person.getIdentifiers().iterator();
-
-        // remove any old system identifiers
-        while ( iterator.hasNext() )
-        {
-            Identifier identifier = iterator.next();
-
-            if ( identifier.getType() == null )
-            {
-                iterator.remove();
-            }
-        }
-
-        String systemId = PatientIdentifierGenerator.getNewIdentifier( birthDate, gender );
-
-        PatientIdentifier patientIdentifier = patientIdentifierService.get( null, systemId );
-
-        while ( patientIdentifier != null )
-        {
-            systemId = PatientIdentifierGenerator.getNewIdentifier( birthDate, gender );
-            patientIdentifier = patientIdentifierService.get( null, systemId );
-        }
-
-        Identifier identifier = new Identifier();
-        identifier.setValue( systemId );
-
-        person.getIdentifiers().add( identifier );
-    }
-
     // -------------------------------------------------------------------------
     // UPDATE
     // -------------------------------------------------------------------------
@@ -396,6 +330,7 @@
         importSummary.setDataValueCount( null );
 
         Patient patient = manager.get( Patient.class, person.getPerson() );
+        System.err.println( "Patient: " + person );
         OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, person.getOrgUnit() );
 
         List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
@@ -443,6 +378,13 @@
         patient.setDobType( dateOfBirth.getType().getValue().charAt( 0 ) );
         patient.setBirthDate( dateOfBirth.getDate() );
 
+        updateSystemIdentifier( person );
+        removeIdentifiers( patient );
+        removeAttributeValues( patient );
+        patientService.updatePatient( patient );
+
+        updateIdentifiers( person, patient );
+        updateAttributeValues( person, patient );
         patientService.updatePatient( patient );
 
         System.err.println( "Patient: " + getPerson( patient ) );
@@ -553,7 +495,7 @@
         return importConflicts;
     }
 
-    private void addAttributes( Person person, Patient patient )
+    private void updateAttributeValues( Person person, Patient patient )
     {
         for ( Attribute attribute : person.getAttributes() )
         {
@@ -572,4 +514,115 @@
             }
         }
     }
+
+    private void updateSystemIdentifier( Person person )
+    {
+        Date birthDate = person.getDateOfBirth() != null ? person.getDateOfBirth().getDate() : null;
+        String gender = person.getGender() != null ? person.getGender().getValue() : null;
+
+        if ( birthDate == null || gender == null )
+        {
+            birthDate = new Date();
+            gender = "F";
+        }
+
+        Iterator<Identifier> iterator = person.getIdentifiers().iterator();
+
+        // remove any old system identifiers
+        while ( iterator.hasNext() )
+        {
+            Identifier identifier = iterator.next();
+
+            if ( identifier.getType() == null )
+            {
+                iterator.remove();
+            }
+        }
+
+        Identifier identifier = generateSystemIdentifier( birthDate, gender );
+
+        if ( person.getPerson() != null )
+        {
+            identifier = generateSystemIdentifier( birthDate, gender );
+            Patient patient = manager.get( Patient.class, person.getPerson() );
+
+            for ( PatientIdentifier patientIdentifier : patient.getIdentifiers() )
+            {
+                if ( patientIdentifier.getIdentifierType() == null && patientIdentifier.getIdentifier() != null )
+                {
+                    identifier = new Identifier( patientIdentifier.getIdentifier() );
+                    break;
+                }
+            }
+        }
+
+        person.getIdentifiers().add( identifier );
+    }
+
+    private Identifier generateSystemIdentifier( Date birthDate, String gender )
+    {
+        String systemId = PatientIdentifierGenerator.getNewIdentifier( birthDate, gender );
+
+        PatientIdentifier patientIdentifier = patientIdentifierService.get( null, systemId );
+
+        while ( patientIdentifier != null )
+        {
+            systemId = PatientIdentifierGenerator.getNewIdentifier( birthDate, gender );
+            patientIdentifier = patientIdentifierService.get( null, systemId );
+        }
+
+        Identifier identifier = new Identifier();
+        identifier.setValue( systemId );
+
+        return identifier;
+    }
+
+    // add identifiers from person => patient
+    private void updateIdentifiers( Person person, Patient patient )
+    {
+        for ( Identifier identifier : person.getIdentifiers() )
+        {
+            if ( identifier.getType() == null )
+            {
+                PatientIdentifier patientIdentifier = new PatientIdentifier();
+                patientIdentifier.setIdentifier( identifier.getValue().trim() );
+                patientIdentifier.setIdentifierType( null );
+                patientIdentifier.setPatient( patient );
+
+                patient.getIdentifiers().add( patientIdentifier );
+
+                continue;
+            }
+
+            PatientIdentifierType type = manager.get( PatientIdentifierType.class, identifier.getType() );
+
+            if ( type != null && nullIfEmpty( identifier.getValue() ) != null )
+            {
+                PatientIdentifier patientIdentifier = new PatientIdentifier();
+                patientIdentifier.setIdentifier( identifier.getValue().trim() );
+                patientIdentifier.setIdentifierType( type );
+                patientIdentifier.setPatient( patient );
+
+                patient.getIdentifiers().add( patientIdentifier );
+            }
+        }
+    }
+
+    private void removeIdentifiers( Patient patient )
+    {
+        for ( PatientIdentifier patientIdentifier : patient.getIdentifiers() )
+        {
+            patientIdentifierService.deletePatientIdentifier( patientIdentifier );
+        }
+
+        patient.setIdentifiers( new HashSet<PatientIdentifier>() );
+        patientService.updatePatient( patient );
+    }
+
+    private void removeAttributeValues( Patient patient )
+    {
+        patientAttributeValueService.deletePatientAttributeValue( patient );
+        patient.setAttributes( new HashSet<PatientAttribute>() );
+        patientService.updatePatient( patient );
+    }
 }