Class EntityImporter

java.lang.Object
org.fastnate.data.EntityImporter

public class EntityImporter extends Object
Main class for importing entities. Discovers all implementations of DataProvider and creates one big SQL file for the generated entities.
Author:
Andreas Penski, Tobias Liefke
  • Field Details

    • GENERATION_ABORTED_MESSAGE

      public static final String GENERATION_ABORTED_MESSAGE
      The String in the SQL, if the generation was aborted. Can be used by other utilities that perform further modifications on the generated files.
      See Also:
    • DATA_FOLDER_KEY

      public static final String DATA_FOLDER_KEY
      Settings key for the folder that contains any data to import. May point to a package name, which indicates that the folder is part of the class path. This setting may be overriden by the optional first command line argument of the importer. For importing entities with generic data providers, there has to be an "entities" folder in that folder, which contains the generic data files.
      See Also:
    • OUTPUT_FILE_KEY

      public static final String OUTPUT_FILE_KEY
      Settings key for the generated SQL file.
      See Also:
    • OUTPUT_ENCODING_KEY

      public static final String OUTPUT_ENCODING_KEY
      Settings key for the encoding of the generated SQL file.
      See Also:
    • PREFIX_KEY

      public static final String PREFIX_KEY
      Settings key for a part to write into the output file before the generated content.
      See Also:
    • POSTFIX_KEY

      public static final String POSTFIX_KEY
      Settings key for a part to write into the output file after the generated content.
      See Also:
    • FACTORY_KEY

      public static final String FACTORY_KEY
      Settings key for the fully qualified name of a class that scans and instantiates the DataProvider. Defaults to DefaultDataProviderFactory.
      See Also:
    • PACKAGES_KEY

      public static final String PACKAGES_KEY
      Settings key for the packages to scan (separated by ';', ',', ':' or whitespaces).
      See Also:
    • ENTITY_PACKAGES_KEY

      public static final String ENTITY_PACKAGES_KEY
      Settings key for the packages to scan for entity classes on startup (separated by ';', ',', ':' or whitespaces).
      See Also:
    • STATEMENTS_WRITER_KEY

      public static final String STATEMENTS_WRITER_KEY
      Settings key that indicates the type of the used StatementsWriter. Allowed values of the setting:
      • FileStatementsWriter (default)
      • PostgreSqlBulkWriter
      • ConnectedStatementsWriter
      • LiquibaseStatementsWriter
      • any fully qualified class which has a constructor that accepts a GeneratorContext
      See Also:
  • Constructor Details

    • EntityImporter

      public EntityImporter()
      Creates a new default instance of an EntityImporter.
    • EntityImporter

      public EntityImporter(org.fastnate.generator.context.GeneratorContext context)
      Creates a new instance of an EntityImporter.
      Parameters:
      context - the current generator context
    • EntityImporter

      public EntityImporter(Properties settings)
      Creates a new instance of an EntityImporter.
      Parameters:
      settings - the settings of this importer, the data providers and the SQL generator
  • Method Details

    • main

      public static void main(String[] args) throws IOException
      Starts the entity importer from the command line. Command line arguments:
      1. (optional) the name or path of the generated file - defaults to "data.sql"
      2. (optional) the path / package of the base folder for reading input files (only used by DataProviders) - defaults to the setting of DATA_FOLDER_KEY.
      You can set any other properties for EntityImporter(Properties) or GeneratorContext with system properties.
      Parameters:
      args - the command line arguments (see above)
      Throws:
      IOException - the exception
    • addDataProvider

      public void addDataProvider(DataProvider provider)
      Adds a provider to the list of available providers. The provider will be added after the last provider with the same or a smaller order criteria.
      Parameters:
      provider - the provider
    • addDataProvider

      public void addDataProvider(DataProvider provider, int maximumOrderOfDepenendencies)
      Adds a provider to the list of available providers. The provider will be added after the last provider with the same or a smaller order criteria.
      Parameters:
      provider - the provider
      maximumOrderOfDepenendencies - the maximum ordering of dependencies of the provider
    • findDataProvider

      public <P extends DataProvider> P findDataProvider(Class<P> providerClass)
      Resolves the first provider that is an instance of the given class.
      Parameters:
      providerClass - the provider class
      Returns:
      the provider with that class or null if no such provider exists
    • getSettings

      public Properties getSettings()
      The settings of the context.
      Returns:
      settings used by this importer
    • importData

      public void importData() throws IOException
      Imports the data and creates the SQL. Depending on the setting STATEMENTS_WRITER_KEY, this will either create a file or execute the SQL against a connection.
      Throws:
      IOException - if one of the data importers or the file writer throws one
    • importData

      public void importData(Connection connection) throws IOException, SQLException
      Asks the data providers to generate their entities and writes the SQL to a database connection at the end.
      Parameters:
      connection - the target connection for the SQL statements
      Throws:
      SQLException - if the connection throws an exception
      IOException - if the generator throws an exception
    • importData

      public void importData(org.fastnate.generator.EntitySqlGenerator generator) throws IOException
      Asks the data providers to generate their entities and writes the SQL afterwards.
      Parameters:
      generator - the current entity SQL generator
      Throws:
      IOException - if the generator throws an exception
    • importData

      public void importData(File targetFile) throws IOException
      Imports the data and creates the given SQL file.
      Parameters:
      targetFile - the SQL file to generate
      Throws:
      IOException - if one of the data importers or the file writer throws one
    • importData

      public void importData(Writer writer) throws IOException
      Asks the data providers to generate their entities and writes the SQL to a file at the end.
      Parameters:
      writer - the target writer for the SQL statements
      Throws:
      IOException - if the generator or writer throws an exception
    • getDataFolder

      public DataFolder getDataFolder()
    • getContext

      public org.fastnate.generator.context.GeneratorContext getContext()
    • getDataProviders

      public List<DataProvider> getDataProviders()