1
package org.hisp.dhis.importexport.dxf.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.util.Collection;
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;
50
* @author Lars Helge Overland
53
public class DataElementCategoryOptionComboConverter
54
extends AbstractDataElementCategoryOptionComboConverter implements XMLConverter
56
public static final String COLLECTION_NAME = "categoryOptionCombos";
57
public static final String ELEMENT_NAME = "categoryOptionCombo";
59
private static final String FIELD_ID = "id";
60
private static final String FIELD_NAME = "name";
62
private static final String SUB_ELEMENT_NAME = "categoryCombo";
64
private static final String SUB_COLLECTION_NAME = "categoryOptions";
65
private static final String SUB_COLLECTION_ELEMENT_NAME = "categoryOption";
67
private DataElementCategoryOptionService categoryOptionService;
69
private DataElementCategoryComboService categoryComboService;
71
private Map<Object, Integer> categoryComboMapping;
72
private Map<Object, Integer> categoryOptionMapping;
74
// -------------------------------------------------------------------------
76
// -------------------------------------------------------------------------
79
* Constructor for write operations.
81
public DataElementCategoryOptionComboConverter()
86
* Constructor for read operations.
88
* @param importObjectService the importObjectService to use.
89
* @param categoryOptionService the dataElementCategoryOptionService to use.
91
public DataElementCategoryOptionComboConverter( ImportObjectService importObjectService,
92
Map<Object, Integer> categoryComboMapping,
93
Map<Object, Integer> categoryOptionMapping,
94
DataElementCategoryOptionComboService categoryOptionComboService,
95
DataElementCategoryOptionService categoryOptionService,
96
DataElementCategoryComboService categoryComboService )
98
this.importObjectService = importObjectService;
99
this.categoryComboMapping = categoryComboMapping;
100
this.categoryOptionMapping = categoryOptionMapping;
101
this.categoryOptionComboService = categoryOptionComboService;
102
this.categoryOptionService = categoryOptionService;
103
this.categoryComboService = categoryComboService;
106
// -------------------------------------------------------------------------
107
// XMLConverter implementation
108
// -------------------------------------------------------------------------
110
public void write( XMLWriter writer, ExportParams params )
112
Collection<DataElementCategoryOptionCombo> categoryOptionCombos = params.getCategoryOptionCombos();
114
if ( categoryOptionCombos != null && categoryOptionCombos.size() > 0 )
116
writer.openElement( COLLECTION_NAME );
118
for ( DataElementCategoryOptionCombo categoryOptionCombo : categoryOptionCombos )
120
writer.openElement( ELEMENT_NAME );
122
writer.writeElement( FIELD_ID, String.valueOf( categoryOptionCombo.getId() ) );
124
// -------------------------------------------------------------
125
// Write CategoryCombo
126
// -------------------------------------------------------------
128
writer.openElement( SUB_ELEMENT_NAME );
130
writer.writeElement( FIELD_ID, String.valueOf( categoryOptionCombo.getCategoryCombo().getId() ) );
131
writer.writeElement( FIELD_NAME, categoryOptionCombo.getCategoryCombo().getName() );
133
writer.closeElement();
135
// -------------------------------------------------------------
136
// Write CategoryOptions
137
// -------------------------------------------------------------
139
writer.openElement( SUB_COLLECTION_NAME );
141
for ( DataElementCategoryOption categoryOption : categoryOptionCombo.getCategoryOptions() )
143
writer.openElement( SUB_COLLECTION_ELEMENT_NAME );
145
writer.writeElement( FIELD_ID, String.valueOf( categoryOption.getId() ) );
146
writer.writeElement( FIELD_NAME, String.valueOf( categoryOption.getName() ) );
148
writer.closeElement();
151
writer.closeElement();
153
writer.closeElement();
156
writer.closeElement();
160
public void read( XMLReader reader, ImportParams params )
162
while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
164
final DataElementCategoryOptionCombo categoryOptionCombo = new DataElementCategoryOptionCombo();
166
reader.moveToStartElement( FIELD_ID );
167
categoryOptionCombo.setId( Integer.parseInt( reader.getElementValue() ) );
169
reader.moveToStartElement( FIELD_ID );
170
int categoryComboId = Integer.parseInt( reader.getElementValue() );
172
reader.moveToStartElement( FIELD_NAME );
173
String categoryComboName = reader.getElementValue();
175
// -----------------------------------------------------------------
176
// Setting the persisted CategoryCombo on the CategoryOptionCombo
178
// -----------------------------------------------------------------
180
DataElementCategoryCombo categoryCombo = new DataElementCategoryCombo();
182
if ( params.isPreview() )
184
categoryCombo.setId( categoryComboId );
185
categoryCombo.setName( categoryComboName );
189
categoryCombo = categoryComboService.getDataElementCategoryCombo( categoryComboMapping.get( categoryComboId ) );
192
categoryOptionCombo.setCategoryCombo( categoryCombo );
194
// -----------------------------------------------------------------
195
// Setting the persisted CategoryOptions on the CategoryOptionCombo
197
// -----------------------------------------------------------------
199
while ( reader.moveToStartElement( SUB_COLLECTION_ELEMENT_NAME, SUB_COLLECTION_NAME ) )
201
DataElementCategoryOption categoryOption = new DataElementCategoryOption();
203
reader.moveToStartElement( FIELD_ID );
204
int categoryOptionId = Integer.parseInt( reader.getElementValue() );
206
reader.moveToStartElement( FIELD_NAME );
207
String categoryOptionName = reader.getElementValue();
209
if ( params.isPreview() )
211
categoryOption.setId( categoryOptionId );
212
categoryOption.setName( categoryOptionName );
216
categoryOption = categoryOptionService.getDataElementCategoryOption( categoryOptionMapping.get( categoryOptionId ) );
219
categoryOptionCombo.getCategoryOptions().add( categoryOption );
222
NameMappingUtil.addCategoryOptionComboMapping( categoryOptionCombo.getId(), categoryOptionCombo );
224
read( categoryOptionCombo, GroupMemberType.NONE, params );