~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/util/ParserUtil.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.datamart.util;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.util.HashSet;
 
31
import java.util.Map;
 
32
import java.util.Set;
 
33
import java.util.regex.Matcher;
 
34
import java.util.regex.Pattern;
 
35
 
 
36
import org.hisp.dhis.dataelement.Operand;
 
37
 
 
38
import static org.hisp.dhis.expression.Expression.SEPARATOR;
 
39
 
 
40
/**
 
41
 * @author Lars Helge Overland
 
42
 * @version $Id: ParserUtil.java 5510 2008-07-30 16:30:27Z larshelg $
 
43
 */
 
44
public class ParserUtil
 
45
{
 
46
    private static final String NULL_REPLACEMENT = "0";    
 
47
    
 
48
    /**
 
49
     * Returns the data element identifiers in the given expression. Returns null
 
50
     * if the formula is null, returns an empty set if no data elements exist in
 
51
     * the formula, otherwise a set of the data elements in the formula.
 
52
     * 
 
53
     * @param formula The formula to be parsed.
 
54
     */
 
55
    public static Set<Integer> getDataElementIdsInExpression( String expression )
 
56
    {
 
57
        Set<Integer> dataElementIdsInExpression = null;
 
58
 
 
59
        if ( expression != null )
 
60
        {
 
61
            dataElementIdsInExpression = new HashSet<Integer>();
 
62
 
 
63
            final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
 
64
 
 
65
            while ( matcher.find() )
 
66
            {
 
67
                String replaceString = matcher.group();
 
68
 
 
69
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
 
70
 
 
71
                replaceString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );
 
72
 
 
73
                final int dataElementId = Integer.parseInt( replaceString );
 
74
 
 
75
                dataElementIdsInExpression.add( dataElementId );
 
76
            }
 
77
        }
 
78
 
 
79
        return dataElementIdsInExpression;
 
80
    }
 
81
    
 
82
    /**
 
83
     * Generates an expression where the Operand identifiers, consisting of 
 
84
     * data element id and category option combo id, are replaced
 
85
     * by the aggregated value for the relevant combination of data element,
 
86
     * period, and source.
 
87
     * 
 
88
     * @param formula The formula to parse.
 
89
     * @param valueMap The map containing data element identifiers and aggregated value.
 
90
     */
 
91
    public static String generateExpression( final String formula, final Map<Operand, Double> valueMap )
 
92
    {       
 
93
        try
 
94
        {           
 
95
            final Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
 
96
            
 
97
            final Matcher matcher = pattern.matcher( formula );
 
98
            
 
99
            final StringBuffer buffer = new StringBuffer();            
 
100
            
 
101
            Double aggregatedValue = null;
 
102
            
 
103
            while ( matcher.find() )
 
104
            {
 
105
                String replaceString = matcher.group();
 
106
                
 
107
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
 
108
                
 
109
                int dataElementId = Integer.parseInt( replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) ) );
 
110
                int categoryOptionComboId = Integer.parseInt( replaceString.substring( replaceString.indexOf( SEPARATOR ) + 1 ) );
 
111
                
 
112
                final Operand operand = new Operand( dataElementId, categoryOptionComboId );
 
113
                
 
114
                aggregatedValue = valueMap.get( operand );
 
115
                
 
116
                replaceString = ( aggregatedValue == null ) ? NULL_REPLACEMENT : String.valueOf( aggregatedValue );
 
117
                
 
118
                matcher.appendReplacement( buffer, replaceString );
 
119
            }
 
120
 
 
121
            matcher.appendTail( buffer );
 
122
 
 
123
            return buffer.toString();
 
124
        }
 
125
        catch ( NumberFormatException ex )
 
126
        {
 
127
            throw new RuntimeException( "Illegal data element id", ex );
 
128
        }
 
129
    }
 
130
 
 
131
    /**
 
132
     * Returns a matcher object compiled with the given regex and matched with the given expression.
 
133
     * 
 
134
     * @param regex The regular expression.
 
135
     * @param formula The formula.
 
136
     */
 
137
    public static Matcher getMatcher( final String regex, final String formula )
 
138
    {
 
139
        final Pattern pattern = Pattern.compile( regex );
 
140
        
 
141
        return pattern.matcher( formula );
 
142
    }    
 
143
}