← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21178: server side validation of mandatory attributes

 

------------------------------------------------------------
revno: 21178
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-11-24 09:56:45 +0700
message:
  server side validation of mandatory attributes
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/exception/MissingMandatoryAttributeValueException.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.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 09:06:48 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java	2015-11-24 02:56:45 +0000
@@ -182,7 +182,7 @@
      */
     int getAttributeValueCount();
 
-    <T extends IdentifiableObject> void updateAttributeValues( T object, List<String> jsonAttributeValues ) throws NonUniqueAttributeValueException;
+    <T extends IdentifiableObject> void updateAttributeValues( T object, List<String> jsonAttributeValues ) throws Exception;
 
-    <T extends IdentifiableObject> void updateAttributeValues( T object, Set<AttributeValue> attributeValues ) throws NonUniqueAttributeValueException;
+    <T extends IdentifiableObject> void updateAttributeValues( T object, Set<AttributeValue> attributeValues ) throws Exception;
 }

=== modified 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	2015-11-23 07:21:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/exception/MissingMandatoryAttributeValueException.java	2015-11-24 02:56:45 +0000
@@ -28,15 +28,20 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.attribute.Attribute;
+
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public class MissingMandatoryAttributeValueException extends Exception
 {
-    public MissingMandatoryAttributeValueException( AttributeValue attributeValue )
+    public MissingMandatoryAttributeValueException( List<Attribute> mandatoryAttributes )
     {
-        super( "Missing mandatory attribute " + attributeValue.getAttribute().getName() + "(" + attributeValue.getAttribute().getUid() + ")" );
+        super( String.valueOf( mandatoryAttributes.stream()
+            .map( att -> "Attribute " + att.getDisplayName() + " (" + att.getUid() + ")" )
+            .collect( Collectors.toList() ) ) );
     }
 }

=== 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 09:11:11 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java	2015-11-24 02:56:45 +0000
@@ -29,6 +29,7 @@
  */
 
 import net.sf.json.JSONObject;
+import org.hisp.dhis.attribute.exception.MissingMandatoryAttributeValueException;
 import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
@@ -272,18 +273,19 @@
     }
 
     @Override
-    public <T extends IdentifiableObject> void updateAttributeValues( T object, List<String> jsonAttributeValues ) throws NonUniqueAttributeValueException
+    public <T extends IdentifiableObject> void updateAttributeValues( T object, List<String> jsonAttributeValues ) throws Exception
     {
         updateAttributeValues( object, getJsonAttributeValues( jsonAttributeValues ) );
     }
 
     @Override
-    public <T extends IdentifiableObject> void updateAttributeValues( T object, Set<AttributeValue> attributeValues ) throws NonUniqueAttributeValueException
+    public <T extends IdentifiableObject> void updateAttributeValues( T object, Set<AttributeValue> attributeValues ) throws Exception
     {
         Map<String, AttributeValue> attributeValueMap = attributeValues.stream()
             .collect( Collectors.toMap( av -> av.getAttribute().getUid(), av -> av ) );
 
         Iterator<AttributeValue> iterator = object.getAttributeValues().iterator();
+        List<Attribute> mandatoryAttributes = getMandatoryAttributes( object.getClass() );
 
         while ( iterator.hasNext() )
         {
@@ -310,6 +312,7 @@
                 }
 
                 attributeValueMap.remove( attributeValue.getAttribute().getUid() );
+                mandatoryAttributes.remove( attributeValue.getAttribute() );
             }
             else
             {
@@ -321,6 +324,12 @@
         {
             AttributeValue attributeValue = attributeValueMap.get( uid );
             addAttributeValue( object, attributeValue );
+            mandatoryAttributes.remove( attributeValue.getAttribute() );
+        }
+
+        if ( !mandatoryAttributes.isEmpty() )
+        {
+            throw new MissingMandatoryAttributeValueException( mandatoryAttributes );
         }
     }