← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12080: added method to getAll patienIdentifers with a certain value. They should be unique, but in reali...

 

------------------------------------------------------------
revno: 12080
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-09-13 14:36:44 +0200
message:
  added method to getAll patienIdentifers with a certain value. They should be unique, but in reality there can be duplicates (for historical reasons).
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.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/patient/PatientIdentifierService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierService.java	2013-09-11 04:12:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierService.java	2013-09-13 12:36:44 +0000
@@ -62,6 +62,8 @@
 
     PatientIdentifier get( PatientIdentifierType type, String identifier );
 
+    Collection<PatientIdentifier> getAll( PatientIdentifierType type, String identifier );
+
     Patient getPatient( PatientIdentifierType idenType, String value );
 
     Collection<Patient> getPatientsByIdentifier( String identifier, int min, int max );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java	2013-09-13 07:40:23 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java	2013-09-13 12:36:44 +0000
@@ -48,6 +48,9 @@
 
     PatientIdentifier get( PatientIdentifierType type, String identifier );
 
+    /* We need this since we have allowed identifiers with duplicate values in the past. This returns a list instead. */
+    Collection<PatientIdentifier> getAll( PatientIdentifierType type, String identifier );
+
     Collection<PatientIdentifier> getByIdentifier( String identifier );
 
     Collection<PatientIdentifier> getByType( PatientIdentifierType identifierType );

=== 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 10:42:46 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java	2013-09-13 12:36:44 +0000
@@ -48,8 +48,12 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import static org.hisp.dhis.system.util.TextUtils.nullIfEmpty;
 
@@ -306,8 +310,12 @@
     @Override
     public Person savePerson( Person person )
     {
-        checkForRequiredIdentifiers( person );
-        checkForRequiredAttributes( person );
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        importConflicts.addAll( checkForRequiredIdentifiers( person ) );
+        importConflicts.addAll( checkForRequiredAttributes( person ) );
+
+        System.err.println( importConflicts );
+
         addSystemIdentifier( person );
 
         Patient patient = getPatient( person );
@@ -319,14 +327,71 @@
         return getPerson( patient );
     }
 
-    private ImportConflict checkForRequiredIdentifiers( Person person )
+    private List<ImportConflict> checkForRequiredIdentifiers( Person person )
     {
-        return null;
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        Collection<PatientIdentifierType> patientIdentifierTypes = manager.getAll( PatientIdentifierType.class );
+        Map<String, String> cacheMap = new HashMap<String, String>();
+
+        for ( Identifier identifier : person.getIdentifiers() )
+        {
+            if ( identifier.getValue() != null )
+            {
+                cacheMap.put( identifier.getType(), identifier.getValue() );
+            }
+        }
+
+        for ( PatientIdentifierType patientIdentifierType : patientIdentifierTypes )
+        {
+            if ( patientIdentifierType.isMandatory() )
+            {
+                if ( !cacheMap.keySet().contains( patientIdentifierType.getUid() ) )
+                {
+                    importConflicts.add(
+                        new ImportConflict( "Identifier.type", "Missing required identifier type " + patientIdentifierType.getUid() ) );
+                }
+            }
+
+            Collection<PatientIdentifier> patientIdentifiers = patientIdentifierService.getAll(
+                patientIdentifierType, cacheMap.get( patientIdentifierType.getUid() ) );
+
+            if ( !patientIdentifiers.isEmpty() )
+            {
+                importConflicts.add(
+                    new ImportConflict( "Identifier.value", "Value already exists for identifier type " + patientIdentifierType.getUid() ) );
+            }
+        }
+
+        return importConflicts;
     }
 
-    private ImportConflict checkForRequiredAttributes( Person person )
+    private List<ImportConflict> checkForRequiredAttributes( Person person )
     {
-        return null;
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        Collection<PatientAttribute> patientAttributes = manager.getAll( PatientAttribute.class );
+        Set<String> cache = new HashSet<String>();
+
+        for ( Identifier identifier : person.getIdentifiers() )
+        {
+            if ( identifier.getValue() != null )
+            {
+                cache.add( identifier.getType() );
+            }
+        }
+
+        for ( PatientAttribute patientAttribute : patientAttributes )
+        {
+            if ( patientAttribute.isMandatory() )
+            {
+                if ( !cache.contains( patientAttribute.getUid() ) )
+                {
+                    importConflicts.add(
+                        new ImportConflict( "Identifier.type", "Missing required attribute type " + patientAttribute.getUid() ) );
+                }
+            }
+        }
+
+        return importConflicts;
     }
 
     private void addAttributes( Patient patient, Person person )

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java	2013-09-11 04:12:50 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java	2013-09-13 12:36:44 +0000
@@ -28,10 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.springframework.transaction.annotation.Transactional;
+
 import java.util.Collection;
 
-import org.springframework.transaction.annotation.Transactional;
-
 /**
  * @author Abyot Asalefew Gizaw
  * @version $Id$
@@ -115,6 +115,11 @@
         return patientIdentifierStore.get( type, identifier );
     }
 
+    public Collection<PatientIdentifier> getAll( PatientIdentifierType type, String identifier )
+    {
+        return patientIdentifierStore.getAll( type, identifier );
+    }
+
     public Patient getPatient( PatientIdentifierType idenType, String value )
     {
         return patientIdentifierStore.getPatient( idenType, value );
@@ -135,7 +140,7 @@
     {
         return patientIdentifierStore.get( identifierTypes, patient );
     }
-    
+
     @Override
     public boolean checkDuplicateIdentifier( String identifier )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.java	2013-09-11 16:02:06 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.java	2013-09-13 12:36:44 +0000
@@ -29,6 +29,7 @@
  */
 
 import java.util.Collection;
+import java.util.Set;
 
 import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
@@ -65,6 +66,13 @@
     }
 
     @SuppressWarnings( "unchecked" )
+    public Collection<PatientIdentifier> getAll( PatientIdentifierType type, String identifier )
+    {
+        return getCriteria( Restrictions.eq( "identifierType", type ),
+            Restrictions.eq( "identifier", identifier ) ).list();
+    }
+
+    @SuppressWarnings( "unchecked" )
     public Collection<PatientIdentifier> getByIdentifier( String identifier )
     {
         return getCriteria( Restrictions.ilike( "identifier", "%" + identifier + "%" ) ).list();