=== 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-10 04:59:03 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java 2014-02-10 08:47:17 +0000 @@ -585,6 +585,15 @@ { this.objects = objects; } + + @Override public String toString() + { + return "MethodDescriptor{" + + "method=" + method + + ", collection=" + collection + + ", identifiableObject=" + identifiableObject + + '}'; + } } public static Map getJacksonClassMap( Class clazz ) @@ -669,7 +678,7 @@ if ( deep ) { - Map classMap = getJacksonClassMap( klass, level ); + Map classMap = getJacksonClassMap( returnType, level ); descriptor.setObjects( classMap ); } } === 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-10 04:59:03 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java 2014-02-10 08:47:17 +0000 @@ -87,9 +87,10 @@ @RequestMapping( value = "/filtered", method = RequestMethod.GET ) public void getJacksonClassMap( @RequestParam( required = false ) String include, + @RequestParam( required = false ) String exclude, @RequestParam Map parameters, HttpServletResponse response ) throws IOException { - if ( include == null ) + if ( include == null && exclude == null ) { JacksonUtils.toJson( response.getOutputStream(), ReflectionUtils.getJacksonClassMap( getEntityClass() ) ); return; @@ -104,7 +105,7 @@ postProcessEntities( entityList ); postProcessEntities( entityList, options, parameters ); - // List objects = WebUtils.filterFields( entityList, include ); + List objects = WebUtils.filterFields( entityList, include ); Map output = Maps.newLinkedHashMap(); if ( options.hasPaging() ) @@ -112,7 +113,7 @@ output.put( "pager", metaData.getPager() ); } - output.put( "objects", WebUtils.parseFieldExpression( include ) ); + 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-10 04:59:03 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java 2014-02-10 08:47:17 +0000 @@ -186,7 +186,7 @@ } @SuppressWarnings( "unchecked" ) - private static void putInMap( Map map, String path ) + private static void putInMap( Map map, String path ) { for ( String p : path.split( "\\." ) ) { @@ -195,14 +195,14 @@ map.put( p, Maps.newHashMap() ); } - map = (Map) map.get( p ); + map = (Map) map.get( p ); } } - public static Map parseFieldExpression( String fields ) + public static Map parseFieldExpression( String fields ) { List prefixList = Lists.newArrayList(); - Map parsed = Maps.newHashMap(); + Map parsed = Maps.newHashMap(); StringBuilder builder = new StringBuilder(); @@ -254,4 +254,125 @@ output = output.isEmpty() ? builder.toString() : (output + "." + builder.toString()); return output; } + + public static List filterFields( List objects, String fields ) + { + List output = Lists.newArrayList(); + + if ( objects.isEmpty() || fields == null ) + { + return output; + } + + Map fieldMap = parseFieldExpression( fields ); + + for ( Object object : objects ) + { + output.add( buildObjectOutput( object, fieldMap ) ); + } + + return output; + } + + @SuppressWarnings( "unchecked" ) + private static Map buildObjectOutput( Object object, Map fieldMap ) + { + Map output = Maps.newHashMap(); + Map classMap = ReflectionUtils.getJacksonClassMap( object.getClass() ); + + for ( String key : fieldMap.keySet() ) + { + if ( !classMap.containsKey( key ) ) + { + continue; + } + + Map value = fieldMap.get( key ); + ReflectionUtils.MethodDescriptor descriptor = classMap.get( key ); + + if ( value.isEmpty() ) + { + if ( !descriptor.isCollection() && !descriptor.isIdentifiableObject() ) + { + Object returned = ReflectionUtils.invokeMethod( object, descriptor.getMethod() ); + output.put( key, returned ); + } + else if ( descriptor.isIdentifiableObject() && !descriptor.isCollection() ) + { + Object returned = getIdentifiableObjectProperties( object ); + output.put( key, returned ); + } + else if ( descriptor.isCollection() && descriptor.isIdentifiableObject() ) + { + Object returned = ReflectionUtils.invokeMethod( object, descriptor.getMethod() ); + returned = getIdentifiableObjectCollectionProperties( returned ); + output.put( key, returned ); + } + } + } + + return output; + } + + private static Object getIdentifiableObjectCollectionProperties( Object object ) + { + List fields = Lists.newArrayList( "id", "name", "code", "created", "lastUpdated" ); + return getIdentifiableObjectCollectionProperties( object, fields ); + } + + @SuppressWarnings( "unchecked" ) + private static Object getIdentifiableObjectCollectionProperties( Object object, List fields ) + { + List> idPropertiesList = Lists.newArrayList(); + Collection identifiableObjects; + + try + { + identifiableObjects = (Collection) object; + } + catch ( ClassCastException ex ) + { + ex.printStackTrace(); + return idPropertiesList; + } + + for ( IdentifiableObject identifiableObject : identifiableObjects ) + { + Map properties = getIdentifiableObjectProperties( identifiableObject, fields ); + idPropertiesList.add( properties ); + } + + return idPropertiesList; + } + + private static Map getIdentifiableObjectProperties( Object object ) + { + List fields = Lists.newArrayList( "id", "name", "code", "created", "lastUpdated" ); + return getIdentifiableObjectProperties( object, fields ); + } + + private static Map getIdentifiableObjectProperties( Object object, List fields ) + { + Map idProps = Maps.newLinkedHashMap(); + Map classMap = ReflectionUtils.getJacksonClassMap( object.getClass() ); + + for ( String field : fields ) + { + ReflectionUtils.MethodDescriptor descriptor = classMap.get( field ); + + if ( descriptor == null ) + { + continue; + } + + Object o = ReflectionUtils.invokeMethod( object, descriptor.getMethod() ); + + if ( o != null ) + { + idProps.put( field, o ); + } + } + + return idProps; + } }