~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/IndicatorConverter.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.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;
 
47
 
 
48
/**
 
49
 * @author Lars Helge Overland
 
50
 * @version $Id: IndicatorConverter.java 6455 2008-11-24 08:59:37Z larshelg $
 
51
 */
 
52
public class IndicatorConverter
 
53
    extends AbstractIndicatorConverter implements XMLConverter
 
54
{
 
55
    public static final String COLLECTION_NAME = "indicators";
 
56
    public static final String ELEMENT_NAME = "indicator";
 
57
    
 
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";
 
73
    
 
74
    // -------------------------------------------------------------------------
 
75
    // Properties
 
76
    // -------------------------------------------------------------------------
 
77
 
 
78
    private ExpressionService expressionService;
 
79
    
 
80
    private Map<Object, Integer> indicatorTypeMapping;
 
81
    
 
82
    private Map<Object, Integer> dataElementMapping;
 
83
    
 
84
    private Map<Object, Integer> categoryOptionComboMapping;
 
85
    
 
86
    // -------------------------------------------------------------------------
 
87
    // Constructor
 
88
    // -------------------------------------------------------------------------
 
89
 
 
90
    /**
 
91
     * Constructor for write operations.
 
92
     */
 
93
    public IndicatorConverter()
 
94
    {   
 
95
    }
 
96
    
 
97
    /**
 
98
     * Constructor for read operations.
 
99
     * 
 
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.
 
107
     */
 
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 )
 
115
    {
 
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;
 
123
    }    
 
124
 
 
125
    // -------------------------------------------------------------------------
 
126
    // XMLConverter implementation
 
127
    // -------------------------------------------------------------------------
 
128
 
 
129
    public void write( XMLWriter writer, ExportParams params )
 
130
    {
 
131
        Collection<Indicator> indicators = params.getIndicators();
 
132
        
 
133
        if ( indicators != null && indicators.size() > 0 )
 
134
        {
 
135
            writer.openElement( COLLECTION_NAME );
 
136
            
 
137
            for ( Indicator indicator : indicators )
 
138
            {
 
139
                writer.openElement( ELEMENT_NAME );
 
140
                
 
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() );
 
156
                            
 
157
                writer.closeElement();
 
158
            }
 
159
            
 
160
            writer.closeElement();
 
161
        }
 
162
    }
 
163
    
 
164
    public void read( XMLReader reader, ImportParams params )
 
165
    {
 
166
        while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
 
167
        {
 
168
            final Indicator indicator = new Indicator();
 
169
            
 
170
            final IndicatorType type = new IndicatorType();
 
171
            indicator.setIndicatorType( type );
 
172
            
 
173
            reader.moveToStartElement( FIELD_ID );
 
174
            indicator.setId( Integer.parseInt( reader.getElementValue() ) );
 
175
            
 
176
            reader.moveToStartElement( FIELD_UUID );            
 
177
            indicator.setUuid( reader.getElementValue() );
 
178
            
 
179
            reader.moveToStartElement( FIELD_NAME );
 
180
            indicator.setName( reader.getElementValue() );
 
181
            
 
182
            reader.moveToStartElement( FIELD_ALTERNATIVE_NAME );
 
183
            indicator.setAlternativeName( reader.getElementValue() );
 
184
 
 
185
            reader.moveToStartElement( FIELD_SHORT_NAME );
 
186
            indicator.setShortName( reader.getElementValue() );
 
187
 
 
188
            reader.moveToStartElement( FIELD_CODE );
 
189
            indicator.setCode( reader.getElementValue() );
 
190
 
 
191
            reader.moveToStartElement( FIELD_DESCRIPTION );
 
192
            indicator.setDescription( reader.getElementValue() );
 
193
 
 
194
            reader.moveToStartElement( FIELD_ANNUALIZED );
 
195
            indicator.setAnnualized( Boolean.parseBoolean( reader.getElementValue() ) );
 
196
            
 
197
            reader.moveToStartElement( FIELD_INDICATOR_TYPE );
 
198
            indicator.getIndicatorType().setId( indicatorTypeMapping.get( Integer.parseInt( reader.getElementValue() ) ) );
 
199
            
 
200
            reader.moveToStartElement( FIELD_NUMERATOR );
 
201
            indicator.setNumerator( expressionService.convertExpression( reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
 
202
            
 
203
            reader.moveToStartElement( FIELD_NUMERATOR_DESCRIPTION );
 
204
            indicator.setNumeratorDescription( reader.getElementValue() );
 
205
 
 
206
            reader.moveToStartElement( FIELD_NUMERATOR_AGGREGATION_OPERATOR );
 
207
            indicator.setNumeratorAggregationOperator( reader.getElementValue() );
 
208
            
 
209
            reader.moveToStartElement( FIELD_DENOMINATOR );
 
210
            indicator.setDenominator( expressionService.convertExpression( reader.getElementValue(), dataElementMapping, categoryOptionComboMapping ) );
 
211
            
 
212
            reader.moveToStartElement( FIELD_DENOMINATOR_DESCRIPTION );
 
213
            indicator.setDenominatorDescription( reader.getElementValue() );
 
214
 
 
215
            reader.moveToStartElement( FIELD_DENOMINATOR_AGGREGATION_OPERATOR );
 
216
            indicator.setDenominatorAggregationOperator( reader.getElementValue() );
 
217
            
 
218
            NameMappingUtil.addIndicatorMapping( indicator.getId(), indicator.getName() );
 
219
            
 
220
            read( indicator, GroupMemberType.NONE, params );
 
221
        }
 
222
    }
 
223
}
 
 
b'\\ No newline at end of file'