-
Notifications
You must be signed in to change notification settings - Fork 148
dev/add datatypes to excel columns #230
base: master
Are you sure you want to change the base?
Changes from 21 commits
90303fa
999647c
5a75486
b82e728
8c21387
8919900
aae088f
7ae1b26
77a1b80
84c6f06
b4d9a20
1a34285
3785c91
66749e9
a8ab4b6
57108bc
138b147
5088744
4bfcc41
0701bab
9c3976e
cc85725
23a7781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,25 +38,38 @@ public final class ExcelConfiguration extends BaseObject implements | |
| public static final int NO_COLUMN_NAME_LINE = 0; | ||
| public static final int DEFAULT_COLUMN_NAME_LINE = 1; | ||
|
|
||
| private final int getNumberOfLinesToScan; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you rename
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with next commit. |
||
| private final int columnNameLineNumber; | ||
| private final ColumnNamingStrategy columnNamingStrategy; | ||
| private final boolean skipEmptyLines; | ||
| private final boolean skipEmptyColumns; | ||
| private final boolean detectColumnTypes; | ||
|
|
||
| public ExcelConfiguration() { | ||
| this(DEFAULT_COLUMN_NAME_LINE, true, false); | ||
| } | ||
|
|
||
| public ExcelConfiguration(int columnNameLineNumber, boolean skipEmptyLines, boolean skipEmptyColumns) { | ||
| this(columnNameLineNumber, null, skipEmptyLines, skipEmptyColumns); | ||
| this(columnNameLineNumber, null, skipEmptyLines, skipEmptyColumns, false, 1000); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if the default value of a "1000" was in a constant, and maybe you want to set the default to zero in case you pass "false" as an argument for the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with next commit. |
||
| } | ||
|
|
||
| public ExcelConfiguration(int columnNameLineNumber, ColumnNamingStrategy columnNamingStrategy, | ||
| Boolean skipEmptyLines, Boolean skipEmptyColumns) { | ||
| this(columnNameLineNumber, columnNamingStrategy, skipEmptyLines, skipEmptyColumns, false, 1000); | ||
| } | ||
|
|
||
| public ExcelConfiguration(int columnNameLineNumber, boolean skipEmptyLines, boolean skipEmptyColumns, boolean detectColumnTypes) { | ||
| this(columnNameLineNumber, null, skipEmptyLines, skipEmptyColumns, detectColumnTypes, 1000); | ||
| } | ||
|
|
||
| public ExcelConfiguration(int columnNameLineNumber, ColumnNamingStrategy columnNamingStrategy, | ||
| boolean skipEmptyLines, boolean skipEmptyColumns) { | ||
| boolean skipEmptyLines, boolean skipEmptyColumns, boolean detectColumnTypes, int eagerness) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you rename
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with next commit. |
||
| this.columnNameLineNumber = columnNameLineNumber; | ||
| this.skipEmptyLines = skipEmptyLines; | ||
| this.skipEmptyColumns = skipEmptyColumns; | ||
| this.columnNamingStrategy = columnNamingStrategy; | ||
| this.detectColumnTypes = detectColumnTypes; | ||
| this.getNumberOfLinesToScan = eagerness; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -102,17 +115,33 @@ public boolean isSkipEmptyColumns() { | |
| return skipEmptyColumns; | ||
| } | ||
|
|
||
| /** | ||
| * Defines if columns in the excel spreadsheet should be validated on datatypes while | ||
| * reading the spreadsheet. | ||
| * | ||
| * @return a boolean indicating whether or not to validate column types. | ||
| */ | ||
| public boolean isDetectColumnTypes() { | ||
| return detectColumnTypes; | ||
| } | ||
|
|
||
| @Override | ||
| protected void decorateIdentity(List<Object> identifiers) { | ||
| identifiers.add(columnNameLineNumber); | ||
| identifiers.add(skipEmptyLines); | ||
| identifiers.add(skipEmptyColumns); | ||
| identifiers.add(detectColumnTypes); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with next commit. |
||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ExcelConfiguration[columnNameLineNumber=" | ||
| + columnNameLineNumber + ", skipEmptyLines=" + skipEmptyLines | ||
| + ", skipEmptyColumns=" + skipEmptyColumns + "]"; | ||
| + ", skipEmptyColumns=" + skipEmptyColumns +", detectColumnTypes=" | ||
| + detectColumnTypes + "]"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with next commit. |
||
| } | ||
|
|
||
| public int getEagerness() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you rename
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with next commit. |
||
| return getNumberOfLinesToScan; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,21 +20,23 @@ | |
|
|
||
| import java.util.Date; | ||
|
|
||
| import org.apache.poi.ss.usermodel.Cell; | ||
| import org.apache.poi.ss.usermodel.CellStyle; | ||
| import org.apache.poi.ss.usermodel.FillPatternType; | ||
| import org.apache.poi.ss.usermodel.Font; | ||
| import org.apache.poi.ss.usermodel.HorizontalAlignment; | ||
| import org.apache.poi.ss.usermodel.Row; | ||
| import org.apache.metamodel.MetaModelException; | ||
| import org.apache.metamodel.data.Style; | ||
| import org.apache.metamodel.data.Style.Color; | ||
| import org.apache.metamodel.data.Style.SizeUnit; | ||
| import org.apache.metamodel.data.Style.TextAlignment; | ||
| import org.apache.metamodel.insert.AbstractRowInsertionBuilder; | ||
| import org.apache.metamodel.insert.RowInsertionBuilder; | ||
| import org.apache.metamodel.schema.Column; | ||
| import org.apache.metamodel.schema.ColumnType; | ||
| import org.apache.metamodel.schema.Table; | ||
| import org.apache.metamodel.util.LazyRef; | ||
| import org.apache.poi.ss.usermodel.Cell; | ||
| import org.apache.poi.ss.usermodel.CellStyle; | ||
| import org.apache.poi.ss.usermodel.FillPatternType; | ||
| import org.apache.poi.ss.usermodel.Font; | ||
| import org.apache.poi.ss.usermodel.HorizontalAlignment; | ||
| import org.apache.poi.ss.usermodel.Row; | ||
|
|
||
| /** | ||
| * {@link RowInsertionBuilder} for excel spreadsheets. | ||
|
|
@@ -149,8 +151,33 @@ protected CellStyle fetch() { | |
| cell.setCellStyle(cellStyle.get()); | ||
| } | ||
| } | ||
| validateUpdateType(row); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the value is of adding this here. I think that for now it may be best to skip the "validation" part.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see: ExcelDataContextTest.testUpdateDifferentDataTypes It looks if the Column has a specific ColumnType and if so it will validate the given row.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SociopathicPixel I'm sorry, but I still don't get why this has been added here. |
||
| } | ||
| } | ||
|
|
||
| private void validateUpdateType(final Row original) { | ||
| for (int index = 0; index < this.getColumns().length; index++) { | ||
| final ColumnType columnType = getColumns()[index].getType(); | ||
| if (columnType != null && getValues()[index] != null) { | ||
| switch (columnType.getName()) { | ||
| case "INTEGER": | ||
| try { | ||
| Integer.decode(getValues()[index].toString()); | ||
| } catch (NumberFormatException ex) { | ||
| throw new MetaModelException(original.getCell(index) | ||
| + " should be an Integer!"); | ||
| } | ||
| break; | ||
| case "STRING": | ||
| // fall through | ||
| case "VARCHAR": | ||
| // fall through | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a percentage based font size to excel "pt" scale. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -414,13 +414,13 @@ public static Iterator<Row> getRowIterator(Sheet sheet, ExcelConfiguration confi | |
| */ | ||
| public static DefaultRow createRow(Workbook workbook, Row row, DataSetHeader header) { | ||
| final int size = header.size(); | ||
| final String[] values = new String[size]; | ||
| final Object[] values = new Object[size]; | ||
| final Style[] styles = new Style[size]; | ||
| if (row != null) { | ||
| for (int i = 0; i < size; i++) { | ||
| final int columnNumber = header.getSelectItem(i).getColumn().getColumnNumber(); | ||
| final Cell cell = row.getCell(columnNumber); | ||
| final String value = ExcelUtils.getCellValue(workbook, cell); | ||
| final Object value = ExcelUtils.getCellValue(workbook, cell); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning an Object doesn't do the trick, we know based on the column type, what type of object should be returned, so use that to either return a String, Integer, Boolean or Date object. |
||
| final Style style = ExcelUtils.getCellStyle(workbook, cell); | ||
| values[i] = value; | ||
| styles[i] = style; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change
eagernessto something likenumberOfLinesToScan?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with next commit.