=== modified file 'dhis-2/dhis-api/pom.xml' --- dhis-2/dhis-api/pom.xml 2014-04-25 10:44:39 +0000 +++ dhis-2/dhis-api/pom.xml 2014-05-30 15:30:58 +0000 @@ -83,6 +83,10 @@ org.hibernate hibernate-validator + + org.javassist + javassist + === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditLogUtil.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditLogUtil.java 2014-03-18 08:10:10 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditLogUtil.java 2014-05-30 15:30:58 +0000 @@ -28,6 +28,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import javassist.util.proxy.ProxyFactory; import org.apache.commons.logging.Log; public class AuditLogUtil @@ -52,7 +53,15 @@ StringBuilder builder = new StringBuilder(); builder.append( "'" ).append( username ).append( "' " ).append( action ); - builder.append( " " ).append( object.getClass().getName() ); + + if ( !ProxyFactory.isProxyClass( object.getClass() ) ) + { + builder.append( " " ).append( object.getClass().getName() ); + } + else + { + builder.append( " " ).append( object.getClass().getSuperclass().getName() ); + } if ( idObject.getName() != null && !idObject.getName().isEmpty() ) { === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java 2014-05-28 19:02:46 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java 2014-05-30 15:30:58 +0000 @@ -177,8 +177,6 @@ ImportTypeSummary importTypeSummary = doImport( user, objects, importOptions ); - sessionFactory.getCurrentSession().flush(); - if ( importTypeSummary != null ) { importSummary.getImportTypeSummaries().add( importTypeSummary ); === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2014-05-30 08:45:46 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2014-05-30 15:30:58 +0000 @@ -28,6 +28,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,7 +39,6 @@ import org.hisp.dhis.attribute.AttributeValue; import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.common.IdentifiableObject; -import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.NameableObject; import org.hisp.dhis.dataelement.DataElementOperand; import org.hisp.dhis.dataelement.DataElementOperandService; @@ -53,6 +53,7 @@ import org.hisp.dhis.dxf2.metadata.ObjectBridge; import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandler; import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandlerUtils; +import org.hisp.dhis.eventreport.EventReport; import org.hisp.dhis.expression.Expression; import org.hisp.dhis.expression.ExpressionService; import org.hisp.dhis.period.Period; @@ -60,10 +61,10 @@ import org.hisp.dhis.period.PeriodType; import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.program.ProgramStageDataElement; +import org.hisp.dhis.program.ProgramStageDataElementService; import org.hisp.dhis.program.ProgramTrackedEntityAttribute; -import org.hisp.dhis.system.util.CollectionUtils; +import org.hisp.dhis.program.ProgramValidation; import org.hisp.dhis.system.util.ReflectionUtils; -import org.hisp.dhis.system.util.functional.Function1; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.user.User; @@ -72,8 +73,6 @@ import java.lang.reflect.Field; import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -111,7 +110,7 @@ private DataElementOperandService dataElementOperandService; @Autowired - private IdentifiableObjectManager manager; + private ProgramStageDataElementService programStageDataElementService; @Autowired private ObjectBridge objectBridge; @@ -263,17 +262,17 @@ private Set extractDataElementOperands( T object, String fieldName ) { - Set dataElementOperands = new HashSet(); + Set dataElementOperands = Sets.newHashSet(); if ( ReflectionUtils.findGetterMethod( fieldName, object ) != null ) { Set detachedDataElementOperands = ReflectionUtils.invokeGetterMethod( fieldName, object ); - dataElementOperands = new HashSet( detachedDataElementOperands ); + dataElementOperands = Sets.newHashSet( detachedDataElementOperands ); if ( detachedDataElementOperands.size() > 0 ) { detachedDataElementOperands.clear(); - ReflectionUtils.invokeSetterMethod( fieldName, object, new HashSet() ); + ReflectionUtils.invokeSetterMethod( fieldName, object, Sets.newHashSet() ); } } @@ -282,7 +281,7 @@ private Set extractAttributeValues( T object ) { - Set attributeValues = new HashSet(); + Set attributeValues = Sets.newHashSet(); if ( ReflectionUtils.findGetterMethod( "attributeValues", object ) != null ) { @@ -290,7 +289,7 @@ if ( attributeValues.size() > 0 ) { - ReflectionUtils.invokeSetterMethod( "attributeValues", object, new HashSet() ); + ReflectionUtils.invokeSetterMethod( "attributeValues", object, Sets.newHashSet() ); } } @@ -351,14 +350,10 @@ { Set attributeValues = extractAttributeValues( object ); - CollectionUtils.forEach( attributeValues, new Function1() + for ( AttributeValue attributeValue : attributeValues ) { - @Override - public void apply( AttributeValue attributeValue ) - { - attributeService.deleteAttributeValue( attributeValue ); - } - } ); + attributeService.deleteAttributeValue( attributeValue ); + } } } @@ -366,23 +361,19 @@ { if ( attributeValues.size() > 0 ) { - CollectionUtils.forEach( attributeValues, new Function1() + for ( AttributeValue attributeValue : attributeValues ) { - @Override - public void apply( AttributeValue attributeValue ) + Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() ); + + if ( attribute == null ) { - Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() ); - - if ( attribute == null ) - { - log.debug( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue ); - return; - } - - attributeValue.setId( 0 ); - attributeValue.setAttribute( attribute ); + log.debug( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue ); + return; } - } ); + + attributeValue.setId( 0 ); + attributeValue.setAttribute( attribute ); + } for ( AttributeValue attributeValue : attributeValues ) { @@ -407,14 +398,53 @@ { Set dataElementOperands = extractDataElementOperands( object, fieldName ); - CollectionUtils.forEach( dataElementOperands, new Function1() - { - @Override - public void apply( DataElementOperand dataElementOperand ) + for ( DataElementOperand dataElementOperand : dataElementOperands ) + { + dataElementOperandService.deleteDataElementOperand( dataElementOperand ); + } + } + + private Set extractProgramTrackedEntityAttributes( T object ) + { + Set programTrackedEntityAttributeSet = Sets.newHashSet(); + + if ( ReflectionUtils.isCollection( "attributes", object, ProgramTrackedEntityAttribute.class ) ) + { + Set programTrackedEntityAttributes = ReflectionUtils.invokeGetterMethod( "attributes", object ); + + for ( ProgramTrackedEntityAttribute trackedEntityAttribute : programTrackedEntityAttributes ) { - dataElementOperandService.deleteDataElementOperand( dataElementOperand ); + if ( sessionFactory.getCurrentSession().contains( trackedEntityAttribute ) ) + { + sessionFactory.getCurrentSession().delete( trackedEntityAttribute ); + } + + programTrackedEntityAttributeSet.add( trackedEntityAttribute ); } - } ); + + sessionFactory.getCurrentSession().flush(); + programTrackedEntityAttributes.clear(); + sessionFactory.getCurrentSession().flush(); + } + + return programTrackedEntityAttributeSet; + } + + private void deleteProgramTrackedEntityAttributes( T object ) + { + extractProgramTrackedEntityAttributes( object ); + } + + private void saveProgramTrackedEntityAttributes( T object, Set programTrackedEntityAttributes ) + { + for ( ProgramTrackedEntityAttribute programTrackedEntityAttribute : programTrackedEntityAttributes ) + { + Map identifiableObjects = detachFields( programTrackedEntityAttribute ); + reattachFields( programTrackedEntityAttribute, identifiableObjects ); + sessionFactory.getCurrentSession().persist( programTrackedEntityAttribute ); + } + + ReflectionUtils.invokeSetterMethod( "programTrackedEntityAttributes", object, programTrackedEntityAttributes ); } private Set extractProgramStageDataElements( T object ) @@ -424,44 +454,23 @@ if ( ReflectionUtils.findGetterMethod( "programStageDataElements", object ) != null ) { programStageDataElements = ReflectionUtils.invokeGetterMethod( "programStageDataElements", object ); - ReflectionUtils.invokeSetterMethod( "programStageDataElements", object, new HashSet() ); + + for ( ProgramStageDataElement programStageDataElement : programStageDataElements ) + { + if ( sessionFactory.getCurrentSession().contains( programStageDataElement ) ) + { + programStageDataElementService.deleteProgramStageDataElement( programStageDataElement ); + } + } + + sessionFactory.getCurrentSession().flush(); + ReflectionUtils.invokeSetterMethod( "programStageDataElements", object, Sets.newHashSet() ); + sessionFactory.getCurrentSession().flush(); } return programStageDataElements; } - private void deleteProgramTrackedEntityAttributes( T object ) - { - Set programTrackedEntityAttributes = extractProgramTrackedEntityAttributes( object ); - - CollectionUtils.forEach( programTrackedEntityAttributes, new Function1() - { - @Override - public void apply( ProgramTrackedEntityAttribute programTrackedEntityAttribute ) - { - sessionFactory.getCurrentSession().delete( programTrackedEntityAttribute ); - } - } ); - } - - private void saveProgramTrackedEntityAttributes( T object, Set programTrackedEntityAttributes ) - { - - } - - private Set extractProgramTrackedEntityAttributes( T object ) - { - Set trackedEntityAttributes = Sets.newHashSet(); - - if ( ReflectionUtils.isCollection( "attributes", object, ProgramTrackedEntityAttribute.class ) ) - { - trackedEntityAttributes = ReflectionUtils.invokeGetterMethod( "attributes", object ); - ReflectionUtils.invokeSetterMethod( "attributes", object, new HashSet() ); - } - - return trackedEntityAttributes; - } - private void saveProgramStageDataElements( T object, Set programStageDataElements ) { for ( ProgramStageDataElement programStageDataElement : programStageDataElements ) @@ -474,7 +483,7 @@ programStageDataElement.setProgramStage( (ProgramStage) object ); } - sessionFactory.getCurrentSession().persist( programStageDataElement ); + programStageDataElementService.addProgramStageDataElement( programStageDataElement ); } ReflectionUtils.invokeSetterMethod( "programStageDataElements", object, programStageDataElements ); @@ -482,16 +491,8 @@ private void deleteProgramStageDataElements( T object ) { - Set programStageDataElements = extractProgramStageDataElements( object ); - - CollectionUtils.forEach( programStageDataElements, new Function1() - { - @Override - public void apply( ProgramStageDataElement programStageDataElement ) - { - sessionFactory.getCurrentSession().delete( programStageDataElement ); - } - } ); + // deletion will be done in extractProgramStageDataElements + extractProgramStageDataElements( object ); } } @@ -638,6 +639,14 @@ return false; } + // for now, don't support ProgramStage, ProgramValidation and EventReport for dryRun + if ( (ProgramStage.class.isAssignableFrom( persistedObject.getClass() ) + || ProgramValidation.class.isAssignableFrom( persistedObject.getClass() ) + || EventReport.class.isAssignableFrom( persistedObject.getClass() )) && options.isDryRun() ) + { + return true; + } + NonIdentifiableObjects nonIdentifiableObjects = new NonIdentifiableObjects(); nonIdentifiableObjects.extract( object ); nonIdentifiableObjects.delete( persistedObject ); @@ -663,6 +672,7 @@ reattachFields( object, fields ); persistedObject.mergeWith( object ); + updatePeriodTypes( persistedObject ); reattachCollectionFields( persistedObject, collectionFields ); @@ -982,23 +992,19 @@ private Map detachFields( final Object object ) { - final Map fieldMap = new HashMap(); + final Map fieldMap = Maps.newHashMap(); final Collection fieldCollection = ReflectionUtils.collectFields( object.getClass(), idObjects ); - CollectionUtils.forEach( fieldCollection, new Function1() + for ( Field field : fieldCollection ) { - @Override - public void apply( Field field ) + Object ref = ReflectionUtils.invokeGetterMethod( field.getName(), object ); + + if ( ref != null ) { - Object ref = ReflectionUtils.invokeGetterMethod( field.getName(), object ); - - if ( ref != null ) - { - fieldMap.put( field, ref ); - ReflectionUtils.invokeSetterMethod( field.getName(), object, new Object[]{ null } ); - } + fieldMap.put( field, ref ); + ReflectionUtils.invokeSetterMethod( field.getName(), object, new Object[]{ null } ); } - } ); + } return fieldMap; } @@ -1027,24 +1033,20 @@ private Map> detachCollectionFields( final Object object ) { - final Map> collectionFields = new HashMap>(); + final Map> collectionFields = Maps.newHashMap(); final Collection fieldCollection = ReflectionUtils.collectFields( object.getClass(), idObjectCollectionsWithScanned ); - CollectionUtils.forEach( fieldCollection, new Function1() + for ( Field field : fieldCollection ) { - @Override - public void apply( Field field ) + Collection objects = ReflectionUtils.invokeGetterMethod( field.getName(), object ); + + if ( objects != null && !objects.isEmpty() ) { - Collection objects = ReflectionUtils.invokeGetterMethod( field.getName(), object ); - - if ( objects != null && !objects.isEmpty() ) - { - collectionFields.put( field, objects ); - Collection emptyCollection = ReflectionUtils.newCollectionInstance( field.getType() ); - ReflectionUtils.invokeSetterMethod( field.getName(), object, emptyCollection ); - } + collectionFields.put( field, objects ); + Collection emptyCollection = ReflectionUtils.newCollectionInstance( field.getType() ); + ReflectionUtils.invokeSetterMethod( field.getName(), object, emptyCollection ); } - } ); + } return collectionFields; } @@ -1056,26 +1058,22 @@ Collection collection = collectionFields.get( field ); final Collection objects = ReflectionUtils.newCollectionInstance( field.getType() ); - CollectionUtils.forEach( collection, new Function1() + for ( Object object : collection ) { - @Override - public void apply( Object object ) - { - IdentifiableObject ref = findObjectByReference( (IdentifiableObject) object ); + IdentifiableObject ref = findObjectByReference( (IdentifiableObject) object ); - if ( ref != null ) - { - objects.add( ref ); - } - else - { - if ( ExchangeClasses.getImportMap().get( idObject.getClass() ) != null || UserCredentials.class.isAssignableFrom( idObject.getClass() ) ) - { - reportReferenceError( idObject, object ); - } - } - } - } ); + if ( ref != null ) + { + objects.add( ref ); + } + else + { + if ( ExchangeClasses.getImportMap().get( idObject.getClass() ) != null || UserCredentials.class.isAssignableFrom( idObject.getClass() ) ) + { + reportReferenceError( idObject, object ); + } + } + } if ( !options.isDryRun() ) {