org.nuiton.util.csv
Class Import<E>

java.lang.Object
  extended by org.nuiton.util.csv.Import<E>
All Implemented Interfaces:
Closeable, Iterable<E>
Direct Known Subclasses:
ImportToMap

public class Import<E>
extends Object
implements Iterable<E>, Closeable

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:
bleny , tchemit , fdesbois

Field Summary
protected  ImportModel<E> model
          Csv import model.
protected  com.csvreader.CsvReader reader
          Csv reader (this is the input).
protected  boolean validate
          A flag to know if model was already validated.
 
Constructor Summary
protected Import(ImportModel<E> model, InputStream inputStream)
           
protected Import(ImportModel<E> model, Reader reader)
           
 
Method Summary
protected  void checkAllMandatoryHeadersArePresent(String[] headers)
           
protected  void checkHeaderNamesAreAllKnown(String[] headers)
           
protected  void checkUniqueModelColumnNames()
           
 void close()
           
protected  List<ImportableColumn<E,?>> getAllMandatoryHeaders()
           
protected  String[] getHeaders()
           
protected  List<ImportableColumn<E,Object>> getNonIgnoredHeaders()
           
 Iterator<E> iterator()
          Define iterator over import.
static
<E> Import<E>
newImport(ImportModel<E> model, InputStream inputStream)
           
static
<E> Import<E>
newImport(ImportModel<E> model, Reader reader)
           
protected
<T> T
parseValue(ImportableColumn<E,T> field, int lineNumber, String value)
           
 void prepareAndValidate()
          Read the first mandatory headers line and validate it with the input model.
protected  boolean readRow()
          Read the next row from the reader and return true if line was successfully read.
protected
<T> String
readValue(ImportableColumn<E,T> field, int lineNumber)
           
protected
<T> void
setValue(ImportableColumn<E,T> field, int lineNumber, E element, T parsedValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

model

protected ImportModel<E> model
Csv import model.


reader

protected com.csvreader.CsvReader reader
Csv reader (this is the input).


validate

protected boolean validate
A flag to know if model was already validated.

Save once done to prevent multiple headers read leading to consider first lines as headers.

Constructor Detail

Import

protected Import(ImportModel<E> model,
                 InputStream inputStream)

Import

protected Import(ImportModel<E> model,
                 Reader reader)
Method Detail

newImport

public static <E> Import<E> newImport(ImportModel<E> model,
                                      InputStream inputStream)

newImport

public static <E> Import<E> newImport(ImportModel<E> model,
                                      Reader reader)

iterator

public Iterator<E> 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.

Specified by:
iterator in interface Iterable<E>
Returns:
the Iterator used for csv iteration
See Also:
prepareAndValidate()

close

public void close()
Specified by:
close in interface Closeable

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 with ImportModel.pushCsvHeaderNames(List) call.

Since:
2.4.1

readValue

protected <T> String readValue(ImportableColumn<E,T> field,
                               int lineNumber)

parseValue

protected <T> T parseValue(ImportableColumn<E,T> field,
                           int lineNumber,
                           String value)

setValue

protected <T> void setValue(ImportableColumn<E,T> field,
                            int lineNumber,
                            E element,
                            T parsedValue)

checkHeaderNamesAreAllKnown

protected void checkHeaderNamesAreAllKnown(String[] headers)

checkUniqueModelColumnNames

protected void checkUniqueModelColumnNames()

checkAllMandatoryHeadersArePresent

protected void checkAllMandatoryHeadersArePresent(String[] headers)

getHeaders

protected String[] getHeaders()
                       throws ImportRuntimeException
Throws:
ImportRuntimeException

getNonIgnoredHeaders

protected List<ImportableColumn<E,Object>> getNonIgnoredHeaders()

getAllMandatoryHeaders

protected List<ImportableColumn<E,?>> getAllMandatoryHeaders()

readRow

protected boolean readRow()
                   throws ImportRuntimeException
Read the next row from the reader and return true if line was successfully read.

Returns:
true if line was successfully read, says in fact there is something after this line.
Throws:
ImportRuntimeException - if could not read line


Copyright © 2011-2012 CodeLutin. All Rights Reserved.