← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15524: simplifications to DefaultFilterService code

 

------------------------------------------------------------
revno: 15524
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-06-02 23:35:16 +0200
message:
  simplifications to DefaultFilterService code
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java	2014-06-02 21:07:50 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java	2014-06-02 21:35:16 +0000
@@ -47,6 +47,9 @@
  */
 public class DefaultFilterService implements FilterService
 {
+    private static final List<String> IDENTIFIABLE_PROPERTIES =
+        Lists.newArrayList( "id", "name", "code", "created", "lastUpdated" );
+
     @Autowired
     private ParserService parserService;
 
@@ -132,65 +135,62 @@
         Map<String, Object> output = Maps.newHashMap();
         Schema schema = schemaService.getDynamicSchema( object.getClass() );
 
-        for ( String key : fieldMap.keySet() )
+        for ( String fieldKey : fieldMap.keySet() )
         {
-            if ( !schema.getPropertyMap().containsKey( key ) )
-            {
-                continue;
-            }
-
-            Map value = fieldMap.get( key );
-            Property descriptor = schema.getPropertyMap().get( key );
-
-            Object returned = ReflectionUtils.invokeMethod( object, descriptor.getGetterMethod() );
-
-            if ( returned == null )
-            {
-                continue;
-            }
-
-            if ( value.isEmpty() )
-            {
-                if ( !descriptor.isIdentifiableObject() )
+            if ( !schema.getPropertyMap().containsKey( fieldKey ) )
+            {
+                continue;
+            }
+
+            Property property = schema.getPropertyMap().get( fieldKey );
+            Object returnValue = ReflectionUtils.invokeMethod( object, property.getGetterMethod() );
+
+            if ( returnValue == null )
+            {
+                continue;
+            }
+
+            Map fieldValue = fieldMap.get( fieldKey );
+
+            if ( fieldValue.isEmpty() )
+            {
+                if ( !property.isIdentifiableObject() )
                 {
-                    output.put( key, returned );
+                    output.put( fieldKey, returnValue );
                 }
-                else if ( !descriptor.isCollection() )
+                else if ( !property.isCollection() )
                 {
-                    Map<String, Object> properties = getIdentifiableObjectProperties( returned );
-                    output.put( key, properties );
+                    output.put( fieldKey, getIdentifiableObjectProperties( returnValue ) );
                 }
                 else
                 {
-                    List<Map<String, Object>> properties = getIdentifiableObjectCollectionProperties( returned );
-                    output.put( key, properties );
+                    output.put( fieldKey, getIdentifiableObjectCollectionProperties( returnValue ) );
                 }
             }
             else
             {
-                if ( descriptor.isCollection() )
+                if ( property.isCollection() )
                 {
-                    Collection<?> objects = (Collection<?>) returned;
-                    List<Object> arrayList = Lists.newArrayList();
-                    output.put( key, arrayList );
+                    List<Object> list = Lists.newArrayList();
+                    output.put( fieldKey, list );
 
-                    for ( Object obj : objects )
+                    for ( Object obj : (Collection<?>) returnValue )
                     {
-                        Map<String, Object> properties = buildObjectOutput( obj, value );
+                        Map<String, Object> properties = buildObjectOutput( obj, fieldValue );
 
                         if ( !properties.isEmpty() )
                         {
-                            arrayList.add( properties );
+                            list.add( properties );
                         }
                     }
                 }
                 else
                 {
-                    Map<String, Object> properties = buildObjectOutput( returned, value );
+                    Map<String, Object> map = buildObjectOutput( returnValue, fieldValue );
 
-                    if ( !properties.isEmpty() )
+                    if ( !map.isEmpty() )
                     {
-                        output.put( key, properties );
+                        output.put( fieldKey, map );
                     }
                 }
             }
@@ -201,8 +201,7 @@
 
     private List<Map<String, Object>> getIdentifiableObjectCollectionProperties( Object object )
     {
-        List<String> fields = Lists.newArrayList( "id", "name", "code", "created", "lastUpdated" );
-        return getIdentifiableObjectCollectionProperties( object, fields );
+        return getIdentifiableObjectCollectionProperties( object, IDENTIFIABLE_PROPERTIES );
     }
 
     @SuppressWarnings( "unchecked" )
@@ -232,33 +231,32 @@
 
     private Map<String, Object> getIdentifiableObjectProperties( Object object )
     {
-        List<String> fields = Lists.newArrayList( "id", "name", "code", "created", "lastUpdated" );
-        return getIdentifiableObjectProperties( object, fields );
+        return getIdentifiableObjectProperties( object, IDENTIFIABLE_PROPERTIES );
     }
 
     private Map<String, Object> getIdentifiableObjectProperties( Object object, List<String> fields )
     {
-        Map<String, Object> idProps = Maps.newLinkedHashMap();
+        Map<String, Object> map = Maps.newLinkedHashMap();
         Schema schema = schemaService.getDynamicSchema( object.getClass() );
 
         for ( String field : fields )
         {
-            Property descriptor = schema.getPropertyMap().get( field );
+            Property property = schema.getPropertyMap().get( field );
 
-            if ( descriptor == null )
+            if ( property == null )
             {
                 continue;
             }
 
-            Object o = ReflectionUtils.invokeMethod( object, descriptor.getGetterMethod() );
+            Object o = ReflectionUtils.invokeMethod( object, property.getGetterMethod() );
 
             if ( o != null )
             {
-                idProps.put( field, o );
+                map.put( field, o );
             }
         }
 
-        return idProps;
+        return map;
     }
 
     @SuppressWarnings( "unchecked" )