1
package org.hisp.dhis.importexport.dxf.converter;
4
* Copyright (c) 2004-2007, University of Oslo
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.
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.
30
import java.util.Collection;
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;
47
* @author Lars Helge Overland
48
* @version $Id: ValidationRuleConverter.java 6455 2008-11-24 08:59:37Z larshelg $
50
public class ValidationRuleConverter
51
extends AbstractValidationRuleConverter implements XMLConverter
53
public static final String COLLECTION_NAME = "validationRules";
54
public static final String ELEMENT_NAME = "validationRule";
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";
65
// -------------------------------------------------------------------------
67
// -------------------------------------------------------------------------
69
private Map<Object, Integer> dataElementMapping;
70
private Map<Object, Integer> categoryOptionComboMapping;
72
// -------------------------------------------------------------------------
74
// -------------------------------------------------------------------------
77
* Constructor for write operations.
79
public ValidationRuleConverter()
84
* Constructor for read operations.
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.
91
public ValidationRuleConverter( ImportObjectService importObjectService,
92
ValidationRuleService validationRuleService,
93
ExpressionService expressionService,
94
Map<Object, Integer> dataElementMapping,
95
Map<Object, Integer> categoryOptionComboMapping )
97
this.importObjectService = importObjectService;
98
this.validationRuleService = validationRuleService;
99
this.expressionService = expressionService;
100
this.dataElementMapping = dataElementMapping;
101
this.categoryOptionComboMapping = categoryOptionComboMapping;
104
// -------------------------------------------------------------------------
105
// XMLConverter implementation
106
// -------------------------------------------------------------------------
108
public void write( XMLWriter writer, ExportParams params )
110
Collection<ValidationRule> validationRules = params.getValidationRules();
112
if ( validationRules != null && validationRules.size() > 0 )
114
writer.openElement( COLLECTION_NAME );
116
for ( ValidationRule rule : validationRules )
118
writer.openElement( ELEMENT_NAME );
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() );
129
writer.closeElement();
132
writer.closeElement();
136
public void read( XMLReader reader, ImportParams params )
138
while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
140
final ValidationRule validationRule = new ValidationRule();
142
final Expression leftSide = new Expression();
143
final Expression rightSide = new Expression();
145
validationRule.setLeftSide( leftSide );
146
validationRule.setRightSide( rightSide );
148
reader.moveToStartElement( FIELD_NAME );
149
validationRule.setName( reader.getElementValue() );
151
reader.moveToStartElement( FIELD_DESCRIPTION );
152
validationRule.setDescription( reader.getElementValue() );
154
reader.moveToStartElement( FIELD_TYPE );
155
validationRule.setType( reader.getElementValue() );
157
reader.moveToStartElement( FIELD_OPERATOR );
158
validationRule.setOperator( reader.getElementValue() );
160
reader.moveToStartElement( FIELD_LEFTSIDE_EXPRESSION );
161
validationRule.getLeftSide().setExpression( expressionService.convertExpression(
162
reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
164
reader.moveToStartElement( FIELD_LEFTSIDE_DESCRIPTION );
165
validationRule.getLeftSide().setDescription( reader.getElementValue() );
167
validationRule.getLeftSide().setDataElementsInExpression(
168
expressionService.getDataElementsInExpression( validationRule.getLeftSide().getExpression() ) );
170
reader.moveToStartElement( FIELD_RIGHTSIDE_EXPRESSION );
171
validationRule.getRightSide().setExpression( expressionService.convertExpression(
172
reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
174
reader.moveToStartElement( FIELD_RIGHTSIDE_DESCRIPTION );
175
validationRule.getRightSide().setDescription( reader.getElementValue() );
177
validationRule.getRightSide().setDataElementsInExpression(
178
expressionService.getDataElementsInExpression( validationRule.getRightSide().getExpression() ) );
180
read( validationRule, GroupMemberType.NONE, params );