1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.io.dozer;
17
18 import static org.dozer.loader.api.TypeMappingOptions.oneWay;
19 import static org.dozer.loader.api.TypeMappingOptions.wildcard;
20
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.dozer.DozerBeanMapper;
27 import org.dozer.loader.api.BeanMappingBuilder;
28 import org.dozer.loader.api.FieldsMappingOptions;
29 import org.dozer.loader.api.TypeMappingBuilder;
30 import org.supercsv.cellprocessor.ift.CellProcessor;
31 import org.supercsv.io.AbstractCsvWriter;
32 import org.supercsv.io.CsvBeanWriter;
33 import org.supercsv.prefs.CsvPreference;
34 import org.supercsv.util.Util;
35
36
37
38
39
40
41
42 public class CsvDozerBeanWriter extends AbstractCsvWriter implements ICsvDozerBeanWriter {
43
44 private final DozerBeanMapper dozerBeanMapper;
45
46
47 private final CsvDozerBeanData beanData = new CsvDozerBeanData();
48
49
50 private final List<Object> processedColumns = new ArrayList<Object>();
51
52
53
54
55
56
57
58
59
60
61
62
63 public CsvDozerBeanWriter(final Writer writer, final CsvPreference preference) {
64 super(writer, preference);
65 this.dozerBeanMapper = new DozerBeanMapper();
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public CsvDozerBeanWriter(final Writer writer, final CsvPreference preference, final DozerBeanMapper dozerBeanMapper) {
82 super(writer, preference);
83 if( dozerBeanMapper == null ) {
84 throw new NullPointerException("dozerBeanMapper should not be null");
85 }
86 this.dozerBeanMapper = dozerBeanMapper;
87 }
88
89
90
91
92 public void configureBeanMapping(final Class<?> clazz, final String[] fieldMapping) {
93 dozerBeanMapper.addMapping(new MappingBuilder(clazz, fieldMapping));
94 }
95
96
97
98
99 public void write(final Object source) throws IOException {
100
101 if( source == null ) {
102 throw new NullPointerException("object to write should not be null");
103 }
104
105
106 super.incrementRowAndLineNo();
107
108
109 beanData.getColumns().clear();
110 dozerBeanMapper.map(source, beanData);
111
112
113 super.writeRow(beanData.getColumns());
114 }
115
116
117
118
119 public void write(final Object source, final CellProcessor[] processors) throws IOException {
120
121 if( source == null ) {
122 throw new NullPointerException("object to write should not be null");
123 } else if( processors == null ) {
124 throw new NullPointerException("processors should not be null");
125 }
126
127
128 super.incrementRowAndLineNo();
129
130
131 beanData.getColumns().clear();
132 dozerBeanMapper.map(source, beanData);
133
134
135 Util.executeCellProcessors(processedColumns, beanData.getColumns(), processors, getLineNumber(), getRowNumber());
136
137
138 super.writeRow(processedColumns);
139 }
140
141
142
143
144 private static class MappingBuilder extends BeanMappingBuilder {
145
146 private final Class<?> clazz;
147 private final String[] fieldMapping;
148
149
150
151
152
153
154
155
156
157
158
159 public MappingBuilder(final Class<?> clazz, final String[] fieldMapping) {
160 if( clazz == null ) {
161 throw new NullPointerException("clazz should not be null");
162 } else if( fieldMapping == null ) {
163 throw new NullPointerException("fieldMapping should not be null");
164 }
165 this.clazz = clazz;
166 this.fieldMapping = fieldMapping;
167 }
168
169 @Override
170 protected void configure() {
171
172
173
174
175
176
177
178
179
180 final TypeMappingBuilder mappingBuilder = mapping(clazz, type(CsvDozerBeanData.class).mapNull(true),
181 oneWay(), wildcard(false));
182
183 for( int i = 0; i < fieldMapping.length; i++ ) {
184
185 final String mapping = fieldMapping[i];
186
187 if( mapping == null ) {
188
189 throw new NullPointerException(String.format("fieldMapping at index %d should not be null", i));
190 }
191
192
193 mappingBuilder.fields(mapping, "columns[" + i + "]", FieldsMappingOptions.copyByReference());
194 }
195 }
196 }
197 }