1
package org.hisp.dhis.importexport.dhis14.xml.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 static org.hisp.dhis.importexport.dhis14.util.Dhis14TypeHandler.convertAggregationOperatorFromDhis14;
31
import static org.hisp.dhis.importexport.dhis14.util.Dhis14TypeHandler.convertAggregationOperatorToDhis14;
32
import static org.hisp.dhis.importexport.dhis14.util.Dhis14TypeHandler.convertBooleanFromDhis14;
33
import static org.hisp.dhis.importexport.dhis14.util.Dhis14TypeHandler.convertBooleanToDhis14;
34
import static org.hisp.dhis.importexport.dhis14.util.Dhis14TypeHandler.convertTypeToDhis14;
36
import java.util.Collection;
37
import java.util.HashSet;
40
import org.amplecode.staxwax.reader.XMLReader;
41
import org.amplecode.staxwax.writer.XMLWriter;
42
import org.hisp.dhis.dataelement.CalculatedDataElement;
43
import org.hisp.dhis.dataelement.DataElement;
44
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
45
import org.hisp.dhis.dataelement.DataElementService;
46
import org.hisp.dhis.expression.Expression;
47
import org.hisp.dhis.importexport.ExportParams;
48
import org.hisp.dhis.importexport.GroupMemberType;
49
import org.hisp.dhis.importexport.ImportObjectService;
50
import org.hisp.dhis.importexport.ImportParams;
51
import org.hisp.dhis.importexport.XMLConverter;
52
import org.hisp.dhis.importexport.analysis.ImportAnalyser;
53
import org.hisp.dhis.importexport.converter.AbstractDataElementConverter;
54
import org.hisp.dhis.importexport.dhis14.util.Dhis14ObjectMappingUtil;
55
import org.hisp.dhis.importexport.dhis14.util.Dhis14ParsingUtils;
56
import org.hisp.dhis.importexport.mapping.NameMappingUtil;
59
* @author Lars Helge Overland
60
* @version $Id: DataElementConverter.java 6455 2008-11-24 08:59:37Z larshelg $
62
public class DataElementConverter
63
extends AbstractDataElementConverter implements XMLConverter
65
public static final String ELEMENT_NAME = "DataElement";
67
private static final String FIELD_ID = "DataElementID";
68
private static final String FIELD_SORT_ORDER = "SortOrder";
69
private static final String FIELD_CODE = "DataElementCode";
70
private static final String FIELD_NAME = "DataElementName";
71
private static final String FIELD_SHORT_NAME = "DataElementShort";
72
private static final String FIELD_DOS = "DataElementDOS";
73
private static final String FIELD_PROMPT = "DataElementPrompt";
74
private static final String FIELD_META = "MetaDataElement";
75
private static final String FIELD_DATA_TYPE = "DataTypeID";
76
private static final String FIELD_PERIOD_TYPE = "DataPeriodTypeID";
77
private static final String FIELD_VALID_FROM = "ValidFrom";
78
private static final String FIELD_VALID_TO = "ValidTo";
79
private static final String FIELD_DESCRIPTION = "DataElementDescription";
80
private static final String FIELD_COMMENT = "Comment";
81
private static final String FIELD_CALCULATED = "Calculated";
82
private static final String FIELD_SAVE_CALCULATED = "SaveCalculated";
83
private static final String FIELD_AGGREGATION_START_LEVEL = "AggregateStartLevel";
84
private static final String FIELD_AGGREGATION_OPERATOR = "AggregateOperator";
85
private static final String FIELD_SELECTED = "Selected";
86
private static final String FIELD_LAST_USER = "LastUserID";
87
private static final String FIELD_LAST_UPDATED = "LastUpdated";
89
private static final int VALID_FROM = 34335;
90
private static final int VALID_TO = 2958465;
91
private static final int AGG_START_LEVEL = 5;
93
// -------------------------------------------------------------------------
95
// -------------------------------------------------------------------------
97
private Map<Integer, String> expressionMap;
99
// -------------------------------------------------------------------------
101
// -------------------------------------------------------------------------
104
* Constructor for write operations.
106
public DataElementConverter()
111
* Constructor for read operations.
113
* @param importObjectService the importObjectService to use.
114
* @param dataElementService the dataElementService to use.
116
public DataElementConverter( ImportObjectService importObjectService,
117
DataElementService dataElementService,
118
Map<Integer, String> expressionMap,
119
ImportAnalyser importAnalyser )
121
this.importObjectService = importObjectService;
122
this.dataElementService = dataElementService;
123
this.expressionMap = expressionMap;
124
this.importAnalyser = importAnalyser;
127
// -------------------------------------------------------------------------
128
// AbstractDataElementConverter implementation
129
// -------------------------------------------------------------------------
132
protected DataElement getMatching( DataElement object )
134
DataElement element = super.getMatching( object );
136
if ( element instanceof CalculatedDataElement )
138
((CalculatedDataElement)element).isSaved(); // Loading saved property into memory
139
((CalculatedDataElement)element).getExpression().getExpression(); // Loading expression into memory
145
// -------------------------------------------------------------------------
146
// XMLConverter implementation
147
// -------------------------------------------------------------------------
149
public void write( XMLWriter writer, ExportParams params )
151
Collection<DataElement> elements = params.getDataElements();
152
elements.addAll( params.getCalculatedDataElements() ); // Calculated elements handled together
154
for ( DataElement object : elements )
156
final boolean calculated = object instanceof CalculatedDataElement;
157
final boolean saveCalculated = calculated ? ((CalculatedDataElement)object).isSaved() : false;
159
writer.openElement( ELEMENT_NAME );
161
writer.writeElement( FIELD_ID, String.valueOf( object.getId() ) );
162
writer.writeElement( FIELD_SORT_ORDER, String.valueOf( object.getId() ) );
163
writer.writeElement( FIELD_CODE, object.getCode() );
164
writer.writeElement( FIELD_NAME, object.getName() );
165
writer.writeElement( FIELD_SHORT_NAME, object.getShortName() );
166
writer.writeElement( FIELD_DOS, object.getShortName().replaceAll( "[^a-zA-Z0-9]", "" ) );
167
writer.writeElement( FIELD_PROMPT, object.getName() );
168
writer.writeElement( FIELD_META, String.valueOf( 0 ) );
169
writer.writeElement( FIELD_DATA_TYPE, convertTypeToDhis14( object.getType() ) );
170
writer.writeElement( FIELD_PERIOD_TYPE, String.valueOf( 1 ) );
171
writer.writeElement( FIELD_VALID_FROM, String.valueOf( VALID_FROM ) );
172
writer.writeElement( FIELD_VALID_TO, String.valueOf( VALID_TO ) );
173
writer.writeElement( FIELD_DESCRIPTION, object.getDescription() );
174
writer.writeElement( FIELD_COMMENT, "" );
175
writer.writeElement( FIELD_CALCULATED, convertBooleanToDhis14( calculated ) );
176
writer.writeElement( FIELD_SAVE_CALCULATED, convertBooleanToDhis14( saveCalculated ) );
177
writer.writeElement( FIELD_AGGREGATION_START_LEVEL, String.valueOf( AGG_START_LEVEL ) );
178
writer.writeElement( FIELD_AGGREGATION_OPERATOR, convertAggregationOperatorToDhis14( object.getAggregationOperator() ) );
179
writer.writeElement( FIELD_SELECTED, String.valueOf( 0 ) );
180
writer.writeElement( FIELD_LAST_USER, String.valueOf( 1 ) );
181
writer.writeElement( FIELD_LAST_UPDATED, String.valueOf( VALID_FROM ) );
183
writer.closeElement();
185
NameMappingUtil.addDataElementAggregationOperatorMapping( object.getId(), object.getAggregationOperator() );
189
public void read( XMLReader reader, ImportParams params )
191
final Map<String, String> values = reader.readElements( ELEMENT_NAME );
193
final boolean calculated = convertBooleanFromDhis14( values.get( FIELD_CALCULATED ) );
195
final DataElementCategoryCombo categoryCombo = new DataElementCategoryCombo();
199
final CalculatedDataElement element = new CalculatedDataElement();
201
element.setCategoryCombo( categoryCombo );
203
element.setId( Integer.valueOf( values.get( FIELD_ID ) ) );
204
element.setName( values.get( FIELD_NAME ) );
205
element.setShortName( values.get( FIELD_SHORT_NAME ) );
206
element.setDescription( Dhis14ParsingUtils.removeNewLine( values.get( FIELD_DESCRIPTION ) ) );
207
element.setActive( true );
208
element.setType( Dhis14ObjectMappingUtil.getDataElementTypeMap().get( Integer.parseInt( values.get( FIELD_DATA_TYPE ) ) ) );
209
element.setAggregationOperator( convertAggregationOperatorFromDhis14( values.get( FIELD_AGGREGATION_OPERATOR ) ) );
210
element.getCategoryCombo().setId( 1 );
211
element.setSaved( convertBooleanFromDhis14( values.get( FIELD_SAVE_CALCULATED ) ) );
212
element.setExpression( new Expression( expressionMap.get( element.getId() ), null, new HashSet<DataElement>() ) );
214
NameMappingUtil.addDataElementAggregationOperatorMapping( element.getId(), element.getAggregationOperator() );
216
read( element, GroupMemberType.NONE, params );
220
final DataElement element = new DataElement();
222
element.setCategoryCombo( categoryCombo );
224
element.setId( Integer.valueOf( values.get( FIELD_ID ) ) );
225
element.setName( values.get( FIELD_NAME ) );
226
element.setShortName( values.get( FIELD_SHORT_NAME ) );
227
element.setDescription( Dhis14ParsingUtils.removeNewLine( values.get( FIELD_DESCRIPTION ) ) );
228
element.setActive( true );
229
element.setType( Dhis14ObjectMappingUtil.getDataElementTypeMap().get( Integer.parseInt( values.get( FIELD_DATA_TYPE ) ) ) );
230
element.setAggregationOperator( convertAggregationOperatorFromDhis14( values.get( FIELD_AGGREGATION_OPERATOR ) ) );
231
element.getCategoryCombo().setId( 1 );
233
NameMappingUtil.addDataElementAggregationOperatorMapping( element.getId(), element.getAggregationOperator() );
235
read( element, GroupMemberType.NONE, params );