Package org.fastnate.data.csv
Class CsvDataImporter<E>
java.lang.Object
org.fastnate.data.properties.PropertyDataImporter
org.fastnate.data.csv.CsvDataImporter<E>
- Type Parameters:
E- The type of the generated objects
public class CsvDataImporter<E>
extends org.fastnate.data.properties.PropertyDataImporter
An importer that reads entities of a specific class from a CSV file.
One can either
add his own column mapping or use the
default properties mapping.
Column names are handled case insensitive.- Author:
- Tobias Liefke
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe name of the property in thesettingsthat contains the delimiter for elements ofCollections respectiveMaps.static final StringThe name of the property in thesettingsthat contains the column delimiter.static final StringThe name of the property in thesettingsthat contains the line delimiter.static final StringThe name of the property in thesettingsthat contains the delimiter for the key and the value for eachMapentry (the entries are delimited byCOLLECTION_DELIMITER). -
Constructor Summary
ConstructorsConstructorDescriptionCsvDataImporter(Class<E> entityClass) Creates a new instance ofCsvDataImporterfor a specific type.CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass) Creates a new instance ofCsvDataImporterfor a specific type.CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.fastnate.data.EntityRegistration entityRegistration) Creates a new instance ofCsvDataImporterfor a specific type.CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.supercsv.prefs.CsvPreference csvSettings) Creates a new instance ofCsvDataImporter.CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.supercsv.prefs.CsvPreference csvSettings, org.fastnate.data.EntityRegistration entityRegistration) Creates a new instance ofCsvDataImporter. -
Method Summary
Modifier and TypeMethodDescription<T> voidaddColumnMapping(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.voidaddColumnMapping(String column, BiConsumer<E, String> propertyMapping) Defines the mapping from a column name to a property.<T> voidaddColumnMapping(String column, Function<String, T> converter, BiConsumer<E, T> propertyMapping) Defines the mapping from a column name to a property with an intermediate converter.voidaddDefaultColumnMapping(String columnName) Defines the mapping from a column name to a property by using the one frombuildMapping(Property).voidaddDefaultColumnMapping(String columnName, String propertyName) Defines the mapping from a column name to a property by using the one frombuildMapping(Property).voidaddIgnoredColumn(String column) Ignores a column during import.voidaddPostProcessor(Consumer<E> postProcessor) Adds a consumer that is called for each new entity.protected booleanapplyColumn(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.createEntities(DataRow row) Builds one or more entities from the given row.protected ECreates a new empty entity for the current converter.protected EcreateEntity(DataRow row) Builds one entity from the given row.The pattern to splitCollectionelements orMapentries, ifmapProperties()oraddDefaultColumnMapping(String)is used.org.supercsv.prefs.CsvPreferenceThe settings to use when importing a file.protected CharsetDefines the default encoding for CSV files, if it can't be determined from the BOM.org.fastnate.generator.context.EntityClass<E> The description of the type of the created entities.org.fastnate.data.EntityRegistrationThe pattern to split key and value of each entry in aMap, ifmapProperties()oraddDefaultColumnMapping(String)is used.importFile(org.fastnate.data.files.DataFile file) Reads entities from the given file.booleanisIgnoredColumn(String column) Indicates that the given column is ignored during import.booleanIndicates to ignore any column that is not found in the CSV file.booleanIndicates to ignore any column that can't be mapped to a property.voidMaps the name of each property of theentity classto the CSV columns.voidMaps the database columns of all properties that are part of the entity table to CSV columns.protected org.supercsv.io.CsvListReaderopenCsvListReader(org.fastnate.data.files.DataFile importFile) Opens a CSV file.removeColumnMapping(String column) Removes the mapping of the given column.voidremoveIgnoredColumn(String column) Removes an ignored column.voidsetCollectionDelimiter(String collectionDelimiter) The pattern to splitCollectionelements orMapentries, ifmapProperties()oraddDefaultColumnMapping(String)is used.voidsetCsvSettings(org.supercsv.prefs.CsvPreference csvSettings) The settings to use when importing a file.voidsetIgnoreMissingColumns(boolean ignoreMissingColumns) Indicates to ignore any column that is not found in the CSV file.voidsetIgnoreUnknownColumns(boolean ignoreUnknownColumns) Indicates to ignore any column that can't be mapped to a property.voidsetMapDelimiter(String mapDelimiter) The pattern to split key and value of each entry in aMap, ifmapProperties()oraddDefaultColumnMapping(String)is used.Methods inherited from class org.fastnate.data.properties.PropertyDataImporter
addConverter, findConverter
-
Field Details
-
COLUMN_DELIMITER
The name of the property in thesettingsthat contains the column delimiter. The default value for this property is ','.- See Also:
-
LINE_DELIMITER
The name of the property in thesettingsthat contains the line delimiter. The default value for this property is '\n'.- See Also:
-
COLLECTION_DELIMITER
The name of the property in thesettingsthat contains the delimiter for elements ofCollections respectiveMaps. The default value for this property is ','.- See Also:
-
MAP_DELIMITER
The name of the property in thesettingsthat contains the delimiter for the key and the value for eachMapentry (the entries are delimited byCOLLECTION_DELIMITER). The default value for this property is ':'.- See Also:
-
-
Constructor Details
-
CsvDataImporter
Creates a new instance ofCsvDataImporterfor a specific type. Will use its owncontextandEntityRegistration, so use only as standalone importer.- Parameters:
entityClass- the type of the created entities
-
CsvDataImporter
Creates a new instance ofCsvDataImporterfor a specific type. Will use its ownEntityRegistration, so use only, if you don't need to exchange entity references between different importers. Will use the defaultCSV settings, by taking the properties from the context of the entity class into account.- Parameters:
entityClass- the description of the type of the created entities.
-
CsvDataImporter
public CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.supercsv.prefs.CsvPreference csvSettings) Creates a new instance ofCsvDataImporter. Will use its ownEntityRegistration, so use only, if you don't need to exchange entity references between different importers.- Parameters:
entityClass- the description of the type of the created entities.csvSettings- the settings to use
-
CsvDataImporter
public CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.supercsv.prefs.CsvPreference csvSettings, org.fastnate.data.EntityRegistration entityRegistration) Creates a new instance ofCsvDataImporter.- Parameters:
entityClass- the description of the type of the created entities.csvSettings- the settings to useentityRegistration- used to store unique keys for entities
-
CsvDataImporter
public CsvDataImporter(org.fastnate.generator.context.EntityClass<E> entityClass, org.fastnate.data.EntityRegistration entityRegistration) Creates a new instance ofCsvDataImporterfor a specific type.- Parameters:
entityClass- the description of the type of the created entities.entityRegistration- used to store unique keys for entities
-
-
Method Details
-
addColumnMapping
Defines the mapping from a column name to a property.- Parameters:
column- the name of the columnpropertyMapping- converts and sets the value for this column
-
addColumnMapping
public <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.- Type Parameters:
T- the type of the property- Parameters:
column- the name of the columnpropertyClass- the type of the propertyconverter- converts the content of the column to the propertypropertyMapping- sets the value for this column
-
addColumnMapping
public <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.- Type Parameters:
T- the type of the property- Parameters:
column- the name of the columnconverter- converts the content of the column to the propertypropertyMapping- sets the value for this column
-
addDefaultColumnMapping
Defines the mapping from a column name to a property by using the one frombuildMapping(Property).- Parameters:
columnName- the name of the column and property
-
addDefaultColumnMapping
Defines the mapping from a column name to a property by using the one frombuildMapping(Property).- Parameters:
columnName- the name of the column and propertypropertyName- the name of the property
-
addIgnoredColumn
Ignores a column during import. Ignored columns only need to be specified ifisIgnoreUnknownColumns()is set tofalse.- Parameters:
column- the name of the column that is ignored
-
addPostProcessor
Adds a consumer that is called for each new entity.- Parameters:
postProcessor- the function to call for each entity
-
applyColumn
Sets a property for an entity from a column value.- Parameters:
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 column- Returns:
trueif an appropriate property was found,falseif a matching property was not found and#isIgnoreUnknownColumns()istrue- Throws:
IllegalArgumentException- if a matching property was not found or it was not convertable
-
buildMapping
protected <T,V> BiConsumer<T,String> buildMapping(org.fastnate.generator.context.Property<? super T, V> property) Builds the mapping for the given property.- Type Parameters:
V- the type of the property- Parameters:
property- the property to map- Returns:
- the mapper of the property or
nullif no specific mapping can be created and the property should be ignored
-
createEntities
Builds one or more entities from the given row.- Parameters:
row- contains the current row data- Returns:
- the list of entities from that row
-
createEntity
Creates a new empty entity for the current converter. Used by the default implementation ofcreateEntities(DataRow).- Returns:
- the new entity
-
createEntity
Builds one entity from the given row.- Parameters:
row- contains the current row data- Returns:
- the created and filled entity
-
getDefaultEncoding
Defines the default encoding for CSV files, if it can't be determined from the BOM.- Returns:
- the default encoding
-
importFile
public List<E> importFile(org.fastnate.data.files.DataFile file) throws IOException, org.fastnate.data.DataImportException Reads entities from the given file.- Parameters:
file- the file to import- Returns:
- all found entities
- Throws:
IOException- if the file was not accessibleorg.fastnate.data.DataImportException- if the file contents was invalid
-
isIgnoredColumn
Indicates that the given column is ignored during import.- Parameters:
column- the name of the column- Returns:
trueif the column is not imported
-
mapProperties
public void mapProperties()Maps the name of each property of theentity classto the CSV columns. Useful if the CSV file is a property export. -
mapTableColumns
public void mapTableColumns()Maps the database columns of all properties that are part of the entity table to CSV columns. Useful if the CSV file is a database export. -
openCsvListReader
protected org.supercsv.io.CsvListReader openCsvListReader(org.fastnate.data.files.DataFile importFile) throws IOException Opens a CSV file. If the given file ends with "gz", then the file is decompressed using aGZIPInputStream.- Parameters:
importFile- the CSV file- Returns:
- the reader used to read the CSV file
- Throws:
IOException- if the file was not accessible
-
removeColumnMapping
Removes the mapping of the given column.- Parameters:
column- the column to remove- Returns:
- the previous mapping or
nullif none was registered
-
removeIgnoredColumn
Removes an ignored column.- Parameters:
column- the name of the column that is ignored- See Also:
-
getEntityClass
The description of the type of the created entities. -
getEntityRegistration
public org.fastnate.data.EntityRegistration getEntityRegistration() -
getCsvSettings
public org.supercsv.prefs.CsvPreference getCsvSettings()The settings to use when importing a file. -
getCollectionDelimiter
The pattern to splitCollectionelements orMapentries, ifmapProperties()oraddDefaultColumnMapping(String)is used. Defaults to theCOLLECTION_DELIMITERproperty from thesettings. -
getMapDelimiter
The pattern to split key and value of each entry in aMap, ifmapProperties()oraddDefaultColumnMapping(String)is used. Defaults to theMAP_DELIMITERproperty from thesettings. -
isIgnoreMissingColumns
public boolean isIgnoreMissingColumns()Indicates to ignore any column that is not found in the CSV file. -
isIgnoreUnknownColumns
public boolean isIgnoreUnknownColumns()Indicates to ignore any column that can't be mapped to a property. -
setCsvSettings
public void setCsvSettings(org.supercsv.prefs.CsvPreference csvSettings) The settings to use when importing a file. -
setCollectionDelimiter
The pattern to splitCollectionelements orMapentries, ifmapProperties()oraddDefaultColumnMapping(String)is used. Defaults to theCOLLECTION_DELIMITERproperty from thesettings. -
setMapDelimiter
The pattern to split key and value of each entry in aMap, ifmapProperties()oraddDefaultColumnMapping(String)is used. Defaults to theMAP_DELIMITERproperty from thesettings. -
setIgnoreMissingColumns
public void setIgnoreMissingColumns(boolean ignoreMissingColumns) Indicates to ignore any column that is not found in the CSV file. -
setIgnoreUnknownColumns
public void setIgnoreUnknownColumns(boolean ignoreUnknownColumns) Indicates to ignore any column that can't be mapped to a property.
-