T - public class DefaultCsvCallback<T> extends Object implements CsvCallback<T>
This handles transaction creation (if no current transaction existed) and transaction commit or rollback on error.
For customising the processing you can extend this object and override the appropriate methods.
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
createdTransaction
Flag set when we created the transaction.
|
protected long |
exeTime
The execution time of the process.
|
protected int |
logInfoFrequency
Used to log a message to indicate progress through large files.
|
protected int |
persistBatchSize
The batch size used when saving the beans.
|
protected EbeanServer |
server
The EbeanServer used to save the beans.
|
protected long |
startTime
The time the process started.
|
protected Transaction |
transaction
The transaction to use (if not using CsvCallback).
|
| Constructor and Description |
|---|
DefaultCsvCallback()
Construct with a default batch size of 30 and logging info messages every
1000 rows.
|
DefaultCsvCallback(int persistBatchSize,
int logInfoFrequency)
Construct with explicit batch size and logging info frequency.
|
| Modifier and Type | Method and Description |
|---|---|
void |
begin(EbeanServer server)
Create a transaction if required.
|
protected void |
commitTransactionIfCreated()
If we created a transaction commit it.
|
void |
end(int row)
Commit the transaction if one was created.
|
void |
endWithError(int row,
Exception e)
Rollback the transaction if one was created.
|
protected void |
initTransactionIfRequired()
Create a transaction if one is not already active and set its batch mode
and batch size.
|
void |
processBean(int row,
String[] line,
T bean)
Will save the bean.
|
boolean |
processLine(int row,
String[] line)
Validate that the content is valid and return false if the row should be
ignored.
|
void |
readHeader(String[] line)
Override to read the heading line.
|
protected void |
rollbackTransactionIfCreated(Throwable e)
Rollback the transaction if we where not successful in processing all the
rows.
|
protected Transaction transaction
protected boolean createdTransaction
protected EbeanServer server
protected final int logInfoFrequency
protected final int persistBatchSize
protected long startTime
protected long exeTime
public DefaultCsvCallback()
public DefaultCsvCallback(int persistBatchSize, int logInfoFrequency)
public void begin(EbeanServer server)
begin in interface CsvCallback<T>public void readHeader(String[] line)
This is only called if CsvReader.setHasHeader(boolean,boolean) is
set to true.
By default this does nothing (effectively ignoring the heading).
readHeader in interface CsvCallback<T>line - the header line content.public boolean processLine(int row, String[] line)
By default this just returns true.
Override this to add custom validation logic returning false if you want the row to be ignored. For example, if all the content is empty return false to ignore the row (rather than having the processing fail with some error).
processLine in interface CsvCallback<T>public void processBean(int row, String[] line, T bean)
Override this method to customise the bean (set additional properties etc) or to control the saving of other related beans (when you can't/don't want to use Cascade.PERSIST etc).
processBean in interface CsvCallback<T>row - the index of the content being processedline - the content that has been used to load the beanbean - the entity bean after it has been loaded from the csv contentpublic void end(int row)
end in interface CsvCallback<T>public void endWithError(int row, Exception e)
endWithError in interface CsvCallback<T>row - the row that the error has occured one - the error that occuredprotected void initTransactionIfRequired()
protected void commitTransactionIfCreated()
protected void rollbackTransactionIfCreated(Throwable e)
Copyright © 2016. All rights reserved.