← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12959: Impl hashcode/equals for PatientAttributeValue and PatientIdentifier. Changed from List to Set fo...

 

------------------------------------------------------------
revno: 12959
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-11-18 12:50:20 +0100
message:
  Impl hashcode/equals for PatientAttributeValue and PatientIdentifier. Changed from List to Set for attribute values argument in createPatient method. Modified createPatient logic
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifier.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValue.java
  dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/AddPatientAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.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/PatientIdentifier.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifier.java	2013-11-07 05:44:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifier.java	2013-11-18 11:50:20 +0000
@@ -75,6 +75,76 @@
     // Getters and setters
     // -------------------------------------------------------------------------
 
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + ( ( identifierType == null) ? 0 : identifierType.hashCode() );
+        result = prime * result + ( ( patient == null) ? 0 : patient.hashCode() );
+        result = prime * result + ( ( identifier == null) ? 0 : identifier.hashCode() );
+        return result;
+    }
+
+    @Override
+    public boolean equals( Object object )
+    {
+        if ( this == object )
+        {
+            return true;
+        }
+        
+        if ( !super.equals( object ) )
+        {
+            return false;
+        }
+        
+        if ( getClass() != object.getClass() )
+        {
+            return false;
+        }
+        
+        final PatientIdentifier other = (PatientIdentifier) object;
+        
+        if ( identifierType == null )
+        {
+            if ( other.identifierType != null )
+            {
+                return false;
+            }
+        }
+        else if ( !identifierType.equals( other.identifierType ) )
+        {
+            return false;
+        }
+        
+        if ( patient == null )
+        {
+            if ( other.patient != null )
+            {
+                return false;
+            }
+        }
+        else if ( !patient.equals( other.patient ) )
+        {
+            return false;
+        }
+
+        if ( identifier == null )
+        {
+            if ( other.identifier != null )
+            {
+                return false;
+            }
+        }
+        else if ( !identifier.equals( other.identifier ) )
+        {
+            return false;
+        }
+        
+        return true;
+    }
+
     @JsonProperty
     @JsonSerialize( as = BaseIdentifiableObject.class )
     @JsonView( { DetailedView.class } )

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java	2013-11-05 08:07:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java	2013-11-18 11:50:20 +0000
@@ -38,6 +38,7 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
+import java.util.Set;
 
 /**
  * @author Abyot Asalefew Gizaw
@@ -247,12 +248,12 @@
      * @param patient Patient
      * @param representativeId The id of patient who is representative
      * @param relationshipTypeId The id of relationship type defined
-     * @param patientAttributeValues List of attribute values
+     * @param attributeValues Set of attribute values
      * 
      * @return The error code after registering patient
      */
     int createPatient( Patient patient, Integer representativeId, Integer relationshipTypeId,
-        List<PatientAttributeValue> patientAttributeValues );
+        Set<PatientAttributeValue> attributeValues );
 
     /**
      * Update information of an patient existed

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValue.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValue.java	2013-09-16 17:07:25 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValue.java	2013-11-18 11:50:20 +0000
@@ -81,6 +81,19 @@
     // hashCode and equals
     // -------------------------------------------------------------------------
 
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ( ( patient == null) ? 0 : patient.hashCode() );
+        result = prime * result + ( ( patientAttribute == null) ? 0 : patientAttribute.hashCode() );
+        result = prime * result + ( ( patientAttributeOption == null) ? 0 : patientAttributeOption.hashCode() );
+        result = prime * result + ( ( value == null) ? 0 : value.hashCode() );
+        return result;
+    }
+
     @Override
     public boolean equals( Object object )
     {
@@ -88,44 +101,77 @@
         {
             return true;
         }
-
+        
         if ( object == null )
         {
             return false;
         }
-
-        if ( getClass() != object.getClass() )
+        
+        if ( !getClass().isAssignableFrom( object.getClass() ) )
         {
             return false;
         }
-
+        
         final PatientAttributeValue other = (PatientAttributeValue) object;
-
-        return patientAttribute.equals( other.getPatientAttribute() ) && patient.equals( other.getPatient() );
-
-    }
-
-    @Override
-    public int hashCode()
-    {
-        final int prime = 31;
-        int result = 1;
-
-        result = result * prime + patientAttribute.hashCode();
-        result = result * prime + patient.hashCode();
-
-        return result;
-    }
-
+        
+        if ( patient == null )
+        {
+            if ( other.patient != null )
+            {
+                return false;
+            }
+        }
+        else if ( !patient.equals( other.patient ) )
+        {
+            return false;
+        }
+        
+        if ( patientAttribute == null )
+        {
+            if ( other.patientAttribute != null )
+            {
+                return false;
+            }
+        }
+        else if ( !patientAttribute.equals( other.patientAttribute ) )
+        {
+            return false;
+        }
+        
+        if ( patientAttributeOption == null )
+        {
+            if ( other.patientAttributeOption != null )
+            {
+                return false;
+            }
+        }
+        else if ( !patientAttributeOption.equals( other.patientAttributeOption ) )
+        {
+            return false;
+        }
+        
+        if ( value == null )
+        {
+            if ( other.value != null )
+            {
+                return false;
+            }
+        }
+        else if ( !value.equals( other.value ) )
+        {
+            return false;
+        }
+        
+        return true;
+    }
+    
     @Override
     public String toString()
     {
-        return "PatientAttributeValue{" +
-            "patientAttribute=" + patientAttribute +
+        return "[Patient attribute=" + patientAttribute +
             ", patient=" + patient +
-            ", value='" + value + '\'' +
-            ", patientAttributeOption=" + patientAttributeOption +
-            '}';
+            ", value='" + value + "'" +
+            ", attribute option=" + patientAttributeOption + "]";
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java	2013-11-12 08:07:20 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java	2013-11-18 11:50:20 +0000
@@ -1798,7 +1798,7 @@
 
         Set<org.hisp.dhis.patient.PatientIdentifier> patientIdentifierSet = new HashSet<org.hisp.dhis.patient.PatientIdentifier>();
         Set<org.hisp.dhis.patient.PatientAttribute> patientAttributeSet = new HashSet<org.hisp.dhis.patient.PatientAttribute>();
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+        Set<PatientAttributeValue> patientAttributeValues = new HashSet<PatientAttributeValue>();
 
         Collection<org.hisp.dhis.api.mobile.model.PatientIdentifier> identifiersMobile = patient.getIdentifiers();
 

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java	2013-11-18 10:26:25 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java	2013-11-18 11:50:20 +0000
@@ -125,17 +125,19 @@
 
     @Override
     public int createPatient( Patient patient, Integer representativeId, Integer relationshipTypeId,
-        List<PatientAttributeValue> patientAttributeValues )
+        Set<PatientAttributeValue> patientAttributeValues )
     {
-        int patientid = savePatient( patient );
+        int id = savePatient( patient );
 
         for ( PatientAttributeValue pav : patientAttributeValues )
         {
             patientAttributeValueService.savePatientAttributeValue( pav );
+            patient.getAttributeValues().add( pav );
         }
-        // -------------------------------------------------------------------------
-        // If underAge = true : save representative information.
-        // -------------------------------------------------------------------------
+        
+        // ---------------------------------------------------------------------
+        // If under age, save representative information
+        // ---------------------------------------------------------------------
 
         if ( patient.isUnderAge() )
         {
@@ -162,8 +164,10 @@
                 }
             }
         }
+        
+        updatePatient( patient ); // Save patient to update associations
 
-        return patientid;
+        return id;
     }
 
     @Override
@@ -327,6 +331,7 @@
         {
             return patientStore.getByNames( value, null, null );
         }
+        
         return null;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java	2013-11-18 10:26:25 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java	2013-11-18 11:50:20 +0000
@@ -38,6 +38,7 @@
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -105,8 +106,6 @@
 
     private int attributeId;
 
-    private int identifierTypeId;
-
     private Program programA;
 
     private Program programB;
@@ -125,7 +124,7 @@
         organisationUnitService.addOrganisationUnit( organisationUnitB );
 
         PatientIdentifierType patientIdentifierType = createPatientIdentifierType( 'A' );
-        identifierTypeId = identifierTypeService.savePatientIdentifierType( patientIdentifierType );
+        identifierTypeService.savePatientIdentifierType( patientIdentifierType );
 
         patientAttribute = createPatientAttribute( 'A' );
         attributeId = patientAttributeService.savePatientAttribute( patientAttribute );
@@ -322,7 +321,7 @@
     }
 
     @Test
-    public void testGetPatientsByAttributeAndIdentifier()
+    public void testGetPatientsByAttribute()
     {
         patientService.savePatient( patientA2 );
         patientService.savePatient( patientA3 );
@@ -330,7 +329,7 @@
         patientService.savePatient( patientB2 );
 
         PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+        Set<PatientAttributeValue> patientAttributeValues = new HashSet<PatientAttributeValue>();
         patientAttributeValues.add( attributeValue );
 
         patientService.createPatient( patientA3, null, null, patientAttributeValues );
@@ -340,10 +339,9 @@
         assertEquals( 1, patients.size() );
         assertTrue( patients.contains( patientA3 ) );
         
-        /*
-        patients = patientService.getPatient( identifierTypeId, null, "IdentifierA" );
-        assertEquals( 1, patients.size() );
-        assertTrue( patients.contains( patientA3 ) );*/
+        Patient patient = patients.iterator().next();
+        assertEquals( 1, patient.getAttributeValues().size() );
+        assertTrue( patient.getAttributeValues().contains( attributeValue ) );
     }
 
     @Test
@@ -420,7 +418,7 @@
 
         patientA1.setUnderAge( true );
         PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA1, patientAttribute );
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+        Set<PatientAttributeValue> patientAttributeValues = new HashSet<PatientAttributeValue>();
         patientAttributeValues.add( attributeValue );
 
         int idA = patientService.createPatient( patientA1, idB, relationshipTypeId, patientAttributeValues );
@@ -438,16 +436,16 @@
         patientA3.setUnderAge( true );
         patientA3.setName( "B" );
         PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+        Set<PatientAttributeValue> patientAttributeValues = new HashSet<PatientAttributeValue>();
         patientAttributeValues.add( attributeValue );
         int idA = patientService.createPatient( patientA3, idB, relationshipTypeId, patientAttributeValues );
         assertNotNull( patientService.getPatient( idA ) );
 
         attributeValue.setValue( "AttributeB" );
-        patientAttributeValues = new ArrayList<PatientAttributeValue>();
-        patientAttributeValues.add( attributeValue );
+        List<PatientAttributeValue> attributeValues = new ArrayList<PatientAttributeValue>(); //TODO use set
+        attributeValues.add( attributeValue );
 
-        patientService.updatePatient( patientA3, idB, relationshipTypeId, patientAttributeValues,  new ArrayList<PatientAttributeValue>(),  new ArrayList<PatientAttributeValue>() );
+        patientService.updatePatient( patientA3, idB, relationshipTypeId, attributeValues, new ArrayList<PatientAttributeValue>(), new ArrayList<PatientAttributeValue>() );
         assertEquals( "B", patientService.getPatient( idA ).getName() );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java	2013-11-07 05:44:33 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java	2013-11-18 11:50:20 +0000
@@ -373,5 +373,4 @@
         assertEquals( 0, validatePatientA1 );
         assertEquals( 2, validatePatientB1 );
     }
-
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/AddPatientAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/AddPatientAction.java	2013-10-24 08:31:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/AddPatientAction.java	2013-11-18 11:50:20 +0000
@@ -31,7 +31,9 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -295,7 +297,7 @@
 
         Collection<PatientAttribute> attributes = patientAttributeService.getAllPatientAttributes();
 
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+        Set<PatientAttributeValue> patientAttributeValues = new HashSet<PatientAttributeValue>();
 
         PatientAttributeValue attributeValue = null;
 

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java	2013-09-27 17:04:23 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java	2013-11-18 11:50:20 +0000
@@ -335,7 +335,7 @@
         Patient patient = new Patient();
         Set<PatientIdentifier> patientIdentifierSet = new HashSet<PatientIdentifier>();
         Set<PatientAttribute> patientAttributeSet = new HashSet<PatientAttribute>();
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+        Set<PatientAttributeValue> patientAttributeValues = new HashSet<PatientAttributeValue>();
 
         patientIdentifierTypes = patientIdentifierTypeService.getAllPatientIdentifierTypes();
         patientAttributes = patientAttributeService.getAllPatientAttributes();