← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13948: in web-api filtering, expose collections as lists of idObjects

 

------------------------------------------------------------
revno: 13948
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-02-06 13:37:09 +0700
message:
  in web-api filtering, expose collections as lists of idObjects
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-02-04 12:54:31 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-02-06 06:37:09 +0000
@@ -31,6 +31,7 @@
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.google.common.collect.Maps;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.system.util.functional.Function1;
 import org.hisp.dhis.system.util.functional.Predicate;
 import org.springframework.util.StringUtils;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2014-02-06 05:59:21 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2014-02-06 06:37:09 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Maps;
 import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.controller.exception.NotFoundForQueryException;
 import org.hisp.dhis.api.utils.WebUtils;
@@ -102,13 +103,16 @@
         postProcessEntities( entityList );
         postProcessEntities( entityList, options, parameters );
 
-        Map<String, Object> output = WebUtils.filterFields( entityList, fields );
+        List<Object> objects = WebUtils.filterFields( entityList, fields );
+        Map<String, Object> output = Maps.newLinkedHashMap();
 
         if ( options.hasPaging() )
         {
             output.put( "pager", metaData.getPager() );
         }
 
+        output.put( "objects", objects );
+
         JacksonUtils.toJson( response.getOutputStream(), output );
     }
 

=== 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-02-06 06:09:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java	2014-02-06 06:37:09 +0000
@@ -131,7 +131,7 @@
         generateLinks( object, true );
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public static void generateLinks( Object object, boolean deep )
     {
         if ( object == null )
@@ -185,15 +185,13 @@
         }
     }
 
-    public static <T extends IdentifiableObject> Map<String, Object> filterFields( List<T> entityList, String fields )
+    public static <T extends IdentifiableObject> List<Object> filterFields( List<T> entityList, String fields )
     {
-        Map<String, Object> output = Maps.newHashMap();
-        ArrayList<Object> objects = Lists.newArrayList();
-        output.put( "objects", objects );
+        List<Object> objects = Lists.newArrayList();
 
         if ( entityList.isEmpty() || fields == null )
         {
-            return output;
+            return objects;
         }
 
         Map<String, Method> classMap = ReflectionUtils.getJacksonClassMap( entityList.get( 0 ).getClass() );
@@ -201,7 +199,7 @@
 
         for ( T object : entityList )
         {
-            Map<String, Object> objMap = Maps.newHashMap();
+            Map<String, Object> objMap = Maps.newLinkedHashMap();
 
             for ( String field : split )
             {
@@ -209,17 +207,56 @@
                 {
                     Object o = ReflectionUtils.invokeMethod( object, classMap.get( field ) );
 
-                    // skip collections for now
+                    if ( o == null )
+                    {
+                        continue;
+                    }
+
                     if ( !ReflectionUtils.isCollection( o ) )
                     {
                         objMap.put( field, o );
                     }
+                    else
+                    {
+                        objMap.put( field, getIdentifiableObjectProperties( o ) );
+                    }
                 }
             }
 
             objects.add( objMap );
         }
 
-        return output;
+        return objects;
+    }
+
+    @SuppressWarnings( "unchecked" )
+    private static List<Map<String, Object>> getIdentifiableObjectProperties( Object o )
+    {
+        List<Map<String, Object>> idPropertiesList = Lists.newArrayList();
+        Collection<IdentifiableObject> identifiableObjects;
+
+        try
+        {
+            identifiableObjects = (Collection<IdentifiableObject>) o;
+        }
+        catch ( ClassCastException ex )
+        {
+            return null;
+        }
+
+        for ( IdentifiableObject identifiableObject : identifiableObjects )
+        {
+            Map<String, Object> idProps = Maps.newLinkedHashMap();
+
+            idProps.put( "id", identifiableObject.getUid() );
+            idProps.put( "name", identifiableObject.getDisplayName() );
+            idProps.put( "code", identifiableObject.getCode() );
+            idProps.put( "created", identifiableObject.getCreated() );
+            idProps.put( "lastUpdated", identifiableObject.getLastUpdated() );
+
+            idPropertiesList.add( idProps );
+        }
+
+        return idPropertiesList;
     }
 }