View Javadoc
1   package org.csveed.bean;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Locale;
6   
7   import org.csveed.bean.conversion.Converter;
8   import org.csveed.common.Column;
9   import org.csveed.row.RowInstructions;
10  import org.csveed.row.RowInstructionsImpl;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  public class BeanInstructionsImpl implements BeanInstructions {
15  
16      private static final Logger LOG = LoggerFactory.getLogger(BeanInstructionsImpl.class);
17  
18      private RowInstructions rowInstructions = new RowInstructionsImpl();
19  
20      private BeanProperties properties;
21  
22      private Class<? extends AbstractMapper> mappingStrategy = ColumnIndexMapper.class;
23  
24      private Class beanClass;
25  
26      private boolean settingsLogged;
27  
28      private Column startIndexDynamicColumns;
29  
30      private boolean useHeader = true;
31  
32      public BeanInstructionsImpl(Class beanClass) {
33          this.properties = new BeanProperties(beanClass);
34          this.beanClass = beanClass;
35      }
36  
37      @Override
38      public void logSettings() {
39          if (settingsLogged) {
40              return;
41          }
42          LOG.info("- CSV config / mapping strategy: {}", this.getMappingStrategy());
43          for (BeanProperty property : properties) {
44              LOG.info("{}", getPropertyLogLine(property));
45          }
46          settingsLogged = true;
47      }
48  
49      protected String getPropertyLogLine(BeanProperty property) {
50          List<String> lineParts = new ArrayList<>();
51          lineParts.add("- CSV config");
52          if (mappingStrategy.equals(ColumnIndexMapper.class)) {
53              lineParts.add("Column index " + property.getColumnIndex() + " -> property [" + property.getPropertyName() + "]");
54          } else if (mappingStrategy.equals(ColumnNameMapper.class)) {
55              lineParts.add("Column name [" + property.getColumnName() + "] -> property [" + property.getPropertyName() + "]");
56          }
57          lineParts.add("Required: " + (property.isRequired() ? "yes" : "no"));
58          if (property.getConverter() != null) {
59              lineParts.add("Converter: " + property.getConverter().getClass().getSimpleName() + " for " + property.getPropertyType());
60          }
61  
62          StringBuilder logLine = new StringBuilder();
63          boolean first = true;
64          for (String linePart : lineParts) {
65              if (!first) {
66                  logLine.append(" | ");
67              }
68              logLine.append(linePart);
69              first = false;
70          }
71          return logLine.toString();
72      }
73  
74      @Override
75      public RowInstructions getRowInstructions() {
76          return this.rowInstructions;
77      }
78  
79      @Override
80      public BeanInstructions setUseHeader(boolean useHeader) {
81          this.rowInstructions.setUseHeader(useHeader);
82          this.useHeader = useHeader;
83          return this;
84      }
85  
86      @Override
87      public BeanInstructions setStartRow(int startRow) {
88          this.rowInstructions.setStartRow(startRow);
89          return this;
90      }
91  
92      @Override
93      public BeanInstructions setEscape(char symbol) {
94          this.rowInstructions.setEscape(symbol);
95          return this;
96      }
97  
98      @Override
99      public BeanInstructions setQuote(char symbol) {
100         this.rowInstructions.setQuote(symbol);
101         return this;
102     }
103 
104     @Override
105     public BeanInstructions setQuotingEnabled(boolean enabled) {
106         this.rowInstructions.setQuotingEnabled(enabled);
107         return this;
108     }
109 
110     @Override
111     public BeanInstructions setSeparator(char symbol) {
112         this.rowInstructions.setSeparator(symbol);
113         return this;
114     }
115 
116     @Override
117     public BeanInstructions setComment(char symbol) {
118         this.rowInstructions.setComment(symbol);
119         return this;
120     }
121 
122     @Override
123     public BeanInstructions setEndOfLine(char[] symbols) {
124         this.rowInstructions.setEndOfLine(symbols);
125         return this;
126     }
127 
128     @Override
129     public BeanInstructions skipEmptyLines(boolean skip) {
130         this.rowInstructions.skipEmptyLines(skip);
131         return this;
132     }
133 
134     @Override
135     public BeanInstructions skipCommentLines(boolean skip) {
136         this.rowInstructions.skipCommentLines(skip);
137         return this;
138     }
139 
140     @Override
141     public BeanInstructions setMapper(Class<? extends AbstractMapper> mapper) {
142         this.mappingStrategy = mapper;
143         return this;
144     }
145 
146     @Override
147     public BeanInstructions setDate(String propertyName, String dateFormat) {
148         this.getProperties().setDate(propertyName, dateFormat);
149         return this;
150     }
151 
152     @Override
153     public BeanInstructions setLocalizedNumber(String propertyName, Locale locale) {
154         this.getProperties().setLocalizedNumber(propertyName, locale);
155         return this;
156     }
157 
158     @Override
159     public BeanInstructions setRequired(String propertyName, boolean required) {
160         this.getProperties().setRequired(propertyName, required);
161         return this;
162     }
163 
164     @Override
165     public BeanInstructions setConverter(String propertyName, Converter converter) {
166         this.getProperties().setConverter(propertyName, converter);
167         return this;
168     }
169 
170     @Override
171     public BeanInstructions ignoreProperty(String propertyName) {
172         this.getProperties().ignoreProperty(propertyName);
173         return this;
174     }
175 
176     @Override
177     public BeanInstructions mapColumnIndexToProperty(int columnIndex, String propertyName) {
178         this.getProperties().mapIndexToProperty(columnIndex, propertyName);
179         return this;
180     }
181 
182     @Override
183     public BeanInstructions mapColumnNameToProperty(String columnName, String propertyName) {
184         this.getProperties().mapNameToProperty(columnName, propertyName);
185         return this;
186     }
187 
188     @Override
189     public BeanInstructions setHeaderNameToProperty(String propertyName) {
190         this.getProperties().setHeaderNameProperty(propertyName);
191         return this;
192     }
193 
194     @Override
195     public BeanInstructions setHeaderValueToProperty(String propertyName) {
196         this.getProperties().setHeaderValueProperty(propertyName);
197         return this;
198     }
199 
200     @Override
201     public BeanInstructions setStartIndexDynamicColumns(int startIndex) {
202         this.startIndexDynamicColumns = startIndex == 0 ? null : new Column(startIndex);
203         return this;
204     }
205 
206     @Override
207     public Class<? extends AbstractMapper> getMappingStrategy() {
208         return this.mappingStrategy;
209     }
210 
211     @Override
212     public BeanProperties getProperties() {
213         return this.properties;
214     }
215 
216     @Override
217     public Column getStartIndexDynamicColumns() {
218         return this.startIndexDynamicColumns;
219     }
220 
221     @Override
222     public Class getBeanClass() {
223         return this.beanClass;
224     }
225 
226     @Override
227     public boolean useHeader() {
228         return useHeader;
229     }
230 
231 }