← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21162: minor fixes, update AttributeService.updateAttributeValues() to support a list of AttributeValue ...

 

------------------------------------------------------------
revno: 21162
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-11-23 14:21:18 +0700
message:
  minor fixes, update AttributeService.updateAttributeValues() to support a list of AttributeValue directly, means it can later be reused by dxf2 metadata importer
removed:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/NonUniqueAttributeValueException.java
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/
  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/NonUniqueAttributeValueException.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java
  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/dataelement/DataElementStoreTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.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
=== 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 <mortenoh@xxxxxxxxx>
@@ -287,4 +289,6 @@
     int getAttributeValueCount();
 
     <T extends IdentifiableObject> void updateAttributeValues( T object, List<String> jsonAttributeValues ) throws NonUniqueAttributeValueException;
+
+    <T extends IdentifiableObject> void updateAttributeValues( T object, Set<AttributeValue> 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 <mortenoh@xxxxxxxxx>
- */
-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 <mortenoh@xxxxxxxxx>
+ */
+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 <mortenoh@xxxxxxxxx>
+ */
+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 <T extends IdentifiableObject> void updateAttributeValues( T object, List<String> jsonAttributeValues ) throws NonUniqueAttributeValueException
     {
-        Map<String, AttributeValue> attributeValueMap = getJsonAttributeValueMap( jsonAttributeValues );
+        updateAttributeValues( object, getJsonAttributeValues( jsonAttributeValues ) );
+    }
+
+    @Override
+    public <T extends IdentifiableObject> void updateAttributeValues( T object, Set<AttributeValue> attributeValues ) throws NonUniqueAttributeValueException
+    {
+        Map<String, AttributeValue> attributeValueMap = attributeValues.stream()
+            .collect( Collectors.toMap( av -> av.getAttribute().getUid(), av -> av ) );
+
         Iterator<AttributeValue> iterator = object.getAttributeValues().iterator();
 
         while ( iterator.hasNext() )
@@ -391,9 +401,13 @@
         }
     }
 
-    private Map<String, AttributeValue> getJsonAttributeValueMap( List<String> jsonAttributeValues )
+    //--------------------------------------------------------------------------------------------------
+    // Helpers
+    //--------------------------------------------------------------------------------------------------
+
+    private Set<AttributeValue> getJsonAttributeValues( List<String> jsonAttributeValues )
     {
-        Map<String, AttributeValue> map = new HashMap<>();
+        Set<AttributeValue> 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;