dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #27938
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13929: added new method to ReflectionUtils called getJacksonClassMap, will build a map of all serializab...
------------------------------------------------------------
revno: 13929
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-02-04 19:54:31 +0700
message:
added new method to ReflectionUtils called getJacksonClassMap, will build a map of all serializable properties with their @JsonProperty.value as key, and Method as value. Mapped as /api/type/map for now, will be moved later.
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
--
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-03 08:47:12 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java 2014-02-04 12:54:31 +0000
@@ -30,6 +30,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.system.util.functional.Function1;
import org.hisp.dhis.system.util.functional.Predicate;
import org.springframework.util.StringUtils;
@@ -46,6 +47,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
@@ -520,4 +522,53 @@
return fieldName;
}
+
+ private static Map<Class<?>, Map<String, Method>> classMapCache = Maps.newHashMap();
+
+ public static Map<String, Method> getJacksonClassMap( Class<?> clazz )
+ {
+ if ( classMapCache.containsKey( clazz ) )
+ {
+ return classMapCache.get( clazz );
+ }
+
+ Map<String, Method> output = Maps.newLinkedHashMap();
+
+ List<Method> allMethods = getAllMethods( clazz );
+
+ for ( Method method : allMethods )
+ {
+ if ( method.isAnnotationPresent( JsonProperty.class ) )
+ {
+ JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
+
+ if ( StringUtils.isEmpty( jsonProperty.value() ) )
+ {
+ String[] getters = new String[]{
+ "is", "has", "get"
+ };
+
+ String name = method.getName();
+
+ for ( String getter : getters )
+ {
+ if ( name.startsWith( getter ) )
+ {
+ name = name.substring( getter.length() );
+ }
+ }
+
+ output.put( StringUtils.uncapitalize( name ), method );
+ }
+ else
+ {
+ output.put( jsonProperty.value(), method );
+ }
+ }
+ }
+
+ classMapCache.put( clazz, output );
+
+ return output;
+ }
}
=== 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 2013-12-17 13:06:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java 2014-02-04 12:54:31 +0000
@@ -55,7 +55,9 @@
import javax.servlet.http.HttpServletRequest;
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;
@@ -82,7 +84,13 @@
// GET
//--------------------------------------------------------------------------
- @RequestMapping(method = RequestMethod.GET)
+ @RequestMapping( value = "/map", method = RequestMethod.GET )
+ public void getJacksonClassMap( OutputStream outputStream ) throws IOException
+ {
+ JacksonUtils.toJson( outputStream, ReflectionUtils.getJacksonClassMap( getEntityClass() ).keySet() );
+ }
+
+ @RequestMapping( method = RequestMethod.GET )
public String getObjectList( @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
{
WebOptions options = new WebOptions( parameters );
@@ -109,7 +117,7 @@
return StringUtils.uncapitalize( getEntitySimpleName() ) + "List";
}
- @RequestMapping(value = "/query/{query}", method = RequestMethod.GET)
+ @RequestMapping( value = "/query/{query}", method = RequestMethod.GET )
public String query( @PathVariable String query, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
{
WebOptions options = new WebOptions( parameters );
@@ -138,8 +146,8 @@
return StringUtils.uncapitalize( getEntitySimpleName() ) + "List";
}
- @RequestMapping(value = "/{uid}", method = RequestMethod.GET)
- public String getObject( @PathVariable("uid") String uid, @RequestParam Map<String, String> parameters,
+ @RequestMapping( value = "/{uid}", method = RequestMethod.GET )
+ public String getObject( @PathVariable( "uid" ) String uid, @RequestParam Map<String, String> parameters,
Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
{
WebOptions options = new WebOptions( parameters );
@@ -169,7 +177,7 @@
return StringUtils.uncapitalize( getEntitySimpleName() );
}
- @RequestMapping(value = "/search/{query}", method = RequestMethod.GET)
+ @RequestMapping( value = "/search/{query}", method = RequestMethod.GET )
public String search( @PathVariable String query, @RequestParam Map<String, String> parameters,
Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
{
@@ -199,13 +207,13 @@
// POST
//--------------------------------------------------------------------------
- @RequestMapping(method = RequestMethod.POST, consumes = { "application/xml", "text/xml" })
+ @RequestMapping( method = RequestMethod.POST, consumes = { "application/xml", "text/xml" } )
public void postXmlObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
{
throw new HttpRequestMethodNotSupportedException( RequestMethod.POST.toString() );
}
- @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
+ @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
{
throw new HttpRequestMethodNotSupportedException( RequestMethod.POST.toString() );
@@ -215,16 +223,16 @@
// PUT
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" })
- @ResponseStatus(value = HttpStatus.NO_CONTENT)
- public void putXmlObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, InputStream input ) throws Exception
+ @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" } )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void putXmlObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream input ) throws Exception
{
throw new HttpRequestMethodNotSupportedException( RequestMethod.PUT.toString() );
}
- @RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
- @ResponseStatus(value = HttpStatus.NO_CONTENT)
- public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, InputStream input ) throws Exception
+ @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream input ) throws Exception
{
throw new HttpRequestMethodNotSupportedException( RequestMethod.PUT.toString() );
}
@@ -233,9 +241,9 @@
// DELETE
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
- @ResponseStatus(value = HttpStatus.NO_CONTENT)
- public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid ) throws Exception
+ @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws Exception
{
throw new HttpRequestMethodNotSupportedException( RequestMethod.DELETE.toString() );
}
@@ -416,7 +424,7 @@
return entitySimpleName;
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
protected T getEntityInstance()
{
try