E - The type of the generated objectspublic class CsvDataImporter<E>
extends org.fastnate.data.properties.PropertyDataImporter
add his own column mapping or use the
default properties mapping.
Column names are handled case insensitive.| Modifier and Type | Field and Description |
|---|---|
static String |
COLLECTION_DELIMITER
The name of the property in the
settings that contains the delimiter for
elements of Collections respective Maps. |
static String |
COLUMN_DELIMITER
The name of the property in the
settings that contains the column
delimiter. |
static String |
LINE_DELIMITER
The name of the property in the
settings that contains the line delimiter. |
static String |
MAP_DELIMITER
The name of the property in the
settings that contains the delimiter for
the key and the value for each Map entry (the entries are delimited by COLLECTION_DELIMITER). |
| Constructor and Description |
|---|
CsvDataImporter(Class<E> entityClass)
Creates a new instance of
CsvDataImporter for a specific type. |
CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass)
Creates a new instance of
CsvDataImporter for a specific type. |
CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass,
org.supercsv.prefs.CsvPreference csvSettings)
Creates a new instance of
CsvDataImporter. |
CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass,
org.supercsv.prefs.CsvPreference csvSettings,
org.fastnate.data.EntityRegistration entityRegistration)
Creates a new instance of
CsvDataImporter. |
CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass,
org.fastnate.data.EntityRegistration entityRegistration)
Creates a new instance of
CsvDataImporter for a specific type. |
| Modifier and Type | Method and Description |
|---|---|
void |
addColumnMapping(String column,
BiConsumer<E,String> propertyMapping)
Defines the mapping from a column name to a property.
|
<T> void |
addColumnMapping(String column,
Class<T> propertyClass,
org.fastnate.data.properties.PropertyConverter<T> converter,
BiConsumer<E,T> propertyMapping)
Defines the mapping from a column name to a property with an intermediate converter.
|
<T> void |
addColumnMapping(String column,
Function<String,T> converter,
BiConsumer<E,T> propertyMapping)
Defines the mapping from a column name to a property with an intermediate converter.
|
void |
addDefaultColumnMapping(String columnName)
Defines the mapping from a column name to a property by using the one from
buildMapping(Property). |
void |
addDefaultColumnMapping(String columnName,
String propertyName)
Defines the mapping from a column name to a property by using the one from
buildMapping(Property). |
void |
addIgnoredColumn(String column)
Ignores a column during import.
|
void |
addPostProcessor(Consumer<E> postProcessor)
Adds a consumer that is called for each new entity.
|
protected boolean |
applyColumn(E entity,
String column,
String value)
Sets a property for an entity from a column value.
|
protected <T,V> BiConsumer<T,String> |
buildMapping(org.fastnate.generator.context.Property<? super T,V> property)
Builds the mapping for the given property.
|
protected List<? extends E> |
createEntities(DataRow row)
Builds one or more entities from the given row.
|
protected E |
createEntity()
Creates a new empty entity for the current converter.
|
protected E |
createEntity(DataRow row)
Builds one entity from the given row.
|
String |
getCollectionDelimiter()
The pattern to split
Collection elements or Map entries, if mapProperties() or
addDefaultColumnMapping(String) is used. |
org.supercsv.prefs.CsvPreference |
getCsvSettings()
The settings to use when importing a file.
|
protected Charset |
getDefaultEncoding()
Defines the default encoding for CSV files, if it can't be determined from the BOM.
|
org.fastnate.generator.context.EntityClass<E> |
getEntityClass()
The description of the type of the created entities.
|
org.fastnate.data.EntityRegistration |
getEntityRegistration() |
String |
getMapDelimiter()
The pattern to split key and value of each entry in a
Map, if mapProperties() or
addDefaultColumnMapping(String) is used. |
List<E> |
importFile(org.fastnate.data.files.DataFile file)
Reads entities from the given file.
|
boolean |
isIgnoredColumn(String column)
Indicates that the given column is ignored during import.
|
boolean |
isIgnoreMissingColumns()
Indicates to ignore any column that is not found in the CSV file.
|
boolean |
isIgnoreUnknownColumns()
Indicates to ignore any column that can't be mapped to a property.
|
void |
mapProperties()
Maps the name of each property of the
entity class to the CSV columns. |
void |
mapTableColumns()
Maps the database columns of all properties that are part of the entity table to CSV columns.
|
protected org.supercsv.io.CsvListReader |
openCsvListReader(org.fastnate.data.files.DataFile importFile)
Opens a CSV file.
|
BiConsumer<E,String> |
removeColumnMapping(String column)
Removes the mapping of the given column.
|
void |
removeIgnoredColumn(String column)
Removes an ignored column.
|
void |
setCollectionDelimiter(String collectionDelimiter)
The pattern to split
Collection elements or Map entries, if mapProperties() or
addDefaultColumnMapping(String) is used. |
void |
setCsvSettings(org.supercsv.prefs.CsvPreference csvSettings)
The settings to use when importing a file.
|
void |
setIgnoreMissingColumns(boolean ignoreMissingColumns)
Indicates to ignore any column that is not found in the CSV file.
|
void |
setIgnoreUnknownColumns(boolean ignoreUnknownColumns)
Indicates to ignore any column that can't be mapped to a property.
|
void |
setMapDelimiter(String mapDelimiter)
The pattern to split key and value of each entry in a
Map, if mapProperties() or
addDefaultColumnMapping(String) is used. |
public static final String COLUMN_DELIMITER
settings that contains the column
delimiter.
The default value for this property is ','.public static final String LINE_DELIMITER
settings that contains the line delimiter.
The default value for this property is '\n'.public static final String COLLECTION_DELIMITER
settings that contains the delimiter for
elements of Collections respective Maps.
The default value for this property is ','.public static final String MAP_DELIMITER
settings that contains the delimiter for
the key and the value for each Map entry (the entries are delimited by COLLECTION_DELIMITER).
The default value for this property is ':'.public CsvDataImporter(Class<E> entityClass)
CsvDataImporter for a specific type.
Will use its own context and EntityRegistration, so use only as standalone
importer.entityClass - the type of the created entitiespublic CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass)
CsvDataImporter for a specific type.
Will use its own EntityRegistration, so use only, if you don't need to exchange entity references between
different importers.
Will use the default CSV settings, by taking the properties from the context of the entity
class into account.entityClass - the description of the type of the created entities.public CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.supercsv.prefs.CsvPreference csvSettings)
CsvDataImporter.
Will use its own EntityRegistration, so use only, if you don't need to exchange entity references between
different importers.entityClass - the description of the type of the created entities.csvSettings - the settings to usepublic CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.supercsv.prefs.CsvPreference csvSettings, org.fastnate.data.EntityRegistration entityRegistration)
CsvDataImporter.entityClass - the description of the type of the created entities.csvSettings - the settings to useentityRegistration - used to store unique keys for entitiespublic CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.fastnate.data.EntityRegistration entityRegistration)
CsvDataImporter for a specific type.entityClass - the description of the type of the created entities.entityRegistration - used to store unique keys for entitiespublic void addColumnMapping(String column, BiConsumer<E,String> propertyMapping)
column - the name of the columnpropertyMapping - converts and sets the value for this columnpublic <T> void addColumnMapping(String column, Class<T> propertyClass, org.fastnate.data.properties.PropertyConverter<T> converter, BiConsumer<E,T> propertyMapping)
T - the type of the propertycolumn - the name of the columnpropertyClass - the type of the propertyconverter - converts the content of the column to the propertypropertyMapping - sets the value for this columnpublic <T> void addColumnMapping(String column, Function<String,T> converter, BiConsumer<E,T> propertyMapping)
T - the type of the propertycolumn - the name of the columnconverter - converts the content of the column to the propertypropertyMapping - sets the value for this columnpublic void addDefaultColumnMapping(String columnName)
buildMapping(Property).columnName - the name of the column and propertypublic void addDefaultColumnMapping(String columnName, String propertyName)
buildMapping(Property).columnName - the name of the column and propertypropertyName - the name of the propertypublic void addIgnoredColumn(String column)
isIgnoreUnknownColumns() is set to false.column - the name of the column that is ignoredpublic void addPostProcessor(Consumer<E> postProcessor)
postProcessor - the function to call for each entityprotected boolean applyColumn(E entity, String column, String value)
entity - the entity to modifycolumn - the name of the current column (in lower case, as the mapping is stored in lower case, too)value - the value of the columntrue if an appropriate property was found, false if a matching property was not found and
#isIgnoreUnknownColumns() is trueIllegalArgumentException - if a matching property was not found or it was not convertableprotected <T,V> BiConsumer<T,String> buildMapping(org.fastnate.generator.context.Property<? super T,V> property)
V - the type of the propertyproperty - the property to mapnull if no specific mapping can be created and the property should
be ignoredprotected List<? extends E> createEntities(DataRow row)
row - contains the current row dataprotected E createEntity()
createEntities(DataRow).protected E createEntity(DataRow row)
row - contains the current row dataprotected Charset getDefaultEncoding()
public List<E> importFile(org.fastnate.data.files.DataFile file) throws IOException, org.fastnate.data.DataImportException
file - the file to importIOException - if the file was not accessibleorg.fastnate.data.DataImportException - if the file contents was invalidpublic boolean isIgnoredColumn(String column)
column - the name of the columntrue if the column is not importedpublic void mapProperties()
entity class to the CSV columns.
Useful if the CSV file is a property export.public void mapTableColumns()
protected org.supercsv.io.CsvListReader openCsvListReader(org.fastnate.data.files.DataFile importFile)
throws IOException
GZIPInputStream.importFile - the CSV fileIOException - if the file was not accessiblepublic BiConsumer<E,String> removeColumnMapping(String column)
column - the column to removenull if none was registeredpublic void removeIgnoredColumn(String column)
column - the name of the column that is ignoredaddIgnoredColumn(String)public org.fastnate.generator.context.EntityClass<E> getEntityClass()
public org.fastnate.data.EntityRegistration getEntityRegistration()
public org.supercsv.prefs.CsvPreference getCsvSettings()
public String getCollectionDelimiter()
Collection elements or Map entries, if mapProperties() or
addDefaultColumnMapping(String) is used. Defaults to the COLLECTION_DELIMITER property from the
settings.public String getMapDelimiter()
Map, if mapProperties() or
addDefaultColumnMapping(String) is used. Defaults to the MAP_DELIMITER property from the
settings.public boolean isIgnoreMissingColumns()
public boolean isIgnoreUnknownColumns()
public void setCsvSettings(org.supercsv.prefs.CsvPreference csvSettings)
public void setCollectionDelimiter(String collectionDelimiter)
Collection elements or Map entries, if mapProperties() or
addDefaultColumnMapping(String) is used. Defaults to the COLLECTION_DELIMITER property from the
settings.public void setMapDelimiter(String mapDelimiter)
Map, if mapProperties() or
addDefaultColumnMapping(String) is used. Defaults to the MAP_DELIMITER property from the
settings.public void setIgnoreMissingColumns(boolean ignoreMissingColumns)
public void setIgnoreUnknownColumns(boolean ignoreUnknownColumns)
Copyright © 2026 fastnate.org. All rights reserved.