← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13952: parser for filtered idObject/collections-with-idObjects in web-api, wip

 

------------------------------------------------------------
revno: 13952
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-02-07 13:32:38 +0700
message:
  parser for filtered idObject/collections-with-idObjects in web-api, wip
modified:
  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-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 09:59:02 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java	2014-02-07 06:32:38 +0000
@@ -30,6 +30,7 @@
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.api.controller.WebMetaData;
@@ -195,13 +196,13 @@
         }
 
         Map<String, Method> classMap = ReflectionUtils.getJacksonClassMap( entityList.get( 0 ).getClass() );
-        String[] split = fields.split( "," );
+        List<String> parsedFields = parseFieldExpression( fields );
 
         for ( T object : entityList )
         {
             Map<String, Object> objMap = Maps.newLinkedHashMap();
 
-            for ( String field : split )
+            for ( String field : parsedFields )
             {
                 if ( classMap.containsKey( field ) )
                 {
@@ -277,4 +278,60 @@
 
         return idProps;
     }
+
+    private static List<String> parseFieldExpression( String fields )
+    {
+        List<String> splitFields = Lists.newArrayList();
+
+        StringBuilder builder = new StringBuilder();
+        ArrayList<String> prefixList = Lists.newArrayList();
+
+        for ( String c : fields.split( "" ) )
+        {
+            if ( c.equals( "," ) )
+            {
+                splitFields.add( joinedWithPrefix( builder, prefixList ) );
+                builder = new StringBuilder();
+                continue;
+            }
+
+            if ( c.equals( "[" ) )
+            {
+                prefixList.add( builder.toString() );
+                builder = new StringBuilder();
+                continue;
+            }
+
+            if ( c.equals( "]" ) )
+            {
+                if ( !builder.toString().isEmpty() )
+                {
+                    splitFields.add( joinedWithPrefix( builder, prefixList ) );
+                }
+
+                prefixList.remove( prefixList.size() - 1 );
+                builder = new StringBuilder();
+                continue;
+            }
+
+            if ( StringUtils.isAlpha( c ) )
+            {
+                builder.append( c );
+            }
+        }
+
+        if ( !builder.toString().isEmpty() )
+        {
+            splitFields.add( joinedWithPrefix( builder, prefixList ) );
+        }
+
+        return splitFields;
+    }
+
+    private static String joinedWithPrefix( StringBuilder builder, List<String> prefixList )
+    {
+        String output = StringUtils.join( prefixList, "." );
+        output = output.isEmpty() ? builder.toString() : (output + "." + builder.toString());
+        return output;
+    }
 }