~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/DataValueConverter.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 java.io.BufferedReader;
 
31
import java.io.BufferedWriter;
 
32
import java.io.IOException;
 
33
import java.util.Map;
 
34
 
 
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;
 
49
 
 
50
/**
 
51
 * @author Lars Helge Overland
 
52
 * @version $Id$
 
53
 */
 
54
public class DataValueConverter
 
55
    extends AbstractDataValueConverter implements CSVConverter 
 
56
{
 
57
    private static final String SEPARATOR = ",";
 
58
    
 
59
    // -------------------------------------------------------------------------
 
60
    // Properties
 
61
    // -------------------------------------------------------------------------
 
62
 
 
63
    private Map<Object, Integer> dataElementMapping;    
 
64
    private Map<Object, Integer> periodMapping;    
 
65
    private Map<Object, Integer> sourceMapping;
 
66
 
 
67
    // -------------------------------------------------------------------------
 
68
    // Constructor
 
69
    // -------------------------------------------------------------------------
 
70
 
 
71
    /**
 
72
     * Constructor for read operations.
 
73
     */
 
74
    public DataValueConverter( BatchHandler importDataValueBatchHandler,
 
75
        DataValueService dataValueService,
 
76
        ImportObjectService importObjectService,
 
77
        ImportParams params )
 
78
    {
 
79
        this.importDataValueBatchHandler = importDataValueBatchHandler;
 
80
        this.dataValueService = dataValueService;
 
81
        this.importObjectService = importObjectService;
 
82
        this.params = params;
 
83
        this.dataElementMapping = new MimicingHashMap<Object, Integer>();
 
84
        this.periodMapping = new MimicingHashMap<Object, Integer>();
 
85
        this.sourceMapping = new MimicingHashMap<Object, Integer>();
 
86
    }
 
87
 
 
88
    // -------------------------------------------------------------------------
 
89
    // CSVConverter implementation
 
90
    // -------------------------------------------------------------------------
 
91
    
 
92
    public void write( BufferedWriter writer, ExportParams params )
 
93
    {
 
94
        // Not implemented
 
95
    }
 
96
 
 
97
    public void read( BufferedReader reader, ImportParams params )
 
98
    {
 
99
        String line = new String();
 
100
        
 
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();
 
106
        
 
107
        categoryOptionCombo.setId( 1 );
 
108
        
 
109
        try
 
110
        {
 
111
            reader.readLine(); // Skip CSV header
 
112
            
 
113
            while( ( line = reader.readLine() ) != null )
 
114
            {
 
115
                String[] values = line.split( SEPARATOR );
 
116
                
 
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] ) ) );
 
120
                
 
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 );
 
127
                
 
128
                read( value, GroupMemberType.NONE, params );
 
129
            }
 
130
        }
 
131
        catch ( IOException ex )
 
132
        {
 
133
            throw new RuntimeException( "Failed to read data", ex );
 
134
        }        
 
135
    }
 
136
 
 
137
    // -------------------------------------------------------------------------
 
138
    // CSVConverter implementation
 
139
    // -------------------------------------------------------------------------
 
140
    
 
141
    private String handleValue( String value )
 
142
    {
 
143
        if ( value != null )
 
144
        {
 
145
            value = value.replaceAll( "\"", "" );
 
146
            value = value.replace( ".", "" );
 
147
        }
 
148
        
 
149
        return value;
 
150
    }
 
151
}