← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15845: support renaming of property in web-api, e.g. ?fields=id, name|rename(n), dataSets::size|rename(dat...

 

------------------------------------------------------------
revno: 15845
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-06-25 18:27:12 +0700
message:
  support renaming of property in web-api, e.g. ?fields=id,name|rename(n),dataSets::size|rename(dataSetSize)
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/RenameNodeTransformer.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/LinearNodePipeline.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Transformer.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/FieldMap.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/parser/DefaultParserService.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-api/src/main/java/org/hisp/dhis/node/LinearNodePipeline.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/LinearNodePipeline.java	2014-06-25 09:30:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/LinearNodePipeline.java	2014-06-25 11:27:12 +0000
@@ -48,7 +48,7 @@
     {
         for ( NodeTransformer transformer : nodeTransformers )
         {
-            node = transformer.transform( node );
+            node = transformer.transform( node, Lists.<String>newArrayList() );
         }
 
         return node;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Transformer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Transformer.java	2014-06-25 09:30:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Transformer.java	2014-06-25 11:27:12 +0000
@@ -28,10 +28,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  */
 
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface Transformer<T>
 {
-    T transform( T object );
+    String name();
+
+    T transform( T object, List<String> args );
 }

=== 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/RenameNodeTransformer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/RenameNodeTransformer.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/transformers/RenameNodeTransformer.java	2014-06-25 11:27:12 +0000
@@ -0,0 +1,62 @@
+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.AbstractNode;
+import org.hisp.dhis.node.Node;
+import org.hisp.dhis.node.NodeTransformer;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@Component
+public class RenameNodeTransformer implements NodeTransformer
+{
+    @Override
+    public String name()
+    {
+        return "rename";
+    }
+
+    @Override
+    public Node transform( Node node, List<String> args )
+    {
+        checkNotNull( node );
+        checkArgument( args.size() > 0, "rename requires a name parameter, .e.g: property|rename(newName)" );
+        ((AbstractNode) node).setName( args.get( 0 ) );
+
+        return node;
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java	2014-06-20 11:14:18 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java	2014-06-25 11:27:12 +0000
@@ -37,6 +37,7 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.PresetProvider;
 import org.hisp.dhis.dxf2.parser.ParserService;
+import org.hisp.dhis.node.AbstractNode;
 import org.hisp.dhis.node.NodePropertyConverter;
 import org.hisp.dhis.node.types.CollectionNode;
 import org.hisp.dhis.node.types.ComplexNode;
@@ -51,6 +52,8 @@
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -68,7 +71,7 @@
     @Autowired(required = false)
     private Set<PresetProvider> presetProviders = Sets.newHashSet();
 
-    @Autowired( required = false )
+    @Autowired(required = false)
     private Set<NodePropertyConverter> nodePropertyConverters = Sets.newHashSet();
 
     private ImmutableMap<String, PresetProvider> presets = ImmutableMap.of();
@@ -135,7 +138,7 @@
         return collectionNode;
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     private ComplexNode buildComplexNode( FieldMap fieldMap, Class<?> klass, Object object )
     {
         Schema schema = schemaService.getDynamicSchema( klass );
@@ -152,6 +155,8 @@
 
         for ( String fieldKey : fieldMap.keySet() )
         {
+            AbstractNode child = null;
+
             if ( !schema.getPropertyMap().containsKey( fieldKey ) )
             {
                 continue;
@@ -179,9 +184,8 @@
 
                 if ( converter.canConvertTo( property, returnValue ) )
                 {
-                    complexNode.addChild( converter.convertTo( property, returnValue ) );
+                    child = (AbstractNode) converter.convertTo( property, returnValue );
                 }
-
             }
             else if ( fieldValue.isEmpty() )
             {
@@ -191,14 +195,14 @@
                 {
                     Collection<?> collection = (Collection<?>) returnValue;
 
-                    CollectionNode collectionNode = complexNode.addChild( new CollectionNode( property.getCollectionName() ) );
-                    collectionNode.setNamespace( property.getNamespace() );
+                    child = complexNode.addChild( new CollectionNode( property.getCollectionName() ) );
+                    child.setNamespace( property.getNamespace() );
 
                     if ( property.isIdentifiableObject() )
                     {
                         for ( Object collectionObject : collection )
                         {
-                            collectionNode.addChild( getProperties( property, collectionObject, fields ) );
+                            child.addChild( getProperties( property, collectionObject, fields ) );
                         }
                     }
                     else if ( !property.isSimple() )
@@ -211,7 +215,7 @@
 
                             if ( !node.getChildren().isEmpty() )
                             {
-                                collectionNode.addChild( node );
+                                child.addChild( node );
                             }
                         }
                     }
@@ -219,13 +223,13 @@
                     {
                         for ( Object collectionObject : collection )
                         {
-                            collectionNode.addChild( new SimpleNode( property.getName(), collectionObject ) );
+                            child.addChild( new SimpleNode( property.getName(), collectionObject ) );
                         }
                     }
                 }
                 else if ( property.isIdentifiableObject() )
                 {
-                    complexNode.addChild( getProperties( property, returnValue, fields ) );
+                    child = getProperties( property, returnValue, fields );
                 }
                 else
                 {
@@ -235,12 +239,11 @@
                         simpleNode.setAttribute( property.isAttribute() );
                         simpleNode.setNamespace( property.getNamespace() );
 
-                        complexNode.addChild( simpleNode );
+                        child = simpleNode;
                     }
                     else
                     {
-                        complexNode.addChild( buildComplexNode( getFullFieldMap( propertySchema ), property.getKlass(),
-                            returnValue ) );
+                        child = buildComplexNode( getFullFieldMap( propertySchema ), property.getKlass(), returnValue );
                     }
                 }
             }
@@ -248,8 +251,8 @@
             {
                 if ( property.isCollection() )
                 {
-                    CollectionNode collectionNode = complexNode.addChild( new CollectionNode( property.getCollectionName() ) );
-                    collectionNode.setNamespace( property.getNamespace() );
+                    child = complexNode.addChild( new CollectionNode( property.getCollectionName() ) );
+                    child.setNamespace( property.getNamespace() );
 
                     for ( Object collectionObject : (Collection<?>) returnValue )
                     {
@@ -257,19 +260,24 @@
 
                         if ( !node.getChildren().isEmpty() )
                         {
-                            collectionNode.addChild( node );
+                            child.addChild( node );
                         }
                     }
                 }
                 else
                 {
-                    ComplexNode node = buildComplexNode( fieldValue, property.getKlass(), returnValue );
-
-                    if ( !node.getChildren().isEmpty() )
-                    {
-                        complexNode.addChild( node );
-                    }
-                }
+                    child = buildComplexNode( fieldValue, property.getKlass(), returnValue );
+                }
+            }
+
+            if ( child != null )
+            {
+                if ( fieldValue.getAlias() != null )
+                {
+                    child.setName( fieldValue.getAlias() );
+                }
+
+                complexNode.addChild( child );
             }
         }
 
@@ -288,6 +296,8 @@
         Schema schema = schemaService.getDynamicSchema( klass );
         List<String> cleanupFields = Lists.newArrayList();
 
+        Pattern pattern = Pattern.compile( "(\\w+)(?:::(\\w+))?(?:\\|rename\\((\\w+)\\))?" );
+
         for ( String fieldKey : Sets.newHashSet( fieldMap.keySet() ) )
         {
             if ( "*".equals( fieldKey ) )
@@ -327,21 +337,28 @@
             {
                 cleanupFields.add( fieldKey );
             }
-            else if ( fieldKey.contains( "::" ) )
+            else if ( fieldKey.contains( "::" ) || fieldKey.contains( "|rename(" ) )
             {
-                String[] split = fieldKey.split( "::" );
-
-                if ( split.length == 2 )
-                {
-                    FieldMap value = new FieldMap();
-
-                    if ( converters.containsKey( split[1] ) )
+                Matcher matcher = pattern.matcher( fieldKey );
+
+                if ( !matcher.find() )
+                {
+                    continue;
+                }
+
+                FieldMap value = new FieldMap();
+
+                if ( matcher.group( 2 ) != null )
+                {
+                    if ( converters.containsKey( matcher.group( 2 ) ) )
                     {
-                        value.setNodePropertyConverter( converters.get( split[1] ) );
-                        fieldMap.put( split[0], value );
+                        value.setNodePropertyConverter( converters.get( matcher.group( 2 ) ) );
                     }
                 }
 
+                value.setAlias( matcher.group( 3 ) );
+                fieldMap.put( matcher.group( 1 ), value );
+
                 cleanupFields.add( fieldKey );
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/FieldMap.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/FieldMap.java	2014-06-20 11:14:18 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/FieldMap.java	2014-06-25 11:27:12 +0000
@@ -44,6 +44,8 @@
 
     private NodePropertyConverter nodePropertyConverter;
 
+    private String alias;
+
     @Override
     protected Map<String, FieldMap> delegate()
     {
@@ -65,11 +67,22 @@
         return nodePropertyConverter != null;
     }
 
+    public String getAlias()
+    {
+        return alias;
+    }
+
+    public void setAlias( String alias )
+    {
+        this.alias = alias;
+    }
+
     @Override
     public String toString()
     {
         return Objects.toStringHelper( this )
             .add( "map", standardToString() )
+            .add( "alias", alias )
             .add( "nodePropertyConverter", nodePropertyConverter )
             .toString();
     }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/parser/DefaultParserService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/parser/DefaultParserService.java	2014-06-20 11:14:18 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/parser/DefaultParserService.java	2014-06-25 11:27:12 +0000
@@ -84,14 +84,14 @@
                 continue;
             }
 
-            if ( c.equals( "[" ) || c.equals( "(" ) || c.equals( "{" ) )
+            if ( c.equals( "[" ) )
             {
                 prefixList.add( builder.toString() );
                 builder = new StringBuilder();
                 continue;
             }
 
-            if ( c.equals( "]" ) || c.equals( ")" ) || c.equals( "}" ) )
+            if ( c.equals( "]" ) )
             {
                 if ( !builder.toString().isEmpty() )
                 {
@@ -103,7 +103,8 @@
                 continue;
             }
 
-            if ( StringUtils.isAlpha( c ) || c.equals( "*" ) || c.equals( ":" ) || c.equals( "!" ) )
+            if ( StringUtils.isAlpha( c ) || c.equals( "*" ) || c.equals( ":" ) || c.equals( "!" )
+                || c.equals( "|" ) || c.equals( "{" ) || c.equals( "}" ) || c.equals( "(" ) || c.equals( ")" ) )
             {
                 builder.append( c );
             }
@@ -124,7 +125,7 @@
         return prefixes;
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     private void putInMap( FieldMap fieldMap, String path )
     {
         if ( StringUtils.isEmpty( path ) )