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 java.io.BufferedReader;
31
import java.io.BufferedWriter;
32
import java.io.IOException;
35
import org.hisp.dhis.dataelement.DataElement;
36
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
37
import org.hisp.dhis.datavalue.DataValue;
38
import org.hisp.dhis.datavalue.DataValueService;
39
import org.hisp.dhis.importexport.CSVConverter;
40
import org.hisp.dhis.importexport.ExportParams;
41
import org.hisp.dhis.importexport.GroupMemberType;
42
import org.hisp.dhis.importexport.ImportObjectService;
43
import org.hisp.dhis.importexport.ImportParams;
44
import org.hisp.dhis.importexport.converter.AbstractDataValueConverter;
45
import org.hisp.dhis.jdbc.BatchHandler;
46
import org.hisp.dhis.organisationunit.OrganisationUnit;
47
import org.hisp.dhis.period.Period;
48
import org.hisp.dhis.system.util.MimicingHashMap;
51
* @author Lars Helge Overland
54
public class DataValueConverter
55
extends AbstractDataValueConverter implements CSVConverter
57
private static final String SEPARATOR = ",";
59
// -------------------------------------------------------------------------
61
// -------------------------------------------------------------------------
63
private Map<Object, Integer> dataElementMapping;
64
private Map<Object, Integer> periodMapping;
65
private Map<Object, Integer> sourceMapping;
67
// -------------------------------------------------------------------------
69
// -------------------------------------------------------------------------
72
* Constructor for read operations.
74
public DataValueConverter( BatchHandler importDataValueBatchHandler,
75
DataValueService dataValueService,
76
ImportObjectService importObjectService,
79
this.importDataValueBatchHandler = importDataValueBatchHandler;
80
this.dataValueService = dataValueService;
81
this.importObjectService = importObjectService;
83
this.dataElementMapping = new MimicingHashMap<Object, Integer>();
84
this.periodMapping = new MimicingHashMap<Object, Integer>();
85
this.sourceMapping = new MimicingHashMap<Object, Integer>();
88
// -------------------------------------------------------------------------
89
// CSVConverter implementation
90
// -------------------------------------------------------------------------
92
public void write( BufferedWriter writer, ExportParams params )
97
public void read( BufferedReader reader, ImportParams params )
99
String line = new String();
101
final DataValue value = new DataValue();
102
final DataElement dataElement = new DataElement();
103
final Period period = new Period();
104
final OrganisationUnit organisationUnit = new OrganisationUnit();
105
final DataElementCategoryOptionCombo categoryOptionCombo = new DataElementCategoryOptionCombo();
107
categoryOptionCombo.setId( 1 );
111
reader.readLine(); // Skip CSV header
113
while( ( line = reader.readLine() ) != null )
115
String[] values = line.split( SEPARATOR );
117
dataElement.setId( dataElementMapping.get( Integer.parseInt( values[2] ) ) );
118
period.setId( periodMapping.get( Integer.parseInt( values[3] ) ) );
119
organisationUnit.setId( sourceMapping.get( Integer.parseInt( values[1] ) ) );
121
value.setDataElement( dataElement );
122
value.setPeriod( period );
123
value.setSource( organisationUnit );
124
value.setValue( handleValue( values[6] ) );
125
value.setComment( values[13] );
126
value.setOptionCombo( categoryOptionCombo );
128
read( value, GroupMemberType.NONE, params );
131
catch ( IOException ex )
133
throw new RuntimeException( "Failed to read data", ex );
137
// -------------------------------------------------------------------------
138
// CSVConverter implementation
139
// -------------------------------------------------------------------------
141
private String handleValue( String value )
145
value = value.replaceAll( "\"", "" );
146
value = value.replace( ".", "" );