~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/dhis14/xml/converter/DataElementConverter.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.dhis14.xml.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 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;
 
35
 
 
36
import java.util.Collection;
 
37
import java.util.HashSet;
 
38
import java.util.Map;
 
39
 
 
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;
 
57
 
 
58
/**
 
59
 * @author Lars Helge Overland
 
60
 * @version $Id: DataElementConverter.java 6455 2008-11-24 08:59:37Z larshelg $
 
61
 */
 
62
public class DataElementConverter
 
63
    extends AbstractDataElementConverter implements XMLConverter
 
64
{
 
65
    public static final String ELEMENT_NAME = "DataElement";
 
66
    
 
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";
 
88
 
 
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;
 
92
 
 
93
    // -------------------------------------------------------------------------
 
94
    // Properties
 
95
    // -------------------------------------------------------------------------
 
96
 
 
97
    private Map<Integer, String> expressionMap;
 
98
    
 
99
    // -------------------------------------------------------------------------
 
100
    // Constructor
 
101
    // -------------------------------------------------------------------------
 
102
 
 
103
    /**
 
104
     * Constructor for write operations.
 
105
     */
 
106
    public DataElementConverter()
 
107
    {
 
108
    }
 
109
 
 
110
    /**
 
111
     * Constructor for read operations.
 
112
     * 
 
113
     * @param importObjectService the importObjectService to use.
 
114
     * @param dataElementService the dataElementService to use.
 
115
     */
 
116
    public DataElementConverter( ImportObjectService importObjectService,
 
117
        DataElementService dataElementService,
 
118
        Map<Integer, String> expressionMap,
 
119
        ImportAnalyser importAnalyser )
 
120
    {
 
121
        this.importObjectService = importObjectService;
 
122
        this.dataElementService = dataElementService;
 
123
        this.expressionMap = expressionMap;
 
124
        this.importAnalyser = importAnalyser;
 
125
    }
 
126
 
 
127
    // -------------------------------------------------------------------------
 
128
    // AbstractDataElementConverter implementation
 
129
    // -------------------------------------------------------------------------
 
130
        
 
131
    @Override
 
132
    protected DataElement getMatching( DataElement object )
 
133
    {
 
134
        DataElement element = super.getMatching( object );
 
135
        
 
136
        if ( element instanceof CalculatedDataElement )
 
137
        {
 
138
            ((CalculatedDataElement)element).isSaved(); // Loading saved property into memory
 
139
            ((CalculatedDataElement)element).getExpression().getExpression(); // Loading expression into memory
 
140
        }
 
141
        
 
142
        return element;
 
143
    }
 
144
    
 
145
    // -------------------------------------------------------------------------
 
146
    // XMLConverter implementation
 
147
    // -------------------------------------------------------------------------
 
148
    
 
149
    public void write( XMLWriter writer, ExportParams params )
 
150
    {
 
151
        Collection<DataElement> elements = params.getDataElements();
 
152
        elements.addAll( params.getCalculatedDataElements() ); // Calculated elements handled together
 
153
        
 
154
        for ( DataElement object : elements )
 
155
        {
 
156
            final boolean calculated = object instanceof CalculatedDataElement;
 
157
            final boolean saveCalculated = calculated ? ((CalculatedDataElement)object).isSaved() : false;
 
158
            
 
159
            writer.openElement( ELEMENT_NAME );
 
160
            
 
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 ) );
 
182
            
 
183
            writer.closeElement();
 
184
            
 
185
            NameMappingUtil.addDataElementAggregationOperatorMapping( object.getId(), object.getAggregationOperator() );
 
186
        }
 
187
    }
 
188
    
 
189
    public void read( XMLReader reader, ImportParams params )
 
190
    {
 
191
        final Map<String, String> values = reader.readElements( ELEMENT_NAME );
 
192
        
 
193
        final boolean calculated = convertBooleanFromDhis14( values.get( FIELD_CALCULATED ) );
 
194
        
 
195
        final DataElementCategoryCombo categoryCombo = new DataElementCategoryCombo();
 
196
        
 
197
        if ( calculated )
 
198
        {
 
199
            final CalculatedDataElement element = new CalculatedDataElement();
 
200
 
 
201
            element.setCategoryCombo( categoryCombo );
 
202
            
 
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>() ) );
 
213
            
 
214
            NameMappingUtil.addDataElementAggregationOperatorMapping( element.getId(), element.getAggregationOperator() );
 
215
            
 
216
            read( element, GroupMemberType.NONE, params );
 
217
        }
 
218
        else
 
219
        {
 
220
            final DataElement element = new DataElement();
 
221
 
 
222
            element.setCategoryCombo( categoryCombo );
 
223
            
 
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 );
 
232
            
 
233
            NameMappingUtil.addDataElementAggregationOperatorMapping( element.getId(), element.getAggregationOperator() );
 
234
            
 
235
            read( element, GroupMemberType.NONE, params );
 
236
        }        
 
237
    }    
 
238
}