View Javadoc
1   package org.csveed.bean;
2   
3   import java.util.Set;
4   
5   import org.csveed.api.Header;
6   import org.csveed.api.Row;
7   import org.csveed.bean.conversion.BeanWrapper;
8   import org.csveed.bean.conversion.ConversionException;
9   import org.csveed.bean.conversion.DefaultConverters;
10  import org.csveed.common.Column;
11  import org.csveed.report.CsvException;
12  import org.csveed.report.RowError;
13  
14  public abstract class AbstractMapper<T> {
15  
16      protected BeanInstructions beanInstructions;
17  
18      private boolean verified;
19  
20      private DefaultConvertersaultConverters">DefaultConverters defaultConverters = new DefaultConverters();
21  
22      public abstract BeanProperty getBeanProperty(Column currentColumn);
23  
24      protected abstract Set<Column> keys();
25  
26      protected abstract void checkKey(Header header, Column key);
27  
28      public void verifyHeader(Header header) {
29          if (verified) {
30              return;
31          }
32          for (Column key : keys()) {
33              if (!getBeanProperty(key).isDynamicColumnProperty()) {
34                  checkKey(header, key);
35              }
36          }
37          verified = true;
38      }
39  
40      protected abstract Column getColumn(Row row);
41  
42      public T convert(T bean, Row row, int lineNumber, DynamicColumn currentDynamicColumn) {
43          BeanWrapperml#BeanWrapper">BeanWrapper beanWrapper = new BeanWrapper(defaultConverters, bean);
44  
45          Column currentColumn = null;
46          for (String cell : row) {
47              currentColumn = currentColumn == null ? getColumn(row) : currentColumn.nextColumn();
48  
49              if (currentDynamicColumn.isDynamicColumnActive(currentColumn)) {
50                  setDynamicColumnProperties(row, lineNumber, beanWrapper, currentColumn);
51                  continue;
52              }
53  
54              BeanProperty beanProperty = getBeanProperty(currentColumn);
55              if (beanProperty == null) {
56                  continue;
57              }
58              if (beanProperty.isRequired() && (cell == null || cell.equals(""))) {
59                  throw new CsvException(
60                          new RowError("Bean property \"" + beanProperty.getPropertyName() +
61                                  "\" is required and may not be empty or null",
62                                  row.reportOnColumn(currentColumn.getColumnIndex()), lineNumber));
63              }
64              setBeanProperty(row, lineNumber, beanWrapper, currentColumn, cell, beanProperty);
65          }
66          return bean;
67      }
68  
69      private void setDynamicColumnProperties(Row row, int lineNumber, BeanWrapper beanWrapper, Column currentColumn) {
70          BeanProperty headerNameProperty = beanInstructions.getProperties().getHeaderNameProperty();
71          if (headerNameProperty != null) {
72              String dynamicHeaderName = row.getHeader().getName(currentColumn.getColumnIndex());
73              setBeanProperty(row, lineNumber, beanWrapper, currentColumn, dynamicHeaderName, headerNameProperty);
74          }
75  
76          BeanProperty headerValueProperty = beanInstructions.getProperties().getHeaderValueProperty();
77          if (headerValueProperty != null) {
78              String dynamicHeaderValue = row.get(currentColumn.getColumnIndex());
79              setBeanProperty(row, lineNumber, beanWrapper, currentColumn, dynamicHeaderValue, headerValueProperty);
80          }
81      }
82  
83      private void setBeanProperty(Row row, int lineNumber, BeanWrapper beanWrapper, Column currentColumn, String cell, BeanProperty beanProperty) {
84          try {
85              beanWrapper.setProperty(beanProperty, cell);
86          } catch (ConversionException err) {
87              String message =
88                      err.getMessage() + " cell" + currentColumn.getColumnText() + " [" + cell + "] to " +
89                              beanProperty.getPropertyName() + ": " + err.getTypeDescription();
90              throw new CsvException(new RowError(message, row.reportOnColumn(currentColumn.getColumnIndex()), lineNumber));
91          }
92      }
93  
94      public void setBeanInstructions(BeanInstructions beanInstructions) {
95          this.beanInstructions = beanInstructions;
96      }
97  
98  }