Class CsvContentImporter
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.plugins.contentio.AbstractContentImporter
-
- org.ametys.plugins.contentio.in.csv.CsvContentImporter
-
- All Implemented Interfaces:
ContentImporter
,LogEnabled
,Configurable
,Serviceable
public class CsvContentImporter extends AbstractContentImporter
ContentImporter
importing contents from a CSV file. Each CSV record (line) contains content properties. Configuration options:- The CSV file charset (default: auto-detect)
- The CSV delimiter character (default: auto-detect from the header)
- The CSV quote character (default to the double-quote:
"
) - Structure of the header line: fixed pattern or column list.
- Mapping from CSV columns to content metadatas.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.ametys.plugins.contentio.AbstractContentImporter
AbstractContentImporter.ContentReference
-
-
Field Summary
Fields Modifier and Type Field Description protected Charset
_charset
The file charset.protected boolean
_columnHeaderLine
True if the supported CSV files have a header formed of the columns (this is not always the case).protected Map<String,String>
_columnToMetadata
Contains mapping from CSV column to content metadata path.protected ContentTypesHelper
_cTypeHelper
The content type helper.protected Character
_delimiterChar
The CSV delimiter character.protected Set<String>
_matchColumns
Determine if the file is supported by detecting the following columns in the header.protected Pattern
_matchPattern
Determine if the file is supported by matching the header line against this pattern.protected Character
_quoteChar
The CSV quote character.-
Fields inherited from class org.ametys.plugins.contentio.AbstractContentImporter
_CONTENT_ID_MAP_KEY, _CONTENT_LINK_MAP_KEY, _CONTENT_REPEATER_SIZE_MAP, _contentTypes, _contentWorkflowHelper, _editActionId, _extensions, _initialActionId, _language, _mixins, _priority, _resolver, _workflowName, DEFAULT_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description CsvContentImporter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private ICsvMapReader
_getMapReader(BufferedReader reader, CsvPreference preference)
void
configure(Configuration configuration)
protected void
configureCsvProperties(Configuration configuration)
Configure CSV parsing and mapping properties.protected CsvPreference
getCsvPreference(String header, Map<String,Object> params)
Get the CSV preference.protected Collection<String>
getDefaultExtensions()
Get the default allowed extensions.protected char
getDelimiter(String header, Map<String,Object> params)
Get the CSV character delimiter.protected MetadataDefinition
getMetadataDefinition(Content content, MetadataDefinition parentMetaDef, String name)
Get a metadata definition, either from the parent metadata definition or from the content itself.protected Map<String,String>
getMetadataFromProperties(Map<String,String> properties)
Get the content metadata from a CSV record.protected char
getQuoteChar(Map<String,Object> params)
Get the CSV quote character.protected Reader
getReader(InputStream in)
Get a reader on the data stream, optionally detecting the charset.protected String
importContent(Map<String,String> properties, Map<String,Object> params, int lineNumber)
Import a content from a CSV record.Set<String>
importContents(InputStream is, Map<String,Object> params)
Import the contents from the file.protected boolean
matchColumns(InputStream is)
Test if the importer supports the given file by testing if it contains the configured columns in its header.protected boolean
matchHeaderPattern(InputStream is)
Test if the importer supports the given file by matching its first line against the configured pattern.void
service(ServiceManager serviceManager)
protected void
setMetadata(ModifiableContent content, String path, String value, Map<String,Object> params)
Set a metadata from its string value.protected void
setMetadata(ModifiableCompositeMetadata meta, MetadataDefinition metaDef, String name, String value, Map<String,Object> params)
Set a single metadata.protected void
setMetadatas(ModifiableContent content, Map<String,String> metaValues, Map<String,Object> params)
Set the content metadatas from the CSV values.protected void
setRichText(ModifiableCompositeMetadata meta, String name, String value)
Set a RichText metadata from a String value.boolean
supports(InputStream is, String name)
Tests if theContentImporter
is able to import the corresponding stream.
Implementing classes should first test the file extension, if possible, and if the name is provided.-
Methods inherited from class org.ametys.plugins.contentio.AbstractContentImporter
addContentReference, addContentReference, addContentReferences, configureContentCreation, configureExtensions, configureWorkflow, createContent, createContent, createContent, createContent, createContent, getContentFromProperties, getContentIdMap, getContentRefMap, getContentRepeaterSizeMap, getContentTypes, getEditActionId, getInitialActionId, getLanguage, getMixins, getPriority, getReferencedContentId, getWorkflowName, isExtensionValid, parseDate, parseDate, restoreContentReferences, setBinaryMetadata, setBooleanMetadata, setDateMetadata, setDoubleMetadata, setGeocodeMetadata, setGeocodeMetadata, setLongMetadata, setReferenceMetadatas, setRepeaterSize, setRichText, setStringMetadata
-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
-
-
-
Field Detail
-
_cTypeHelper
protected ContentTypesHelper _cTypeHelper
The content type helper.
-
_delimiterChar
protected Character _delimiterChar
The CSV delimiter character.
-
_quoteChar
protected Character _quoteChar
The CSV quote character.
-
_columnHeaderLine
protected boolean _columnHeaderLine
True if the supported CSV files have a header formed of the columns (this is not always the case).
-
_matchPattern
protected Pattern _matchPattern
Determine if the file is supported by matching the header line against this pattern.
-
_matchColumns
protected Set<String> _matchColumns
Determine if the file is supported by detecting the following columns in the header.
-
_columnToMetadata
protected Map<String,String> _columnToMetadata
Contains mapping from CSV column to content metadata path.
-
-
Constructor Detail
-
CsvContentImporter
public CsvContentImporter()
-
-
Method Detail
-
service
public void service(ServiceManager serviceManager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Overrides:
service
in classAbstractContentImporter
- Throws:
ServiceException
-
configure
public void configure(Configuration configuration) throws ConfigurationException
- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classAbstractContentImporter
- Throws:
ConfigurationException
-
configureCsvProperties
protected void configureCsvProperties(Configuration configuration) throws ConfigurationException
Configure CSV parsing and mapping properties.- Parameters:
configuration
- the CSV configuration.- Throws:
ConfigurationException
- if an error occurs.
-
getDefaultExtensions
protected Collection<String> getDefaultExtensions()
Description copied from class:AbstractContentImporter
Get the default allowed extensions.- Overrides:
getDefaultExtensions
in classAbstractContentImporter
- Returns:
- the default allowed extensions, without leading dots. Cannot be null.
-
supports
public boolean supports(InputStream is, String name) throws IOException
Description copied from interface:ContentImporter
Tests if theContentImporter
is able to import the corresponding stream.
Implementing classes should first test the file extension, if possible, and if the name is provided.- Parameters:
is
- an input stream on the file, cannot be null. The stream is closed by the caller.name
- the file name, can be null.- Returns:
- true if the
ContentImporter
can import the corresponding stream, false otherwise. - Throws:
IOException
- if an error occurs.
-
matchHeaderPattern
protected boolean matchHeaderPattern(InputStream is) throws IOException
Test if the importer supports the given file by matching its first line against the configured pattern.- Parameters:
is
- an input stream on the data to test.- Returns:
- true if the data's first line matches the pattern, false otherwise.
- Throws:
IOException
- if a read error occurs.
-
matchColumns
protected boolean matchColumns(InputStream is) throws IOException
Test if the importer supports the given file by testing if it contains the configured columns in its header.- Parameters:
is
- an input stream on the data to test.- Returns:
- true if the CSV columns contain all the configured columns, false otherwise.
- Throws:
IOException
- if a read error occurs.
-
_getMapReader
private ICsvMapReader _getMapReader(BufferedReader reader, CsvPreference preference) throws IOException
- Throws:
IOException
-
importContents
public Set<String> importContents(InputStream is, Map<String,Object> params) throws IOException
Description copied from interface:ContentImporter
Import the contents from the file.- Parameters:
is
- an input stream on the file, cannot be null. The stream is closed by the caller.params
- input or output parameters.- Returns:
- a Set of the imported content IDs.
- Throws:
IOException
- if an error occurs importing the contents.
-
getReader
protected Reader getReader(InputStream in)
Get a reader on the data stream, optionally detecting the charset.- Parameters:
in
- the data stream.- Returns:
- the reader with the correct character set.
-
getCsvPreference
protected CsvPreference getCsvPreference(String header, Map<String,Object> params)
Get the CSV preference.- Parameters:
header
- the CSV first line.params
- the import parameters.- Returns:
- a
CsvPreference
object.
-
getDelimiter
protected char getDelimiter(String header, Map<String,Object> params)
Get the CSV character delimiter.- Parameters:
header
- the CSV first line.params
- the import parameters.- Returns:
- the CSV character delimiter.
-
getQuoteChar
protected char getQuoteChar(Map<String,Object> params)
Get the CSV quote character.- Parameters:
params
- the import parameters.- Returns:
- the CSV quote character.
-
importContent
protected String importContent(Map<String,String> properties, Map<String,Object> params, int lineNumber)
Import a content from a CSV record.- Parameters:
properties
- the CSV record as a Map of values, indexed by column name or number.params
- the import parameters.lineNumber
- the line number of the record being imported, for logging purposes.- Returns:
- the content ID or null if the content was not created.
-
getMetadataFromProperties
protected Map<String,String> getMetadataFromProperties(Map<String,String> properties)
Get the content metadata from a CSV record.- Parameters:
properties
- the CSV record as a Map of values, indexed by column name or number.- Returns:
- a Map of metadata values, indexed by metadata path.
-
setMetadatas
protected void setMetadatas(ModifiableContent content, Map<String,String> metaValues, Map<String,Object> params)
Set the content metadatas from the CSV values.- Parameters:
content
- the content to populate.metaValues
- the metadata values, extracted from the CSV record.params
- the import parameters.
-
setMetadata
protected void setMetadata(ModifiableContent content, String path, String value, Map<String,Object> params)
Set a metadata from its string value.- Parameters:
content
- the content to populate.path
- the metadata path.value
- the metadata string value.params
- the import parameters.
-
getMetadataDefinition
protected MetadataDefinition getMetadataDefinition(Content content, MetadataDefinition parentMetaDef, String name)
Get a metadata definition, either from the parent metadata definition or from the content itself.- Parameters:
content
- the imported content.parentMetaDef
- the parent metadata definition.name
- the metadata name.- Returns:
- the metadata definition.
-
setMetadata
protected void setMetadata(ModifiableCompositeMetadata meta, MetadataDefinition metaDef, String name, String value, Map<String,Object> params) throws IOException
Set a single metadata.- Parameters:
meta
- the metadata holder.metaDef
- the metadata definition.name
- the metadata name.value
- the metadata value as a String.params
- the import parameters.- Throws:
IOException
- if an error occurs.
-
setRichText
protected void setRichText(ModifiableCompositeMetadata meta, String name, String value) throws IOException
Set a RichText metadata from a String value.- Parameters:
meta
- the metadata holder.name
- the metadata name.value
- the String value.- Throws:
IOException
- if something goes wrong when manipulating files
-
-