dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #19434
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8467: Validation in data entry for custom data entry forms: The validation rules which are considered r...
------------------------------------------------------------
revno: 8467
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-10-10 20:17:25 +0200
message:
Validation in data entry for custom data entry forms: The validation rules which are considered relevant are now only those which contains operands (data element + option combo) which are actually present in the form markup. This to avoid validation rules to fail for a form when the actual operands are outside the form.
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java
dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.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/dataentryform/DataEntryFormService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java 2012-09-16 14:44:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java 2012-10-10 18:17:25 +0000
@@ -33,6 +33,7 @@
import java.util.regex.Pattern;
import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementOperand;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.i18n.I18n;
@@ -145,6 +146,8 @@
Set<DataElement> getDataElementsInDataEntryForm( DataSet dataSet );
+ Set<DataElementOperand> getOperandsInDataEntryForm( DataSet dataSet );
+
Collection<DataEntryForm> listDisctinctDataEntryFormByProgramStageIds( List<Integer> programStageIds );
Collection<DataEntryForm> listDisctinctDataEntryFormByDataSetIds( List<Integer> dataSetIds );
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2012-10-10 10:12:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2012-10-10 18:17:25 +0000
@@ -172,24 +172,24 @@
@Override
public Period createPeriod( String isoDate )
{
- Date date = null;
- try
- {
- date = new SimpleDateFormat( ISO_FORMAT ).parse( isoDate );
- return createPeriod( date );
- }
- catch ( ParseException ex1 )
- {
- // if at first you don't succeed ... try again
- try
- {
- date = new SimpleDateFormat( ALTERNATIVE_ISO_FORMAT ).parse( isoDate );
- return createPeriod( date );
- }
- catch ( ParseException ex2 )
- {
- throw new RuntimeException( ex2 );
- }
+ try
+ {
+ Date date = new SimpleDateFormat( ISO_FORMAT ).parse( isoDate );
+ return createPeriod( date );
+ }
+ catch ( ParseException ex )
+ {
+ // Ignore and try alternative format
+ }
+
+ try
+ {
+ Date date = new SimpleDateFormat( ALTERNATIVE_ISO_FORMAT ).parse( isoDate );
+ return createPeriod( date );
+ }
+ catch ( ParseException ex )
+ {
+ throw new RuntimeException( ex );
}
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java 2012-09-18 09:17:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java 2012-10-10 18:17:25 +0000
@@ -41,6 +41,7 @@
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementOperand;
import org.hisp.dhis.dataelement.DataElementService;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.i18n.I18n;
@@ -397,6 +398,37 @@
return dataElements;
}
+
+ public Set<DataElementOperand> getOperandsInDataEntryForm( DataSet dataSet )
+ {
+ if ( dataSet == null || dataSet.getDataEntryForm() == null )
+ {
+ return null;
+ }
+
+ Set<DataElementOperand> operands = new HashSet<DataElementOperand>();
+
+ Matcher inputMatcher = INPUT_PATTERN.matcher( dataSet.getDataEntryForm().getHtmlCode() );
+
+ while ( inputMatcher.find() )
+ {
+ String inputHtml = inputMatcher.group();
+
+ Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher( inputHtml );
+
+ if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 )
+ {
+ int dataElementId = Integer.parseInt( identifierMatcher.group( 1 ) );
+ int categoryOptionComboId = Integer.parseInt( identifierMatcher.group( 2 ) );
+
+ DataElementOperand operand = new DataElementOperand( dataElementId, categoryOptionComboId );
+
+ operands.add( operand );
+ }
+ }
+
+ return operands;
+ }
public Collection<DataEntryForm> listDisctinctDataEntryFormByProgramStageIds( List<Integer> programStageIds )
{
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java 2012-09-22 21:59:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java 2012-10-10 18:17:25 +0000
@@ -33,7 +33,8 @@
import static org.hisp.dhis.i18n.I18nUtils.getObjectsByName;
import static org.hisp.dhis.i18n.I18nUtils.i18n;
import static org.hisp.dhis.system.util.MathUtils.expressionIsTrue;
-import static org.hisp.dhis.system.util.MathUtils.*;
+import static org.hisp.dhis.system.util.MathUtils.getRounded;
+import static org.hisp.dhis.system.util.MathUtils.zeroIfNull;
import java.util.Collection;
import java.util.Date;
@@ -45,6 +46,7 @@
import org.hisp.dhis.constant.ConstantService;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementOperand;
+import org.hisp.dhis.dataentryform.DataEntryFormService;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.datavalue.DataValueService;
import org.hisp.dhis.expression.ExpressionService;
@@ -92,6 +94,13 @@
{
this.expressionService = expressionService;
}
+
+ private DataEntryFormService dataEntryFormService;
+
+ public void setDataEntryFormService( DataEntryFormService dataEntryFormService )
+ {
+ this.dataEntryFormService = dataEntryFormService;
+ }
private PeriodService periodService;
@@ -206,7 +215,16 @@
{
Map<Integer, Double> constantMap = constantService.getConstantMap();
- Collection<ValidationRule> relevantRules = getRelevantValidationRules( dataSet.getDataElements() );
+ Collection<ValidationRule> relevantRules = null;
+
+ if ( DataSet.TYPE_CUSTOM.equals( dataSet.getDataSetType() ) )
+ {
+ relevantRules = getRelevantValidationRules( dataSet );
+ }
+ else
+ {
+ relevantRules = getRelevantValidationRules( dataSet.getDataElements() );
+ }
Set<DataElement> dataElements = getDataElementsInValidationRules( relevantRules );
@@ -318,6 +336,29 @@
return relevantValidationRules;
}
+
+ public Collection<ValidationRule> getRelevantValidationRules( DataSet dataSet )
+ {
+ Set<ValidationRule> relevantValidationRules = new HashSet<ValidationRule>();
+
+ Set<DataElementOperand> operands = dataEntryFormService.getOperandsInDataEntryForm( dataSet );
+
+ Set<DataElementOperand> validationRuleOperands = new HashSet<DataElementOperand>();
+
+ for ( ValidationRule validationRule : getAllValidationRules() )
+ {
+ validationRuleOperands.clear();
+ validationRuleOperands.addAll( expressionService.getOperandsInExpression( validationRule.getLeftSide().getExpression() ) );
+ validationRuleOperands.addAll( expressionService.getOperandsInExpression( validationRule.getRightSide().getExpression() ) );
+
+ if ( operands.containsAll( validationRuleOperands ) )
+ {
+ relevantValidationRules.add( validationRule );
+ }
+ }
+
+ return relevantValidationRules;
+ }
/**
* Returns all validation rules referred to in the left and right side expressions
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2012-09-27 17:19:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2012-10-10 18:17:25 +0000
@@ -364,6 +364,7 @@
<property name="validationRuleStore" ref="org.hisp.dhis.validation.ValidationRuleStore" />
<property name="validationRuleGroupStore" ref="org.hisp.dhis.validation.ValidationRuleGroupStore" />
<property name="expressionService" ref="org.hisp.dhis.expression.ExpressionService" />
+ <property name="dataEntryFormService" ref="org.hisp.dhis.dataentryform.DataEntryFormService" />
<property name="periodService" ref="org.hisp.dhis.period.PeriodService" />
<property name="constantService" ref="org.hisp.dhis.constant.ConstantService" />
<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java 2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java 2012-10-10 18:17:25 +0000
@@ -34,11 +34,13 @@
import java.util.Collection;
import java.util.List;
+import java.util.Set;
import org.hisp.dhis.DhisSpringTest;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementOperand;
import org.hisp.dhis.dataelement.DataElementService;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.dataset.DataSetService;
@@ -243,12 +245,28 @@
assertEquals( dataSets.size(), 2 );
}
+
+ @Test
+ public void testGetOperands()
+ {
+ String html = "<table><tr><td><input id=\"1434-11-val\" style=\"width:4em;text-align:center\" /></td></tr></table>";
+ DataEntryForm dataEntryForm = new DataEntryForm( "FormA", html );
+ DataSet dataSet = createDataSet( 'A', new MonthlyPeriodType() );
+ dataSet.setDataEntryForm( dataEntryForm );
+
+ Set<DataElementOperand> operands = dataEntryFormService.getOperandsInDataEntryForm( dataSet );
+
+ DataElementOperand operand = new DataElementOperand( 1434, 11 );
+
+ assertEquals( 1, operands.size() );
+ assertTrue( operands.contains( operand ) );
+ }
@Test
public void testPrepareForSave()
{
- String html = "<table><tr><td><input id=\"value[1434].value:value[11].value\" style=\"width:4em;text-align:center\" title=\"[ 1434 - Expected Births - 11 - (default) - int ]\" value=\"[ Expected Births - (default) ]\" /></td></tr></table>";
- String expected = "<table><tr><td><input id=\"value[1434].value:value[11].value\" style=\"width:4em;text-align:center\" title=\"\" value=\"\" /></td></tr></table>";
+ String html = "<table><tr><td><input id=\"1434-11-val\" style=\"width:4em;text-align:center\" title=\"[ 1434 - Expected Births - 11 - (default) - int ]\" value=\"[ Expected Births - (default) ]\" /></td></tr></table>";
+ String expected = "<table><tr><td><input id=\"1434-11-val\" style=\"width:4em;text-align:center\" title=\"\" value=\"\" /></td></tr></table>";
String actual = dataEntryFormService.prepareDataEntryFormForSave( html );
assertEquals( expected, actual );
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java 2012-09-26 08:46:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java 2012-10-10 18:17:25 +0000
@@ -38,7 +38,6 @@
import org.hisp.dhis.system.scheduling.Scheduler;
import org.hisp.dhis.user.CurrentUserService;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;