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.io.Writer;
14  import java.util.Collection;
15  
16  import org.csveed.bean.conversion.BeanWrapper;
17  import org.csveed.bean.conversion.ConversionException;
18  import org.csveed.bean.conversion.DefaultConverters;
19  import org.csveed.row.HeaderImpl;
20  import org.csveed.row.LineWithInfo;
21  import org.csveed.row.RowImpl;
22  import org.csveed.row.RowWriter;
23  import org.csveed.row.RowWriterImpl;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * The Class BeanWriterImpl.
29   *
30   * @param <T>
31   *            the generic type
32   */
33  public class BeanWriterImpl<T> implements BeanWriter<T> {
34  
35      /** The Constant logger. */
36      private static final Logger logger = LoggerFactory.getLogger(BeanWriterImpl.class);
37  
38      /** The row writer. */
39      private final RowWriter rowWriter;
40  
41      /** The bean instructions. */
42      private final BeanInstructions beanInstructions;
43  
44      /** The header written. */
45      private boolean headerWritten;
46  
47      /** The default converters. */
48      private DefaultConverters defaultConverters = new DefaultConverters();
49  
50      /** The header. */
51      private HeaderImpl header;
52  
53      /**
54       * Instantiates a new bean writer impl.
55       *
56       * @param writer
57       *            the writer
58       * @param beanClass
59       *            the bean class
60       */
61      public BeanWriterImpl(Writer writer, Class<T> beanClass) {
62          this(writer, new BeanParser().getBeanInstructions(beanClass));
63      }
64  
65      /**
66       * Instantiates a new bean writer impl.
67       *
68       * @param writer
69       *            the writer
70       * @param beanInstructions
71       *            the bean instructions
72       */
73      public BeanWriterImpl(Writer writer, BeanInstructions beanInstructions) {
74          this.beanInstructions = beanInstructions;
75          this.rowWriter = new RowWriterImpl(writer, this.beanInstructions.getRowInstructions());
76      }
77  
78      @Override
79      public void writeBeans(Collection<T> beans) {
80          for (T bean : beans) {
81              writeBean(bean);
82          }
83      }
84  
85      @Override
86      public void writeBean(T bean) {
87          writeHeader();
88  
89          LineWithInfo line = new LineWithInfo();
90  
91          BeanWrapper beanWrapper = new BeanWrapper(defaultConverters, bean);
92          for (BeanProperty property : beanInstructions.getProperties()) {
93              try {
94                  line.addCell(beanWrapper.getProperty(property));
95              } catch (ConversionException e) {
96                  logger.error("{}", e.getMessage());
97                  logger.trace("", e);
98              }
99          }
100         rowWriter.writeRow(new RowImpl(line, header));
101     }
102 
103     @Override
104     public void writeHeader() {
105         if (!beanInstructions.useHeader() || headerWritten) {
106             return;
107         }
108         LineWithInfo line = new LineWithInfo();
109         for (BeanProperty property : beanInstructions.getProperties()) {
110             line.addCell(property.getColumnName());
111         }
112         header = new HeaderImpl(line);
113         rowWriter.writeHeader(header);
114         headerWritten = true;
115     }
116 
117     @Override
118     public RowWriter getRowWriter() {
119         return rowWriter;
120     }
121 
122 }