← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15270: Rewrite methods in tracker to use criteria instead of hql.

 

------------------------------------------------------------
revno: 15270
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-05-14 22:05:57 +0800
message:
  Rewrite methods in tracker to use criteria instead of hql.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageDataElement.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStore.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueStore.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/DefaultTrackedEntityDataValueService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/hibernate/HibernateTrackedEntityDataValueStore.java
  dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStoreTest.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/program/ProgramStageDataElement.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageDataElement.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageDataElement.java	2014-05-14 14:05:57 +0000
@@ -28,16 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.io.Serializable;
+
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.ExportView;
+import org.hisp.dhis.dataelement.DataElement;
+
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
-import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.view.DetailedView;
-import org.hisp.dhis.common.view.ExportView;
-import org.hisp.dhis.dataelement.DataElement;
-
-import java.io.Serializable;
 
 /**
  * @author Viet Nguyen

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueService.java	2014-05-14 14:05:57 +0000
@@ -73,7 +73,7 @@
      * 
      * @return Error code. If this code is 0, deleting succeed.
      */
-    int deleteTrackedEntityDataValue( ProgramStageInstance programStageInstance );
+    void deleteTrackedEntityDataValue( ProgramStageInstance programStageInstance );
 
     /**
      * Retrieve data values of a event

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStore.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStore.java	2014-05-14 14:05:57 +0000
@@ -60,7 +60,7 @@
      * 
      * @param programStageInstance ProgramStageInstance.
      */
-    int delete( ProgramStageInstance programStageInstance );
+    int detele( ProgramStageInstance programStageInstance );
 
     /**
      * Retrieve data values of a event

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2014-05-09 15:56:43 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2014-05-14 14:05:57 +0000
@@ -123,9 +123,12 @@
     @SuppressWarnings( "unchecked" )
     public List<ProgramStageInstance> get( TrackedEntityInstance entityInstance, Boolean completed )
     {
-        String hql = "from ProgramStageInstance where programInstance.entityInstance = :entityInstance and completed = :completed";
-
-        return getQuery( hql ).setEntity( "entityInstance", entityInstance ).setBoolean( "completed", completed ).list();
+        Criteria criteria = getCriteria();
+        criteria.createAlias( "programInstance", "programInstance" );
+        criteria.add( Restrictions.eq( "programInstance.entityInstance", entityInstance));
+        criteria.add(  Restrictions.eq( "completed", completed ));
+        
+        return criteria.list();
     }
 
     // TODO this class must be re-written from here

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueStore.java	2014-05-09 15:49:36 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueStore.java	2014-05-14 14:05:57 +0000
@@ -132,11 +132,8 @@
     @SuppressWarnings( "unchecked" )
     public Collection<TrackedEntityAttributeValue> get( TrackedEntityInstance entityInstance, Program program )
     {
-        String hql = "SELECT pav FROM TrackedEntityAttributeValue as pav WHERE pav.entityInstance=:entityInstance and pav.attribute.program=:program";
-        Query query = getQuery( hql );
-        query.setEntity( "entityInstance", entityInstance );
-        query.setEntity( "program", program );
-
-        return query.list();
+        return getCriteria(
+            Restrictions.and( Restrictions.eq( "entityInstance", entityInstance ),
+            Restrictions.eq( "attribute.program", program ) ) ).list();
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/DefaultTrackedEntityDataValueService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/DefaultTrackedEntityDataValueService.java	2014-05-09 15:49:36 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/DefaultTrackedEntityDataValueService.java	2014-05-14 14:05:57 +0000
@@ -74,9 +74,9 @@
         dataValueStore.delete( dataValue );
     }
 
-    public int deleteTrackedEntityDataValue( ProgramStageInstance programStageInstance )
+    public void deleteTrackedEntityDataValue( ProgramStageInstance programStageInstance )
     {
-        return dataValueStore.delete( programStageInstance );
+        dataValueStore.detele( programStageInstance );
     }
 
     public void updateTrackedEntityDataValue( TrackedEntityDataValue dataValue )

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/hibernate/HibernateTrackedEntityDataValueStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/hibernate/HibernateTrackedEntityDataValueStore.java	2014-05-09 15:49:36 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentitydatavalue/hibernate/HibernateTrackedEntityDataValueStore.java	2014-05-14 14:05:57 +0000
@@ -32,6 +32,7 @@
 import java.util.Collection;
 import java.util.Date;
 
+import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.dataelement.DataElement;
@@ -53,8 +54,8 @@
     {
         sessionFactory.getCurrentSession().save( dataValue );
     }
-
-    public int delete( ProgramStageInstance programStageInstance )
+   
+    public int detele( ProgramStageInstance programStageInstance )
     {
         Query query = getQuery( "delete from TrackedEntityDataValue where programStageInstance = :programStageInstance" );
         query.setEntity( "programStageInstance", programStageInstance );
@@ -76,11 +77,7 @@
             return new ArrayList<TrackedEntityDataValue>();
         }
         
-        String hql = "from TrackedEntityDataValue pdv where pdv.dataElement in ( :dataElements ) "
-            + "and pdv.programStageInstance = :programStageInstance";
-
-        return getQuery( hql ).setParameterList( "dataElements", dataElements )
-            .setEntity( "programStageInstance", programStageInstance ).list();
+        return getCriteria( Restrictions.in( "dataElement", dataElements ), Restrictions.eq( "programStageInstance", programStageInstance ) ).list();
     }
 
     @SuppressWarnings( "unchecked" )
@@ -103,18 +100,19 @@
     @SuppressWarnings( "unchecked" )
     public Collection<TrackedEntityDataValue> get( TrackedEntityInstance entityInstance, Collection<DataElement> dataElements, Date startDate,
         Date endDate )
-    {
+     {
         if ( dataElements == null || dataElements.isEmpty() )
         {
             return new ArrayList<TrackedEntityDataValue>();
         }
         
-        String hql = "from TrackedEntityDataValue pdv where pdv.dataElement in ( :dataElements ) "
-            + "and pdv.programStageInstance.programInstance.entityInstance = :entityInstance "
-            + "and pdv.programStageInstance.executionDate >= :startDate and pdv.programStageInstance.executionDate <= :endDate ";
-
-        return getQuery( hql ).setParameterList( "dataElements", dataElements ).setEntity( "entityInstance", entityInstance )
-            .setDate( "startDate", startDate ).setDate( "endDate", endDate ).list();
+        Criteria criteria = getCriteria();
+        criteria.createAlias( "programStageInstance", "programStageInstance" );
+        criteria.createAlias( "programStageInstance.programInstance", "programInstance" );
+        criteria.add( Restrictions.in( "dataElement", dataElements ) );
+        criteria.add( Restrictions.eq( "programInstance.entityInstance", entityInstance ) );
+        criteria.add( Restrictions.between( "programStageInstance.executionDate", startDate, endDate ) );
+        return criteria.list();
     }
 
     public TrackedEntityDataValue get( ProgramStageInstance programStageInstance, DataElement dataElement )
@@ -122,4 +120,5 @@
         return (TrackedEntityDataValue) getCriteria( Restrictions.eq( "programStageInstance", programStageInstance ),
             Restrictions.eq( "dataElement", dataElement ) ).uniqueResult();
     }
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStoreTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStoreTest.java	2014-04-25 11:14:08 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentitydatavalue/TrackedEntityDataValueStoreTest.java	2014-05-14 14:05:57 +0000
@@ -187,7 +187,7 @@
         assertNotNull( dataValueStore.get( stageInstanceB, dataElementA ) );
         assertNotNull( dataValueStore.get( stageInstanceB, dataElementB ) );
 
-        dataValueStore.delete( stageInstanceA );
+        dataValueStore.detele( stageInstanceA );
         assertNull( dataValueStore.get( stageInstanceA, dataElementA ) );
         assertNull( dataValueStore.get( stageInstanceA, dataElementB ) );
         assertNotNull( dataValueStore.get( stageInstanceB, dataElementA ) );