Class DefaultCsvCallback<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    CsvCallback<T>

    public class DefaultCsvCallback<T>
    extends Object
    implements CsvCallback<T>
    Provides the default implementation of CsvCallback.

    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.

    • Field Detail

      • transaction

        protected Transaction transaction
        The transaction to use (if not using CsvCallback).
      • createdTransaction

        protected boolean createdTransaction
        Flag set when we created the transaction.
      • server

        protected Database server
        The EbeanServer used to save the beans.
      • logInfoFrequency

        protected final int logInfoFrequency
        Used to log a message to indicate progress through large files.
      • persistBatchSize

        protected final int persistBatchSize
        The batch size used when saving the beans.
      • getGeneratedKeys

        protected boolean getGeneratedKeys
      • startTime

        protected long startTime
        The time the process started.
      • exeTime

        protected long exeTime
        The execution time of the process.
    • Constructor Detail

      • DefaultCsvCallback

        public DefaultCsvCallback()
        Construct with a default batch size of 30 and logging info messages every 1000 rows.
      • DefaultCsvCallback

        public DefaultCsvCallback​(int persistBatchSize,
                                  int logInfoFrequency)
        Construct with explicit batch size and logging info frequency.
    • Method Detail

      • begin

        public void begin​(Database server)
        Create a transaction if required.
        Specified by:
        begin in interface CsvCallback<T>
      • processLine

        public boolean processLine​(int row,
                                   String[] line)
        Validate that the content is valid and return false if the row should be ignored.

        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).

        Specified by:
        processLine in interface CsvCallback<T>
      • processBean

        public void processBean​(int row,
                                String[] line,
                                T bean)
        Will save the 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).

        Specified by:
        processBean in interface CsvCallback<T>
        Parameters:
        row - the index of the content being processed
        line - the content that has been used to load the bean
        bean - the entity bean after it has been loaded from the csv content
      • end

        public void end​(int row)
        Commit the transaction if one was created.
        Specified by:
        end in interface CsvCallback<T>
      • endWithError

        public void endWithError​(int row,
                                 Exception e)
        Rollback the transaction if one was created.
        Specified by:
        endWithError in interface CsvCallback<T>
        Parameters:
        row - the row that the error has occurred on
        e - the error that occurred
      • initTransactionIfRequired

        protected void initTransactionIfRequired()
        Create a transaction if one is not already active and set its batch mode and batch size.
      • commitTransactionIfCreated

        protected void commitTransactionIfCreated()
        If we created a transaction commit it. We have successfully processed all the rows.
      • rollbackTransactionIfCreated

        protected void rollbackTransactionIfCreated​(Throwable e)
        Rollback the transaction if we where not successful in processing all the rows.