← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20408: Validate TEA ValueType in TEAService instead of DXF2 TEIService

 

------------------------------------------------------------
revno: 20408
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-09-30 13:14:51 +0700
message:
  Validate TEA ValueType in TEAService instead of DXF2 TEIService
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttributeService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityAttributeService.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-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttributeService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttributeService.java	2015-09-30 05:42:06 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityAttributeService.java	2015-09-30 06:14:51 +0000
@@ -42,103 +42,97 @@
 
     /**
      * Adds an {@link TrackedEntityAttribute}
-     * 
+     *
      * @param attribute The to TrackedEntityAttribute add.
-     * 
      * @return A generated unique id of the added {@link TrackedEntityAttribute}
-     *         .
+     * .
      */
     int addTrackedEntityAttribute( TrackedEntityAttribute attribute );
 
     /**
      * Deletes a {@link TrackedEntityAttribute}.
-     * 
+     *
      * @param attribute the TrackedEntityAttribute to delete.
      */
     void deleteTrackedEntityAttribute( TrackedEntityAttribute attribute );
 
     /**
      * Updates an {@link TrackedEntityAttribute}.
-     * 
+     *
      * @param attribute the TrackedEntityAttribute to update.
      */
     void updateTrackedEntityAttribute( TrackedEntityAttribute attribute );
 
     /**
      * Returns a {@link TrackedEntityAttribute}.
-     * 
+     *
      * @param id the id of the TrackedEntityAttribute to return.
-     * 
      * @return the TrackedEntityAttribute with the given id
      */
     TrackedEntityAttribute getTrackedEntityAttribute( int id );
 
     /**
      * Returns the {@link TrackedEntityAttribute} with the given UID.
-     * 
+     *
      * @param uid the UID.
      * @return the TrackedEntityAttribute with the given UID, or null if no
-     *         match.
+     * match.
      */
     TrackedEntityAttribute getTrackedEntityAttribute( String uid );
 
     /**
      * Returns a {@link TrackedEntityAttribute} with a given name.
-     * 
+     *
      * @param name the name of the TrackedEntityAttribute to return.
-     * 
      * @return the TrackedEntityAttribute with the given name, or null if no
-     *         match.
+     * match.
      */
     TrackedEntityAttribute getTrackedEntityAttributeByName( String name );
 
     /**
      * Returns a {@link TrackedEntityAttribute} with a given short name.
-     * 
+     *
      * @param name the short name of the TrackedEntityAttribute to return.
-     * 
      * @return the TrackedEntityAttribute with the given short name, or null if no
-     *         match.
+     * match.
      */
     TrackedEntityAttribute getTrackedEntityAttributeByShortName( String name );
 
     /**
      * Returns a {@link TrackedEntityAttribute} with a given code.
-     * 
+     *
      * @param name the code of the TrackedEntityAttribute to return.
-     * 
      * @return the TrackedEntityAttribute with the given code, or null if no
-     *         match.
+     * match.
      */
     TrackedEntityAttribute getTrackedEntityAttributeByCode( String code );
 
     /**
      * Returns all {@link TrackedEntityAttribute}
-     * 
+     *
      * @return a List of all TrackedEntityAttribute, or an empty
-     *         List if there are no TrackedEntityAttributes.
+     * List if there are no TrackedEntityAttributes.
      */
     List<TrackedEntityAttribute> getAllTrackedEntityAttributes();
 
     /**
      * Get attributes without groups
-     * 
+     *
      * @return List of attributes
      */
     List<TrackedEntityAttribute> getOptionalAttributesWithoutGroup();
 
     /**
      * Get attributes without groups
-     * 
+     *
      * @return List of attributes without group
      */
     List<TrackedEntityAttribute> getTrackedEntityAttributesWithoutGroup();
 
     /**
      * Get attributes which are displayed in visit schedule
-     * 
+     *
      * @param displayOnVisitSchedule True/False value
-     * 
      * @return List of attributes
      */
     List<TrackedEntityAttribute> getTrackedEntityAttributesByDisplayOnVisitSchedule(
@@ -146,56 +140,51 @@
 
     /**
      * Get attributes which are displayed in visit schedule
-     * 
+     *
      * @param displayInListNoProgram True/False value
-     * 
      * @return List of attributes
      */
     List<TrackedEntityAttribute> getTrackedEntityAttributesWithoutProgram();
 
     /**
      * Get attributes which are displayed in visit schedule
-     * 
+     *
      * @param displayInListNoProgram True/False value
-     * 
      * @return List of attributes
      */
     List<TrackedEntityAttribute> getTrackedEntityAttributesDisplayInList();
 
     /**
      * Returns {@link TrackedEntityAttribute} list with paging
-     * 
+     *
      * @param name Keyword for searching by name
      * @param min
      * @param max
      * @return a collection of all TrackedEntityAttribute, or an empty
-     *         collection if there are no TrackedEntityAttributes.
+     * collection if there are no TrackedEntityAttributes.
      */
     List<TrackedEntityAttribute> getTrackedEntityAttributesBetweenByName( String name, int min, int max );
 
     /**
      * Returns The number of all TrackedEntityAttribute available
-     * 
      */
     int getTrackedEntityAttributeCount();
 
     /**
      * Returns {@link TrackedEntityAttribute} list with paging
-     * 
+     *
      * @param min
      * @param max
      * @return a collection of all TrackedEntityAttribute, or an empty
-     *         collection if there are no TrackedEntityAttributes.
+     * collection if there are no TrackedEntityAttributes.
      */
     List<TrackedEntityAttribute> getTrackedEntityAttributesBetween( int min, int max );
 
     /**
      * Returns The number of TrackedEntityAttributes with the key searched
-     * 
+     *
      * @param name Keyword for searching by name
-     * 
      * @return A number
-     * 
      */
     int getTrackedEntityAttributeCountByName( String name );
 
@@ -210,4 +199,13 @@
      */
     boolean validateScope( TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute trackedEntityAttribute,
         OrganisationUnit organisationUnit, String value );
+
+    /**
+     * Validate value against tracked entity attribute value type.
+     *
+     * @param trackedEntityAttribute TrackedEntityAttribute
+     * @param value                  Value
+     * @return null if valid, a message if not
+     */
+    String validateValueType( TrackedEntityAttribute trackedEntityAttribute, String value );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityAttributeService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityAttributeService.java	2015-09-30 05:42:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityAttributeService.java	2015-09-30 06:14:51 +0000
@@ -28,20 +28,24 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.common.QueryOperator;
+import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramService;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.system.util.MathUtils;
+import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * @author Abyot Asalefew
  */
@@ -70,6 +74,9 @@
     @Autowired
     private TrackedEntityInstanceService trackedEntityInstanceService;
 
+    @Autowired
+    private UserService userService;
+
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
@@ -190,7 +197,7 @@
     public int getTrackedEntityAttributeCountByName( String name )
     {
         return attributeStore.getCountLikeName( name );
-    }  
+    }
 
     @Override
     public boolean validateScope( TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute trackedEntityAttribute,
@@ -222,4 +229,46 @@
 
         return instances.getHeight() == 0 || instances.getHeight() == 1 && instances.getRow( 0 ).contains( trackedEntityInstance.getUid() );
     }
+
+    @Override
+    public String validateValueType( TrackedEntityAttribute trackedEntityAttribute, String value )
+    {
+        Assert.notNull( trackedEntityAttribute, "trackedEntityAttribute is required." );
+        ValueType valueType = trackedEntityAttribute.getValueType();
+
+        if ( value.length() > 255 )
+        {
+            return "Value length is greater than 256 chars for attribute " + trackedEntityAttribute.getUid();
+        }
+
+        if ( ValueType.NUMBER == valueType && !MathUtils.isNumeric( value ) )
+        {
+            return "Value is not numeric for attribute " + trackedEntityAttribute.getUid();
+        }
+        else if ( ValueType.BOOLEAN == valueType && !MathUtils.isBool( value ) )
+        {
+            return "Value is not boolean for attribute " + trackedEntityAttribute.getUid();
+        }
+        else if ( ValueType.DATE == valueType && !DateUtils.dateIsValid( value ) )
+        {
+            return "Value is not date for attribute " + trackedEntityAttribute.getUid();
+        }
+        else if ( ValueType.TRUE_ONLY == valueType && !"true".equals( value ) )
+        {
+            return "Value is not true (true-only value type) for attribute " + trackedEntityAttribute.getUid();
+        }
+        else if ( ValueType.USERNAME == valueType )
+        {
+            if ( userService.getUserCredentialsByUsername( value ) == null )
+            {
+                return "Value is not pointing to a valid username for attribute " + trackedEntityAttribute.getUid();
+            }
+        }
+        else if ( ValueType.OPTION_SET == valueType && !trackedEntityAttribute.isValidOptionValue( value ) )
+        {
+            return "Value is not pointing to a valid option for attribute " + trackedEntityAttribute.getUid();
+        }
+
+        return null;
+    }
 }

=== 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	2015-09-30 05:42:06 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2015-09-30 06:14:51 +0000
@@ -31,7 +31,6 @@
 import com.google.common.collect.Lists;
 import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.common.IdentifiableObjectManager;
-import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.commons.collection.CachingMap;
 import org.hisp.dhis.dbms.DbmsManager;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
@@ -43,8 +42,6 @@
 import org.hisp.dhis.relationship.RelationshipService;
 import org.hisp.dhis.relationship.RelationshipType;
 import org.hisp.dhis.system.callable.IdentifiableObjectSearchCallable;
-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;
@@ -442,37 +439,11 @@
             return importConflicts;
         }
 
-        if ( attribute.getValue().length() > 255 )
-        {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Value length is greater than 256 chars for attribute: " + attribute ) );
-        }
+        String message = trackedEntityAttributeService.validateValueType( teAttribute, attribute.getValue() );
 
-        if ( ValueType.NUMBER == teAttribute.getValueType() && !MathUtils.isNumeric( attribute.getValue() ) )
-        {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not numeric for attribute: " + attribute ) );
-        }
-        else if ( ValueType.BOOLEAN == teAttribute.getValueType() && !MathUtils.isBool( attribute.getValue() ) )
-        {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not boolean for attribute: " + attribute ) );
-        }
-        else if ( ValueType.DATE == teAttribute.getValueType() && !DateUtils.dateIsValid( attribute.getValue() ) )
-        {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not date for attribute: " + attribute ) );
-        }
-        else if ( ValueType.TRUE_ONLY == teAttribute.getValueType() && !"true".equals( attribute.getValue() ) )
-        {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not true (true-only value type) for attribute: " + attribute ) );
-        }
-        else if ( ValueType.USERNAME == teAttribute.getValueType() )
-        {
-            if ( userService.getUserCredentialsByUsername( attribute.getValue() ) == null )
-            {
-                importConflicts.add( new ImportConflict( "Attribute.value", "Value is not pointing to a valid username for attribute: " + attribute ) );
-            }
-        }
-        else if ( ValueType.OPTION_SET == teAttribute.getValueType() && !teAttribute.isValidOptionValue( attribute.getValue() ) )
-        {
-            importConflicts.add( new ImportConflict( "Attribute.value", "Value is not pointing to a valid option for attribute: " + attribute ) );
+        if ( message != null )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.value", message ) );
         }
 
         return importConflicts;