dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #16926
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6576: Improved importer. Collections are properly collected and auto-set.
------------------------------------------------------------
revno: 6576
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-04-14 19:20:11 +0300
message:
Improved importer. Collections are properly collected and auto-set.
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroup.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSet.java
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/DefaultImporter.java
dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.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-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java 2012-04-06 21:31:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java 2012-04-14 16:20:11 +0000
@@ -98,7 +98,7 @@
this.name = name;
}
- public BaseIdentifiableObject(IdentifiableObject identifiableObject)
+ public BaseIdentifiableObject( IdentifiableObject identifiableObject )
{
this.id = identifiableObject.getId();
this.uid = identifiableObject.getUid();
@@ -121,7 +121,7 @@
// -------------------------------------------------------------------------
@JsonProperty( value = "internalId" )
- @JsonView( { DetailedView.class, IdentifiableObjectView.class, ExportView.class } )
+ @JsonView( {DetailedView.class, IdentifiableObjectView.class, ExportView.class} )
@JacksonXmlProperty( isAttribute = true )
public int getId()
{
@@ -146,7 +146,7 @@
}
@JsonProperty
- @JsonView( { DetailedView.class, IdentifiableObjectView.class, ExportView.class } )
+ @JsonView( {DetailedView.class, IdentifiableObjectView.class, ExportView.class} )
@JacksonXmlProperty( isAttribute = true )
public String getCode()
{
@@ -154,12 +154,12 @@
}
public void setCode( String code )
- {
+ {
this.code = code;
}
@JsonProperty
- @JsonView( { DetailedView.class, IdentifiableObjectView.class, ExportView.class } )
+ @JsonView( {DetailedView.class, IdentifiableObjectView.class, ExportView.class} )
@JacksonXmlProperty( isAttribute = true )
public String getName()
{
@@ -172,7 +172,7 @@
}
@JsonProperty
- @JsonView( { DetailedView.class, IdentifiableObjectView.class, ExportView.class } )
+ @JsonView( {DetailedView.class, IdentifiableObjectView.class, ExportView.class} )
@JacksonXmlProperty( isAttribute = true )
public Date getLastUpdated()
{
@@ -194,6 +194,32 @@
this.displayName = displayName;
}
+ @Override
+ public boolean equals( Object o )
+ {
+ if ( this == o ) return true;
+ if ( o == null || getClass() != o.getClass() ) return false;
+
+ BaseIdentifiableObject that = (BaseIdentifiableObject) o;
+
+ if ( code != null ? !code.equals( that.code ) : that.code != null ) return false;
+ if ( name != null ? !name.equals( that.name ) : that.name != null ) return false;
+ if ( uid != null ? !uid.equals( that.uid ) : that.uid != null ) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int result = uid != null ? uid.hashCode() : 0;
+ result = 31 * result + (code != null ? code.hashCode() : 0);
+ result = 31 * result + (name != null ? name.hashCode() : 0);
+ result = 31 * result + (lastUpdated != null ? lastUpdated.hashCode() : 0);
+
+ return result;
+ }
+
// -------------------------------------------------------------------------
// Logic
// -------------------------------------------------------------------------
@@ -213,7 +239,7 @@
/**
* Get a map of uids to internal identifiers
- *
+ *
* @param objects the IdentifiableObjects to put in the map
* @return the map
*/
@@ -234,7 +260,7 @@
/**
* Get a map of codes to internal identifiers
- *
+ *
* @param objects the NameableObjects to put in the map
* @return the map
*/
@@ -253,7 +279,7 @@
/**
* Get a map of names to internal identifiers
- *
+ *
* @param objects the NameableObjects to put in the map
* @return the map
*/
@@ -273,14 +299,8 @@
@Override
public String toString()
{
- return "IdentifiableObject{" +
- "id=" + id +
- ", uid='" + uid + '\'' +
- ", code='" + code + '\'' +
- ", name='" + name + '\'' +
- ", lastUpdated=" + lastUpdated +
- ", displayName='" + displayName + '\'' +
- '}';
+ return "{" + "id=" + id + ", uid='" + uid + '\'' + ", code='" +
+ code + '\'' + ", name='" + name + '\'' + ", lastUpdated=" + lastUpdated + "} ";
}
@Override
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java 2012-04-06 21:31:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java 2012-04-14 16:20:11 +0000
@@ -90,6 +90,12 @@
this.description = description;
}
+ @Override
+ public String toString()
+ {
+ return super.toString();
+ }
+
@JsonProperty
@JsonView( {DetailedView.class, ExportView.class} )
@JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java 2012-04-11 04:56:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java 2012-04-14 16:20:11 +0000
@@ -500,12 +500,6 @@
// -------------------------------------------------------------------------
@Override
- public int hashCode()
- {
- return name.hashCode();
- }
-
- @Override
public boolean equals( Object o )
{
if ( this == o )
@@ -531,7 +525,7 @@
@Override
public String toString()
{
- return "[" + name + "]";
+ return super.toString();
}
// -------------------------------------------------------------------------
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroup.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroup.java 2012-04-10 21:41:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroup.java 2012-04-14 16:20:11 +0000
@@ -121,12 +121,6 @@
// -------------------------------------------------------------------------
@Override
- public int hashCode()
- {
- return name.hashCode();
- }
-
- @Override
public boolean equals( Object o )
{
if ( this == o )
@@ -152,7 +146,10 @@
@Override
public String toString()
{
- return "[" + name + "]";
+ return "OrganisationUnitGroup{" +
+ "members=" + members +
+ ", groupSet=" + groupSet +
+ "} " + super.toString();
}
// -------------------------------------------------------------------------
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSet.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSet.java 2012-04-11 04:56:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSet.java 2012-04-14 16:20:11 +0000
@@ -150,12 +150,6 @@
// -------------------------------------------------------------------------
@Override
- public int hashCode()
- {
- return name.hashCode();
- }
-
- @Override
public boolean equals( Object o )
{
if ( this == o )
@@ -181,7 +175,11 @@
@Override
public String toString()
{
- return "[" + name + "]";
+ return "OrganisationUnitGroupSet{" +
+ "organisationUnitGroups=" + organisationUnitGroups +
+ ", description='" + description + '\'' +
+ ", compulsory=" + compulsory +
+ "} " + super.toString();
}
// -------------------------------------------------------------------------
=== 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-13 14:11:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java 2012-04-14 16:20:11 +0000
@@ -102,9 +102,9 @@
*/
doImport( metaData.getOrganisationUnits(), importOptions, importSummary );
-
-/*
doImport( metaData.getOrganisationUnitGroups(), importOptions, importSummary );
+
+/*
doImport( metaData.getOrganisationUnitGroupSets(), importOptions, importSummary );
doImport( metaData.getSqlViews(), importOptions, importSummary );
doImport( metaData.getUsers(), importOptions, importSummary );
=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultImporter.java 2012-04-13 14:11:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultImporter.java 2012-04-14 16:20:11 +0000
@@ -40,6 +40,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.util.*;
/**
@@ -120,12 +121,14 @@
log.info( "Trying to save new object with UID: " + object.getUid() );
- findAndUpdateReferences( object );
- manager.save( object );
+ saveOrUpdateObjectWithReferences( object, false );
+ manager.update( object );
updateIdMaps( object );
log.info( "Save successful." );
+ log.info( object );
+
return null;
}
@@ -146,12 +149,14 @@
log.info( "Starting update of object " + getDisplayName( oldObject ) + " (" + oldObject.getClass().getSimpleName() + ")" );
- findAndUpdateReferences( object );
+ saveOrUpdateObjectWithReferences( object, true );
oldObject.mergeWith( object );
manager.update( oldObject );
log.info( "Update successful." );
+ log.info( object );
+
return null;
}
@@ -589,12 +594,14 @@
return match;
}
- private void findAndUpdateReferences( T object )
+ private void saveOrUpdateObjectWithReferences( T object, boolean update )
{
Field[] fields = object.getClass().getDeclaredFields();
log.info( "-> Finding and updating references." );
+ Map<String, Set<? extends IdentifiableObject>> collectedCollections = new HashMap<String, Set<? extends IdentifiableObject>>();
+
for ( Field field : fields )
{
if ( ReflectionUtils.isType( field, IdentifiableObject.class ) )
@@ -629,33 +636,43 @@
if ( b )
{
- Collection<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod( field.getName(), object );
+ Collection<IdentifiableObject> objects = ReflectionUtils.invokeGetterMethod( field.getName(), object );
- if ( identifiableObjects != null && !identifiableObjects.isEmpty() )
+ if ( objects != null && !objects.isEmpty() )
{
- for ( IdentifiableObject identifiableObject : identifiableObjects )
- {
- if ( Period.class.isAssignableFrom( identifiableObject.getClass() ) )
- {
- // FIXME
- log.info( "Skipping Period.class" );
- continue;
- }
-
- IdentifiableObject ref = findObjectByReference( identifiableObject );
-
- if ( ref != null )
- {
- ReflectionUtils.invokeSetterMethod( field.getName(), object, ref );
- }
- else
- {
- log.info( "--> Ignored reference " + getDisplayName( identifiableObject ) + "." );
- }
- }
+ Set<IdentifiableObject> identifiableObjects = new HashSet<IdentifiableObject>( objects );
+ collectedCollections.put( field.getName(), identifiableObjects );
+ objects.clear();
}
}
}
}
+
+ if ( !update )
+ {
+ manager.save( object );
+ }
+
+ for ( String collectionKey : collectedCollections.keySet() )
+ {
+ Collection<? extends IdentifiableObject> identifiableObjects = collectedCollections.get( collectionKey );
+ Collection<IdentifiableObject> objects = new HashSet<IdentifiableObject>();
+
+ for ( IdentifiableObject identifiableObject : identifiableObjects )
+ {
+ IdentifiableObject ref = findObjectByReference( identifiableObject );
+
+ if ( ref != null )
+ {
+ objects.add( ref );
+ }
+ else
+ {
+ log.info( "--> Ignored reference " + getDisplayName( identifiableObject ) + "." );
+ }
+ }
+
+ ReflectionUtils.invokeSetterMethod( collectionKey, object, objects );
+ }
}
}
=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java 2012-03-30 11:04:07 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java 2012-04-14 16:20:11 +0000
@@ -57,7 +57,7 @@
static
{
- ObjectMapper[] objectMappers = new ObjectMapper[]{ jsonMapper, xmlMapper };
+ ObjectMapper[] objectMappers = new ObjectMapper[]{jsonMapper, xmlMapper};
for ( ObjectMapper objectMapper : objectMappers )
{
@@ -130,7 +130,7 @@
return jsonMapper.writerWithView( viewClass ).writeValueAsString( value );
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
public static <T> T fromJson( InputStream input, Class<?> clazz ) throws IOException
{
return (T) jsonMapper.readValue( input, clazz );
@@ -160,7 +160,7 @@
return xmlMapper.writerWithView( viewClass ).writeValueAsString( value );
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
public static <T> T fromXml( InputStream input, Class<?> clazz ) throws IOException
{
return (T) xmlMapper.readValue( input, 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-04-14 06:48:32 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java 2012-04-14 16:20:11 +0000
@@ -27,6 +27,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;
import java.lang.reflect.*;
@@ -37,11 +39,12 @@
*/
public class ReflectionUtils
{
+ private static final Log log = LogFactory.getLog( ReflectionUtils.class );
/**
* Invokes method getId() for this object and returns the return value. An
* int return type is expected. If the operation fails -1 is returned.
- *
+ *
* @param object object to call getId() on.
* @return The identifier.
*/
@@ -52,8 +55,7 @@
Method method = object.getClass().getMethod( "getId" );
return (Integer) method.invoke( object );
- }
- catch ( Exception ex )
+ } catch ( Exception ex )
{
return -1;
}
@@ -61,8 +63,8 @@
/**
* 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.
*/
@@ -75,8 +77,7 @@
Method method = object.getClass().getMethod( "get" + property );
return (String) method.invoke( object );
- }
- catch ( Exception ex )
+ } catch ( Exception ex )
{
return null;
}
@@ -85,16 +86,16 @@
/**
* Sets a property for the supplied object. Throws an
* UnsupportedOperationException 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 )
{
@@ -105,8 +106,7 @@
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 );
}
@@ -116,11 +116,11 @@
/**
* Sets a property for the supplied object. Throws an
* UnsupportedOperationException if the operation fails.
- *
- * @param object Object to modify
+ *
+ * @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 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 )
{
@@ -132,7 +132,7 @@
/**
* Returns the name of the class that the object is an instance of
* org.hisp.dhis.indicator.Indicactor returns Indicator.
- *
+ *
* @param object object to determine className for.
* @return String containing the class name.
*/
@@ -143,7 +143,7 @@
/**
* 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.
*/
@@ -167,8 +167,7 @@
try
{
field = object.getClass().getDeclaredField( fieldName );
- }
- catch ( NoSuchFieldException e )
+ } catch ( NoSuchFieldException e )
{
return false;
}
@@ -189,8 +188,7 @@
}
}
- }
- catch ( ClassCastException e )
+ } catch ( ClassCastException e )
{
return false;
}
@@ -198,34 +196,54 @@
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 Method findSetterMethod( String fieldName, Object object )
- {
- try
- {
- return object.getClass().getMethod( "set" + StringUtils.capitalize( fieldName ) );
- }
- catch ( NoSuchMethodException e )
- {
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
+ public static Method findGetterMethod( String fieldName, Object object, Class<?>... classes )
+ {
+ try
+ {
+ return object.getClass().getMethod( "get" + StringUtils.capitalize( fieldName ), classes );
+ } catch ( NoSuchMethodException e )
+ {
+ log.info( "Getter method was not found for fieldName: " + fieldName );
+ return null;
+ }
+ }
+
+ public static Method findSetterMethod( String fieldName, Object object, Class<?>... classes )
+ {
+ Method method = null;
+
+ try
+ {
+ method = object.getClass().getMethod( "set" + StringUtils.capitalize( fieldName ), classes );
+ } catch ( NoSuchMethodException e )
+ {
+ }
+
+ // if parameter classes was not given, we will retry using the type of the field
+ if ( method == null && classes.length == 0 )
+ {
+ try
+ {
+ Field field = object.getClass().getDeclaredField( fieldName );
+ method = findSetterMethod( fieldName, object, field.getType() );
+ } catch ( NoSuchFieldException e )
+ {
+ }
+ }
+
+ if ( method == null )
+ {
+ log.info( "Setter method was not found for fieldName: " + fieldName );
+ }
+
+ return method;
+ }
+
+ @SuppressWarnings( "unchecked" )
public static <T> T invokeGetterMethod( String fieldName, Object object )
{
Method method = findGetterMethod( fieldName, object );
+ log.info( method );
if ( method == null )
{
@@ -235,21 +253,22 @@
try
{
return (T) method.invoke( object );
- }
- catch ( InvocationTargetException e )
+ } catch ( InvocationTargetException e )
{
+ log.info( "InvocationTargetException for fieldName: " + fieldName );
return null;
- }
- catch ( IllegalAccessException e )
+ } catch ( IllegalAccessException e )
{
+ log.info( "IllegalAccessException for fieldName: " + fieldName );
return null;
}
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
public static <T> T invokeSetterMethod( String fieldName, Object object, Object... objects )
{
Method method = findSetterMethod( fieldName, object );
+ log.info( method );
if ( method == null )
{
@@ -259,13 +278,13 @@
try
{
return (T) method.invoke( object, objects );
- }
- catch ( InvocationTargetException e )
+ } catch ( InvocationTargetException e )
{
+ log.info( "InvocationTargetException for fieldName: " + fieldName );
return null;
- }
- catch ( IllegalAccessException e )
+ } catch ( IllegalAccessException e )
{
+ log.info( "IllegalAccessException for fieldName: " + fieldName );
return null;
}
}