← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12181: Removed Patient.programs in query of HiberantePatientStore.

 

------------------------------------------------------------
revno: 12181
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-09-20 10:13:45 +0700
message:
  Removed Patient.programs in query of HiberantePatientStore.
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


--
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-09-18 03:49:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java	2013-09-20 03:13:45 +0000
@@ -91,9 +91,9 @@
         Collection<PatientAttribute> patientAttributes, Collection<PatientIdentifierType> identifierTypes, Integer min,
         Integer max );
 
-    Collection<Patient> getByOrgUnitProgramGender( OrganisationUnit organisationUnit, Program program, String gender, int min, int max );
-
-    Collection<Patient> getByProgram( Program program, int min, int max );
-
-    Collection<Patient> getByProgram( Program program, String gender, int min, int max );
+    Collection<Patient> getByOrgUnitProgramGender( OrganisationUnit organisationUnit, Program program, String gender, Integer min, Integer max );
+
+    Collection<Patient> getByProgram( Program program, Integer min, Integer max );
+
+    Collection<Patient> getByProgram( Program program, String gender, Integer min, Integer max );
 }

=== 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-09-19 05:24:04 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-09-20 03:13:45 +0000
@@ -118,8 +118,7 @@
         List<Patient> patients = new ArrayList<Patient>();
 
         fullName = fullName.toLowerCase();
-        String sql = "SELECT patientid FROM patient " + "where lower( " + statementBuilder.getPatientFullName() + ") "
-            + "like '%" + fullName + "%'";
+        String sql = "SELECT patientid FROM patient where lower( name ) " + "like '%" + fullName + "%'";
 
         if ( min != null && max != null )
         {
@@ -198,83 +197,143 @@
     }
 
     @Override
-    @SuppressWarnings( "unchecked" )
     public Collection<Patient> getByOrgUnitProgram( OrganisationUnit organisationUnit, Program program, Integer min,
         Integer max )
     {
-        Criteria criteria;
-        if ( organisationUnit != null )
-        {
-            criteria = getCriteria( Restrictions.eq( "organisationUnit", organisationUnit ) ).createAlias( "programs",
-                "program" ).add( Restrictions.eq( "program.id", program.getId() ) );
-        }
-        else
-        {
-            criteria = getCriteria().createAlias( "programs", "program" ).add(
-                Restrictions.eq( "program.id", program.getId() ) );
-        }
-        criteria.addOrder( Order.desc( "id" ) );
+        List<Patient> patients = new ArrayList<Patient>();
+
+        String sql = "select p.patientid from patient p join programinstance pi on p.patientid=pi.patientid "
+            + "where p.organisationunitid=" + organisationUnit.getId() + " and pi.programid=" + program.getId()
+            + " and pi.status=" + ProgramInstance.STATUS_ACTIVE;
 
         if ( min != null && max != null )
         {
-            criteria.setFirstResult( min ).setMaxResults( max );
-        }
-        return criteria.list();
+            sql += statementBuilder.limitRecord( min, max );
+        }
+
+        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;
     }
 
     @Override
-    @SuppressWarnings( "unchecked" )
     public Collection<Patient> getByOrgUnitProgramGender( OrganisationUnit organisationUnit, Program program,
-        String gender, int min, int max )
-    {
-        String hql = "select p from Patient p where p.organisationUnit = :organisationUnit and p.gender = :gender "
-            + " and :program member of p.programs" + " order by p.id DESC";
-
-        Query query = getQuery( hql );
-        query.setEntity( "organisationUnit", organisationUnit );
-        query.setEntity( "program", program );
-        query.setString( "gender", gender );
-
-        query.setFirstResult( min ).setMaxResults( max );
-
-        return query.list();
-    }
-
-    @Override
-    @SuppressWarnings( "unchecked" )
-    public Collection<Patient> getByProgram( Program program, int min, int max )
-    {
-        String hql = "select p from Patient p where :program member of p.programs order by p.id DESC";
-
-        Query query = getQuery( hql );
-        query.setEntity( "program", program );
-
-        query.setFirstResult( min ).setMaxResults( max );
-
-        return query.list();
-    }
-
-    @Override
-    @SuppressWarnings( "unchecked" )
-    public Collection<Patient> getByProgram( Program program, String gender, int min, int max )
-    {
-        String hql = "select p from Patient p where p.gender = :gender and :program member of p.programs order by p.id DESC";
-
-        Query query = getQuery( hql );
-        query.setString( "gender", gender );
-        query.setEntity( "program", program );
-
-        query.setFirstResult( min ).setMaxResults( max );
-
-        return query.list();
+        String gender, Integer min, Integer max )
+    {
+        List<Patient> patients = new ArrayList<Patient>();
+
+        String sql = "select p.patientid from patient p join programinstance pi on p.patientid=pi.patientid "
+            + "where p.organisationunitid=" + organisationUnit.getId() + " and pi.programid=" + program.getId()
+            + " and pi.status=" + ProgramInstance.STATUS_ACTIVE + " and p.gender='" + gender + "'";
+
+        if ( min != null && max != null )
+        {
+            sql += statementBuilder.limitRecord( min, max );
+        }
+
+        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;
+    }
+
+    @Override
+    public Collection<Patient> getByProgram( Program program, Integer min, Integer max )
+    {
+        List<Patient> patients = new ArrayList<Patient>();
+
+        String sql = "select p.patientid from patient p join programinstance pi on p.patientid=pi.patientid "
+            + "where pi.programid=" + program.getId() + " and pi.status=" + ProgramInstance.STATUS_ACTIVE;
+
+        if ( min != null && max != null )
+        {
+            sql += statementBuilder.limitRecord( min, max );
+        }
+
+        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;
+    }
+
+    @Override
+    public Collection<Patient> getByProgram( Program program, String gender, Integer min, Integer max )
+    {
+        List<Patient> patients = new ArrayList<Patient>();
+
+        String sql = "select p.patientid from patient p join programinstance pi on p.patientid=pi.patientid "
+            + "where pi.programid=" + program.getId() + " and pi.status=" + ProgramInstance.STATUS_ACTIVE
+            + " and p.gender='" + gender + "'";
+        
+        if ( min != null && max != null )
+        {
+            sql += statementBuilder.limitRecord( min, max );
+        }
+
+        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;
     }
 
     @Override
     public int countGetPatientsByName( String fullName )
     {
         fullName = fullName.toLowerCase();
-        String sql = "SELECT count(*) FROM patient where lower( " + statementBuilder.getPatientFullName() + ") "
-            + "like '%" + fullName + "%' ";
+        String sql = "SELECT count(*) FROM patient where lower( name ) " + "like '%" + fullName + "%' ";
 
         return jdbcTemplate.queryForObject( sql, Integer.class );
     }
@@ -292,13 +351,14 @@
     }
 
     @Override
+    @SuppressWarnings( "deprecation" )
     public int countGetPatientsByOrgUnitProgram( OrganisationUnit organisationUnit, Program program )
     {
-        Number rs = (Number) getCriteria( Restrictions.eq( "organisationUnit", organisationUnit ) )
-            .createAlias( "programs", "program" ).add( Restrictions.eq( "program.id", program.getId() ) )
-            .setProjection( Projections.rowCount() ).uniqueResult();
-
-        return rs != null ? rs.intValue() : 0;
+        String sql = "select count(p.patientid) from patient p join programinstance pi on p.patientid=pi.patientid "
+            + "where p.organisationunitid=" + organisationUnit.getId() + " and pi.programid=" + program.getId()
+            + " and pi.status=" + ProgramInstance.STATUS_ACTIVE;
+        
+        return jdbcTemplate.queryForInt( sql );
     }
 
     @Override
@@ -502,11 +562,11 @@
                 String opt = "";
                 for ( String v : keyValues )
                 {
-                    patientWhere += opt + " lower( p.name ) like '%" + v
-                        + "%' or lower(pi.identifier) like '%" + v + "%' ";
+                    patientWhere += opt + " lower( p.name ) like '%" + v + "%' or lower(pi.identifier) like '%" + v
+                        + "%' ";
                     opt = "or";
                 }
-                
+
                 if ( keyValues.length == 2 )
                 {
                     String otherId = keyValues[0] + "  " + keyValues[1];
@@ -520,17 +580,18 @@
             {
                 sql += "(select value from patientattributevalue where patientid=p.patientid and patientattributeid="
                     + id + " ) as " + Patient.PREFIX_PATIENT_ATTRIBUTE + "_" + id + ",";
-                
+
                 String[] keyValues = value.split( " " );
                 otherWhere += operator + "(";
                 String opt = "";
                 for ( String v : keyValues )
                 {
-                    otherWhere += opt + " lower(" + Patient.PREFIX_PATIENT_ATTRIBUTE + "_" + id + ") like '%" + v + "%'";
+                    otherWhere += opt + " lower(" + Patient.PREFIX_PATIENT_ATTRIBUTE + "_" + id + ") like '%" + v
+                        + "%'";
                     opt = "or";
                 }
                 otherWhere += ")";
-               
+
                 operator = " and ";
             }
             else if ( keys[0].equals( Patient.PREFIX_PROGRAM ) )