dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #01396
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 412: Code optimization in ExpressionService
------------------------------------------------------------
revno: 412
committer: Lars Helge Oeverland larshelge@xxxxxxxxx
branch nick: trunk
timestamp: Fri 2009-07-03 01:13:13 +0200
message:
Code optimization in ExpressionService
modified:
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java
dhis-2/pom.xml
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java 2009-07-01 18:52:56 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java 2009-07-02 23:13:13 +0000
@@ -66,6 +66,8 @@
private static final Log log = LogFactory.getLog( DefaultExpressionService.class );
private static final String NULL_REPLACEMENT = "0";
+ private static final String FORMULA_EXPRESSION = "(\\[\\d+\\" + SEPARATOR + "\\d+\\])";
+ private static final String DESCRIPTION_EXPRESSION = "\\[.+?\\" + SEPARATOR + ".+?\\]";
// -------------------------------------------------------------------------
// Dependencies
@@ -163,12 +165,7 @@
while ( matcher.find() )
{
- String replaceString = matcher.group();
-
- replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
- replaceString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );
-
- final DataElement dataElement = dataElementService.getDataElement( Integer.parseInt( replaceString ) );
+ final DataElement dataElement = dataElementService.getDataElement( getOperand( matcher.group() ).getDataElementId() );
if ( dataElement != null )
{
@@ -186,29 +183,26 @@
if ( expression != null )
{
- final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
+ final Matcher matcher = getMatcher( FORMULA_EXPRESSION, expression );
while ( matcher.find() )
{
String match = matcher.group();
- match = match.replaceAll( "[\\[\\]]", "" );
-
- final int dataElementId = Integer.parseInt( match.substring( 0, match.indexOf( SEPARATOR ) ) );
- final int categoryOptionComboId = Integer.parseInt( match.substring( match.indexOf( SEPARATOR ) + 1, match.length() ) );
+ final Operand operand = getOperand( match );
- final Integer mappedDataElementId = dataElementMapping.get( dataElementId );
- final Integer mappedCategoryOptionComboId = categoryOptionComboMapping.get( categoryOptionComboId );
+ final Integer mappedDataElementId = dataElementMapping.get( operand.getDataElementId() );
+ final Integer mappedCategoryOptionComboId = categoryOptionComboMapping.get( operand.getOptionComboId() );
if ( mappedDataElementId == null )
{
- log.info( "Data element identifier refers to non-existing object: " + dataElementId );
+ log.info( "Data element identifier refers to non-existing object: " + operand.getDataElementId() );
match = NULL_REPLACEMENT;
}
else if ( mappedCategoryOptionComboId == null )
{
- log.info( "Category option combo identifer refers to non-existing object: " + categoryOptionComboId );
+ log.info( "Category option combo identifer refers to non-existing object: " + operand.getOptionComboId() );
match = NULL_REPLACEMENT;
}
@@ -234,18 +228,11 @@
{
operandsInExpression = new HashSet<Operand>();
- final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
+ final Matcher matcher = getMatcher( FORMULA_EXPRESSION, expression );
while ( matcher.find() )
{
- final Operand operand = new Operand();
-
- final String match = matcher.group().replaceAll( "[\\[\\]]", "" );
-
- operand.setDataElementId( Integer.parseInt( match.substring( 0, match.indexOf( SEPARATOR ) ) ) );
- operand.setOptionComboId( Integer.parseInt( match.substring( match.indexOf( SEPARATOR ) + 1, match.length() ) ) );
-
- operandsInExpression.add( operand );
+ operandsInExpression.add( getOperand( matcher.group() ) );
}
}
@@ -256,7 +243,7 @@
{
StringBuffer buffer = new StringBuffer();
- final Matcher matcher = getMatcher( "\\[.+?\\" + SEPARATOR + ".+?\\]", formula );
+ final Matcher matcher = getMatcher( DESCRIPTION_EXPRESSION, formula );
int dataElementId = -1;
int categoryOptionComboId = -1;
@@ -324,55 +311,28 @@
{
buffer = new StringBuffer();
- final Matcher matcher = getMatcher( "\\[.+?\\" + SEPARATOR + ".+?\\]", formula );
-
- int dataElementId = -1;
- int categoryOptionComboId = -1;
-
- DataElement dataElement = null;
- DataElementCategoryOptionCombo categoryOptionCombo = null;
+ final Matcher matcher = getMatcher( DESCRIPTION_EXPRESSION, formula );
while ( matcher.find() )
{
String replaceString = matcher.group();
-
- replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
-
- final String dataElementIdString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );
- final String optionComboIdString = replaceString.substring( replaceString.indexOf( SEPARATOR ) + 1, replaceString.length() );
-
- try
- {
- dataElementId = Integer.parseInt( dataElementIdString );
- }
- catch ( NumberFormatException ex )
- {
- throw new IllegalArgumentException( "Data element identifier must be a number: " + replaceString );
- }
-
- try
- {
- categoryOptionComboId = Integer.parseInt( optionComboIdString );
- }
- catch ( NumberFormatException ex )
- {
- throw new IllegalArgumentException( "Category option combo identifier must be a number: "
- + replaceString );
- }
-
- dataElement = dataElementService.getDataElement( dataElementId );
- categoryOptionCombo = categoryOptionComboService.getDataElementCategoryOptionCombo( categoryOptionComboId );
+
+ final Operand operand = getOperand( replaceString );
+
+ final DataElement dataElement = dataElementService.getDataElement( operand.getDataElementId() );
+ final DataElementCategoryOptionCombo categoryOptionCombo =
+ categoryOptionComboService.getDataElementCategoryOptionCombo( operand.getOptionComboId() );
if ( dataElement == null )
{
throw new IllegalArgumentException( "Identifier does not reference a data element: "
- + dataElementId );
+ + operand.getDataElementId() );
}
if ( categoryOptionCombo == null )
{
throw new IllegalArgumentException( "Identifier does not reference a category option combo: "
- + categoryOptionComboId );
+ + operand.getOptionComboId() );
}
replaceString = dataElement.getName() + SEPARATOR + categoryOptionComboService.getOptionNames( categoryOptionCombo );
@@ -411,7 +371,7 @@
}
}
- final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
+ final Matcher matcher = getMatcher( FORMULA_EXPRESSION, expression );
while ( matcher.find() )
{
@@ -442,7 +402,7 @@
if ( expression != null )
{
- final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
+ final Matcher matcher = getMatcher( FORMULA_EXPRESSION, expression );
buffer = new StringBuffer();
@@ -450,28 +410,16 @@
{
String replaceString = matcher.group();
- replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
-
- final String dataElementIdString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );
- final String categoryOptionComboIdString = replaceString.substring( replaceString.indexOf( SEPARATOR ) + 1, replaceString.length() );
-
- final int dataElementId = Integer.parseInt( dataElementIdString );
- final int optionComboId = Integer.parseInt( categoryOptionComboIdString );
-
- final DataElement dataElement = dataElementService.getDataElement( dataElementId );
- final DataElementCategoryOptionCombo categoryOptionCombo = categoryOptionComboService.getDataElementCategoryOptionCombo( optionComboId );
+ final Operand operand = getOperand( replaceString );
+
+ final DataElement dataElement = dataElementService.getDataElement( operand.getDataElementId() );
+ final DataElementCategoryOptionCombo categoryOptionCombo =
+ categoryOptionComboService.getDataElementCategoryOptionCombo( operand.getOptionComboId() );
final DataValue dataValue = dataValueService.getDataValue( source, dataElement, period, categoryOptionCombo );
- if ( dataValue == null )
- {
- replaceString = NULL_REPLACEMENT;
- }
- else
- {
- replaceString = String.valueOf( dataValue.getValue() );
- }
-
+ replaceString = ( dataValue == null ) ? NULL_REPLACEMENT : dataValue.getValue();
+
matcher.appendReplacement( buffer, replaceString );
}
@@ -491,4 +439,14 @@
return pattern.matcher( expression );
}
+
+ private Operand getOperand( String formula )
+ {
+ formula = formula.replaceAll( "[\\[\\]]", "" );
+
+ final int dataElementId = Integer.parseInt( formula.substring( 0, formula.indexOf( SEPARATOR ) ) );
+ final int categoryOptionComboId = Integer.parseInt( formula.substring( formula.indexOf( SEPARATOR ) + 1, formula.length() ) );
+
+ return new Operand( dataElementId, categoryOptionComboId );
+ }
}
=== modified file 'dhis-2/pom.xml'
--- dhis-2/pom.xml 2009-06-30 14:52:21 +0000
+++ dhis-2/pom.xml 2009-07-02 23:13:13 +0000
@@ -24,6 +24,7 @@
<module>dhis-useradminandsecurity</module>
</modules>
+ <!--
<repositories>
<repository>
<id>amplecode_maven2_repo</id>
@@ -31,6 +32,7 @@
<url>http://www.amplecode.org/maven2</url>
</repository>
</repositories>
+ -->
<!--
<pluginRepositories>
--
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.