~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/DataElementCategoryOptionComboConverter.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.dataelement.DataElementCategoryCombo;
 
36
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
 
37
import org.hisp.dhis.dataelement.DataElementCategoryOption;
 
38
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
39
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
 
40
import org.hisp.dhis.dataelement.DataElementCategoryOptionService;
 
41
import org.hisp.dhis.importexport.ExportParams;
 
42
import org.hisp.dhis.importexport.GroupMemberType;
 
43
import org.hisp.dhis.importexport.ImportObjectService;
 
44
import org.hisp.dhis.importexport.ImportParams;
 
45
import org.hisp.dhis.importexport.XMLConverter;
 
46
import org.hisp.dhis.importexport.converter.AbstractDataElementCategoryOptionComboConverter;
 
47
import org.hisp.dhis.importexport.mapping.NameMappingUtil;
 
48
 
 
49
/**
 
50
 * @author Lars Helge Overland
 
51
 * @version $Id$
 
52
 */
 
53
public class DataElementCategoryOptionComboConverter
 
54
    extends AbstractDataElementCategoryOptionComboConverter implements XMLConverter
 
55
{
 
56
    public static final String COLLECTION_NAME = "categoryOptionCombos";
 
57
    public static final String ELEMENT_NAME = "categoryOptionCombo";
 
58
    
 
59
    private static final String FIELD_ID = "id";
 
60
    private static final String FIELD_NAME = "name";
 
61
 
 
62
    private static final String SUB_ELEMENT_NAME = "categoryCombo";
 
63
    
 
64
    private static final String SUB_COLLECTION_NAME = "categoryOptions";
 
65
    private static final String SUB_COLLECTION_ELEMENT_NAME = "categoryOption";
 
66
    
 
67
    private DataElementCategoryOptionService categoryOptionService;
 
68
    
 
69
    private DataElementCategoryComboService categoryComboService;
 
70
    
 
71
    private Map<Object, Integer> categoryComboMapping;
 
72
    private Map<Object, Integer> categoryOptionMapping;
 
73
    
 
74
    // -------------------------------------------------------------------------
 
75
    // Constructor
 
76
    // -------------------------------------------------------------------------
 
77
 
 
78
    /**
 
79
     * Constructor for write operations.
 
80
     */
 
81
    public DataElementCategoryOptionComboConverter()
 
82
    {   
 
83
    }
 
84
    
 
85
    /**
 
86
     * Constructor for read operations.
 
87
     * 
 
88
     * @param importObjectService the importObjectService to use.
 
89
     * @param categoryOptionService the dataElementCategoryOptionService to use.
 
90
     */
 
91
    public DataElementCategoryOptionComboConverter( ImportObjectService importObjectService,
 
92
        Map<Object, Integer> categoryComboMapping,
 
93
        Map<Object, Integer> categoryOptionMapping,
 
94
        DataElementCategoryOptionComboService categoryOptionComboService,
 
95
        DataElementCategoryOptionService categoryOptionService,
 
96
        DataElementCategoryComboService categoryComboService )
 
97
    {
 
98
        this.importObjectService = importObjectService;
 
99
        this.categoryComboMapping = categoryComboMapping;
 
100
        this.categoryOptionMapping = categoryOptionMapping;
 
101
        this.categoryOptionComboService = categoryOptionComboService;
 
102
        this.categoryOptionService = categoryOptionService;
 
103
        this.categoryComboService = categoryComboService;
 
104
    }    
 
105
 
 
106
    // -------------------------------------------------------------------------
 
107
    // XMLConverter implementation
 
108
    // -------------------------------------------------------------------------
 
109
 
 
110
    public void write( XMLWriter writer, ExportParams params )
 
111
    {
 
112
        Collection<DataElementCategoryOptionCombo> categoryOptionCombos = params.getCategoryOptionCombos();
 
113
        
 
114
        if ( categoryOptionCombos != null && categoryOptionCombos.size() > 0 )
 
115
        {
 
116
            writer.openElement( COLLECTION_NAME );
 
117
            
 
118
            for ( DataElementCategoryOptionCombo categoryOptionCombo : categoryOptionCombos )
 
119
            {
 
120
                writer.openElement( ELEMENT_NAME );
 
121
                
 
122
                writer.writeElement( FIELD_ID, String.valueOf( categoryOptionCombo.getId() ) );
 
123
                
 
124
                // -------------------------------------------------------------
 
125
                // Write CategoryCombo
 
126
                // -------------------------------------------------------------
 
127
 
 
128
                writer.openElement( SUB_ELEMENT_NAME );
 
129
                
 
130
                writer.writeElement( FIELD_ID, String.valueOf( categoryOptionCombo.getCategoryCombo().getId() ) );
 
131
                writer.writeElement( FIELD_NAME, categoryOptionCombo.getCategoryCombo().getName() );
 
132
                
 
133
                writer.closeElement();
 
134
 
 
135
                // -------------------------------------------------------------
 
136
                // Write CategoryOptions
 
137
                // -------------------------------------------------------------
 
138
 
 
139
                writer.openElement( SUB_COLLECTION_NAME );
 
140
                
 
141
                for ( DataElementCategoryOption categoryOption : categoryOptionCombo.getCategoryOptions() )
 
142
                {
 
143
                    writer.openElement( SUB_COLLECTION_ELEMENT_NAME );
 
144
                    
 
145
                    writer.writeElement( FIELD_ID, String.valueOf( categoryOption.getId() ) );
 
146
                    writer.writeElement( FIELD_NAME, String.valueOf( categoryOption.getName() ) );
 
147
                    
 
148
                    writer.closeElement();
 
149
                }
 
150
                
 
151
                writer.closeElement();
 
152
                
 
153
                writer.closeElement();
 
154
            }
 
155
            
 
156
            writer.closeElement();
 
157
        }
 
158
    }
 
159
    
 
160
    public void read( XMLReader reader, ImportParams params )
 
161
    {
 
162
        while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
 
163
        {
 
164
            final DataElementCategoryOptionCombo categoryOptionCombo = new DataElementCategoryOptionCombo();
 
165
            
 
166
            reader.moveToStartElement( FIELD_ID );
 
167
            categoryOptionCombo.setId( Integer.parseInt( reader.getElementValue() ) );
 
168
            
 
169
            reader.moveToStartElement( FIELD_ID );
 
170
            int categoryComboId = Integer.parseInt( reader.getElementValue() );
 
171
            
 
172
            reader.moveToStartElement( FIELD_NAME );
 
173
            String categoryComboName = reader.getElementValue();
 
174
 
 
175
            // -----------------------------------------------------------------
 
176
            // Setting the persisted CategoryCombo on the CategoryOptionCombo
 
177
            // if not in preview
 
178
            // -----------------------------------------------------------------
 
179
 
 
180
            DataElementCategoryCombo categoryCombo = new DataElementCategoryCombo();
 
181
            
 
182
            if ( params.isPreview() )
 
183
            {   
 
184
                categoryCombo.setId( categoryComboId );
 
185
                categoryCombo.setName( categoryComboName );
 
186
            }
 
187
            else
 
188
            {
 
189
                categoryCombo = categoryComboService.getDataElementCategoryCombo( categoryComboMapping.get( categoryComboId ) );
 
190
            }
 
191
            
 
192
            categoryOptionCombo.setCategoryCombo( categoryCombo );
 
193
 
 
194
            // -----------------------------------------------------------------
 
195
            // Setting the persisted CategoryOptions on the CategoryOptionCombo
 
196
            // if not in preview
 
197
            // -----------------------------------------------------------------
 
198
 
 
199
            while ( reader.moveToStartElement( SUB_COLLECTION_ELEMENT_NAME, SUB_COLLECTION_NAME ) )
 
200
            {
 
201
                DataElementCategoryOption categoryOption = new DataElementCategoryOption();
 
202
                
 
203
                reader.moveToStartElement( FIELD_ID );                
 
204
                int categoryOptionId = Integer.parseInt( reader.getElementValue() );
 
205
                
 
206
                reader.moveToStartElement( FIELD_NAME );
 
207
                String categoryOptionName = reader.getElementValue();
 
208
                
 
209
                if ( params.isPreview() )
 
210
                {
 
211
                    categoryOption.setId( categoryOptionId );
 
212
                    categoryOption.setName( categoryOptionName );
 
213
                }
 
214
                else
 
215
                {
 
216
                    categoryOption = categoryOptionService.getDataElementCategoryOption( categoryOptionMapping.get( categoryOptionId ) );                    
 
217
                }
 
218
                
 
219
                categoryOptionCombo.getCategoryOptions().add( categoryOption );
 
220
            }
 
221
            
 
222
            NameMappingUtil.addCategoryOptionComboMapping( categoryOptionCombo.getId(), categoryOptionCombo );
 
223
            
 
224
            read( categoryOptionCombo, GroupMemberType.NONE, params );
 
225
        }
 
226
    }
 
227
}