dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #27971
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13945: simple filtering of web-api types, collections not supported yet (will end up in loop). Available...
------------------------------------------------------------
revno: 13945
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-02-06 12:54:05 +0700
message:
simple filtering of web-api types, collections not supported yet (will end up in loop). Available at /api/type/filtered (will display list of available properties), can be used with /api/type/filtered?field=field1,field2
modified:
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-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-04 12:54:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java 2014-02-06 05:54:05 +0000
@@ -57,7 +57,6 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
@@ -84,10 +83,28 @@
// GET
//--------------------------------------------------------------------------
- @RequestMapping( value = "/map", method = RequestMethod.GET )
- public void getJacksonClassMap( OutputStream outputStream ) throws IOException
+ @RequestMapping( value = "/filtered", method = RequestMethod.GET )
+ public void getJacksonClassMap( @RequestParam( required = false ) String fields,
+ @RequestParam Map<String, String> parameters, HttpServletResponse response ) throws IOException
{
- JacksonUtils.toJson( outputStream, ReflectionUtils.getJacksonClassMap( getEntityClass() ).keySet() );
+ if ( fields == null )
+ {
+ JacksonUtils.toJson( response.getOutputStream(), ReflectionUtils.getJacksonClassMap( getEntityClass() ).keySet() );
+ return;
+ }
+
+ WebOptions options = new WebOptions( parameters );
+ WebMetaData metaData = new WebMetaData();
+ List<T> entityList = getEntityList( metaData, options );
+
+ handleLinksAndAccess( options, metaData, entityList, true );
+
+ postProcessEntities( entityList );
+ postProcessEntities( entityList, options, parameters );
+
+ List<Map<String, Object>> output = WebUtils.filterFields( entityList, fields );
+
+ JacksonUtils.toJson( response.getOutputStream(), output );
}
@RequestMapping( method = RequestMethod.GET )
=== 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 2013-10-16 07:30:34 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java 2014-02-06 05:54:05 +0000
@@ -28,6 +28,8 @@
* 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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hisp.dhis.api.controller.WebMetaData;
@@ -38,9 +40,11 @@
import org.hisp.dhis.user.UserCredentials;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
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;
@@ -127,7 +131,7 @@
generateLinks( object, true );
}
- @SuppressWarnings( "unchecked" )
+ @SuppressWarnings("unchecked")
public static void generateLinks( Object object, boolean deep )
{
if ( object == null )
@@ -180,4 +184,39 @@
}
}
}
+
+ public static <T extends IdentifiableObject> List<Map<String, Object>> filterFields( List<T> entityList, String fields )
+ {
+ if ( entityList.isEmpty() || fields == null )
+ {
+ return Lists.newArrayList();
+ }
+
+ List<Map<String, Object>> output = Lists.newArrayList();
+ Map<String, Method> classMap = ReflectionUtils.getJacksonClassMap( entityList.get( 0 ).getClass() );
+ String[] split = fields.split( "," );
+
+ for ( T object : entityList )
+ {
+ Map<String, Object> objMap = Maps.newHashMap();
+
+ for ( String field : split )
+ {
+ if ( classMap.containsKey( field ) )
+ {
+ Object o = ReflectionUtils.invokeMethod( object, classMap.get( field ) );
+
+ // skip collections for now
+ if ( !ReflectionUtils.isCollection( o ) )
+ {
+ objMap.put( field, o );
+ }
+ }
+ }
+
+ output.add( objMap );
+ }
+
+ return output;
+ }
}