001package io.ebean.text.csv;
002
003import io.ebean.Database;
004
005/**
006 * Provides callback methods for customisation of CSV processing.
007 * <p>
008 * You can provide your own CsvCallback implementation to customise the CSV
009 * processing. It is expected that the DefaultCsvCallback provides a good base
010 * class that you can extend.
011 */
012public interface CsvCallback<T> {
013
014  /**
015   * The processing is about to begin.
016   * <p>
017   * Typically the callback will create a transaction, set batch mode, batch
018   * size etc.
019   * </p>
020   */
021  void begin(Database database);
022
023  /**
024   * Read the header row.
025   * <p>
026   * This is only called if {@link CsvReader#setHasHeader(boolean, boolean)} has
027   * been set to true.
028   * </p>
029   *
030   * @param line the header line content.
031   */
032  void readHeader(String[] line);
033
034  /**
035   * Check that the row should be processed - return true to process the row or
036   * false to ignore the row. Gives ability to handle bad data... empty rows etc
037   * and ignore it rather than fail.
038   */
039  boolean processLine(int row, String[] line);
040
041  /**
042   * Called for each bean after it has been loaded from the CSV content.
043   * <p>
044   * This allows you to process the bean however you like.
045   * </p>
046   * <p>
047   * When you use a CsvCallback the CsvReader *WILL NOT* create a transaction
048   * and will not save the bean for you. You have complete control and must do
049   * these things yourself (if that is want you want).
050   * </p>
051   *
052   * @param row  the index of the content being processed
053   * @param line the content that has been used to load the bean
054   * @param bean the entity bean after it has been loaded from the csv content
055   */
056  void processBean(int row, String[] line, T bean);
057
058  /**
059   * The processing has ended successfully.
060   * <p>
061   * Typically the callback will commit the transaction.
062   * </p>
063   */
064  void end(int row);
065
066  /**
067   * The processing has ended due to an error.
068   * <p>
069   * This gives the callback the opportunity to rollback the transaction if one
070   * was created.
071   * </p>
072   *
073   * @param row the row that the error has occurred on
074   * @param e   the error that occurred
075   */
076  void endWithError(int row, Exception e);
077
078}