=== added directory 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers' === added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/AbstractObjectHandler.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/AbstractObjectHandler.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/AbstractObjectHandler.java 2012-05-23 18:35:22 +0000 @@ -0,0 +1,56 @@ +package org.hisp.dhis.dxf2.metadata.handlers; + +/* + * 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 java.util.List; + +/** + * @author Morten Olav Hansen + */ +public abstract class AbstractObjectHandler implements ObjectHandler +{ + @Override + public void preImportObject( T object ) + { + } + + @Override + public void postImportObject( T object ) + { + } + + @Override + public void preImportObjects( List objects ) + { + } + + @Override + public void postImportObjects( List objects ) + { + } +} === added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/FieldHandler.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/FieldHandler.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/FieldHandler.java 2012-05-23 18:35:22 +0000 @@ -0,0 +1,38 @@ +package org.hisp.dhis.dxf2.metadata.handlers; + +/* + * 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 java.lang.reflect.Field; + +/** + * @author Morten Olav Hansen + */ +public interface FieldHandler +{ + boolean canHandle( Field field ); +} === added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/ObjectHandler.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/ObjectHandler.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/ObjectHandler.java 2012-05-23 18:35:22 +0000 @@ -0,0 +1,48 @@ +package org.hisp.dhis.dxf2.metadata.handlers; + +/* + * 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 java.util.List; + +/** + * @author Morten Olav Hansen + */ +public interface ObjectHandler +{ + void preImportObject(T object); + + void postImportObject(T object); + + void preImportObjects(List objects); + + void postImportObjects(List objects); + + boolean canHandle( Class clazz ); +} + + === added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/OrganisationUnitObjectHandler.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/OrganisationUnitObjectHandler.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/handlers/OrganisationUnitObjectHandler.java 2012-05-23 18:35:22 +0000 @@ -0,0 +1,55 @@ +package org.hisp.dhis.dxf2.metadata.handlers; + +/* + * 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.hisp.dhis.dxf2.utils.OrganisationUnitUtils; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.organisationunit.comparator.OrganisationUnitComparator; + +import java.util.Collections; +import java.util.List; + +/** + * @author Morten Olav Hansen + */ +public class OrganisationUnitObjectHandler + extends AbstractObjectHandler +{ + @Override + public void preImportObjects( List objects ) + { + OrganisationUnitUtils.updateParents( objects ); + Collections.sort( objects, new OrganisationUnitComparator() ); + } + + @Override + public boolean canHandle( Class clazz ) + { + return OrganisationUnit.class.equals( clazz ); + } +} === modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2012-05-23 11:12:18 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2012-05-23 18:35:22 +0000 @@ -44,12 +44,11 @@ import org.hisp.dhis.dxf2.metadata.ImportOptions; import org.hisp.dhis.dxf2.metadata.Importer; import org.hisp.dhis.dxf2.metadata.ObjectBridge; -import org.hisp.dhis.dxf2.utils.OrganisationUnitUtils; +import org.hisp.dhis.dxf2.metadata.handlers.FieldHandler; +import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandler; import org.hisp.dhis.expression.Expression; import org.hisp.dhis.expression.ExpressionService; import org.hisp.dhis.importexport.ImportStrategy; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.organisationunit.comparator.OrganisationUnitComparator; import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodService; import org.hisp.dhis.period.PeriodType; @@ -91,6 +90,12 @@ @Autowired private SessionFactory sessionFactory; + @Autowired( required = false ) + private List objectHandlers; + + @Autowired( required = false ) + private List fieldHandlers; + //------------------------------------------------------------------------------------------------------- // Constructor //------------------------------------------------------------------------------------------------------- @@ -211,15 +216,13 @@ init( options ); - // FIXME a bit too static.. implement "pre handler" for types? - if ( OrganisationUnit.class.isAssignableFrom( objects.get( 0 ).getClass() ) ) - { - OrganisationUnitUtils.updateParents( (Collection) objects ); - Collections.sort( (List) objects, new OrganisationUnitComparator() ); - } + preObjectsHandlers( objects ); for ( T object : objects ) { + preObjectHandlers( object ); + + // FIXME add to scanner, and use field handlers for this Set attributeValues = getAndClearAttributeValues( object ); Set compulsoryDataElementOperands = getAndClearDataElementOperands( object, "compulsoryDataElementOperands" ); Set greyedFields = getAndClearDataElementOperands( object, "greyedFields" ); @@ -241,11 +244,70 @@ sessionFactory.getCurrentSession().flush(); } + + postObjectHandlers( object ); } + postObjectsHandlers( objects ); + return importConflicts; } + + @SuppressWarnings( "unchecked" ) + private void preObjectHandlers( T object ) + { + for ( ObjectHandler objectHandler : objectHandlers ) + { + if ( objectHandler.canHandle( object.getClass() ) ) + { + objectHandler.preImportObject( object ); + } + } + } + + @SuppressWarnings( "unchecked" ) + private void postObjectHandlers( T object ) + { + for ( ObjectHandler objectHandler : objectHandlers ) + { + if ( objectHandler.canHandle( object.getClass() ) ) + { + objectHandler.postImportObject( object ); + } + } + } + + @SuppressWarnings( "unchecked" ) + private void preObjectsHandlers( List objects ) + { + if ( objects.size() > 0 ) + { + for ( ObjectHandler objectHandler : objectHandlers ) + { + if ( objectHandler.canHandle( objects.get( 0 ).getClass() ) ) + { + objectHandler.preImportObjects( objects ); + } + } + } + } + + @SuppressWarnings( "unchecked" ) + private void postObjectsHandlers( List objects ) + { + if ( objects.size() > 0 ) + { + for ( ObjectHandler objectHandler : objectHandlers ) + { + if ( objectHandler.canHandle( objects.get( 0 ).getClass() ) ) + { + objectHandler.postImportObjects( objects ); + } + } + } + } + // FIXME add type check private Expression getAndClearExpression( T object, String field ) { === modified file 'dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml 2012-05-22 11:04:59 +0000 +++ dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml 2012-05-23 18:35:22 +0000 @@ -14,6 +14,11 @@ + + + +