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