← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13949: in web-api type-listing, include idObjects and collections with idObjects

 

------------------------------------------------------------
revno: 13949
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-02-06 15:24:34 +0700
message:
  in web-api type-listing, include idObjects and collections with 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/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-06 06:37:09 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-02-06 08:24:34 +0000
@@ -528,6 +528,11 @@
 
     public static Map<String, Method> getJacksonClassMap( Class<?> clazz )
     {
+        return getJacksonClassMap( clazz, true );
+    }
+
+    public static Map<String, Method> getJacksonClassMap( Class<?> clazz, boolean deep )
+    {
         if ( classMapCache.containsKey( clazz ) )
         {
             return classMapCache.get( clazz );
@@ -543,13 +548,15 @@
             {
                 JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
 
-                if ( StringUtils.isEmpty( jsonProperty.value() ) )
+                String name = jsonProperty.value();
+
+                if ( StringUtils.isEmpty( name ) )
                 {
                     String[] getters = new String[]{
                         "is", "has", "get"
                     };
 
-                    String name = method.getName();
+                    name = method.getName();
 
                     for ( String getter : getters )
                     {
@@ -559,11 +566,44 @@
                         }
                     }
 
-                    output.put( StringUtils.uncapitalize( name ), method );
+                    name = StringUtils.uncapitalize( name );
+                    output.put( name, method );
                 }
                 else
                 {
-                    output.put( jsonProperty.value(), method );
+                    output.put( name, method );
+                }
+
+                Class<?> returnType = method.getReturnType();
+
+                if ( deep && IdentifiableObject.class.isAssignableFrom( returnType ) )
+                {
+                    Map<String, Method> classMap = getJacksonClassMap( returnType, false );
+
+                    for ( String key : classMap.keySet() )
+                    {
+                        output.put( name + "." + key, classMap.get( key ) );
+                    }
+                }
+                else if ( deep && Collection.class.isAssignableFrom( returnType ) )
+                {
+                    Type type = method.getGenericReturnType();
+
+                    if ( ParameterizedType.class.isInstance( type ) )
+                    {
+                        ParameterizedType parameterizedType = (ParameterizedType) type;
+                        Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
+
+                        if ( IdentifiableObject.class.isAssignableFrom( klass ) )
+                        {
+                            Map<String, Method> classMap = getJacksonClassMap( klass, false );
+
+                            for ( String key : classMap.keySet() )
+                            {
+                                output.put( name + "." + key, classMap.get( key ) );
+                            }
+                        }
+                    }
                 }
             }
         }

=== 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:37:09 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java	2014-02-06 08:24:34 +0000
@@ -230,7 +230,7 @@
     }
 
     @SuppressWarnings( "unchecked" )
-    private static List<Map<String, Object>> getIdentifiableObjectProperties( Object o )
+    private static Object getIdentifiableObjectProperties( Object o )
     {
         List<Map<String, Object>> idPropertiesList = Lists.newArrayList();
         Collection<IdentifiableObject> identifiableObjects;
@@ -241,7 +241,7 @@
         }
         catch ( ClassCastException ex )
         {
-            return null;
+            return o;
         }
 
         for ( IdentifiableObject identifiableObject : identifiableObjects )
@@ -250,7 +250,12 @@
 
             idProps.put( "id", identifiableObject.getUid() );
             idProps.put( "name", identifiableObject.getDisplayName() );
-            idProps.put( "code", identifiableObject.getCode() );
+
+            if ( identifiableObject.getCode() == null )
+            {
+                idProps.put( "code", identifiableObject.getCode() );
+            }
+
             idProps.put( "created", identifiableObject.getCreated() );
             idProps.put( "lastUpdated", identifiableObject.getLastUpdated() );