dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #16845
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6521: Added more utils to ReflectionUtils, will be used for auto-getting references on IdentifiableObje...
------------------------------------------------------------
revno: 6521
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-04-07 14:36:39 +0200
message:
Added more utils to ReflectionUtils, will be used for auto-getting references on IdentifiableObject Collections.
added:
dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/IndicatorImporter.java
modified:
dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java
dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractIdentifiableObjectImporter.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-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java 2012-04-07 09:40:09 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java 2012-04-07 12:36:39 +0000
@@ -111,6 +111,7 @@
// Imports.. this could be made even more generic, just need to make sure that everything is imported in
// the correct order
doImport( dxf2.getConstants(), importOptions, importSummary );
+ doImport( dxf2.getIndicators(), importOptions, importSummary );
return importSummary;
}
=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractIdentifiableObjectImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractIdentifiableObjectImporter.java 2012-04-07 09:17:15 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractIdentifiableObjectImporter.java 2012-04-07 12:36:39 +0000
@@ -38,9 +38,12 @@
import org.hisp.dhis.dxf2.metadata.IdScheme;
import org.hisp.dhis.dxf2.metadata.ImportOptions;
import org.hisp.dhis.dxf2.metadata.Importer;
+import org.hisp.dhis.system.util.ReflectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
+import java.lang.reflect.Field;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -95,13 +98,17 @@
*/
protected ImportConflict newObject( T object, ImportOptions options )
{
- if ( !options.isDryRun() )
+ if ( options.isDryRun() )
{
- log.info( "Trying to save new object with UID: " + object.getUid() );
- manager.save( object );
- log.info( "Save successful." );
- updateIdMaps( object );
+ return null;
}
+ log.info( "Trying to save new object with UID: " + object.getUid() );
+
+ findAndUpdateCollections( object );
+ //manager.save( object );
+ //updateIdMaps( object );
+
+ log.info( "Save successful." );
return null;
}
@@ -116,19 +123,48 @@
*/
protected ImportConflict updatedObject( T object, T oldObject, ImportOptions options )
{
- oldObject.mergeWith( object );
-
- if ( !options.isDryRun() )
+ if ( options.isDryRun() )
{
- log.info( "Trying to update object with UID: " + oldObject.getUid() );
- manager.update( oldObject );
- log.info( "Update successful." );
-
+ return null;
}
+ log.info( "Trying to update object with UID: " + oldObject.getUid() );
+
+ findAndUpdateCollections( object );
+ // oldObject.mergeWith( object );
+ // manager.update( oldObject );
+
+ log.info( "Update successful." );
+
return null;
}
+ private void findAndUpdateCollections( T object )
+ {
+ Field[] fields = object.getClass().getDeclaredFields();
+
+ for ( Field field : fields )
+ {
+ if(ReflectionUtils.isType( field, IdentifiableObject.class ))
+ {
+ IdentifiableObject identifiableObject = ReflectionUtils.invokeGetterMethod( field.getName(), object );
+ // we now have the identifiableObject, and can make sure that the reference is OK
+ log.info( identifiableObject );
+ }
+ else
+ {
+ boolean b = ReflectionUtils.isCollection( field.getName(), object, IdentifiableObject.class );
+
+ if ( b )
+ {
+ Collection<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod( field.getName(), object );
+ // we now have the collection, and can make sure that references are OK
+ log.info( identifiableObjects );
+ }
+ }
+ }
+ }
+
/**
* Current object name, used to fill name part of a ImportConflict
*
=== added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/IndicatorImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/IndicatorImporter.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/IndicatorImporter.java 2012-04-07 12:36:39 +0000
@@ -0,0 +1,56 @@
+package org.hisp.dhis.dxf2.metadata.importers;
+
+/*
+ * Copyright (c) 2012, 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.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.constant.Constant;
+import org.hisp.dhis.indicator.Indicator;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@Component
+public class IndicatorImporter
+ extends AbstractIdentifiableObjectImporter<Indicator>
+{
+ private static final Log log = LogFactory.getLog( IndicatorImporter.class );
+
+ @Override
+ protected String getObjectName()
+ {
+ return this.getClass().getName();
+ }
+
+ @Override
+ public boolean canHandle( Class<?> clazz )
+ {
+ return Indicator.class.equals( clazz );
+ }
+}
=== 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 2012-01-19 11:43:06 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java 2012-04-07 12:36:39 +0000
@@ -27,7 +27,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import java.lang.reflect.Method;
+import org.springframework.util.StringUtils;
+
+import java.lang.reflect.*;
import java.util.Collection;
/**
@@ -35,6 +37,8 @@
*/
public class ReflectionUtils
{
+
+
/**
* Invokes method getId() for this object and returns the return value. An int
* return type is expected. If the operation fails -1 is returned.
@@ -49,8 +53,7 @@
Method method = object.getClass().getMethod( "getId" );
return (Integer) method.invoke( object );
- }
- catch ( Exception ex )
+ } catch ( Exception ex )
{
return -1;
}
@@ -59,7 +62,7 @@
/**
* Fetch a property off the object. Returns null if the operation fails.
*
- * @param object the object.
+ * @param object the object.
* @param property name of the property to get.
* @return the value of the property or null.
*/
@@ -72,9 +75,8 @@
Method method = object.getClass().getMethod( "get" + property );
return (String) method.invoke( object );
- }
- catch ( Exception ex )
- {
+ } catch ( Exception ex )
+ {
return null;
}
}
@@ -84,14 +86,14 @@
* if the operation fails.
*
* @param object Object to modify
- * @param name Name of property to set
- * @param value Value the property will be set to
+ * @param name Name of property to set
+ * @param value Value the property will be set to
*/
public static void setProperty( Object object, String name, String value )
{
- Object[] arguments = new Object[] { value };
+ Object[] arguments = new Object[]{value};
- Class<?>[] parameterTypes = new Class<?>[] { String.class };
+ Class<?>[] parameterTypes = new Class<?>[]{String.class};
if ( name.length() > 0 )
{
@@ -100,10 +102,9 @@
try
{
Method concatMethod = object.getClass().getMethod( name, parameterTypes );
-
+
concatMethod.invoke( object, arguments );
- }
- catch ( Exception ex )
+ } catch ( Exception ex )
{
throw new UnsupportedOperationException( "Failed to set property", ex );
}
@@ -114,15 +115,15 @@
* Sets a property for the supplied object. Throws an UnsupportedOperationException
* if the operation fails.
*
- * @param object Object to modify
- * @param namePrefix prefix of the property name to set
- * @param name Name of property to set
- * @param value Value the property will be set to
+ * @param object Object to modify
+ * @param namePrefix prefix of the property name to set
+ * @param name Name of property to set
+ * @param value Value the property will be set to
*/
public static void setProperty( Object object, String namePrefix, String name, String value )
{
String prefixed = namePrefix + name.substring( 0, 1 ).toUpperCase() + name.substring( 1, name.length() );
-
+
setProperty( object, prefixed, value );
}
@@ -140,20 +141,100 @@
/**
* Test whether the object is an array or a Collection.
- *
+ *
* @param value the object.
* @return true if the object is an array or a Collection, false otherwise.
*/
public static boolean isCollection( Object value )
{
if ( value != null )
- {
+ {
if ( value.getClass().isArray() || value instanceof Collection<?> )
{
return true;
}
}
-
+
+ return false;
+ }
+
+ public static boolean isCollection( String fieldName, Object object, Class<?> type )
+ {
+ Field field = null;
+
+ try
+ {
+ field = object.getClass().getDeclaredField( fieldName );
+ } catch ( NoSuchFieldException e )
+ {
+ return false;
+ }
+
+ try
+ {
+ ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
+ Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
+
+ if ( actualTypeArguments.length > 0 )
+ {
+ if ( type.isAssignableFrom( (Class<?>) actualTypeArguments[0] ) )
+ {
+ return true;
+ }
+ }
+
+ } catch ( ClassCastException e )
+ {
+ return false;
+ }
+
+ return false;
+ }
+
+ public static Method findGetterMethod( String fieldName, Object object )
+ {
+ try
+ {
+ return object.getClass().getMethod( "get" + StringUtils.capitalize( fieldName ) );
+ } catch ( NoSuchMethodException e )
+ {
+ return null;
+ }
+ }
+
+ public static <T> T invokeGetterMethod( String fieldName, Object object )
+ {
+ Method method = findGetterMethod( fieldName, object );
+
+ if ( method == null )
+ {
+ return null;
+ }
+
+ try
+ {
+ return (T) method.invoke( object );
+ } catch ( ClassCastException e )
+ {
+ return null;
+ } catch ( InvocationTargetException e )
+ {
+ return null;
+ } catch ( IllegalAccessException e )
+ {
+ return null;
+ }
+ }
+
+ public static boolean isType(Field field, Class<?> clazz)
+ {
+ Class<?> type = field.getType();
+
+ if(clazz.isAssignableFrom( type ))
+ {
+ return true;
+ }
+
return false;
}
}