=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java 2015-11-23 06:05:02 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java 2015-11-23 07:21:18 +0000 @@ -28,9 +28,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException; import org.hisp.dhis.common.IdentifiableObject; import java.util.List; +import java.util.Set; /** * @author Morten Olav Hansen @@ -287,4 +289,6 @@ int getAttributeValueCount(); void updateAttributeValues( T object, List jsonAttributeValues ) throws NonUniqueAttributeValueException; + + void updateAttributeValues( T object, Set attributeValues ) throws NonUniqueAttributeValueException; } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java 2015-11-23 06:05:02 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java 2015-11-23 07:21:18 +0000 @@ -199,4 +199,9 @@ { return attribute != null && attribute.isUnique(); } + + public boolean isMandatory() + { + return attribute != null && attribute.isMandatory(); + } } === removed file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java 2015-11-23 06:05:02 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java 1970-01-01 00:00:00 +0000 @@ -1,47 +0,0 @@ -package org.hisp.dhis.attribute; - -/* - * Copyright (c) 2004-2015, 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. - */ - -/** - * @author Morten Olav Hansen - */ -public class NonUniqueAttributeValueException extends Exception -{ - public NonUniqueAttributeValueException( AttributeValue attributeValue ) - { - super( "Value " + attributeValue.getValue() + " already exists for attribute " - + attributeValue.getAttribute().getName() + "(" + attributeValue.getAttribute().getUid() + ")" ); - } - - public NonUniqueAttributeValueException( AttributeValue attributeValue, String value ) - { - super( "Value " + value + " already exists for attribute " - + attributeValue.getAttribute().getName() + "(" + attributeValue.getAttribute().getUid() + ")" ); - } -} === added directory 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception' === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/MissingMandatoryAttributeValueException.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/MissingMandatoryAttributeValueException.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/MissingMandatoryAttributeValueException.java 2015-11-23 07:21:18 +0000 @@ -0,0 +1,42 @@ +package org.hisp.dhis.attribute.exception; + +/* + * Copyright (c) 2004-2015, 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.attribute.AttributeValue; + +/** + * @author Morten Olav Hansen + */ +public class MissingMandatoryAttributeValueException extends Exception +{ + public MissingMandatoryAttributeValueException( AttributeValue attributeValue ) + { + super( "Missing mandatory attribute " + attributeValue.getAttribute().getName() + "(" + attributeValue.getAttribute().getUid() + ")" ); + } +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/NonUniqueAttributeValueException.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/NonUniqueAttributeValueException.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/NonUniqueAttributeValueException.java 2015-11-23 07:21:18 +0000 @@ -0,0 +1,49 @@ +package org.hisp.dhis.attribute.exception; + +/* + * Copyright (c) 2004-2015, 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.attribute.AttributeValue; + +/** + * @author Morten Olav Hansen + */ +public class NonUniqueAttributeValueException extends Exception +{ + public NonUniqueAttributeValueException( AttributeValue attributeValue ) + { + super( "Value " + attributeValue.getValue() + " already exists for attribute " + + attributeValue.getAttribute().getName() + "(" + attributeValue.getAttribute().getUid() + ")" ); + } + + public NonUniqueAttributeValueException( AttributeValue attributeValue, String value ) + { + super( "Value " + value + " already exists for attribute " + + attributeValue.getAttribute().getName() + "(" + attributeValue.getAttribute().getUid() + ")" ); + } +} === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java 2015-11-23 06:05:02 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java 2015-11-23 07:21:18 +0000 @@ -29,6 +29,7 @@ */ import net.sf.json.JSONObject; +import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.i18n.I18nService; @@ -37,10 +38,12 @@ import org.springframework.util.StringUtils; import java.util.ArrayList; -import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import static org.hisp.dhis.i18n.I18nUtils.i18n; @@ -345,11 +348,18 @@ return attributeValueStore.getCount(); } - // TODO remove when actions are no longer in use @Override public void updateAttributeValues( T object, List jsonAttributeValues ) throws NonUniqueAttributeValueException { - Map attributeValueMap = getJsonAttributeValueMap( jsonAttributeValues ); + updateAttributeValues( object, getJsonAttributeValues( jsonAttributeValues ) ); + } + + @Override + public void updateAttributeValues( T object, Set attributeValues ) throws NonUniqueAttributeValueException + { + Map attributeValueMap = attributeValues.stream() + .collect( Collectors.toMap( av -> av.getAttribute().getUid(), av -> av ) ); + Iterator iterator = object.getAttributeValues().iterator(); while ( iterator.hasNext() ) @@ -391,9 +401,13 @@ } } - private Map getJsonAttributeValueMap( List jsonAttributeValues ) + //-------------------------------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------------------------------- + + private Set getJsonAttributeValues( List jsonAttributeValues ) { - Map map = new HashMap<>(); + Set attributeValues = new HashSet<>(); for ( String jsonValue : jsonAttributeValues ) { @@ -411,9 +425,9 @@ AttributeValue attributeValue = new AttributeValue( value, attribute ); attributeValue.setId( id ); - map.put( attribute.getUid(), attributeValue ); + attributeValues.add( attributeValue ); } - return map; + return attributeValues; } } === modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java 2015-11-20 05:42:56 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java 2015-11-23 07:21:18 +0000 @@ -29,6 +29,7 @@ */ import org.hisp.dhis.DhisSpringTest; +import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.dataelement.DataElement; === modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java 2015-11-20 05:42:56 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java 2015-11-23 07:21:18 +0000 @@ -33,7 +33,7 @@ import org.hisp.dhis.attribute.Attribute; import org.hisp.dhis.attribute.AttributeService; import org.hisp.dhis.attribute.AttributeValue; -import org.hisp.dhis.attribute.NonUniqueAttributeValueException; +import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.dataset.DataSetService; === 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 2015-11-20 05:42:56 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2015-11-23 07:21:18 +0000 @@ -37,7 +37,7 @@ import org.hisp.dhis.attribute.Attribute; import org.hisp.dhis.attribute.AttributeService; import org.hisp.dhis.attribute.AttributeValue; -import org.hisp.dhis.attribute.NonUniqueAttributeValueException; +import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException; import org.hisp.dhis.common.BaseAnalyticalObject; import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.common.DataDimensionItem;