=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/MissingReference.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/MissingReference.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/MissingReference.java 2016-02-09 05:50:33 +0000 @@ -0,0 +1,107 @@ +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.annotation.JsonProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; +import com.google.common.base.Objects; +import org.hisp.dhis.common.DxfNamespaces; +import org.hisp.dhis.schema.Property; + +/** + * @author Morten Olav Hansen + */ +@JacksonXmlRootElement( localName = "missingReference", namespace = DxfNamespaces.DXF_2_0 ) +public class MissingReference +{ + private PreheatIdentifier identifier; + + private String value; + + private Property property; + + public MissingReference( PreheatIdentifier identifier, String value, Property property ) + { + this.identifier = identifier; + this.value = value; + this.property = property; + } + + @JsonProperty + @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) + public PreheatIdentifier getIdentifier() + { + return identifier; + } + + public void setIdentifier( PreheatIdentifier identifier ) + { + this.identifier = identifier; + } + + @JsonProperty + @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) + public String getValue() + { + return value; + } + + public void setValue( String value ) + { + this.value = value; + } + + @JsonProperty + @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) + public String getPropertyName() + { + return property.getName() + " (collection: " + property.isCollection() + ")"; + } + + public Property getProperty() + { + return property; + } + + public void setProperty( Property property ) + { + this.property = property; + } + + @Override + public String toString() + { + return Objects.toStringHelper( this ) + .add( "identifier", identifier ) + .add( "value", value ) + .add( "propertyName", getPropertyName() ) + .toString(); + } +} === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java 2016-02-09 02:36:36 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java 2016-02-09 05:50:33 +0000 @@ -31,6 +31,7 @@ import org.hisp.dhis.common.IdentifiableObject; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Set; @@ -70,6 +71,15 @@ Map, Set>> collectReferences( Collection objects ); /** + * Checks but does not connect any references, returns check report + * + * @param object Object to check + * @param preheat Preheat Cache to use + * @param identifier Use this identifier type to check references + */ + List checkReferences( T object, Preheat preheat, PreheatIdentifier identifier ); + + /** * Connects id object references on a given object using a given identifier + a preheated Preheat cache. * * @param object Object to connect to === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java 2016-02-08 08:44:43 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java 2016-02-09 05:50:33 +0000 @@ -41,6 +41,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -221,6 +222,53 @@ @Override @SuppressWarnings( "unchecked" ) + public List checkReferences( T object, Preheat preheat, PreheatIdentifier identifier ) + { + List missingReferences = new ArrayList<>(); + + if ( object == null ) + { + return missingReferences; + } + + Schema schema = schemaService.getDynamicSchema( object.getClass() ); + schema.getProperties().stream() + .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) ) + .forEach( p -> { + if ( !p.isCollection() ) + { + T refObject = ReflectionUtils.invokeMethod( object, p.getGetterMethod() ); + T ref = preheat.get( identifier, refObject ); + + if ( ref == null && refObject != null ) + { + missingReferences.add( new MissingReference( identifier, identifier.getIdentifier( refObject ), p ) ); + } + } + else + { + Collection objects = ReflectionUtils.newCollectionInstance( p.getKlass() ); + Collection refObjects = ReflectionUtils.invokeMethod( object, p.getGetterMethod() ); + + for ( IdentifiableObject refObject : refObjects ) + { + T ref = preheat.get( identifier, (T) refObject ); + + if ( ref == null && refObject != null ) + { + missingReferences.add( new MissingReference( identifier, identifier.getIdentifier( refObject ), p ) ); + } + } + + ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects ); + } + } ); + + return missingReferences; + } + + @Override + @SuppressWarnings( "unchecked" ) public void connectReferences( T object, Preheat preheat, PreheatIdentifier identifier ) { if ( object == null ) === modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java 2016-02-08 18:43:58 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java 2016-02-09 05:50:33 +0000 @@ -489,6 +489,25 @@ } @Test + public void testPreheatReferenceCheckUID() + { + DataElementGroup dataElementGroup = fromJson( "preheat/degAUidRef_invalid.json", DataElementGroup.class ); + defaultSetup(); + + PreheatParams params = new PreheatParams(); + params.setPreheatMode( PreheatMode.REFERENCE ); + params.setReferences( preheatService.collectReferences( dataElementGroup ) ); + + preheatService.validate( params ); + Preheat preheat = preheatService.preheat( params ); + List missingReferences = preheatService.checkReferences( dataElementGroup, preheat, PreheatIdentifier.UID ); + assertEquals( 3, missingReferences.size() ); + assertEquals( PreheatIdentifier.UID, missingReferences.get( 0 ).getIdentifier() ); + assertEquals( PreheatIdentifier.UID, missingReferences.get( 1 ).getIdentifier() ); + assertEquals( PreheatIdentifier.UID, missingReferences.get( 2 ).getIdentifier() ); + } + + @Test public void testPreheatReferenceConnectUID() { DataElementGroup dataElementGroup = fromJson( "preheat/degAUidRef.json", DataElementGroup.class ); === added file 'dhis-2/dhis-services/dhis-service-core/src/test/resources/preheat/degAUidRef_invalid.json' --- dhis-2/dhis-services/dhis-service-core/src/test/resources/preheat/degAUidRef_invalid.json 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/resources/preheat/degAUidRef_invalid.json 2016-02-09 05:50:33 +0000 @@ -0,0 +1,17 @@ +{ + "name": "DataElementGroupA", + "user": { + "id": "INVALID_USER" + }, + "dataElements": [ + { + "id": "deabcdefghA" + }, + { + "id": "INVALID_DATAELEMENT_1" + }, + { + "id": "INVALID_DATAELEMENT_2" + } + ] +} \ No newline at end of file