← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14256: Trackedentityinstance queyr, WIP

 

------------------------------------------------------------
revno: 14256
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-17 15:00:38 +0100
message:
  Trackedentityinstance queyr, WIP
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.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/common/IdentifiableObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2014-01-07 19:16:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2014-03-17 14:00:38 +0000
@@ -97,6 +97,27 @@
     }
 
     /**
+     * Returns a list of internal identifiers for the given collection of IdentifiableObjects.
+     *
+     * @param objects the list of IdentifiableObjects.
+     * @return a list of uids.
+     */
+    public static <T extends IdentifiableObject> List<Integer> getIdentifiers( Collection<T> objects )
+    {
+        List<Integer> uids = new ArrayList<Integer>();
+
+        if ( objects != null )
+        {
+            for ( T object : objects )
+            {
+                uids.add( object.getId() );
+            }
+        }
+
+        return uids;
+    }
+
+    /**
      * Filters the given list of IdentifiableObjects based on the given key.
      *
      * @param identifiableObjects the list of IdentifiableObjects.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-17 14:00:38 +0000
@@ -47,6 +47,8 @@
     public static final String LAST_UPDATED_ID = "lastupdated";
     public static final String ORG_UNIT_ID = "ou";
     public static final String TRACKED_ENTITY_ID = "te";
+    public static final String TRACKED_ENTITY_ATTRIBUTE_ID = "teattribute";
+    public static final String TRACKED_ENTITY_ATTRIBUTE_VALUE_ID = "tevalue";
     
     private List<QueryItem> items = new ArrayList<QueryItem>();
 
@@ -74,16 +76,25 @@
     // Logic
     // -------------------------------------------------------------------------
 
+    /**
+     * Indicates whether this params specifies a program.
+     */
     public boolean hasProgram()
     {
         return program != null;
     }
     
+    /**
+     * Indicates whether this params specifies a tracked entity.
+     */
     public boolean hasTrackedEntity()
     {
         return trackedEntity != null;
     }
     
+    /**
+     * Indicates whethert this params is of the given organisation unit mode.
+     */
     public boolean isOrganisationUnitMode( String mode )
     {
         return organisationUnitMode != null && organisationUnitMode.equals( mode );

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 13:36:52 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 14:00:38 +0000
@@ -41,6 +41,7 @@
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IllegalQueryException;
+import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -123,7 +124,7 @@
     {
         this.organisationUnitService = organisationUnitService;
     }
-
+        
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
@@ -140,8 +141,15 @@
     {
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
 
+        for ( String item : items )
+        {
+            QueryItem it = getQueryItem( item );
+            
+            params.getItems().add( it );
+        }
+        
         Program pr = program != null ? programService.getProgram( program ) : null;
-
+        
         if ( program != null && pr == null )
         {
             throw new IllegalQueryException( "Program does not exist: " + program );
@@ -149,13 +157,11 @@
         
         TrackedEntity te = trackedEntity != null ? trackedEntityService.getTrackedEntity( trackedEntity ) : null;
         
-        if ( te == null )
+        if ( trackedEntity != null && te == null )
         {
-            throw new IllegalQueryException( "Program does not exist: " + program );
+            throw new IllegalQueryException( "Tracked entity does not exist: " + program );
         }
         
-        Set<OrganisationUnit> ous = new HashSet<OrganisationUnit>();
-        
         for ( String orgUnit : ou )
         {
             OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( orgUnit );
@@ -165,12 +171,7 @@
                 throw new IllegalQueryException( "Organisation unit does not exist: " + orgUnit );
             }
             
-            ous.add( organisationUnit );
-        }
-        
-        for ( String item : items )
-        {
-            String id = DimensionalObjectUtils.getDimensionFromParam( item );
+            params.getOrganisationUnits().add( organisationUnit );
         }
         
         params.setOrganisationUnitMode( ouMode );
@@ -179,6 +180,37 @@
         
         return params;
     }
+
+    private QueryItem getQueryItem( String item )
+    {
+        if ( !item.contains( DimensionalObjectUtils.DIMENSION_NAME_SEP ) )
+        {
+            return getItem( item, null, null );
+        }
+        else // Filter
+        {
+            String[] split = item.split( DimensionalObjectUtils.DIMENSION_NAME_SEP );
+
+            if ( split == null || split.length != 3 )
+            {
+                throw new IllegalQueryException( "Item filter has invalid format: " + item );
+            }
+
+            return getItem( split[0], split[1], split[2] );
+        }
+    }
+
+    private QueryItem getItem( String item, String operator, String filter )
+    {
+        TrackedEntityAttribute at = attributeService.getTrackedEntityAttribute( item );
+
+        if ( at == null )
+        {
+            throw new IllegalQueryException( "Attribute does not exist: " + item );
+        }
+        
+        return new QueryItem( at, operator, filter, at.isNumericType() );
+    }
     
     @Override
     public int addTrackedEntityInstance( TrackedEntityInstance instance )

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-03-17 14:00:38 +0000
@@ -28,8 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
-import static org.hisp.dhis.system.util.TextUtils.getQuotedCommaDelimitedString;
+import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers;
+import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstance.PREFIX_PROGRAM;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstance.PREFIX_PROGRAM_EVENT_BY_STATUS;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstance.PREFIX_PROGRAM_INSTANCE;
@@ -40,6 +40,8 @@
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.ORG_UNIT_ID;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_ID;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_INSTANCE_ID;
+import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_ATTRIBUTE_ID;
+import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_ATTRIBUTE_VALUE_ID;
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -142,10 +144,17 @@
         
         for ( Map<String, String> entity : entities )
         {
+            grid.addRow();
             grid.addValue( entity.get( TRACKED_ENTITY_INSTANCE_ID ) );
+            grid.addValue( entity.get( CREATED_ID ) );
             grid.addValue( entity.get( LAST_UPDATED_ID ) );
             grid.addValue( entity.get( TRACKED_ENTITY_ID ) );
             grid.addValue( entity.get( ORG_UNIT_ID ) );
+            
+            for ( QueryItem item : params.getItems() )
+            {
+                grid.addValue( entity.get( item.getItemId() ) );
+            }
         }
         
         return grid;
@@ -156,7 +165,13 @@
         SqlHelper hlp = new SqlHelper();
         
         String sql = 
-            "select tei.uid, tei.created, tei.lastupdated, te.uid, ou.uid, tav.value, ta.uid " +
+            "select tei.uid as " + TRACKED_ENTITY_INSTANCE_ID + ", " +
+            "tei.created as " + CREATED_ID + ", " +
+            "tei.lastupdated as " + LAST_UPDATED_ID + ", " +
+            "te.uid as " + TRACKED_ENTITY_ID + ", " +
+            "ou.uid as " + ORG_UNIT_ID + ", " +
+            "ta.uid as " + TRACKED_ENTITY_ATTRIBUTE_ID + ", " +
+            "tav.value as " + TRACKED_ENTITY_ATTRIBUTE_VALUE_ID + " " +
             "from trackedentityinstance tei " +
             "left join trackedentity te on tei.trackedentityid = te.trackedentityid " +
             "left join organisationunit ou on tei.organisationunitid = ou.organisationunitid " +
@@ -175,22 +190,16 @@
         
         if ( params.isOrganisationUnitMode( DimensionalObject.OU_MODE_SELECTED ) )
         {
-            sql += hlp.whereAnd() + " tei.organisationunitid in (" + getQuotedCommaDelimitedString( getUids( params.getOrganisationUnits() ) ) + ") ";
+            sql += hlp.whereAnd() + " tei.organisationunitid in (" + getCommaDelimitedString( getIdentifiers( params.getOrganisationUnits() ) ) + ") ";
         }
         
         for ( QueryItem item : params.getItems() )
-        {
-            String filter = statementBuilder.encode( item.getFilter(), false );
-            
-            String valClause = "tav.value " + item.getSqlOperator() + " " + item.getSqlFilter( filter );
-            
+        {   
             if ( item.hasFilter() )
             {
-                sql += hlp.whereAnd() + " (ta.uid = " + item.getItemId() + " and " + valClause + ") ";
-            }
-            else
-            {
-                sql += hlp.whereAnd() + " (" + valClause + ") ";
+                String filter = statementBuilder.encode( item.getFilter(), false );
+                
+                sql += hlp.whereAnd() + " (ta.uid = '" + item.getItemId() + "' and " + "tav.value " + item.getSqlOperator() + " " + item.getSqlFilter( filter ) + ") ";
             }
         }
         
@@ -202,9 +211,9 @@
         
         while ( rowSet.next() )
         {
-            String key = rowSet.getString( "tei.uid" );
-            String att = rowSet.getString( "ta.uid" );
-            String val = rowSet.getString( "tav.value" );
+            String key = rowSet.getString( TRACKED_ENTITY_INSTANCE_ID );
+            String att = rowSet.getString( TRACKED_ENTITY_ATTRIBUTE_ID );
+            String val = rowSet.getString( TRACKED_ENTITY_ATTRIBUTE_VALUE_ID );
             
             Map<String, String> entity = entityMap.get( key );
             
@@ -212,10 +221,10 @@
             {
                 Map<String, String> map = new HashMap<String, String>();
                 map.put( TRACKED_ENTITY_INSTANCE_ID, key );
-                map.put( CREATED_ID, DateUtils.getLongDateString( rowSet.getDate( "teu.created" ) ) );
-                map.put( LAST_UPDATED_ID, DateUtils.getLongDateString( rowSet.getDate( "tei.lastupdated" ) ) );
-                map.put( TRACKED_ENTITY_ID, rowSet.getString( "te.uid" ) );
-                map.put( ORG_UNIT_ID, rowSet.getString( "ou.uid" ) );
+                map.put( CREATED_ID, DateUtils.getLongDateString( rowSet.getDate( CREATED_ID ) ) );
+                map.put( LAST_UPDATED_ID, DateUtils.getLongDateString( rowSet.getDate( LAST_UPDATED_ID ) ) );
+                map.put( TRACKED_ENTITY_ID, rowSet.getString( TRACKED_ENTITY_ID ) );
+                map.put( ORG_UNIT_ID, rowSet.getString( ORG_UNIT_ID ) );
                 entityMap.putEntries( key, map );
             }
             

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-17 13:28:21 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-17 14:00:38 +0000
@@ -92,7 +92,7 @@
     
     @RequestMapping( method = RequestMethod.GET, produces = { "application/json", "application/javascript" } )
     public String queryTrackedEntityInstances( // JSON, JSONP
-        @RequestParam Set<String> items,
+        @RequestParam Set<String> item,
         @RequestParam(required=false) String program,
         @RequestParam(required=false) String trackedEntity,
         @RequestParam String ou,
@@ -103,7 +103,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( items, program, trackedEntity, orgUnits, ouMode, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, program, trackedEntity, orgUnits, ouMode, page, pageSize );
         Grid grid = instanceService.getTrackedEntityInstances( params );
         
         model.addAttribute( "model", grid );