← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7223: fixes to link-generator

 

------------------------------------------------------------
revno: 7223
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-06-06 16:32:25 +0200
message:
  fixes to link-generator
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.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/PredicateUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.java	2012-06-04 13:02:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.java	2012-06-06 14:32:25 +0000
@@ -42,6 +42,10 @@
  */
 public class PredicateUtils
 {
+    public static Predicate<Field> alwaysTrue = new StaticReply( true );
+
+    public static Predicate<Field> alwaysFalse = new StaticReply( false );
+
     public static Predicate<Field> idObjects = new ObjectWithTypePredicate( IdentifiableObject.class );
 
     public static Predicate<Field> collections = new CollectionPredicate();
@@ -52,6 +56,23 @@
 
     public static Predicate<Field> idObjectCollectionsWithScanned = new CollectionWithTypeAndAnnotationPredicate( IdentifiableObject.class, Scanned.class );
 
+    public static class StaticReply
+        implements Predicate<Field>
+    {
+        private boolean value = false;
+
+        public StaticReply( boolean value )
+        {
+            this.value = value;
+        }
+
+        @Override
+        public boolean evaluate( Field object )
+        {
+            return value;
+        }
+    }
+
     public static class CollectionPredicate
         implements Predicate<Field>
     {

=== 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	2012-05-30 09:38:06 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java	2012-06-06 14:32:25 +0000
@@ -44,6 +44,8 @@
 import java.util.Collection;
 import java.util.List;
 
+import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -55,7 +57,7 @@
     public static void generateLinks( WebMetaData metaData )
     {
         Class<?> baseType = null;
-        List<Field> fields = ReflectionUtils.getAllFields( metaData.getClass() );
+        Collection<Field> fields = ReflectionUtils.collectFields( metaData.getClass(), alwaysTrue );
 
         for ( Field field : fields )
         {
@@ -110,26 +112,32 @@
     {
         identifiableObject.setLink( getPathWithUid( identifiableObject ) );
 
-        List<Field> fields = ReflectionUtils.getAllFields( identifiableObject.getClass() );
+        Collection<Field> fields = ReflectionUtils.collectFields( identifiableObject.getClass(), alwaysTrue );
 
         for ( Field field : fields )
         {
             if ( IdentifiableObject.class.isAssignableFrom( field.getType() ) )
             {
-                IdentifiableObject object = ReflectionUtils.getFieldObject( field, identifiableObject );
+                Object object = ReflectionUtils.getFieldObject( field, identifiableObject );
 
                 if ( object != null )
                 {
-                    object.setLink( getPathWithUid( object ) );
+                    IdentifiableObject idObject = (IdentifiableObject) object;
+                    idObject.setLink( getPathWithUid( idObject ) );
                 }
             }
             else if ( ReflectionUtils.isCollection( field.getName(), identifiableObject, IdentifiableObject.class ) )
             {
-                Collection<IdentifiableObject> objects = (Collection<IdentifiableObject>) ReflectionUtils.getFieldObject( field, identifiableObject );
+                Object collection = ReflectionUtils.getFieldObject( field, identifiableObject );
 
-                for ( IdentifiableObject object : objects )
+                if ( collection != null )
                 {
-                    object.setLink( getPathWithUid( object ) );
+                    Collection<IdentifiableObject> objects = (Collection<IdentifiableObject>) collection;
+
+                    for ( IdentifiableObject object : objects )
+                    {
+                        object.setLink( getPathWithUid( object ) );
+                    }
                 }
             }
         }