← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14229: WIP trackedentityinstance search

 

------------------------------------------------------------
revno: 14229
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-17 00:07:04 +0100
message:
  WIP trackedentityinstance search
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttribute.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstance.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.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/trackedentity/TrackedEntityAttribute.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttribute.java	2014-02-07 20:25:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttribute.java	2014-03-16 23:07:04 +0000
@@ -59,27 +59,16 @@
     private static final long serialVersionUID = 3026922158464592390L;
 
     public static final String TYPE_DATE = "date";
-
     public static final String TYPE_STRING = "string";
-
     public static final String TYPE_INT = "number";
-
     public static final String VALUE_TYPE_LETTER = "letter";
-
     public static final String TYPE_BOOL = "bool";
-
     public static final String TYPE_TRUE_ONLY = "trueOnly";
-
     public static final String TYPE_COMBO = "combo";
-
     public static final String TYPE_PHONE_NUMBER = "phoneNumber";
-
     public static final String TYPE_TRACKER_ASSOCIATE = "trackerAssociate";
-
     public static final String TYPE_USERS = "users";
-
     public static final String TYPE_AGE = "age";
-
     public static final String VALUE_TYPE_LOCAL_ID = "localId";
 
     private String description;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstance.java	2014-02-17 15:00:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstance.java	2014-03-16 23:07:04 +0000
@@ -53,21 +53,11 @@
 public class TrackedEntityInstance
     extends BaseIdentifiableObject
 {
-    /**
-     * Determines if a de-serialized file is compatible with this class.
-     */
-    private static final long serialVersionUID = 884114994005945275L;
-
     public static String SEARCH_SAPERATE = "_";
-
     public static String PREFIX_TRACKED_ENTITY_ATTRIBUTE = "attr";
-
     public static String PREFIX_PROGRAM = "prg";
-
     public static String PREFIX_PROGRAM_INSTANCE = "pi";
-
     public static String PREFIX_PROGRAM_EVENT_BY_STATUS = "stat";
-
     public static String PREFIX_PROGRAM_STAGE = "prgst";
 
     private Set<TrackedEntityAttributeValue> attributeValues = new HashSet<TrackedEntityAttributeValue>();
@@ -89,6 +79,22 @@
     }
 
     // -------------------------------------------------------------------------
+    // Logic
+    // -------------------------------------------------------------------------
+
+    public void addAttributeValue( TrackedEntityAttributeValue attributeValue )
+    {
+        attributeValues.add( attributeValue );
+        attributeValue.setEntityInstance( this );
+    }
+
+    public void removeAttributeValue( TrackedEntityAttributeValue attributeValue )
+    {
+        attributeValues.remove( attributeValue );
+        attributeValue.setEntityInstance( null );
+    }
+    
+    // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------
 
@@ -140,22 +146,6 @@
         this.representative = representative;
     }
 
-    // -------------------------------------------------------------------------
-    // Convenience method
-    // -------------------------------------------------------------------------
-
-    public void addAttributeValue( TrackedEntityAttributeValue attributeValue )
-    {
-        attributeValues.add( attributeValue );
-        attributeValue.setEntityInstance( this );
-    }
-
-    public void removeAttributeValue( TrackedEntityAttributeValue attributeValue )
-    {
-        attributeValues.remove( attributeValue );
-        attributeValue.setEntityInstance( null );
-    }
-    
     @JsonProperty
     @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlElementWrapper( localName = "trackedEntity", namespace = DxfNamespaces.DXF_2_0 )

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-16 23:07:04 +0000
@@ -0,0 +1,152 @@
+package org.hisp.dhis.trackedentity;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.program.Program;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class TrackedEntityInstanceQueryParams
+{
+    public static final String TRACKED_ENTITY_INSTANCE_ID = "instance";
+    public static final String CREATED_ID = "created";
+    public static final String LAST_UPDATED_ID = "lastupdated";
+    public static final String ORG_UNIT_ID = "ou";
+    public static final String TRACKED_ENTITY_ID = "te";
+    
+    private List<QueryItem> items = new ArrayList<QueryItem>();
+
+    private Program program;
+    
+    private TrackedEntity trackedEntity;
+    
+    private List<String> organisationUnits = new ArrayList<String>();
+    
+    private String organisationUnitMode;
+
+    private Integer page;
+    
+    private Integer pageSize;
+
+    // -------------------------------------------------------------------------
+    // Constructors
+    // -------------------------------------------------------------------------
+
+    public TrackedEntityInstanceQueryParams()
+    {
+    }
+
+    // -------------------------------------------------------------------------
+    // Logic
+    // -------------------------------------------------------------------------
+
+    public boolean hasProgram()
+    {
+        return program != null;
+    }
+    
+    // -------------------------------------------------------------------------
+    // Getters and setters
+    // -------------------------------------------------------------------------
+
+    public List<QueryItem> getItems()
+    {
+        return items;
+    }
+
+    public void setItems( List<QueryItem> items )
+    {
+        this.items = items;
+    }
+
+    public Program getProgram()
+    {
+        return program;
+    }
+
+    public void setProgram( Program program )
+    {
+        this.program = program;
+    }
+
+    public TrackedEntity getTrackedEntity()
+    {
+        return trackedEntity;
+    }
+
+    public void setTrackedEntity( TrackedEntity trackedEntity )
+    {
+        this.trackedEntity = trackedEntity;
+    }
+
+    public List<String> getOrganisationUnits()
+    {
+        return organisationUnits;
+    }
+
+    public void setOrganisationUnits( List<String> organisationUnits )
+    {
+        this.organisationUnits = organisationUnits;
+    }
+
+    public String getOrganisationUnitMode()
+    {
+        return organisationUnitMode;
+    }
+
+    public void setOrganisationUnitMode( String organisationUnitMode )
+    {
+        this.organisationUnitMode = organisationUnitMode;
+    }
+
+    public Integer getPage()
+    {
+        return page;
+    }
+
+    public void setPage( Integer page )
+    {
+        this.page = page;
+    }
+
+    public Integer getPageSize()
+    {
+        return pageSize;
+    }
+
+    public void setPageSize( Integer pageSize )
+    {
+        this.pageSize = pageSize;
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java	2014-02-26 15:09:48 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java	2014-03-16 23:07:04 +0000
@@ -49,6 +49,8 @@
 
     final int MAX_RESULTS = 50000;
 
+    Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
+    
     /**
      * Search entityInstances who registered in a certain organisation unit
      * 

=== 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-02-26 15:09:48 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-16 23:07:04 +0000
@@ -597,5 +597,4 @@
     {
         return entityInstanceStore.getByAttributeValue( searchText, attributeId, min, max );
     }
-
 }

=== 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-14 10:15:34 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-03-16 23:07:04 +0000
@@ -28,19 +28,26 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.trackedentity.TrackedEntityInstance.PREFIX_TRACKED_ENTITY_ATTRIBUTE;
 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;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstance.PREFIX_PROGRAM_STAGE;
+import static org.hisp.dhis.trackedentity.TrackedEntityInstance.PREFIX_TRACKED_ENTITY_ATTRIBUTE;
+import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.CREATED_ID;
+import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.LAST_UPDATED_ID;
+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 java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -51,6 +58,7 @@
 import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.i18n.I18nFormat;
@@ -61,10 +69,14 @@
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.system.grid.GridUtils;
+import org.hisp.dhis.system.grid.ListGrid;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.system.util.MapMap;
 import org.hisp.dhis.system.util.SqlHelper;
 import org.hisp.dhis.system.util.TextUtils;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 import org.hisp.dhis.trackedentity.TrackedEntityInstance;
-import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.hisp.dhis.trackedentity.TrackedEntityInstanceService;
 import org.hisp.dhis.trackedentity.TrackedEntityInstanceStore;
 import org.hisp.dhis.trackedentity.TrackedEntityQueryParams;
@@ -98,7 +110,76 @@
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
+    
+    @Override
+    public Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
+    {
+        Grid grid = new ListGrid();
+        
+        grid.addHeader( new GridHeader( TRACKED_ENTITY_INSTANCE_ID, "Instance" ) );
+        grid.addHeader( new GridHeader( CREATED_ID, "Created" ) );
+        grid.addHeader( new GridHeader( LAST_UPDATED_ID, "Last updated" ) );
+        grid.addHeader( new GridHeader( TRACKED_ENTITY_ID, "Tracked entity" ) );
+        grid.addHeader( new GridHeader( ORG_UNIT_ID, "Org unit" ) );
+        
+        for ( QueryItem item : params.getItems() )
+        {
+            grid.addHeader( new GridHeader( item.getItem().getUid(), item.getItem().getName() ) );
+        }
+        
+        Collection<Map<String, String>> entities = getEntities( params );
+        
+        for ( Map<String, String> entity : entities )
+        {
+            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 ) );
+        }
+        
+        return grid;
+    }
+    
+    private Collection<Map<String, String>> getEntities( TrackedEntityInstanceQueryParams params )
+    {
+        String sql = 
+            "select tei.uid, tei.created, tei.lastupdated, te.uid, ou.uid, tav.value, ta.uid " +
+            "from trackedentityinstance tei " +
+            "left join trackedentity te on tei.trackedentityid=te.trackedentityid " +
+            "left join organisationunit ou on tei.organisationunitid=ou.organisationunitid " +
+            "left join trackedentityattributevalue tav on tei.trackedentityinstanceid=tav.trackedentityinstanceid " +
+            "left join trackedentityattribute ta on tav.trackedentityattributeid=ta.trackedentityattributeid";
+        
+        MapMap<String, String, String> entityMap = new MapMap<String, String, String>();
 
+        SqlRowSet rowSet = jdbcTemplate.queryForRowSet( sql );
+        
+        while ( rowSet.next() )
+        {
+            String key = rowSet.getString( "tei.uid" );
+            String att = rowSet.getString( "ta.uid" );
+            String val = rowSet.getString( "tav.value" );
+            
+            Map<String, String> entity = entityMap.get( key );
+            
+            if ( entity == null )
+            {
+                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" ) );
+                entityMap.putEntries( key, map );
+            }
+            
+            entityMap.putEntry( key, att, val );            
+        }
+        
+        return entityMap.values();
+    }
+    
     @Override
     @SuppressWarnings( "unchecked" )
     public Collection<TrackedEntityInstance> getByOrgUnit( OrganisationUnit organisationUnit, Integer min, Integer max )

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MapMap.java	2014-03-16 23:07:04 +0000
@@ -45,4 +45,12 @@
         this.put( key, map );
         return null;
     }
+    
+    public void putEntries( T key, Map<U, V> m )
+    {
+        Map<U, V> map = this.get( key );
+        map = map == null ? new HashMap<U, V>() : map;
+        map.putAll( m );
+        this.put( key, map );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java	2014-03-14 10:25:42 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java	2014-03-16 23:07:04 +0000
@@ -28,29 +28,22 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import org.apache.commons.lang3.StringUtils;
+import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.api.controller.WebMetaData;
-import org.hisp.dhis.dxf2.filter.FilterOps;
-import org.hisp.dhis.dxf2.filter.Filters;
-import org.hisp.dhis.dxf2.filter.ops.Op;
 import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.system.util.ReflectionUtils;
 import org.hisp.dhis.user.UserCredentials;
 
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
-
 /**
  * TODO too many inner classes, need to be split up
  *