Package org.ametys.plugins.contentio.csv
Class ImportCSVFileHelper
java.lang.Object
org.ametys.runtime.plugin.component.AbstractLogEnabled
org.ametys.plugins.contentio.csv.ImportCSVFileHelper
- All Implemented Interfaces:
LogEnabled
,Initializable
,Component
,Serviceable
public class ImportCSVFileHelper
extends AbstractLogEnabled
implements Component, Serviceable, Initializable
Import contents from an uploaded CSV file.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Column name of attribute path in CSV mappingstatic final String
Column name of header in CSV mappingstatic final String
Column name to identify if column is an ID in CSV mappingstatic final String
Key in nested mapping to define the ID columnsstatic final String
Key in nested mapping to define the values columnsstatic final String
Result key containing a short an explanation for a general errorstatic final String
Result key containing the filenamestatic final String
Result key containing updated content's idsstatic final String
Result key containing number of errorsstatic final String
Result key containing number of warningsstatic final String
Avalon Role -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
deleteFile
(String path) Delete the file related to the given pathdetectCharset
(InputStream inputStream) Detect the charset of a given resource.String[]
extractHeaders
(InputStream inputStream, CsvPreference csvPreference, Charset charset) Extract headers from the CSV resource.getImportCSVParametersValues
(String contentTypeId) Retrieves the values for import CSV parametersgetMapping
(ContentType contentType, String[] headers) Get the mapping from the headers and corresponding to the content type.importContents
(InputStream inputStream, ContentType contentType, String language, SynchronizeModeEnumerator.ImportMode importMode) Import contents from path, content type and language.importContents
(Map<String, Object> config, Map<String, Object> formValues, List<Map<String, Object>> mappingValues) Gets the configuration for creating/editing a collection of synchronizable contents.void
void
service
(ServiceManager serviceManager) Gets the configuration for creating/editing a collection of synchronizable contents.Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Field Details
-
ROLE
Avalon Role -
RESULT_IMPORTED_COUNT
Result key containing updated content's ids- See Also:
-
RESULT_NB_ERRORS
Result key containing number of errors- See Also:
-
RESULT_NB_WARNINGS
Result key containing number of warnings- See Also:
-
RESULT_ERROR
Result key containing a short an explanation for a general error- See Also:
-
RESULT_FILENAME
Result key containing the filename- See Also:
-
MAPPING_COLUMN_ATTRIBUTE_PATH
Column name of attribute path in CSV mapping- See Also:
-
MAPPING_COLUMN_HEADER
Column name of header in CSV mapping- See Also:
-
MAPPING_COLUMN_IS_ID
Column name to identify if column is an ID in CSV mapping- See Also:
-
NESTED_MAPPING_ID
Key in nested mapping to define the ID columns- See Also:
-
NESTED_MAPPING_VALUES
Key in nested mapping to define the values columns- See Also:
-
-
Constructor Details
-
ImportCSVFileHelper
public ImportCSVFileHelper()
-
-
Method Details
-
service
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
initialize
- Specified by:
initialize
in interfaceInitializable
- Throws:
Exception
-
getImportCSVParametersValues
Retrieves the values for import CSV parameters- Parameters:
contentTypeId
- the configured content type identifier- Returns:
- the values for import CSV parameters
- Throws:
Exception
- if an error occurs.
-
validateConfiguration
public Map<String,Object> validateConfiguration(String contentTypeId, Map formValues, List<Map<String, Object>> mappingValues) throws IOExceptionGets the configuration for creating/editing a collection of synchronizable contents.- Parameters:
contentTypeId
- Content type idformValues
- map of form valuesmappingValues
- list of header and content attribute mapping- Returns:
- A map containing information about what is needed to create/edit a collection of synchronizable contents
- Throws:
IOException
- IOException while reading CSV
-
importContents
public Map<String,Object> importContents(Map<String, Object> config, Map<String, throws IOExceptionObject> formValues, List<Map<String, Object>> mappingValues) Gets the configuration for creating/editing a collection of synchronizable contents.- Parameters:
config
- get all CSV related parameters: path, separating/escaping char, charset and contentTypeformValues
- map of form valuesmappingValues
- list of header and content attribute mapping- Returns:
- A map containing information about what is needed to create/edit a collection of synchronizable contents
- Throws:
IOException
- IOException while reading CSV
-
importContents
public Map<String,Object> importContents(InputStream inputStream, ContentType contentType, String language, SynchronizeModeEnumerator.ImportMode importMode) throws IOException Import contents from path, content type and language. CSV Excel north Europe preferences are used, charset is detected on the file, workflow is detected on content type, default creation (1) and edition (2) actions are used, values are automatically mapped from the header. Headers should contains a star (*) to detect identifier columns.- Parameters:
inputStream
- The (buffered) input stream to the CSV resourcecontentType
- The content typelanguage
- The languageimportMode
- The import mode- Returns:
- the result of the CSV import
- Throws:
IOException
- if an error occurs
-
deleteFile
Delete the file related to the given path- Parameters:
path
- path of the file- Throws:
IOException
- if an error occurs while deleting the file
-
detectCharset
Detect the charset of a given resource.- Parameters:
inputStream
- The input stream to the resource, need to supportInputStream.mark(int)
andInputStream.reset()
.- Returns:
- the charset
- Throws:
IOException
- if an error occurs
-
extractHeaders
public String[] extractHeaders(InputStream inputStream, CsvPreference csvPreference, Charset charset) throws IOException Extract headers from the CSV resource.- Parameters:
inputStream
- The input stream to the resourcecsvPreference
- The CSV preference (separators)charset
- The charset of the resource- Returns:
- the list of headers in file
- Throws:
IOException
- if an error occurs
-
getMapping
Get the mapping from the headers and corresponding to the content type.- Parameters:
contentType
- The content typeheaders
- The CSV headers- Returns:
- the list of columns descriptions. An item of the list is one column, each column is defined by a header "header" (text defined into the CSV header), an
attribute path "attributePath" (real path extracted from the header, empty if the model item does not exist) and an identifier flag "isId" (
true
if the header ends with a star "*").
-