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.ExpressionService;
36
import org.hisp.dhis.importexport.ExportParams;
37
import org.hisp.dhis.importexport.GroupMemberType;
38
import org.hisp.dhis.importexport.ImportObjectService;
39
import org.hisp.dhis.importexport.ImportParams;
40
import org.hisp.dhis.importexport.XMLConverter;
41
import org.hisp.dhis.importexport.converter.AbstractIndicatorConverter;
42
import org.hisp.dhis.importexport.mapping.NameMappingUtil;
43
import org.hisp.dhis.indicator.Indicator;
44
import org.hisp.dhis.indicator.IndicatorService;
45
import org.hisp.dhis.indicator.IndicatorType;
46
import org.hisp.dhis.jdbc.BatchHandler;
49
* @author Lars Helge Overland
50
* @version $Id: IndicatorConverter.java 6455 2008-11-24 08:59:37Z larshelg $
52
public class IndicatorConverter
53
extends AbstractIndicatorConverter implements XMLConverter
55
public static final String COLLECTION_NAME = "indicators";
56
public static final String ELEMENT_NAME = "indicator";
58
private static final String FIELD_ID = "id";
59
private static final String FIELD_UUID = "uuid";
60
private static final String FIELD_NAME = "name";
61
private static final String FIELD_ALTERNATIVE_NAME = "alternativeName";
62
private static final String FIELD_SHORT_NAME = "shortName";
63
private static final String FIELD_CODE = "code";
64
private static final String FIELD_DESCRIPTION = "description";
65
private static final String FIELD_ANNUALIZED = "annualized";
66
private static final String FIELD_INDICATOR_TYPE = "indicatorType";
67
private static final String FIELD_NUMERATOR = "numerator";
68
private static final String FIELD_NUMERATOR_DESCRIPTION = "numeratorDescription";
69
private static final String FIELD_NUMERATOR_AGGREGATION_OPERATOR = "numeratorAggregationOperator";
70
private static final String FIELD_DENOMINATOR = "denominator";
71
private static final String FIELD_DENOMINATOR_DESCRIPTION = "denominatorDescription";
72
private static final String FIELD_DENOMINATOR_AGGREGATION_OPERATOR = "denominatorAggregationOperator";
74
// -------------------------------------------------------------------------
76
// -------------------------------------------------------------------------
78
private ExpressionService expressionService;
80
private Map<Object, Integer> indicatorTypeMapping;
82
private Map<Object, Integer> dataElementMapping;
84
private Map<Object, Integer> categoryOptionComboMapping;
86
// -------------------------------------------------------------------------
88
// -------------------------------------------------------------------------
91
* Constructor for write operations.
93
public IndicatorConverter()
98
* Constructor for read operations.
100
* @param batchHandler the batchHandler to use.
101
* @param importObjectService the importObjectService to use.
102
* @param indicatorService the indicatorService to use.
103
* @param expressionService the expressionService to use.
104
* @param indicatorTypeMapping the indicatorTypeMapping to use.
105
* @param dataElementMapping the dataElementMapping to use.
106
* @param categoryOptionComboMapping the categoryOptionComboMapping to use.
108
public IndicatorConverter( BatchHandler batchHandler,
109
ImportObjectService importObjectService,
110
IndicatorService indicatorService,
111
ExpressionService expressionService,
112
Map<Object, Integer> indicatorTypeMapping,
113
Map<Object, Integer> dataElementMapping,
114
Map<Object, Integer> categoryOptionComboMapping )
116
this.batchHandler = batchHandler;
117
this.importObjectService = importObjectService;
118
this.indicatorService = indicatorService;
119
this.expressionService = expressionService;
120
this.indicatorTypeMapping = indicatorTypeMapping;
121
this.dataElementMapping = dataElementMapping;
122
this.categoryOptionComboMapping = categoryOptionComboMapping;
125
// -------------------------------------------------------------------------
126
// XMLConverter implementation
127
// -------------------------------------------------------------------------
129
public void write( XMLWriter writer, ExportParams params )
131
Collection<Indicator> indicators = params.getIndicators();
133
if ( indicators != null && indicators.size() > 0 )
135
writer.openElement( COLLECTION_NAME );
137
for ( Indicator indicator : indicators )
139
writer.openElement( ELEMENT_NAME );
141
writer.writeElement( FIELD_ID, String.valueOf( indicator.getId() ) );
142
writer.writeElement( FIELD_UUID, indicator.getUuid() );
143
writer.writeElement( FIELD_NAME, indicator.getName() );
144
writer.writeElement( FIELD_ALTERNATIVE_NAME, indicator.getAlternativeName() );
145
writer.writeElement( FIELD_SHORT_NAME, indicator.getShortName() );
146
writer.writeElement( FIELD_CODE, indicator.getCode() );
147
writer.writeElement( FIELD_DESCRIPTION, indicator.getDescription() );
148
writer.writeElement( FIELD_ANNUALIZED, String.valueOf( indicator.getAnnualized() ) );
149
writer.writeElement( FIELD_INDICATOR_TYPE, String.valueOf( indicator.getIndicatorType().getId() ) );
150
writer.writeElement( FIELD_NUMERATOR, indicator.getNumerator() );
151
writer.writeElement( FIELD_NUMERATOR_DESCRIPTION, indicator.getNumeratorDescription() );
152
writer.writeElement( FIELD_NUMERATOR_AGGREGATION_OPERATOR, indicator.getNumeratorAggregationOperator() );
153
writer.writeElement( FIELD_DENOMINATOR, indicator.getDenominator() );
154
writer.writeElement( FIELD_DENOMINATOR_DESCRIPTION, indicator.getDenominatorDescription() );
155
writer.writeElement( FIELD_DENOMINATOR_AGGREGATION_OPERATOR, indicator.getDenominatorAggregationOperator() );
157
writer.closeElement();
160
writer.closeElement();
164
public void read( XMLReader reader, ImportParams params )
166
while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
168
final Indicator indicator = new Indicator();
170
final IndicatorType type = new IndicatorType();
171
indicator.setIndicatorType( type );
173
reader.moveToStartElement( FIELD_ID );
174
indicator.setId( Integer.parseInt( reader.getElementValue() ) );
176
reader.moveToStartElement( FIELD_UUID );
177
indicator.setUuid( reader.getElementValue() );
179
reader.moveToStartElement( FIELD_NAME );
180
indicator.setName( reader.getElementValue() );
182
reader.moveToStartElement( FIELD_ALTERNATIVE_NAME );
183
indicator.setAlternativeName( reader.getElementValue() );
185
reader.moveToStartElement( FIELD_SHORT_NAME );
186
indicator.setShortName( reader.getElementValue() );
188
reader.moveToStartElement( FIELD_CODE );
189
indicator.setCode( reader.getElementValue() );
191
reader.moveToStartElement( FIELD_DESCRIPTION );
192
indicator.setDescription( reader.getElementValue() );
194
reader.moveToStartElement( FIELD_ANNUALIZED );
195
indicator.setAnnualized( Boolean.parseBoolean( reader.getElementValue() ) );
197
reader.moveToStartElement( FIELD_INDICATOR_TYPE );
198
indicator.getIndicatorType().setId( indicatorTypeMapping.get( Integer.parseInt( reader.getElementValue() ) ) );
200
reader.moveToStartElement( FIELD_NUMERATOR );
201
indicator.setNumerator( expressionService.convertExpression( reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
203
reader.moveToStartElement( FIELD_NUMERATOR_DESCRIPTION );
204
indicator.setNumeratorDescription( reader.getElementValue() );
206
reader.moveToStartElement( FIELD_NUMERATOR_AGGREGATION_OPERATOR );
207
indicator.setNumeratorAggregationOperator( reader.getElementValue() );
209
reader.moveToStartElement( FIELD_DENOMINATOR );
210
indicator.setDenominator( expressionService.convertExpression( reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
212
reader.moveToStartElement( FIELD_DENOMINATOR_DESCRIPTION );
213
indicator.setDenominatorDescription( reader.getElementValue() );
215
reader.moveToStartElement( FIELD_DENOMINATOR_AGGREGATION_OPERATOR );
216
indicator.setDenominatorAggregationOperator( reader.getElementValue() );
218
NameMappingUtil.addIndicatorMapping( indicator.getId(), indicator.getName() );
220
read( indicator, GroupMemberType.NONE, params );
b'\\ No newline at end of file'