fr.paris.lutece.portal.service.csv
Class CSVReaderService

java.lang.Object
  extended by fr.paris.lutece.portal.service.csv.CSVReaderService
Direct Known Subclasses:
ImportAdminUserService

public abstract class CSVReaderService
extends Object

Service to get data from a CSV file. The CSV might be a physical file, or a memory file. Implementations can either be statefull or stateless, but if the separator or the escape character are controlled by the user, then it has to be statefull.


Constructor Summary
CSVReaderService()
           
 
Method Summary
protected  List<CSVMessageDescriptor> checkCSVFileValidity(List<String[]> listLines, int nColumnNumber, boolean bSkipFirstLine, Locale locale)
          Check the validity of the whole CSV file.
protected  List<CSVMessageDescriptor> checkCSVLineColumnNumber(String[] strLine, int nColumnNumber, int nLineNumber, Locale locale)
          Check the number of columns of a line.
protected abstract  List<CSVMessageDescriptor> checkLineOfCSVFile(String[] strLineDataArray, int nLineNumber, Locale locale)
          Check the line of the CSV file.
 Character getCSVEscapeCharacter()
          Get the escape character used for CSV files.
 Character getCSVSeparator()
          Get the separator used for CSV files.
static Character getDefaultCSVEscapeCharacter()
          Get the default CSV escape character to use.
static Character getDefaultCSVSeparator()
          Get the default CSV separator to use.
protected abstract  List<CSVMessageDescriptor> getEndOfProcessMessages(int nNbLineParses, int nNbLinesWithoutErrors, Locale locale)
          Get messages after the process is completed.
 List<CSVMessageDescriptor> readCSVFile(File file, int nColumnNumber, boolean bCheckFileBeforeProcessing, boolean bExitOnError, boolean bSkipFirstLine, Locale locale, String strBaseUrl)
          Read a CSV file and call the method readLineOfCSVFile for each of its lines.
 List<CSVMessageDescriptor> readCSVFile(org.apache.commons.fileupload.FileItem fileItem, int nColumnNumber, boolean bCheckFileBeforeProcessing, boolean bExitOnError, boolean bSkipFirstLine, Locale locale, String strBaseUrl)
          Read a CSV file and call the method readLineOfCSVFile for each of its lines.
 List<CSVMessageDescriptor> readCSVFile(PhysicalFile physicalFile, int nColumnNumber, boolean bCheckFileBeforeProcessing, boolean bExitOnError, boolean bSkipFirstLine, Locale locale, String strBaseUrl)
          Read a CSV file and call the method readLineOfCSVFile for each of its lines.
protected  List<CSVMessageDescriptor> readCSVFile(Reader reader, au.com.bytecode.opencsv.CSVReader csvReader, int nColumnNumber, boolean bCheckFileBeforeProcessing, boolean bExitOnError, boolean bSkipFirstLine, Locale locale, String strBaseUrl)
          Read a CSV file and call the method readLineOfCSVFile for each of its lines.
 List<CSVMessageDescriptor> readCSVFile(String strPath, int nColumnNumber, boolean bCheckFileBeforeProcessing, boolean bExitOnError, boolean bSkipFirstLine, Locale locale, String strBaseUrl)
          Read a CSV file and call the method readLineOfCSVFile for each of its lines.
protected abstract  List<CSVMessageDescriptor> readLineOfCSVFile(String[] strLineDataArray, int nLineNumber, Locale locale, String strBaseUrl)
          Read a line of the CSV file.
 void setCSVEscapeCharacter(Character strCSVEscapeCharacter)
          Set the escape character to use for CSV files.
 void setCSVSeparator(Character strCSVSeparator)
          Set the separator to use for CSV files.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVReaderService

public CSVReaderService()
Method Detail

readLineOfCSVFile

protected abstract List<CSVMessageDescriptor> readLineOfCSVFile(String[] strLineDataArray,
                                                                int nLineNumber,
                                                                Locale locale,
                                                                String strBaseUrl)
Read a line of the CSV file.

Parameters:
strLineDataArray - The content of the line of the CSV file.
nLineNumber - Number of the current line
locale - the locale
strBaseUrl - The base URL
Returns:
Returns the list of messages associated with the line.

checkLineOfCSVFile

protected abstract List<CSVMessageDescriptor> checkLineOfCSVFile(String[] strLineDataArray,
                                                                 int nLineNumber,
                                                                 Locale locale)
Check the line of the CSV file. This method is called once on each line of the file if the number of columns is correct. If the file is entirely checked before processing, then this method is called before any line is processed. Otherwise it is called just before the processing of the line.

Parameters:
strLineDataArray - The content of the line of the CSV file.
nLineNumber - Number of the current line
locale - the locale
Returns:
The list of messages of the lines. Lines that contain messages with messages levels other than INFO will NOT be processed, and the global processing may stop if the ExitOnError flag has been set to true !

getEndOfProcessMessages

protected abstract List<CSVMessageDescriptor> getEndOfProcessMessages(int nNbLineParses,
                                                                      int nNbLinesWithoutErrors,
                                                                      Locale locale)
Get messages after the process is completed.

Parameters:
nNbLineParses - The number of lines parses. If the first line was skipped, it is not counted.
nNbLinesWithoutErrors - the number of lines parses whitout error.
locale - The locale
Returns:
A list of messages.

getDefaultCSVSeparator

public static Character getDefaultCSVSeparator()
Get the default CSV separator to use. If the property of the default separator to use is not set, then the semi-colon is returned.

Returns:
the default CSV separator to use

getDefaultCSVEscapeCharacter

public static Character getDefaultCSVEscapeCharacter()
Get the default CSV escape character to use. If the property of the default escape character to use is not set, then the comma is returned.

Returns:
the default CSV escape character to use

readCSVFile

public List<CSVMessageDescriptor> readCSVFile(org.apache.commons.fileupload.FileItem fileItem,
                                              int nColumnNumber,
                                              boolean bCheckFileBeforeProcessing,
                                              boolean bExitOnError,
                                              boolean bSkipFirstLine,
                                              Locale locale,
                                              String strBaseUrl)
Read a CSV file and call the method readLineOfCSVFile for each of its lines.

Parameters:
fileItem - FileItem to get the CSV file from. If the creation of the input stream associated to this file throws a IOException, then an error is returned and the file is not red.
nColumnNumber - Number of columns of each lines. Use 0 to skip column number check (for example if every lines don't have the same number of columns)
bCheckFileBeforeProcessing - Indicates if the file should be check before processing any of its line. If it is set to true, then then no line is processed if the file has any error.
bExitOnError - Indicates if the processing of the CSV file should end on the first error, or at the end of the file.
bSkipFirstLine - Indicates if the first line of the file should be skipped or not.
locale - the locale
strBaseUrl - The base URL
Returns:
Returns the list of errors that occurred during the processing of the file. The returned list is sorted
See Also:
CSVMessageDescriptor.compareTo(CSVMessageDescriptor) for information about sort

readCSVFile

public List<CSVMessageDescriptor> readCSVFile(String strPath,
                                              int nColumnNumber,
                                              boolean bCheckFileBeforeProcessing,
                                              boolean bExitOnError,
                                              boolean bSkipFirstLine,
                                              Locale locale,
                                              String strBaseUrl)
Read a CSV file and call the method readLineOfCSVFile for each of its lines.

Parameters:
strPath - Path if the file to read in the file system.
nColumnNumber - Number of columns of each lines. Use 0 to skip column number check (for example if every lines don't have the same number of columns)
bCheckFileBeforeProcessing - Indicates if the file should be check before processing any of its line. If it is set to true, then then no line is processed if the file has any error.
bExitOnError - Indicates if the processing of the CSV file should end on the first error, or at the end of the file.
bSkipFirstLine - Indicates if the first line of the file should be skipped or not.
locale - the locale
strBaseUrl - The base URL
Returns:
Returns the list of errors that occurred during the processing of the file. The returned list is sorted
See Also:
CSVMessageDescriptor.compareTo(CSVMessageDescriptor) for information about sort

readCSVFile

public List<CSVMessageDescriptor> readCSVFile(File file,
                                              int nColumnNumber,
                                              boolean bCheckFileBeforeProcessing,
                                              boolean bExitOnError,
                                              boolean bSkipFirstLine,
                                              Locale locale,
                                              String strBaseUrl)
Read a CSV file and call the method readLineOfCSVFile for each of its lines.

Parameters:
file - File to get the values from. If the physical file of this file has no value, then it is gotten from the database.
nColumnNumber - Number of columns of each lines. Use 0 to skip column number check (for example if every lines don't have the same number of columns)
bCheckFileBeforeProcessing - Indicates if the file should be check before processing any of its line. If it is set to true, then then no line is processed if the file has any error.
bExitOnError - Indicates if the processing of the CSV file should end on the first error, or at the end of the file.
bSkipFirstLine - Indicates if the first line of the file should be skipped or not.
locale - the locale
strBaseUrl - The base URL
Returns:
Returns the list of errors that occurred during the processing of the file. The returned list is sorted
See Also:
CSVMessageDescriptor.compareTo(CSVMessageDescriptor) for information about sort

readCSVFile

public List<CSVMessageDescriptor> readCSVFile(PhysicalFile physicalFile,
                                              int nColumnNumber,
                                              boolean bCheckFileBeforeProcessing,
                                              boolean bExitOnError,
                                              boolean bSkipFirstLine,
                                              Locale locale,
                                              String strBaseUrl)
Read a CSV file and call the method readLineOfCSVFile for each of its lines.

Parameters:
physicalFile - The physicalFile to get the values from. If the physical file has no value, then it is gotten from the database.
nColumnNumber - Number of columns of each lines. Use 0 to skip column number check (for example if every lines don't have the same number of columns)
bCheckFileBeforeProcessing - Indicates if the file should be check before processing any of its line. If it is set to true, then then no line is processed if the file has any error.
bExitOnError - Indicates if the processing of the CSV file should end on the first error, or at the end of the file.
bSkipFirstLine - Indicates if the first line of the file should be skipped or not.
locale - the locale
strBaseUrl - The base URL
Returns:
Returns the list of errors that occurred during the processing of the file. The returned list is sorted
See Also:
CSVMessageDescriptor.compareTo(CSVMessageDescriptor) for information about sort

readCSVFile

protected List<CSVMessageDescriptor> readCSVFile(Reader reader,
                                                 au.com.bytecode.opencsv.CSVReader csvReader,
                                                 int nColumnNumber,
                                                 boolean bCheckFileBeforeProcessing,
                                                 boolean bExitOnError,
                                                 boolean bSkipFirstLine,
                                                 Locale locale,
                                                 String strBaseUrl)
Read a CSV file and call the method readLineOfCSVFile for each of its lines.

Parameters:
reader - The file reader that was used to create the CSV reader. This reader will be closed by this method
csvReader - CSV reader to use to read the CSV file
nColumnNumber - Number of columns of each lines. Use 0 to skip column number check (for example if every lines don't have the same number of columns)
bCheckFileBeforeProcessing - Indicates if the file should be check before processing any of its line. If it is set to true, then then no line is processed if the file has any error.
bExitOnError - Indicates if the processing of the CSV file should end on the first error, or at the end of the file.
bSkipFirstLine - Indicates if the first line of the file should be skipped or not.
locale - the locale
strBaseUrl - The base URL
Returns:
Returns the list of errors that occurred during the processing of the file. The returned list is sorted
See Also:
CSVMessageDescriptor.compareTo(CSVMessageDescriptor) for information about sort

checkCSVFileValidity

protected List<CSVMessageDescriptor> checkCSVFileValidity(List<String[]> listLines,
                                                          int nColumnNumber,
                                                          boolean bSkipFirstLine,
                                                          Locale locale)
Check the validity of the whole CSV file.

Parameters:
listLines - The list of lines of the file
nColumnNumber - The number of columns every line must have.
bSkipFirstLine - True if the first line should be ignored, false otherwise
locale - The locale
Returns:
Returns a list of errors found in the file.

checkCSVLineColumnNumber

protected List<CSVMessageDescriptor> checkCSVLineColumnNumber(String[] strLine,
                                                              int nColumnNumber,
                                                              int nLineNumber,
                                                              Locale locale)
Check the number of columns of a line.

Parameters:
strLine - The line to check
nColumnNumber - The number of columns the line must have
nLineNumber - The number of the current line
locale - The locale
Returns:
The error if an error is found, or null if there is none.

getCSVSeparator

public Character getCSVSeparator()
Get the separator used for CSV files. If no separator has been set, then the default CSV separator is used.

Returns:
the separator used for CSV files, of the default one if non has been set.

setCSVSeparator

public void setCSVSeparator(Character strCSVSeparator)
Set the separator to use for CSV files.

Parameters:
strCSVSeparator - The separator to use for CSV files.

getCSVEscapeCharacter

public Character getCSVEscapeCharacter()
Get the escape character used for CSV files. If no escape character has been set, then the default CSV escape character is used.

Returns:
the escape character used for CSV files, of the default one if non has been set.

setCSVEscapeCharacter

public void setCSVEscapeCharacter(Character strCSVEscapeCharacter)
Set the escape character to use for CSV files.

Parameters:
strCSVEscapeCharacter - The escape character to use for CSV files.


Copyright © 2014 Mairie de Paris. All Rights Reserved.