← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19098: fixes to Schema introspector and setter methods, updates MergeService and adds tests

 

------------------------------------------------------------
revno: 19098
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-05-07 13:27:49 +0700
message:
  fixes to Schema introspector and setter methods, updates MergeService and adds tests
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/MergeServiceTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/merge/
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/merge/Simple.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.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-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2015-03-24 03:09:08 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2015-05-07 06:27:49 +0000
@@ -40,6 +40,7 @@
 
 import javax.annotation.PostConstruct;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -127,7 +128,7 @@
 
         schema = new Schema( klass, name, name + "s" );
         schema.setDisplayName( beautify( schema.getName() ) );
-        schema.setPropertyMap( Maps.newHashMap( propertyIntrospectorService.getPropertiesMap( schema.getKlass() ) ) );
+        schema.setPropertyMap( new HashMap<>( propertyIntrospectorService.getPropertiesMap( schema.getKlass() ) ) );
         schema.setMetadata( false );
 
         updateSelf( schema );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2015-03-31 06:22:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2015-05-07 06:27:49 +0000
@@ -89,10 +89,10 @@
 
         for ( Property property : properties )
         {
-            Method method = property.getGetterMethod();
-            JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
+            Method getterMethod = property.getGetterMethod();
+            JsonProperty jsonProperty = getterMethod.getAnnotation( JsonProperty.class );
 
-            String fieldName = getFieldName( method );
+            String fieldName = getFieldName( getterMethod );
             property.setName( !StringUtils.isEmpty( jsonProperty.value() ) ? jsonProperty.value() : fieldName );
 
             if ( property.getGetterMethod() != null )
@@ -133,15 +133,15 @@
                 property.setSetterMethod( hibernateProperty.getSetterMethod() );
             }
 
-            if ( method.isAnnotationPresent( Description.class ) )
+            if ( property.getGetterMethod().isAnnotationPresent( Description.class ) )
             {
-                Description description = method.getAnnotation( Description.class );
+                Description description = property.getGetterMethod().getAnnotation( Description.class );
                 property.setDescription( description.value() );
             }
 
-            if ( method.isAnnotationPresent( JacksonXmlProperty.class ) )
+            if ( property.getGetterMethod().isAnnotationPresent( JacksonXmlProperty.class ) )
             {
-                JacksonXmlProperty jacksonXmlProperty = method.getAnnotation( JacksonXmlProperty.class );
+                JacksonXmlProperty jacksonXmlProperty = getterMethod.getAnnotation( JacksonXmlProperty.class );
 
                 if ( StringUtils.isEmpty( jacksonXmlProperty.localName() ) )
                 {
@@ -160,7 +160,7 @@
                 property.setAttribute( jacksonXmlProperty.isAttribute() );
             }
 
-            Class<?> returnType = method.getReturnType();
+            Class<?> returnType = property.getGetterMethod().getReturnType();
             property.setKlass( Primitives.wrap( returnType ) );
 
             if ( Collection.class.isAssignableFrom( returnType ) )
@@ -168,7 +168,7 @@
                 property.setCollection( true );
                 property.setCollectionName( property.getName() );
 
-                Type type = method.getGenericReturnType();
+                Type type = property.getGetterMethod().getGenericReturnType();
 
                 if ( ParameterizedType.class.isInstance( type ) )
                 {
@@ -202,9 +202,9 @@
 
             if ( property.isCollection() )
             {
-                if ( method.isAnnotationPresent( JacksonXmlElementWrapper.class ) )
+                if ( property.getGetterMethod().isAnnotationPresent( JacksonXmlElementWrapper.class ) )
                 {
-                    JacksonXmlElementWrapper jacksonXmlElementWrapper = method.getAnnotation( JacksonXmlElementWrapper.class );
+                    JacksonXmlElementWrapper jacksonXmlElementWrapper = getterMethod.getAnnotation( JacksonXmlElementWrapper.class );
                     property.setCollectionWrapping( jacksonXmlElementWrapper.useWrapping() );
 
                     // TODO what if element-wrapper have different namespace?
@@ -261,18 +261,25 @@
         return StringUtils.uncapitalize( name );
     }
 
-    private static List<Property> collectProperties( Class<?> klass )
+    private List<Property> collectProperties( Class<?> klass )
     {
-        List<Method> allMethods = ReflectionUtils.getAllMethods( klass );
+        Map<String, Method> methodMap = ReflectionUtils.getMethodMap( klass );
         List<Property> properties = Lists.newArrayList();
 
-        for ( Method method : allMethods )
+        for ( Method method : methodMap.values() )
         {
             if ( method.isAnnotationPresent( JsonProperty.class ) )
             {
                 if ( method.getGenericParameterTypes().length == 0 )
                 {
-                    properties.add( new Property( klass, method, null ) );
+                    String fieldName = getFieldName( method );
+                    String setterName = "set" + StringUtils.capitalize( fieldName );
+
+                    Property property = new Property( klass, method, null );
+                    property.setFieldName( fieldName );
+                    property.setSetterMethod( methodMap.get( setterName ) );
+
+                    properties.add( property );
                 }
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java	2015-03-27 15:45:41 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java	2015-05-07 06:27:49 +0000
@@ -28,8 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.Collection;
-
 import org.hisp.dhis.common.MergeStrategy;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
@@ -37,6 +35,8 @@
 import org.hisp.dhis.system.util.ReflectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.Collection;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -81,20 +81,31 @@
                         // one-to-many
                     }
                 }
+                else
+                {
+                    if ( property.isManyToMany() )
+                    {
+                        Schema owningSchema = schemaService.getDynamicSchema( property.getItemKlass() );
+                    }
+                    else
+                    {
+                        // one-to-many
+                    }
+                }
 
                 ReflectionUtils.invokeMethod( source, property.getSetterMethod(), sourceObject );
             }
             else
             {
-                Object targetObject = ReflectionUtils.invokeMethod( target, property.getGetterMethod() );
+                Object sourceObject = ReflectionUtils.invokeMethod( source, property.getGetterMethod() );
 
                 if ( mergeStrategy.isReplace() )
                 {
-                    ReflectionUtils.invokeMethod( source, property.getSetterMethod(), targetObject );
+                    ReflectionUtils.invokeMethod( target, property.getSetterMethod(), sourceObject );
                 }
-                else if ( mergeStrategy.isMerge() && targetObject != null )
+                else if ( mergeStrategy.isMerge() && sourceObject != null )
                 {
-                    ReflectionUtils.invokeMethod( source, property.getSetterMethod(), targetObject );
+                    ReflectionUtils.invokeMethod( target, property.getSetterMethod(), sourceObject );
                 }
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java	2015-03-26 11:50:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java	2015-05-07 06:27:49 +0000
@@ -35,5 +35,12 @@
  */
 public interface MergeService
 {
+    /**
+     * Merges source object into target object, requires a "schema friendly" class.
+     *
+     * @param source        Source object to merge from
+     * @param target        Target object to merge into
+     * @param mergeStrategy Strategy to use
+     */
     <T> void merge( T source, T target, MergeStrategy mergeStrategy );
 }

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/MergeServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/MergeServiceTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/MergeServiceTest.java	2015-05-07 06:27:49 +0000
@@ -0,0 +1,69 @@
+package org.hisp.dhis.dxf2.metadata;
+
+/*
+ * Copyright (c) 2004-2015, 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.DhisSpringTest;
+import org.hisp.dhis.common.MergeStrategy;
+import org.hisp.dhis.dxf2.metadata.merge.Simple;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Date;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class MergeServiceTest
+    extends DhisSpringTest
+{
+    @Autowired
+    private MergeService mergeService;
+
+    @Override
+    public void setUpTest()
+    {
+
+    }
+
+    @Test
+    public void simpleReplace()
+    {
+        Date date = new Date();
+        Simple source = new Simple( "string", 10, date, false );
+        Simple target = new Simple();
+
+        mergeService.merge( source, target, MergeStrategy.REPLACE );
+
+        Assert.assertEquals( "string", target.getString() );
+        Assert.assertEquals( 10, (int) target.getInteger() );
+        Assert.assertEquals( date, target.getDate() );
+        Assert.assertEquals( false, target.getBool() );
+    }
+}

=== added directory 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/merge'
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/merge/Simple.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/merge/Simple.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata/merge/Simple.java	2015-05-07 06:27:49 +0000
@@ -0,0 +1,108 @@
+package org.hisp.dhis.dxf2.metadata.merge;
+
+/*
+ * Copyright (c) 2004-2015, 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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+
+import java.util.Date;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class Simple
+{
+    private String string;
+
+    private Integer integer;
+
+    private Date date;
+
+    private Boolean bool;
+
+    public Simple()
+    {
+    }
+
+    public Simple( String string, Integer integer, Date date, Boolean bool )
+    {
+        this.string = string;
+        this.integer = integer;
+        this.date = date;
+        this.bool = bool;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty
+    public String getString()
+    {
+        return string;
+    }
+
+    public void setString( String string )
+    {
+        this.string = string;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty
+    public Integer getInteger()
+    {
+        return integer;
+    }
+
+    public void setInteger( Integer integer )
+    {
+        this.integer = integer;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty
+    public Date getDate()
+    {
+        return date;
+    }
+
+    public void setDate( Date date )
+    {
+        this.date = date;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty
+    public Boolean getBool()
+    {
+        return bool;
+    }
+
+    public void setBool( Boolean bool )
+    {
+        this.bool = bool;
+    }
+}

=== 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	2015-02-22 20:02:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2015-05-07 06:27:49 +0000
@@ -28,7 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
+import javassist.util.proxy.ProxyFactory;
+import org.hibernate.collection.spi.PersistentCollection;
+import org.hisp.dhis.system.util.functional.Function1;
+import org.hisp.dhis.system.util.functional.Predicate;
+import org.springframework.util.StringUtils;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
@@ -40,16 +44,13 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
-import javassist.util.proxy.ProxyFactory;
-
-import org.hibernate.collection.spi.PersistentCollection;
-import org.hisp.dhis.system.util.functional.Function1;
-import org.hisp.dhis.system.util.functional.Predicate;
-import org.springframework.util.StringUtils;
+import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
 
 /**
  * @author Lars Helge Overland
@@ -419,6 +420,19 @@
         return null;
     }
 
+    public static Map<String, Method> getMethodMap( Class<?> klass )
+    {
+        Map<String, Method> methodMap = new HashMap<>();
+        List<Method> methods = getAllMethods( klass );
+
+        for ( Method method : methods )
+        {
+            methodMap.put( method.getName(), method );
+        }
+
+        return methodMap;
+    }
+
     public static List<Method> getAllMethods( Class<?> clazz )
     {
         Class<?> searchType = clazz;