View Javadoc
1   package org.csveed.bean;
2   
3   import org.csveed.common.Column;
4   
5   /**
6    * <p>
7    *     The concept of dynamic columns comes into play when a sheet contains columns that are not really part
8    *     of the columns, but should have been separate rows. For example, let's say you have a table that looks
9    *     like this:
10   * </p>
11   * <code>
12   *     | name   | town   | jan 14 | jan 15 | jan 16 |
13   *     | Rob    | Leiden |   4    |   1    |   7    |
14   *     | Rob    | Delft  |   0    |   3    |   8    |
15   *     | Erik   | Leiden |   2    |   4    |   1    |
16   *     | Erik   | Sneek  |   1    |   0    |   9    |
17   * </code>
18   * <p>
19   *     Let's say you want to compact this table into the following, normalized format:
20   * <code>
21   *     | name   | town   | date   | visits |
22   *     | Rob    | Leiden | jan 14 |   4    |
23   *     | Rob    | Leiden | jan 15 |   1    |
24   *     | Rob    | Leiden | jan 16 |   7    |
25   *     | Rob    | Delft  | jan 14 |   0    |
26   *     | Rob    | Delft  | jan 15 |   3    |
27   *     | Rob    | Delft  | jan 16 |   8    |
28   *     | Erik   | Leiden | jan 14 |   2    |
29   *     | Erik   | Leiden | jan 15 |   4    |
30   *     | Erik   | Leiden | jan 16 |   1    |
31   *     | Erik   | Sneek  | jan 14 |   1    |
32   *     | Erik   | Sneek  | jan 15 |   0    |
33   *     | Erik   | Sneek  | jan 16 |   9    |
34   * </code>
35   * <p>
36   *     In order to realize this goal, you need to make that startIndexDynamicColumns is set to 3 on @CsvFile.
37   *     This will assume the columns starting with the third and all thereafter are dynamic. For every dynamic
38   *     column, a new bean will be created. All static columns will be copied into every created bean.
39   * </p>
40   * <p>
41   *     The header name and the cell value can be copied into bean properties. In the example, the bean requires
42   *     two fields date and visits. date must be annotated with @CsvHeaderName and visits with @CsvHeaderValue.
43   * </p>
44   * @author Robert Bor
45   */
46  public class DynamicColumn {
47  
48      private Column startColumn;
49  
50      private Column currentColumn;
51  
52      public DynamicColumn(Column configuredStartColumn) {
53          this.startColumn = configuredStartColumn == null ? null : new Column(configuredStartColumn);
54          this.currentColumn = configuredStartColumn == null ? null : new Column(configuredStartColumn);
55      }
56  
57      public void checkForReset(int numberOfColumns) {
58          if (lastDynamicColumnPassed(numberOfColumns)) {
59              reset();
60          }
61      }
62  
63      protected void reset() {
64          this.currentColumn = new Column(this.startColumn);
65      }
66  
67      protected boolean lastDynamicColumnPassed(int numberOfColumns) {
68          return this.currentColumn != null && this.currentColumn.getColumnIndex() > numberOfColumns;
69      }
70  
71      public boolean atFirstDynamicColumn() {
72          return this.startColumn == null || this.startColumn.equals(this.currentColumn);
73      }
74  
75      public void advanceDynamicColumn() {
76          if (currentColumn == null) {
77              return;
78          }
79          this.currentColumn = currentColumn.nextColumn();
80      }
81  
82      public boolean isDynamicColumnActive(Column currentColumn) {
83          return this.currentColumn != null && this.currentColumn.getColumnIndex() == currentColumn.getColumnIndex();
84      }
85  
86  }