← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12633: PatientStore, reimplemented method to use hibernate query instead of fetching identifiers instead...

 

------------------------------------------------------------
revno: 12633
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-10-14 14:24:56 +0200
message:
  PatientStore, reimplemented method to use hibernate query instead of fetching identifiers instead of one object at the time
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.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/PatientStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java	2013-10-14 11:13:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java	2013-10-14 12:24:56 +0000
@@ -44,7 +44,8 @@
 public interface PatientStore
     extends GenericIdentifiableObjectStore<Patient>
 {
-    String ID = PatientStore.class.getName();
+    final String ID = PatientStore.class.getName();
+    final int MAX_RESULTS = 50000;
 
     Collection<Patient> getByBirthDate( Date birthDate );
 
@@ -71,7 +72,7 @@
 
     Collection<Patient> getByPhoneNumber( String phoneNumber, Integer min, Integer max );
 
-    Collection<Patient> getByFullName( String fullName, OrganisationUnit organisationUnit );
+    Collection<Patient> getByFullName( String name, OrganisationUnit organisationUnit );
 
     Collection<Integer> getRegistrationOrgunitIds( Date startDate, Date endDate );
 

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-10-14 11:20:58 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-10-14 12:24:56 +0000
@@ -795,36 +795,17 @@
     }
 
     @Override
-    //TODO this method must be changed - cannot retrieve one by one
-    public Collection<Patient> getByFullName( String fullName, OrganisationUnit organisationUnit )
+    @SuppressWarnings("unchecked")
+    public Collection<Patient> getByFullName( String name, OrganisationUnit organisationUnit )
     {
-        List<Patient> patients = new ArrayList<Patient>();
-
-        fullName = fullName.toLowerCase();
-        String sql = "SELECT patientid FROM patient where lower(name) = '" + fullName + "'";
+        Criteria criteria = getCriteria( Restrictions.eq( "name", name ).ignoreCase() );
         
         if ( organisationUnit != null )
         {
-            sql += " and organisationunitid=" + organisationUnit.getId();
-        }
-
-        try
-        {
-            patients = jdbcTemplate.query( sql, new RowMapper<Patient>()
-            {
-                public Patient mapRow( ResultSet rs, int rowNum )
-                    throws SQLException
-                {
-                    return get( rs.getInt( 1 ) );
-                }
-            } );
-        }
-        catch ( Exception ex )
-        {
-            ex.printStackTrace();
-        }
-
-        return patients;
+            criteria.add( Restrictions.eq( "organisationUnit", organisationUnit ) );
+        }
+        
+        return criteria.setMaxResults( MAX_RESULTS ).list();
     }
 
     @SuppressWarnings( "unchecked" )

=== 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-09-16 17:07:25 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java	2013-10-14 12:24:56 +0000
@@ -33,6 +33,8 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Collection;
+
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -50,6 +52,8 @@
     
     private Patient patientA;
     private Patient patientB;
+    private Patient patientC;
+    private Patient patientD;
     
     private OrganisationUnit organisationUnit;
 
@@ -64,7 +68,8 @@
         
         patientA = createPatient( 'A', organisationUnit );
         patientB = createPatient( 'B', organisationUnit );
-        
+        patientC = createPatient( 'A', null );
+        patientD = createPatient( 'B', organisationUnit );
     }
     
     @Test
@@ -115,4 +120,24 @@
         
         assertTrue( equals( patientStore.getAll(), patientA, patientB ) );
     }
+    
+    @Test
+    public void testGetByFullName()
+    {
+        patientStore.save( patientA );
+        patientStore.save( patientB );
+        patientStore.save( patientC );
+        patientStore.save( patientD );
+        
+        Collection<Patient> patients = patientStore.getByFullName( "NameA", organisationUnit );
+        
+        assertEquals( 1, patients.size() );
+        assertTrue( patients.contains( patientA ) );
+        
+        patients = patientStore.getByFullName( "NameB", organisationUnit );
+        
+        assertEquals( 2, patients.size() );
+        assertTrue( patients.contains( patientB ) );
+        assertTrue( patients.contains( patientD ) );
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2013-10-08 19:10:40 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2013-10-14 12:24:56 +0000
@@ -986,7 +986,7 @@
         Patient patient = new Patient();
         patient.setAutoFields();
 
-        patient.setName( "FirstName" + uniqueChar );
+        patient.setName( "Name" + uniqueChar );
         patient.setGender( Patient.MALE );
         patient.setDobType( Patient.DOB_TYPE_VERIFIED );
         patient.setBirthDate( getDate( 1970, 1, 1 ) );
@@ -1001,7 +1001,7 @@
         Patient patient = new Patient();
         patient.setAutoFields();
 
-        patient.setName( "FirstName" + uniqueChar );
+        patient.setName( "Name" + uniqueChar );
         patient.setGender( gender );
         patient.setDobType( Patient.DOB_TYPE_VERIFIED );
         patient.setBirthDate( getDate( 1970, 1, 1 ) );