1
2
3
4
5
6
7
8
9
10
11 package org.csveed.bean;
12
13 import java.util.Set;
14
15 import org.csveed.api.Header;
16 import org.csveed.api.Row;
17 import org.csveed.bean.conversion.BeanWrapper;
18 import org.csveed.bean.conversion.ConversionException;
19 import org.csveed.bean.conversion.DefaultConverters;
20 import org.csveed.common.Column;
21 import org.csveed.report.CsvException;
22 import org.csveed.report.RowError;
23
24
25
26
27
28
29
30 public abstract class AbstractMapper<T> {
31
32
33 protected BeanInstructions beanInstructions;
34
35
36 private boolean verified;
37
38
39 private DefaultConverters defaultConverters = new DefaultConverters();
40
41
42
43
44
45
46
47
48
49 public abstract BeanProperty getBeanProperty(Column currentColumn);
50
51
52
53
54
55
56 protected abstract Set<Column> keys();
57
58
59
60
61
62
63
64
65
66 protected abstract void checkKey(Header header, Column key);
67
68
69
70
71
72
73
74 public void verifyHeader(Header header) {
75 if (verified) {
76 return;
77 }
78 for (Column key : keys()) {
79 if (!getBeanProperty(key).isDynamicColumnProperty()) {
80 checkKey(header, key);
81 }
82 }
83 verified = true;
84 }
85
86
87
88
89
90
91
92
93
94 protected abstract Column getColumn(Row row);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public T convert(T bean, Row row, int lineNumber, DynamicColumn currentDynamicColumn) {
111 BeanWrapper beanWrapper = new BeanWrapper(defaultConverters, bean);
112
113 Column currentColumn = null;
114 for (String cell : row) {
115 currentColumn = currentColumn == null ? getColumn(row) : currentColumn.nextColumn();
116
117 if (currentDynamicColumn.isDynamicColumnActive(currentColumn)) {
118 setDynamicColumnProperties(row, lineNumber, beanWrapper, currentColumn);
119 continue;
120 }
121
122 BeanProperty beanProperty = getBeanProperty(currentColumn);
123 if (beanProperty == null) {
124 continue;
125 }
126 if (beanProperty.isRequired() && (cell == null || cell.equals(""))) {
127 throw new CsvException(new RowError(
128 "Bean property \"" + beanProperty.getPropertyName()
129 + "\" is required and may not be empty or null",
130 row.reportOnColumn(currentColumn.getColumnIndex()), lineNumber));
131 }
132 setBeanProperty(row, lineNumber, beanWrapper, currentColumn, cell, beanProperty);
133 }
134 return bean;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149 private void setDynamicColumnProperties(Row row, int lineNumber, BeanWrapper beanWrapper, Column currentColumn) {
150 BeanProperty headerNameProperty = beanInstructions.getProperties().getHeaderNameProperty();
151 if (headerNameProperty != null) {
152 String dynamicHeaderName = row.getHeader().getName(currentColumn.getColumnIndex());
153 setBeanProperty(row, lineNumber, beanWrapper, currentColumn, dynamicHeaderName, headerNameProperty);
154 }
155
156 BeanProperty headerValueProperty = beanInstructions.getProperties().getHeaderValueProperty();
157 if (headerValueProperty != null) {
158 String dynamicHeaderValue = row.get(currentColumn.getColumnIndex());
159 setBeanProperty(row, lineNumber, beanWrapper, currentColumn, dynamicHeaderValue, headerValueProperty);
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 private void setBeanProperty(Row row, int lineNumber, BeanWrapper beanWrapper, Column currentColumn, String cell,
180 BeanProperty beanProperty) {
181 try {
182 beanWrapper.setProperty(beanProperty, cell);
183 } catch (ConversionException err) {
184 String message = err.getMessage() + " cell" + currentColumn.getColumnText() + " [" + cell + "] to "
185 + beanProperty.getPropertyName() + ": " + err.getTypeDescription();
186 throw new CsvException(
187 new RowError(message, row.reportOnColumn(currentColumn.getColumnIndex()), lineNumber));
188 }
189 }
190
191
192
193
194
195
196
197 public void setBeanInstructions(BeanInstructions beanInstructions) {
198 this.beanInstructions = beanInstructions;
199 }
200
201 }