dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #30214
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15322: TrackedEntityInstance, moved method from store to service layer
------------------------------------------------------------
revno: 15322
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-05-20 18:53:40 +0200
message:
TrackedEntityInstance, moved method from store to service layer
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java
dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.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/TrackedEntityInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java 2014-05-20 16:30:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java 2014-05-20 16:53:40 +0000
@@ -101,7 +101,7 @@
public static final int ERROR_ENROLLMENT = 2;
- public static final String SAPERATOR = "_";
+ public static final String SEPERATOR = "_";
/**
* Returns a grid with tracked entity instance values based on the given
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java 2014-05-20 16:40:25 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java 2014-05-20 16:53:40 +0000
@@ -33,10 +33,9 @@
import java.util.Map;
import org.hisp.dhis.common.GenericIdentifiableObjectStore;
-import org.hisp.dhis.i18n.I18nFormat;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
-import org.hisp.dhis.validation.ValidationCriteria;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
/**
* @author Abyot Asalefew Gizaw
@@ -75,29 +74,12 @@
Collection<TrackedEntityInstance> getRepresentatives( TrackedEntityInstance instances );
/**
- * Validate entity-instances attribute values and validation criteria by
- * program before registering / updating information
- *
- * @param entityinstance TrackedEntityInstance object
- * @param program Program which person needs to enroll. If this parameter is
- * null, the system check attribute values of the instances
- * @param format I18nFormat
- *
- * @return Error code 0 : Validation is OK 1_<duplicate-value> : The
- * attribute value is duplicated 2_<validation-criteria-id> :
- * Violate validation criteria of the program
- */
- String validate( TrackedEntityInstance entityinstance, Program program, I18nFormat format );
-
- /**
- * Validate entity-instance enrollment
- *
- * @param entityinstance TrackedEntityInstance object
- * @param program Program which person needs to enroll. If this parameter is
- * null, the system check identifiers of the patient
- * @param format I18nFormat
- *
- * @return ValidationCriteria object which is violated
- */
- ValidationCriteria validateEnrollment( TrackedEntityInstance entityinstance, Program program, I18nFormat format );
+ * Returns null if valid, a descriptive, non-null string if invalid.
+ *
+ * @param instance
+ * @param attributeValue
+ * @param program
+ * @return
+ */
+ String validate( TrackedEntityInstance instance, TrackedEntityAttributeValue attributeValue, Program program );
}
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java 2014-05-20 16:30:51 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java 2014-05-20 16:53:40 +0000
@@ -598,12 +598,91 @@
@Override
public String validateTrackedEntityInstance( TrackedEntityInstance instance, Program program, I18nFormat format )
{
- return trackedEntityInstanceStore.validate( instance, program, format );
+ if ( program != null )
+ {
+ ValidationCriteria validationCriteria = validateEnrollment( instance, program, format );
+
+ if ( validationCriteria != null )
+ {
+ return TrackedEntityInstanceService.ERROR_ENROLLMENT + TrackedEntityInstanceService.SEPERATOR
+ + validationCriteria.getId();
+ }
+ }
+
+ if ( instance.getAttributeValues() != null && instance.getAttributeValues().size() > 0 )
+ {
+ for ( TrackedEntityAttributeValue attributeValue : instance.getAttributeValues() )
+ {
+ String valid = trackedEntityInstanceStore.validate( instance, attributeValue, program );
+
+ if ( valid != null )
+ {
+ return valid;
+ }
+ }
+ }
+
+ return TrackedEntityInstanceService.ERROR_NONE + "";
}
-
+
@Override
public ValidationCriteria validateEnrollment( TrackedEntityInstance instance, Program program, I18nFormat format )
{
- return trackedEntityInstanceStore.validateEnrollment( instance, program, format );
+ for ( ValidationCriteria criteria : program.getValidationCriteria() )
+ {
+ String value = "";
+
+ for ( TrackedEntityAttributeValue attributeValue : instance.getAttributeValues() )
+ {
+ if ( attributeValue.getAttribute().getUid().equals( criteria.getProperty() ) )
+ {
+ value = attributeValue.getValue();
+
+ String type = attributeValue.getAttribute().getValueType();
+
+ // For integer type
+ if ( type.equals( TrackedEntityAttribute.TYPE_NUMBER ) )
+ {
+ int value1 = Integer.parseInt( value );
+ int value2 = Integer.parseInt( criteria.getValue() );
+
+ if ( (criteria.getOperator() == ValidationCriteria.OPERATOR_LESS_THAN && value1 >= value2)
+ || (criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO && value1 != value2)
+ || (criteria.getOperator() == ValidationCriteria.OPERATOR_GREATER_THAN && value1 <= value2) )
+ {
+ return criteria;
+ }
+ }
+ // For Date type
+ else if ( type.equals( TrackedEntityAttribute.TYPE_DATE ) )
+ {
+ Date value1 = format.parseDate( value );
+ Date value2 = format.parseDate( criteria.getValue() );
+ int i = value1.compareTo( value2 );
+
+ if ( i != criteria.getOperator() )
+ {
+ return criteria;
+ }
+ }
+ // For other types
+ else
+ {
+ if ( criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO
+ && !value.equals( criteria.getValue() ) )
+ {
+ return criteria;
+ }
+
+ }
+
+ }
+ }
+
+ }
+
+ // Return null if all criteria are met
+
+ return null;
}
}
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java 2014-05-20 16:40:25 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java 2014-05-20 16:53:40 +0000
@@ -44,7 +44,6 @@
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -60,7 +59,6 @@
import org.hisp.dhis.common.SetMap;
import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
import org.hisp.dhis.event.EventStatus;
-import org.hisp.dhis.i18n.I18nFormat;
import org.hisp.dhis.jdbc.StatementBuilder;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
@@ -74,7 +72,6 @@
import org.hisp.dhis.trackedentity.TrackedEntityInstanceService;
import org.hisp.dhis.trackedentity.TrackedEntityInstanceStore;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
-import org.hisp.dhis.validation.ValidationCriteria;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.transaction.annotation.Transactional;
@@ -421,127 +418,46 @@
return getQuery( hql ).setEntity( "representative", instance ).list();
}
- public String validate( TrackedEntityInstance instance, Program program, I18nFormat format )
- {
- if ( instance.getAttributeValues() != null && instance.getAttributeValues().size() > 0 )
- {
-
- for ( TrackedEntityAttributeValue attributeValue : instance.getAttributeValues() )
- {
- TrackedEntityAttribute attribute = attributeValue.getAttribute();
-
- if ( attribute.isUnique() )
- {
- Criteria criteria = getCriteria();
- criteria.add( Restrictions.ne( "id", instance.getId() ) );
- criteria.createAlias( "attributeValues", "attributeValue" );
- criteria.createAlias( "attributeValue.attribute", "attribute" );
- criteria.add( Restrictions.eq( "attributeValue.value", attributeValue.getValue() ) );
- criteria.add( Restrictions.eq( "attributeValue.attribute", attribute ) );
-
- if ( attribute.getId() != 0 )
- {
- criteria.add( Restrictions.ne( "id", attribute.getId() ) );
- }
-
- if ( attribute.getOrgunitScope() )
- {
- criteria.add( Restrictions.eq( "organisationUnit", instance.getOrganisationUnit() ) );
- }
-
- if ( program != null && attribute.getProgramScope() )
- {
- criteria.createAlias( "programInstances", "programInstance" );
- criteria.add( Restrictions.eq( "programInstance.program", program ) );
- }
-
- Number rs = (Number) criteria.setProjection(
- Projections.projectionList().add( Projections.property( "attribute.id" ) ) ).uniqueResult();
-
- if ( rs != null && rs.intValue() > 0 )
- {
- return TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER
- + TrackedEntityInstanceService.SAPERATOR + rs.intValue();
- }
- }
- }
- }
-
- if ( program != null )
- {
- ValidationCriteria validationCriteria = validateEnrollment( instance, program, format );
-
- if ( validationCriteria != null )
- {
- return TrackedEntityInstanceService.ERROR_ENROLLMENT + TrackedEntityInstanceService.SAPERATOR
- + validationCriteria.getId();
- }
- }
-
- return TrackedEntityInstanceService.ERROR_NONE + "";
- }
-
- public ValidationCriteria validateEnrollment( TrackedEntityInstance instance, Program program, I18nFormat format )
- {
- try
- {
- for ( ValidationCriteria criteria : program.getValidationCriteria() )
- {
- String value = "";
- for ( TrackedEntityAttributeValue attributeValue : instance.getAttributeValues() )
- {
- if ( attributeValue.getAttribute().getUid().equals( criteria.getProperty() ) )
- {
- value = attributeValue.getValue();
-
- String type = attributeValue.getAttribute().getValueType();
- // For integer type
- if ( type.equals( TrackedEntityAttribute.TYPE_NUMBER ) )
- {
- int value1 = Integer.parseInt( value );
- int value2 = Integer.parseInt( criteria.getValue() );
-
- if ( (criteria.getOperator() == ValidationCriteria.OPERATOR_LESS_THAN && value1 >= value2)
- || (criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO && value1 != value2)
- || (criteria.getOperator() == ValidationCriteria.OPERATOR_GREATER_THAN && value1 <= value2) )
- {
- return criteria;
- }
- }
- // For Date type
- else if ( type.equals( TrackedEntityAttribute.TYPE_DATE ) )
- {
- Date value1 = format.parseDate( value );
- Date value2 = format.parseDate( criteria.getValue() );
- int i = value1.compareTo( value2 );
- if ( i != criteria.getOperator() )
- {
- return criteria;
- }
- }
- // For other types
- else
- {
- if ( criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO
- && !value.equals( criteria.getValue() ) )
- {
- return criteria;
- }
-
- }
-
- }
- }
-
- }
-
- // Return null if all criteria are met
-
- return null;
- }
- catch ( Exception ex )
- {
- throw new RuntimeException( ex );
- }
+ @Override
+ public String validate( TrackedEntityInstance instance, TrackedEntityAttributeValue attributeValue, Program program )
+ {
+ TrackedEntityAttribute attribute = attributeValue.getAttribute();
+
+ if ( attribute.isUnique() )
+ {
+ Criteria criteria = getCriteria();
+ criteria.add( Restrictions.ne( "id", instance.getId() ) );
+ criteria.createAlias( "attributeValues", "attributeValue" );
+ criteria.createAlias( "attributeValue.attribute", "attribute" );
+ criteria.add( Restrictions.eq( "attributeValue.value", attributeValue.getValue() ) );
+ criteria.add( Restrictions.eq( "attributeValue.attribute", attribute ) );
+
+ if ( attribute.getId() != 0 )
+ {
+ criteria.add( Restrictions.ne( "id", attribute.getId() ) );
+ }
+
+ if ( attribute.getOrgunitScope() )
+ {
+ criteria.add( Restrictions.eq( "organisationUnit", instance.getOrganisationUnit() ) );
+ }
+
+ if ( program != null && attribute.getProgramScope() )
+ {
+ criteria.createAlias( "programInstances", "programInstance" );
+ criteria.add( Restrictions.eq( "programInstance.program", program ) );
+ }
+
+ Number rs = (Number) criteria.setProjection(
+ Projections.projectionList().add( Projections.property( "attribute.id" ) ) ).uniqueResult();
+
+ if ( rs != null && rs.intValue() > 0 )
+ {
+ return TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER
+ + TrackedEntityInstanceService.SEPERATOR + rs.intValue();
+ }
+ }
+
+ return null;
}
}