← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15763: support transformers for collections and string, hardcoded to SizePropertyValueTransformer for now

 

------------------------------------------------------------
revno: 15763
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-06-19 13:25:24 +0200
message:
  support transformers for collections and string, hardcoded to SizePropertyValueTransformer for now
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/PropertyValueTransformer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/SizePropertyValueTransformer.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFieldFilterService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FieldMap.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/PropertyValueTransformer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/PropertyValueTransformer.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/PropertyValueTransformer.java	2014-06-19 11:25:24 +0000
@@ -0,0 +1,60 @@
+package org.hisp.dhis.node;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
+ */
+
+import org.hisp.dhis.schema.Property;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface PropertyValueTransformer
+{
+    /**
+     * @return Public/external name of this transformer.
+     */
+    String name();
+
+    /**
+     * Transform value. Value can be null.
+     *
+     * @param property Property instance belonging to value
+     * @param value    Actual value to transform
+     * @return Value transformed to a Node
+     */
+    Node transform( Property property, Object value );
+
+    /**
+     * Is this property/value supported by this transformer. Value can be null.
+     *
+     * @param property Property instance belonging to value
+     * @param value    Actual value to transform
+     * @return true of false depending on support
+     */
+    boolean canTransform( Property property, Object value );
+}

=== added directory 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers'
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/SizePropertyValueTransformer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/SizePropertyValueTransformer.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/SizePropertyValueTransformer.java	2014-06-19 11:25:24 +0000
@@ -0,0 +1,69 @@
+package org.hisp.dhis.node.transformers;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
+ */
+
+import org.hisp.dhis.node.Node;
+import org.hisp.dhis.node.PropertyValueTransformer;
+import org.hisp.dhis.node.types.SimpleNode;
+import org.hisp.dhis.schema.Property;
+
+import java.util.Collection;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class SizePropertyValueTransformer implements PropertyValueTransformer
+{
+    @Override
+    public String name()
+    {
+        return "size";
+    }
+
+    @Override
+    public Node transform( Property property, Object value )
+    {
+        if ( property.isCollection() )
+        {
+            return new SimpleNode( property.getCollectionName(), ((Collection<?>) value).size(), property.isAttribute() );
+        }
+        else if ( String.class.isInstance( value ) )
+        {
+            return new SimpleNode( property.getName(), ((String) value).length(), property.isAttribute() );
+        }
+
+        throw new IllegalStateException( "Should never get here, this property/value is not supported by this transformer." );
+    }
+
+    @Override
+    public boolean canTransform( Property property, Object value )
+    {
+        return property.isCollection() || String.class.isInstance( value );
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java	2014-06-09 12:27:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java	2014-06-19 11:25:24 +0000
@@ -56,6 +56,12 @@
         this.attribute = false;
     }
 
+    public SimpleNode( String name, Object value, boolean attribute )
+    {
+        this( name, value );
+        this.attribute = attribute;
+    }
+
     public Object getValue()
     {
         return value;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFieldFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFieldFilterService.java	2014-06-19 10:51:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFieldFilterService.java	2014-06-19 11:25:24 +0000
@@ -33,6 +33,7 @@
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.node.transformers.SizePropertyValueTransformer;
 import org.hisp.dhis.node.types.CollectionNode;
 import org.hisp.dhis.node.types.ComplexNode;
 import org.hisp.dhis.node.types.SimpleNode;
@@ -138,7 +139,12 @@
                 updateFields( fieldValue, property.getKlass() );
             }
 
-            if ( fieldValue.isEmpty() )
+            if ( fieldValue.isTransform() )
+            {
+                SizePropertyValueTransformer size = new SizePropertyValueTransformer();
+                complexNode.addChild( size.transform( property, returnValue ) );
+            }
+            else if ( fieldValue.isEmpty() )
             {
                 List<String> fields = FIELD_PRESETS.get( "identifiable" );
 
@@ -280,6 +286,19 @@
             {
                 cleanupFields.add( fieldKey );
             }
+            else if ( fieldKey.contains( "::" ) )
+            {
+                String[] split = fieldKey.split( "::" );
+
+                if ( split.length == 2 )
+                {
+                    FieldMap value = new FieldMap();
+                    value.setTransform( split[1] );
+                    fieldMap.put( split[0], value );
+                }
+
+                cleanupFields.add( fieldKey );
+            }
         }
 
         for ( String field : cleanupFields )

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FieldMap.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FieldMap.java	2014-06-19 10:51:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FieldMap.java	2014-06-19 11:25:24 +0000
@@ -28,8 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  */
 
+import com.google.common.base.Objects;
 import com.google.common.collect.ForwardingMap;
 import com.google.common.collect.Maps;
+import org.springframework.util.StringUtils;
 
 import java.util.Map;
 
@@ -40,9 +42,37 @@
 {
     private final Map<String, FieldMap> delegate = Maps.newHashMap();
 
+    private String transform;
+
+    private String preset;
+
     @Override
     protected Map<String, FieldMap> delegate()
     {
         return delegate;
     }
+
+    public String getTransform()
+    {
+        return transform;
+    }
+
+    public void setTransform( String transform )
+    {
+        this.transform = transform;
+    }
+
+    public boolean isTransform()
+    {
+        return !StringUtils.isEmpty( transform );
+    }
+
+    @Override
+    public String toString()
+    {
+        return Objects.toStringHelper( this )
+            .add( "map", standardToString() )
+            .add( "transform", transform )
+            .toString();
+    }
 }