← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14823: validation of teAttributeValues, wip

 

------------------------------------------------------------
revno: 14823
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-04-14 12:11:56 +0700
message:
  validation of teAttributeValues, wip
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-04-10 02:27:33 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-04-14 05:11:56 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
@@ -46,11 +47,14 @@
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramService;
 import org.hisp.dhis.program.ProgramTrackedEntityAttribute;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
 import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
+import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.Assert;
 
@@ -89,6 +93,9 @@
     @Autowired
     private I18nManager i18nManager;
 
+    @Autowired
+    private UserService userService;
+
     // -------------------------------------------------------------------------
     // READ
     // -------------------------------------------------------------------------
@@ -453,6 +460,7 @@
         for ( Attribute attribute : enrollment.getAttributes() )
         {
             attributeValueMap.put( attribute.getAttribute(), attribute.getValue() );
+            importConflicts.addAll( validateAttributeType( attribute ) );
         }
 
         TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance( enrollment.getTrackedEntityInstance() );
@@ -600,5 +608,53 @@
         }
 
         return program;
+
+    }
+
+    private List<ImportConflict> validateAttributeType( Attribute attribute )
+    {
+        List<ImportConflict> importConflicts = Lists.newArrayList();
+        TrackedEntityAttribute teAttribute = trackedEntityAttributeService.getTrackedEntityAttribute( attribute.getAttribute() );
+
+        if ( teAttribute == null )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.attribute", "Does not point to a valid attribute." ) );
+            return importConflicts;
+        }
+
+        if ( attribute.getValue().length() > 255 )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value length is greater than 256 chars." ) );
+        }
+
+        if ( TrackedEntityAttribute.TYPE_INT.equals( teAttribute.getValueType() ) && !MathUtils.isInteger( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not integer." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_BOOL.equals( teAttribute.getValueType() ) && !MathUtils.isBool( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not boolean." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_DATE.equals( teAttribute.getValueType() ) && !DateUtils.dateIsValid( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not date." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_TRUE_ONLY.equals( teAttribute.getValueType() ) && "true".equals( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not true (true-only value type)." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_USERS.equals( teAttribute.getValueType() ) )
+        {
+            if ( userService.getUserCredentialsByUsername( attribute.getValue() ) == null )
+            {
+                importConflicts.add( new ImportConflict( "Attribute.value", "Value is not pointing to a valid username." ) );
+            }
+        }
+        else if ( TrackedEntityAttribute.TYPE_COMBO.equals( teAttribute.getValueType() ) && !teAttribute.getOptionSet().getOptions().contains( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not pointing to a valid option." ) );
+        }
+
+        return importConflicts;
     }
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-31 05:49:38 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-04-14 05:11:56 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
@@ -39,12 +40,16 @@
 import org.hisp.dhis.relationship.Relationship;
 import org.hisp.dhis.relationship.RelationshipService;
 import org.hisp.dhis.relationship.RelationshipType;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.trackedentity.TrackedEntity;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
 import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.hisp.dhis.trackedentity.TrackedEntityService;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
+import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.Assert;
 
@@ -69,6 +74,9 @@
     private TrackedEntityAttributeValueService attributeValueService;
 
     @Autowired
+    private TrackedEntityAttributeService trackedEntityAttributeService;
+
+    @Autowired
     private RelationshipService relationshipService;
 
     @Autowired
@@ -83,6 +91,9 @@
     @Autowired
     private IdentifiableObjectManager manager;
 
+    @Autowired
+    private UserService userService;
+
     // -------------------------------------------------------------------------
     // READ
     // -------------------------------------------------------------------------
@@ -314,6 +325,8 @@
                     checkScope( tei, entityAttribute, attribute.getValue(), organisationUnit )
                 );
             }
+
+            importConflicts.addAll( validateAttributeType( attribute ) );
         }
 
         return importConflicts;
@@ -430,4 +443,52 @@
 
         teiService.updateTrackedEntityInstance( entityInstance );
     }
+
+
+    private List<ImportConflict> validateAttributeType( Attribute attribute )
+    {
+        List<ImportConflict> importConflicts = Lists.newArrayList();
+        TrackedEntityAttribute teAttribute = trackedEntityAttributeService.getTrackedEntityAttribute( attribute.getAttribute() );
+
+        if ( teAttribute == null )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.attribute", "Does not point to a valid attribute." ) );
+            return importConflicts;
+        }
+
+        if ( attribute.getValue().length() > 255 )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value length is greater than 256 chars." ) );
+        }
+
+        if ( TrackedEntityAttribute.TYPE_INT.equals( teAttribute.getValueType() ) && !MathUtils.isInteger( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not integer." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_BOOL.equals( teAttribute.getValueType() ) && !MathUtils.isBool( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not boolean." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_DATE.equals( teAttribute.getValueType() ) && !DateUtils.dateIsValid( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not date." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_TRUE_ONLY.equals( teAttribute.getValueType() ) && "true".equals( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not true (true-only value type)." ) );
+        }
+        else if ( TrackedEntityAttribute.TYPE_USERS.equals( teAttribute.getValueType() ) )
+        {
+            if ( userService.getUserCredentialsByUsername( attribute.getValue() ) == null )
+            {
+                importConflicts.add( new ImportConflict( "Attribute.value", "Value is not pointing to a valid username." ) );
+            }
+        }
+        else if ( TrackedEntityAttribute.TYPE_COMBO.equals( teAttribute.getValueType() ) && !teAttribute.getOptionSet().getOptions().contains( attribute.getValue() ) )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not pointing to a valid option." ) );
+        }
+
+        return importConflicts;
+    }
 }