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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf/converter/ValidationRuleConverter.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.importexport.dxf.converter;
 
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.Collection;
 
31
import java.util.Map;
 
32
 
 
33
import org.amplecode.staxwax.reader.XMLReader;
 
34
import org.amplecode.staxwax.writer.XMLWriter;
 
35
import org.hisp.dhis.expression.Expression;
 
36
import org.hisp.dhis.expression.ExpressionService;
 
37
import org.hisp.dhis.importexport.ExportParams;
 
38
import org.hisp.dhis.importexport.GroupMemberType;
 
39
import org.hisp.dhis.importexport.ImportObjectService;
 
40
import org.hisp.dhis.importexport.ImportParams;
 
41
import org.hisp.dhis.importexport.XMLConverter;
 
42
import org.hisp.dhis.importexport.converter.AbstractValidationRuleConverter;
 
43
import org.hisp.dhis.validation.ValidationRule;
 
44
import org.hisp.dhis.validation.ValidationRuleService;
 
45
 
 
46
/**
 
47
 * @author Lars Helge Overland
 
48
 * @version $Id: ValidationRuleConverter.java 6455 2008-11-24 08:59:37Z larshelg $
 
49
 */
 
50
public class ValidationRuleConverter
 
51
    extends AbstractValidationRuleConverter implements XMLConverter
 
52
{
 
53
    public static final String COLLECTION_NAME = "validationRules";
 
54
    public static final String ELEMENT_NAME = "validationRule";
 
55
 
 
56
    private static final String FIELD_NAME = "name";
 
57
    private static final String FIELD_DESCRIPTION = "description";
 
58
    private static final String FIELD_TYPE = "type";
 
59
    private static final String FIELD_OPERATOR = "operator";
 
60
    private static final String FIELD_LEFTSIDE_EXPRESSION = "leftSideExpression";
 
61
    private static final String FIELD_LEFTSIDE_DESCRIPTION = "leftSideDescription";
 
62
    private static final String FIELD_RIGHTSIDE_EXPRESSION = "rightSideExpression";
 
63
    private static final String FIELD_RIGHTSIDE_DESCRIPTION = "rightSideDescription";
 
64
 
 
65
    // -------------------------------------------------------------------------
 
66
    // Properties
 
67
    // -------------------------------------------------------------------------
 
68
 
 
69
    private Map<Object, Integer> dataElementMapping;
 
70
    private Map<Object, Integer> categoryOptionComboMapping;
 
71
    
 
72
    // -------------------------------------------------------------------------
 
73
    // Constructor
 
74
    // -------------------------------------------------------------------------
 
75
 
 
76
    /**
 
77
     * Constructor for write operations.
 
78
     */
 
79
    public ValidationRuleConverter()
 
80
    {   
 
81
    }
 
82
    
 
83
    /**
 
84
     * Constructor for read operations.
 
85
     * 
 
86
     * @param importObjectService the importObjectService to use.
 
87
     * @param validationRuleService the ValidationRuleService to use.
 
88
     * @param expressionService the expressionService to use.
 
89
     * @param dataElementMapping the data element mapping to use.
 
90
     */
 
91
    public ValidationRuleConverter( ImportObjectService importObjectService, 
 
92
        ValidationRuleService validationRuleService,
 
93
        ExpressionService expressionService,
 
94
        Map<Object, Integer> dataElementMapping,
 
95
        Map<Object, Integer> categoryOptionComboMapping )
 
96
    {
 
97
        this.importObjectService = importObjectService;
 
98
        this.validationRuleService = validationRuleService;
 
99
        this.expressionService = expressionService;
 
100
        this.dataElementMapping = dataElementMapping;
 
101
        this.categoryOptionComboMapping = categoryOptionComboMapping;
 
102
    }
 
103
 
 
104
    // -------------------------------------------------------------------------
 
105
    // XMLConverter implementation
 
106
    // -------------------------------------------------------------------------
 
107
 
 
108
    public void write( XMLWriter writer, ExportParams params )
 
109
    {
 
110
        Collection<ValidationRule> validationRules = params.getValidationRules();
 
111
        
 
112
        if ( validationRules != null && validationRules.size() > 0 )
 
113
        {
 
114
            writer.openElement( COLLECTION_NAME );
 
115
            
 
116
            for ( ValidationRule rule : validationRules )
 
117
            {
 
118
                writer.openElement( ELEMENT_NAME );
 
119
                
 
120
                writer.writeElement( FIELD_NAME, rule.getName() );
 
121
                writer.writeElement( FIELD_DESCRIPTION, rule.getDescription() );
 
122
                writer.writeElement( FIELD_TYPE, rule.getType() );
 
123
                writer.writeElement( FIELD_OPERATOR, rule.getOperator() );
 
124
                writer.writeElement( FIELD_LEFTSIDE_EXPRESSION, rule.getLeftSide().getExpression() );
 
125
                writer.writeElement( FIELD_LEFTSIDE_DESCRIPTION, rule.getLeftSide().getDescription() );
 
126
                writer.writeElement( FIELD_RIGHTSIDE_EXPRESSION, rule.getRightSide().getExpression() );
 
127
                writer.writeElement( FIELD_RIGHTSIDE_DESCRIPTION, rule.getRightSide().getDescription() );
 
128
                
 
129
                writer.closeElement();
 
130
            }
 
131
            
 
132
            writer.closeElement();
 
133
        }
 
134
    }
 
135
    
 
136
    public void read( XMLReader reader, ImportParams params )
 
137
    {
 
138
        while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
 
139
        {
 
140
            final ValidationRule validationRule = new ValidationRule();
 
141
            
 
142
            final Expression leftSide = new Expression();
 
143
            final Expression rightSide = new Expression();
 
144
            
 
145
            validationRule.setLeftSide( leftSide );
 
146
            validationRule.setRightSide( rightSide );
 
147
            
 
148
            reader.moveToStartElement( FIELD_NAME );
 
149
            validationRule.setName( reader.getElementValue() );
 
150
 
 
151
            reader.moveToStartElement( FIELD_DESCRIPTION );
 
152
            validationRule.setDescription( reader.getElementValue() );
 
153
 
 
154
            reader.moveToStartElement( FIELD_TYPE );
 
155
            validationRule.setType( reader.getElementValue() );
 
156
 
 
157
            reader.moveToStartElement( FIELD_OPERATOR );
 
158
            validationRule.setOperator( reader.getElementValue() );
 
159
            
 
160
            reader.moveToStartElement( FIELD_LEFTSIDE_EXPRESSION );
 
161
            validationRule.getLeftSide().setExpression( expressionService.convertExpression( 
 
162
                reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
 
163
            
 
164
            reader.moveToStartElement( FIELD_LEFTSIDE_DESCRIPTION );
 
165
            validationRule.getLeftSide().setDescription( reader.getElementValue() );
 
166
 
 
167
            validationRule.getLeftSide().setDataElementsInExpression( 
 
168
                expressionService.getDataElementsInExpression( validationRule.getLeftSide().getExpression() ) );
 
169
            
 
170
            reader.moveToStartElement( FIELD_RIGHTSIDE_EXPRESSION );
 
171
            validationRule.getRightSide().setExpression( expressionService.convertExpression( 
 
172
                reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
 
173
            
 
174
            reader.moveToStartElement( FIELD_RIGHTSIDE_DESCRIPTION );
 
175
            validationRule.getRightSide().setDescription( reader.getElementValue() );
 
176
            
 
177
            validationRule.getRightSide().setDataElementsInExpression(
 
178
                expressionService.getDataElementsInExpression( validationRule.getRightSide().getExpression() ) );
 
179
            
 
180
            read( validationRule, GroupMemberType.NONE, params );
 
181
        }
 
182
    }
 
183
}