dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #43280
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21974: check for invalid objects and remove if importMode is create or delete
------------------------------------------------------------
revno: 21974
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2016-02-16 11:53:44 +0700
message:
check for invalid objects and remove if importMode is create or delete
added:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/InvalidObject.java
dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate4.json
dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate5.json
dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate6.json
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java
dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/InvalidObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/InvalidObject.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/InvalidObject.java 2016-02-16 04:53:44 +0000
@@ -0,0 +1,97 @@
+package org.hisp.dhis.preheat;
+
+/*
+ * Copyright (c) 2004-2016, 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.dataformat.xml.annotation.JacksonXmlRootElement;
+import com.google.common.base.MoreObjects;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
+import org.springframework.util.Assert;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@JacksonXmlRootElement( localName = "invalidObject", namespace = DxfNamespaces.DXF_2_0 )
+public class InvalidObject
+{
+ private String name;
+
+ private PreheatIdentifier identifier;
+
+ private Object object;
+
+ public InvalidObject( Object object, PreheatIdentifier identifier )
+ {
+ Assert.notNull( object );
+ Assert.notNull( identifier );
+
+ this.object = object;
+ this.identifier = identifier;
+ this.name = IdentifiableObjectUtils.getDisplayName( object );
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName( String name )
+ {
+ this.name = name;
+ }
+
+ public PreheatIdentifier getIdentifier()
+ {
+ return identifier;
+ }
+
+ public void setIdentifier( PreheatIdentifier identifier )
+ {
+ this.identifier = identifier;
+ }
+
+ public Object getObject()
+ {
+ return object;
+ }
+
+ public void setObject( Object object )
+ {
+ this.object = object;
+ }
+
+ @Override
+ public String toString()
+ {
+ return MoreObjects.toStringHelper( this )
+ .add( "name", name )
+ .add( "identifier", identifier )
+ .toString();
+ }
+}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java 2016-02-08 15:37:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java 2016-02-16 04:53:44 +0000
@@ -51,7 +51,7 @@
AUTO;
@SuppressWarnings("incomplete-switch")
- <T extends IdentifiableObject> String getIdentifier( T object )
+ public <T extends IdentifiableObject> String getIdentifier( T object )
{
switch ( this )
{
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java 2016-02-15 04:57:22 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java 2016-02-16 04:53:44 +0000
@@ -29,15 +29,17 @@
*/
import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.schema.validation.SchemaValidator;
+import org.hisp.dhis.preheat.InvalidObject;
import org.hisp.dhis.preheat.PreheatMode;
import org.hisp.dhis.preheat.PreheatParams;
import org.hisp.dhis.preheat.PreheatService;
+import org.hisp.dhis.schema.validation.SchemaValidator;
import org.hisp.dhis.schema.validation.ValidationViolation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
/**
@@ -77,7 +79,24 @@
for ( Class<? extends IdentifiableObject> klass : bundle.getObjects().keySet() )
{
- objectBundleValidation.addInvalidReferences( klass, preheatService.checkReferences(
+ if ( bundle.getImportMode().isUpdate() || bundle.getImportMode().isDelete() )
+ {
+ Iterator<IdentifiableObject> iterator = bundle.getObjects().get( klass ).iterator();
+
+ while ( iterator.hasNext() )
+ {
+ IdentifiableObject identifiableObject = iterator.next();
+ IdentifiableObject object = bundle.getPreheat().get( bundle.getPreheatIdentifier(), identifiableObject );
+
+ if ( object == null )
+ {
+ objectBundleValidation.addInvalidObject( klass, new InvalidObject( identifiableObject, bundle.getPreheatIdentifier() ) );
+ iterator.remove();
+ }
+ }
+ }
+
+ objectBundleValidation.addPreheatValidations( klass, preheatService.checkReferences(
bundle.getObjects().get( klass ), bundle.getPreheat(), bundle.getPreheatIdentifier() ) );
List<List<ValidationViolation>> validationViolations = new ArrayList<>();
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java 2016-02-15 04:57:22 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java 2016-02-16 04:53:44 +0000
@@ -30,9 +30,11 @@
import com.google.common.base.MoreObjects;
import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.preheat.InvalidObject;
import org.hisp.dhis.preheat.PreheatValidation;
import org.hisp.dhis.schema.validation.ValidationViolation;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -46,11 +48,13 @@
private Map<Class<? extends IdentifiableObject>, List<List<ValidationViolation>>> validationViolations = new HashMap<>();
+ private Map<Class<? extends IdentifiableObject>, List<InvalidObject>> invalidObjects = new HashMap<>();
+
public ObjectBundleValidation()
{
}
- public void addInvalidReferences( Class<? extends IdentifiableObject> klass, List<PreheatValidation> preheatValidations )
+ public void addPreheatValidations( Class<? extends IdentifiableObject> klass, List<PreheatValidation> preheatValidations )
{
this.preheatValidations.put( klass, preheatValidations );
}
@@ -70,6 +74,21 @@
return validationViolations;
}
+ public void addInvalidObject( Class<? extends IdentifiableObject> klass, InvalidObject invalidObject )
+ {
+ if ( !invalidObjects.containsKey( klass ) )
+ {
+ invalidObjects.put( klass, new ArrayList<>() );
+ }
+
+ invalidObjects.get( klass ).add( invalidObject );
+ }
+
+ public Map<Class<? extends IdentifiableObject>, List<InvalidObject>> getInvalidObjects()
+ {
+ return invalidObjects;
+ }
+
@Override
public String toString()
{
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java 2016-02-11 09:13:07 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java 2016-02-16 04:53:44 +0000
@@ -35,6 +35,7 @@
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
import org.hisp.dhis.dataelement.DataElementGroup;
+import org.hisp.dhis.importexport.ImportStrategy;
import org.hisp.dhis.option.OptionSet;
import org.hisp.dhis.preheat.InvalidReference;
import org.hisp.dhis.preheat.PreheatIdentifier;
@@ -227,6 +228,120 @@
}
@Test
+ public void testUpdateRequiresValidReferencesUID() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+ new ClassPathResource( "dxf2/de_validate4.json" ).getInputStream(), RenderFormat.JSON );
+
+ ObjectBundleParams params = new ObjectBundleParams();
+ params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+ params.setPreheatIdentifier( PreheatIdentifier.UID );
+ params.setImportMode( ImportStrategy.UPDATE );
+ params.setObjects( metadata );
+
+ ObjectBundle bundle = objectBundleService.create( params );
+ ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+ assertTrue( validate.getInvalidObjects().containsKey( DataElement.class ) );
+ assertEquals( 3, validate.getInvalidObjects().get( DataElement.class ).size() );
+ }
+
+ @Test
+ public void testUpdateRequiresValidReferencesCODE() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+ new ClassPathResource( "dxf2/de_validate5.json" ).getInputStream(), RenderFormat.JSON );
+
+ ObjectBundleParams params = new ObjectBundleParams();
+ params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+ params.setPreheatIdentifier( PreheatIdentifier.CODE );
+ params.setImportMode( ImportStrategy.UPDATE );
+ params.setObjects( metadata );
+
+ ObjectBundle bundle = objectBundleService.create( params );
+ ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+ assertTrue( validate.getInvalidObjects().containsKey( DataElement.class ) );
+ assertEquals( 3, validate.getInvalidObjects().get( DataElement.class ).size() );
+ }
+
+ @Test
+ public void testUpdateRequiresValidReferencesAUTO() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+ new ClassPathResource( "dxf2/de_validate6.json" ).getInputStream(), RenderFormat.JSON );
+
+ ObjectBundleParams params = new ObjectBundleParams();
+ params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+ params.setPreheatIdentifier( PreheatIdentifier.AUTO );
+ params.setImportMode( ImportStrategy.UPDATE );
+ params.setObjects( metadata );
+
+ ObjectBundle bundle = objectBundleService.create( params );
+ ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+ assertTrue( validate.getInvalidObjects().containsKey( DataElement.class ) );
+ assertEquals( 3, validate.getInvalidObjects().get( DataElement.class ).size() );
+ }
+
+ @Test
+ public void testDeleteRequiresValidReferencesUID() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+ new ClassPathResource( "dxf2/de_validate4.json" ).getInputStream(), RenderFormat.JSON );
+
+ ObjectBundleParams params = new ObjectBundleParams();
+ params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+ params.setPreheatIdentifier( PreheatIdentifier.UID );
+ params.setImportMode( ImportStrategy.DELETE );
+ params.setObjects( metadata );
+
+ ObjectBundle bundle = objectBundleService.create( params );
+ ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+ assertTrue( validate.getInvalidObjects().containsKey( DataElement.class ) );
+ assertEquals( 3, validate.getInvalidObjects().get( DataElement.class ).size() );
+ }
+
+ @Test
+ public void testDeleteRequiresValidReferencesCODE() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+ new ClassPathResource( "dxf2/de_validate5.json" ).getInputStream(), RenderFormat.JSON );
+
+ ObjectBundleParams params = new ObjectBundleParams();
+ params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+ params.setPreheatIdentifier( PreheatIdentifier.CODE );
+ params.setImportMode( ImportStrategy.DELETE );
+ params.setObjects( metadata );
+
+ ObjectBundle bundle = objectBundleService.create( params );
+ ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+ assertTrue( validate.getInvalidObjects().containsKey( DataElement.class ) );
+ assertEquals( 3, validate.getInvalidObjects().get( DataElement.class ).size() );
+ }
+
+ @Test
+ public void testDeleteRequiresValidReferencesAUTO() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+ new ClassPathResource( "dxf2/de_validate6.json" ).getInputStream(), RenderFormat.JSON );
+
+ ObjectBundleParams params = new ObjectBundleParams();
+ params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+ params.setPreheatIdentifier( PreheatIdentifier.AUTO );
+ params.setImportMode( ImportStrategy.DELETE );
+ params.setObjects( metadata );
+
+ ObjectBundle bundle = objectBundleService.create( params );
+ ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+ assertTrue( validate.getInvalidObjects().containsKey( DataElement.class ) );
+ assertEquals( 3, validate.getInvalidObjects().get( DataElement.class ).size() );
+ }
+
+ @Test
public void testPreheatValidationsIncludingMerge() throws IOException
{
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate4.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate4.json 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate4.json 2016-02-16 04:53:44 +0000
@@ -0,0 +1,13 @@
+{
+ "dataElements": [
+ {
+ "id": "InvalidA"
+ },
+ {
+ "id": "InvalidB"
+ },
+ {
+ "id": "InvalidC"
+ }
+ ]
+}
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate5.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate5.json 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate5.json 2016-02-16 04:53:44 +0000
@@ -0,0 +1,13 @@
+{
+ "dataElements": [
+ {
+ "code": "InvalidA"
+ },
+ {
+ "code": "InvalidB"
+ },
+ {
+ "code": "InvalidC"
+ }
+ ]
+}
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate6.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate6.json 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate6.json 2016-02-16 04:53:44 +0000
@@ -0,0 +1,13 @@
+{
+ "dataElements": [
+ {
+ "id": "InvalidA"
+ },
+ {
+ "code": "InvalidB"
+ },
+ {
+ "id": "InvalidC"
+ }
+ ]
+}