net.sourceforge.jdbcimporter
Class EntityImporter

java.lang.Object
  extended bynet.sourceforge.jdbcimporter.EntityImporter

public class EntityImporter
extends java.lang.Object

The EntityImporter class imports an entity into the database using a given import engine. It ensures that all the columns for the entity have their SQL Types defined properly. The data is parsed using the DelimiterParser defined by the EntityDef. Each column value may be transformed by a ColumnTranslator before the current row is passed to the ImportEngine.

Version:
0.6
Author:
Chris Nagy

Field Summary
protected  int batchCount
          The number of rows to import before calling executeBatch.
protected  int commitCount
          The number of rows to import before calling commit on the connection.
protected  ImportEngine defaultEngine
          The import engine that will be used.
protected  boolean failOnError
          Flag indicating that the import should stop on the first error (default is false).
protected  java.util.List importListeners
          The list of import listeners.
protected static org.apache.commons.logging.Log LOG
          The log for debug information.
protected  java.util.Random random
          Random number generator used for columns that will be generated.
protected  boolean supportsBatch
          The flag indicating the JDBC driver supports batch updates.
protected  boolean trimValues
          The flag indicating that values should be trimmed after being read by the delimiter parser.
 
Constructor Summary
EntityImporter()
           
 
Method Summary
 void addImportListener(ImportListener listener)
          Adds an import listener.
protected  java.lang.String columnValuesToString(ColumnValue[] values)
          Convert the column values into a string representation for logging purposes.
protected  void fillInColumnTypes(ImportEntityDef entityDef)
          Fill in the column types.
protected  ColumnValue[] fillInValues(net.sourceforge.jdbcimporter.EntityImporterState state, ColumnValue[] values)
          Fill in the column values with default values, if necessary, and call column translators for the values.
protected  void fireImportEntityBatchEvent(ImportEntityBatchEvent e)
          Fire an import entity batch event to the listeners.
protected  void fireImportEntityRowEvent(ImportEntityRowEvent e)
          Fire an import entity row event to the listeners.
protected  boolean importNextRow(net.sourceforge.jdbcimporter.EntityImporterState state, java.lang.Object nextRow)
          Import the next row.
protected  int processBatchUpdateException(ImportEntityDef entityDef, java.util.List batchRows, java.sql.BatchUpdateException bue)
          Process batch update exception.
 void processEntity(ImportEntityDef entityDef)
          Process the given entity definition by ensuring that all columns defined have an SQL Type, parsing the file with the appropriate DelimiterParser, applying any ColumnTranslator to each column value and using the ImportEngine to import the row.
 void removeImportListener(ImportListener listener)
          Removes an import listener.
 void setBatchCount(int count)
          Sets the number of records to import before executing a batch update.
 void setCommitCount(int count)
          Sets the number of records to import before committing.
 void setFailonerror(boolean flag)
          Sets a flag to indicate that the import should stop after the first error.
 void setImportEngine(ImportEngine newEngine)
          Sets the import engine.
 void setSupportsBatch(boolean flag)
          Sets a flag to indicate whether the connection supports batch updates.
 void setTrimValues(boolean flag)
          Sets a flag to indicate that values should be trimmed after being read by the delimiter parser
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

protected static org.apache.commons.logging.Log LOG
The log for debug information.


defaultEngine

protected ImportEngine defaultEngine
The import engine that will be used.


batchCount

protected int batchCount
The number of rows to import before calling executeBatch.


commitCount

protected int commitCount
The number of rows to import before calling commit on the connection.


supportsBatch

protected boolean supportsBatch
The flag indicating the JDBC driver supports batch updates.


trimValues

protected boolean trimValues
The flag indicating that values should be trimmed after being read by the delimiter parser.


importListeners

protected java.util.List importListeners
The list of import listeners.


failOnError

protected boolean failOnError
Flag indicating that the import should stop on the first error (default is false).


random

protected java.util.Random random
Random number generator used for columns that will be generated.

Constructor Detail

EntityImporter

public EntityImporter()
Method Detail

setImportEngine

public void setImportEngine(ImportEngine newEngine)
Sets the import engine.

Parameters:
newEngine - import engine

setBatchCount

public void setBatchCount(int count)
Sets the number of records to import before executing a batch update.

Parameters:
count - the number of records

setCommitCount

public void setCommitCount(int count)
Sets the number of records to import before committing.

Parameters:
count - the number of records

setSupportsBatch

public void setSupportsBatch(boolean flag)
Sets a flag to indicate whether the connection supports batch updates.

Parameters:
flag - whether the connection supports batch update or not

setTrimValues

public void setTrimValues(boolean flag)
Sets a flag to indicate that values should be trimmed after being read by the delimiter parser

Parameters:
flag - flag

setFailonerror

public void setFailonerror(boolean flag)
Sets a flag to indicate that the import should stop after the first error.

Parameters:
flag - flag
Since:
0.69

processEntity

public void processEntity(ImportEntityDef entityDef)
                   throws java.sql.SQLException,
                          java.io.IOException
Process the given entity definition by ensuring that all columns defined have an SQL Type, parsing the file with the appropriate DelimiterParser, applying any ColumnTranslator to each column value and using the ImportEngine to import the row.

Parameters:
entityDef - entity definition to import
Throws:
java.sql.SQLException
java.io.IOException

addImportListener

public void addImportListener(ImportListener listener)
Adds an import listener.

Parameters:
listener - the listener

removeImportListener

public void removeImportListener(ImportListener listener)
Removes an import listener.

Parameters:
listener - the listener

fillInColumnTypes

protected void fillInColumnTypes(ImportEntityDef entityDef)
                          throws java.sql.SQLException
Fill in the column types.

Parameters:
entityDef - the entity definition
Throws:
java.sql.SQLException - if the column types could not be read from the database meta

importNextRow

protected boolean importNextRow(net.sourceforge.jdbcimporter.EntityImporterState state,
                                java.lang.Object nextRow)
Import the next row.

Parameters:
state - the entity import state
nextRow - the string representing the row
Returns:
flag indicating that the row was imported successfully

fillInValues

protected ColumnValue[] fillInValues(net.sourceforge.jdbcimporter.EntityImporterState state,
                                     ColumnValue[] values)
                              throws MalformedDataException
Fill in the column values with default values, if necessary, and call column translators for the values.

Parameters:
state - the entity import state
values - the column values
Returns:
the values with default values and column translators applied
Throws:
MalformedDataException - if the number of column values is invalid

processBatchUpdateException

protected int processBatchUpdateException(ImportEntityDef entityDef,
                                          java.util.List batchRows,
                                          java.sql.BatchUpdateException bue)
Process batch update exception.

Parameters:
entityDef - the entity definition
batchRows - the rows processed during this batch
bue - the batch update exception
Returns:
number of bad rows

fireImportEntityRowEvent

protected void fireImportEntityRowEvent(ImportEntityRowEvent e)
Fire an import entity row event to the listeners.

Parameters:
e - the import entity row event

fireImportEntityBatchEvent

protected void fireImportEntityBatchEvent(ImportEntityBatchEvent e)
Fire an import entity batch event to the listeners.

Parameters:
e - the import batch event

columnValuesToString

protected java.lang.String columnValuesToString(ColumnValue[] values)
Convert the column values into a string representation for logging purposes.

Parameters:
values - the column values
Returns:
A comma-separated list of values