Class Import<E>
java.lang.Object
org.nuiton.csv.Import<E>
- All Implemented Interfaces:
Closeable, AutoCloseable, Iterable<E>
- Direct Known Subclasses:
ImportToMap
Import engine for a given import model.
It acts as an Iterable, you can use directly inside a foreach.
The method prepareAndValidate() will be invoked before all and
only once. It mainly obtain header from the csv input, pass it to the model
and then validate the model.
- Since:
- 2.4
- Author:
- Brendan Le Ny - leny@codelutin.com, Tony Chemit - chemit@codelutin.com, Florian Desbois
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ImportModel<E> Csv import model.protected com.csvreader.CsvReaderCsv reader (this is the input).protected booleanA flag to know if model was already validated. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedImport(ImportModel<E> model, InputStream inputStream) protectedImport(ImportModel<E> model, InputStream inputStream, boolean safetySwitch) protectedImport(ImportModel<E> model, Reader reader) protectedImport(ImportModel<E> model, Reader reader, boolean safetySwitch) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckAllMandatoryHeadersArePresent(String... headers) protected voidcheckHeaderNamesAreAllKnown(String... headers) protected voidvoidclose()protected List<ImportableColumn<E, ?>> protected String[]protected List<ImportableColumn<E, Object>> iterator()Define iterator over import.static <E> Import<E> newImport(ImportModel<E> model, InputStream inputStream) static <E> Import<E> newImport(ImportModel<E> model, InputStream inputStream, boolean safetySwitch) static <E> Import<E> newImport(ImportModel<E> model, Reader reader) static <E> Import<E> newImport(ImportModel<E> model, Reader reader, boolean safetySwitch) protected <T> TparseValue(ImportableColumn<E, T> field, int lineNumber, String value) voidRead the first mandatory headers line and validate it with the input model.protected booleanreadRow()Read the next row from the reader and returntrueif line was successfully read.protected <T> StringreadValue(ImportableColumn<E, T> field, int lineNumber) protected <T> voidsetValue(ImportableColumn<E, T> field, int lineNumber, E element, T parsedValue) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
model
Csv import model. -
reader
protected com.csvreader.CsvReader readerCsv reader (this is the input). -
validate
protected boolean validateA flag to know if model was already validated.Save once done to prevent multiple headers read leading to consider first lines as headers.
-
-
Constructor Details
-
Import
-
Import
-
Import
-
Import
-
-
Method Details
-
newImport
-
newImport
-
newImport
public static <E> Import<E> newImport(ImportModel<E> model, InputStream inputStream, boolean safetySwitch) -
newImport
-
iterator
Define iterator over import. First of all, the input stream will be validated based on defined model. Iteration will be done on all csv rows except first headers line. -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
prepareAndValidate
public void prepareAndValidate()Read the first mandatory headers line and validate it with the input model. This will check if headers are unique, known by the model and if mandatory headers exist in the file. During this phase, the model will retrieve headers value withImportModel.pushCsvHeaderNames(List)call.- Since:
- 2.4.1
-
readValue
-
parseValue
-
setValue
-
checkHeaderNamesAreAllKnown
-
checkUniqueModelColumnNames
protected void checkUniqueModelColumnNames() -
checkAllMandatoryHeadersArePresent
-
getHeaders
- Throws:
ImportRuntimeException
-
getNonIgnoredHeaders
-
getAllMandatoryHeaders
-
readRow
Read the next row from the reader and returntrueif line was successfully read.- Returns:
trueif line was successfully read, says in fact there is something after this line.- Throws:
ImportRuntimeException- if could not read line
-