Class GeneratorContext

java.lang.Object
org.fastnate.generator.context.GeneratorContext

public class GeneratorContext extends Object
Represents the configuration and state for one or more EntitySqlGenerators.
Author:
Tobias Liefke
  • Field Details

    • log

      private static final org.slf4j.Logger log
    • PROVIDER_KEY

      public static final String PROVIDER_KEY
      The settings key for the JPA provider. Contains the fully qualified class name of an extension of JpaProvider. If none is given, the available from META-INF/services/org.fastnate.generator.provider.JpaProvider is used.
      See Also:
    • PERSISTENCE_FILE_KEY

      public static final String PERSISTENCE_FILE_KEY
      The settings key for the path to the persistence.xml, either relative to the current directory or absolute.
      See Also:
    • PERSISTENCE_UNIT_KEY

      public static final String PERSISTENCE_UNIT_KEY
      The settings key for the name of the persistence unit in the persistence.xml. The first persistence unit is used, if none is explicitly set.
      See Also:
    • DIALECT_KEY

      public static final String DIALECT_KEY
      The settings key for the target SQL dialect.

      Contains either the fully qualified name of a class that extends GeneratorDialect or the simple class name of one of the classes from org.fastnate.generator.dialect. The suffix 'Dialect' may be omitted in that case. For example 'MySql' would map to org.fastnate.generator.dialect.MySqlDialect.

      If no dialect is set explicitly then the configured persistence.xml is scanned for a connection URL or provider specific dialect, which would be converted to our known dialects.

      If nothing is found, H2 is used as default.

      See Also:
    • NULL_VALUES_KEY

      public static final String NULL_VALUES_KEY
      The settings key for writeNullValues.
      See Also:
    • RELATIVE_IDS_KEY

      public static final String RELATIVE_IDS_KEY
      The settings key for writeRelativeIds.
      See Also:
    • QUOTE_ALL_IDENTIFIERS_KEY

      public static final String QUOTE_ALL_IDENTIFIERS_KEY
      The settings key for quoteAllIdentifiers.
      See Also:
    • UNIQUE_PROPERTIES_QUALITY_KEY

      public static final String UNIQUE_PROPERTIES_QUALITY_KEY
      The settings key for the uniquePropertyQuality.
      See Also:
    • UNIQUE_PROPERTIES_MAX_KEY

      public static final String UNIQUE_PROPERTIES_MAX_KEY
      The settings key for the maxUniqueProperties.
      See Also:
    • PREFER_SEQUENCE_CURRENT_VALUE

      public static final String PREFER_SEQUENCE_CURRENT_VALUE
      The settings key for preferSequenceCurentValue.
      See Also:
    • DEFAULT_DIALECTS

      private static final Map<String,Class<? extends GeneratorDialect>> DEFAULT_DIALECTS
    • dialect

      private GeneratorDialect dialect
      Identifies the SQL dialect for generating SQL statements. Encapsulates the database specifica.
    • provider

      private JpaProvider provider
      Identifies the JPA provider to indicate implementation specific details.
    • maxUniqueProperties

      private int maxUniqueProperties
      The maximum count of columns that are used when referencing an entity using it's unique properties.
    • uniquePropertyQuality

      private UniquePropertyQuality uniquePropertyQuality
      Indicates what kind of properties are used for referencing an entity with its unique properties.
    • preferSequenceCurentValue

      private boolean preferSequenceCurentValue
      Indiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking for unique properties.
    • writeRelativeIds

      private boolean writeRelativeIds
      Indicates that we write into a schema that is not empty. By default we write all IDs as absolute values and change the sequences / table generators at the end. But this would crash if there is data in the database already that uses the same IDs. So in the case of incremental updates, one should set this setting to true - which will generate relative IDs which respect the existing IDs.
    • writeNullValues

      private boolean writeNullValues
      Indicates to include null values in statements.
    • quoteAllIdentifiers

      private boolean quoteAllIdentifiers
      Indicates to quote all identifiers. Otherwise only those identifiers are quoted, which are surrounded by '"' or '`'.
    • settings

      private final Properties settings
      Contains the settings that were given during creation, resp. as read from the persistence configuration.
    • descriptions

      private final Map<Class<?>,EntityClass<?>> descriptions
      Contains the extracted metadata to every known class of an Entity.
    • descriptionsByName

      private final Map<String,EntityClass<?>> descriptionsByName
      The mapping from the name of an entity to the extracted metadata.
    • tables

      private final Map<String,GeneratorTable> tables
      Mapping from the names of all known database table to their description (including column information).
    • states

      private final Map<String,Map<Object,GenerationState>> states
      Contains the state of single entities, maps from an entity name to the mapping of an id to its state.
    • generators

      private final Map<GeneratorContext.GeneratorId,IdGenerator> generators
      Mapping from the name of a generator to the generator itself.
    • defaultSequenceGenerators

      private Map<String,SequenceIdGenerator> defaultSequenceGenerators
      The default sequence generator, if none is explicitly specified in a GeneratedValue.
    • defaultTableGenerators

      private Map<String,TableIdGenerator> defaultTableGenerators
      The default table generator, if none is explicitly specified in a GeneratedValue.
    • contextModelListeners

      private List<ContextModelListener> contextModelListeners
      All listeners of this context.
  • Constructor Details

    • GeneratorContext

      public GeneratorContext()
      Creates a default generator context.
    • GeneratorContext

      public GeneratorContext(GeneratorDialect dialect)
      Creates a generator context for a dialect.
      Parameters:
      dialect - the database dialect to use during generation
    • GeneratorContext

      public GeneratorContext(Properties settings)
      Creates a new instance of GeneratorContext.
      Parameters:
      settings - contains the settings
  • Method Details

    • createDefaultProvider

      private static JpaProvider createDefaultProvider()
    • detectDialect

      private static Class<? extends GeneratorDialect> detectDialect(String setting)
    • readPersistenceFile

      private static void readPersistenceFile(Properties settings)
      Tries to read any persistence file defined in the settings.
      Parameters:
      settings - the current settings
    • addContextModelListener

      public void addContextModelListener(ContextModelListener listener)
      Adds a new listener to this context.
      Parameters:
      listener - the listener that is interested in new discovered model elements
    • addContextObject

      private <K, T> T addContextObject(Map<K,? super T> objects, BiConsumer<ContextModelListener,? super T> listenerFunction, K key, T object)
    • adjustIdentifier

      public String adjustIdentifier(String identifier)
      Quotes an object in the current database dialect, if it is marked for quoting with surrounding '"' or '`' in the column definition or if we quote all identifiers.
      Parameters:
      identifier - the name of the object (column, table, ...) according to the annotation
      Returns:
      the quoted object name
    • buildQualifiedName

      public String buildQualifiedName(String catalog, String schema, String objectName)
      Builds the fully qualified name for the given database object.
      Parameters:
      catalog - the optional catalog name
      schema - the optional schema name
      objectName - the name of the database object
      Returns:
      the qualified name, as used by this dialect
    • detectDialect

      private void detectDialect()
    • fireContextObjectAdded

      protected <T> void fireContextObjectAdded(BiConsumer<ContextModelListener,T> listenerFunction, T contextObject)
      Fires an event to all listeners.
      Parameters:
      listenerFunction - the function that is called on the listeners
      contextObject - the object to offer to the listener function
    • getDefaultSequenceGenerator

      private IdGenerator getDefaultSequenceGenerator(GeneratorTable table)
    • getDefaultTableGenerator

      private IdGenerator getDefaultTableGenerator(GeneratorTable table)
    • getDescription

      public <E> EntityClass<E> getDescription(Class<E> entityClass)
      Finds the description for a class.
      Parameters:
      entityClass - the class to lookup
      Returns:
      the description for the class or null if the class is not an Entity
    • getDescription

      public <E> EntityClass<E> getDescription(E entity)
      Finds the description for the class of an entity.
      Parameters:
      entity - the entity to lookup
      Returns:
      the description for the class of the entity
      Throws:
      IllegalArgumentException - if the given object is no Entity
    • getGenerator

      public IdGenerator getGenerator(jakarta.persistence.GeneratedValue generatedValue, GeneratorTable table, GeneratorColumn column)
      Finds the correct generator for the given annotation.
      Parameters:
      generatedValue - the annotation of the current primary key
      table - the name of the current table
      column - the name of the current column
      Returns:
      the generator that is responsible for managing the values
    • getStates

      Map<Object,GenerationState> getStates(EntityClass<?> entityClass)
      The entity states for the given entity class.
      Parameters:
      entityClass - the current entity class
      Returns:
      the states of the entities of that class (with their IDs as keys)
    • registerGenerators

      public void registerGenerators(AnnotatedElement element, GeneratorTable table)
      Registers the TableGenerator and SequenceGenerator declared at the given element. If neither annotation is present, nothing happens.
      Parameters:
      element - the inspected class, method or field
      table - the table of the current entity
    • removeContextModelListener

      public void removeContextModelListener(ContextModelListener listener)
      Removes a listener from this context.
      Parameters:
      listener - the listener that is not interested anymore
    • resolveTable

      public <A extends Annotation> GeneratorTable resolveTable(jakarta.persistence.AssociationOverride override, A annotation, Function<A,String> catalogName, Function<A,String> schemaName, Function<A,String> tableName, String defaultTableName)
      Finds resp. builds the metadata to the given table from the given (optional) annotation.
      Parameters:
      override - contains the overrides for the mapping table
      annotation - the optional annotation that contains any metadata to the table
      catalogName - finds the optional name of the catalog that contains the table
      schemaName - finds the optional name of the schema that contains the table
      tableName - finds the name of the table
      defaultTableName - the name of the talbe, if the annotation is null or contains no value for the table name
      Returns:
      the metadata for the given table
    • resolveTable

      public GeneratorTable resolveTable(String catalogName, String schemaName, String tableName)
      Finds resp. builds the metadata to the given table.
      Parameters:
      catalogName - the optional name of the catalog that contains the table
      schemaName - the optional name of the schema that contains the table
      tableName - the name of the table from the database
      Returns:
      the metadata for the given table
    • writeAlignmentStatements

      public void writeAlignmentStatements(StatementsWriter writer) throws IOException
      Builds all statements that are necessary to align ID generators in the database with the current IDs.
      Parameters:
      writer - the target of any write operation
      Throws:
      IOException - if the writer throws one
    • getDialect

      public GeneratorDialect getDialect()
      Identifies the SQL dialect for generating SQL statements. Encapsulates the database specifica.
    • getProvider

      public JpaProvider getProvider()
      Identifies the JPA provider to indicate implementation specific details.
    • getMaxUniqueProperties

      public int getMaxUniqueProperties()
      The maximum count of columns that are used when referencing an entity using it's unique properties.
    • getUniquePropertyQuality

      public UniquePropertyQuality getUniquePropertyQuality()
      Indicates what kind of properties are used for referencing an entity with its unique properties.
    • isPreferSequenceCurentValue

      public boolean isPreferSequenceCurentValue()
      Indiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking for unique properties.
    • isWriteRelativeIds

      public boolean isWriteRelativeIds()
      Indicates that we write into a schema that is not empty. By default we write all IDs as absolute values and change the sequences / table generators at the end. But this would crash if there is data in the database already that uses the same IDs. So in the case of incremental updates, one should set this setting to true - which will generate relative IDs which respect the existing IDs.
    • isWriteNullValues

      public boolean isWriteNullValues()
      Indicates to include null values in statements.
    • isQuoteAllIdentifiers

      public boolean isQuoteAllIdentifiers()
      Indicates to quote all identifiers. Otherwise only those identifiers are quoted, which are surrounded by '"' or '`'.
    • getSettings

      public Properties getSettings()
      Contains the settings that were given during creation, resp. as read from the persistence configuration.
    • getDescriptions

      public Map<Class<?>,EntityClass<?>> getDescriptions()
      Contains the extracted metadata to every known class of an Entity.
    • getDescriptionsByName

      public Map<String,EntityClass<?>> getDescriptionsByName()
      The mapping from the name of an entity to the extracted metadata.
    • getTables

      public Map<String,GeneratorTable> getTables()
      Mapping from the names of all known database table to their description (including column information).
    • getStates

      public Map<String,Map<Object,GenerationState>> getStates()
      Contains the state of single entities, maps from an entity name to the mapping of an id to its state.
    • getDefaultSequenceGenerators

      public Map<String,SequenceIdGenerator> getDefaultSequenceGenerators()
      The default sequence generator, if none is explicitly specified in a GeneratedValue.
    • getDefaultTableGenerators

      public Map<String,TableIdGenerator> getDefaultTableGenerators()
      The default table generator, if none is explicitly specified in a GeneratedValue.
    • getContextModelListeners

      public List<ContextModelListener> getContextModelListeners()
      All listeners of this context.
    • setDialect

      public void setDialect(GeneratorDialect dialect)
      Identifies the SQL dialect for generating SQL statements. Encapsulates the database specifica.
    • setProvider

      public void setProvider(JpaProvider provider)
      Identifies the JPA provider to indicate implementation specific details.
    • setMaxUniqueProperties

      public void setMaxUniqueProperties(int maxUniqueProperties)
      The maximum count of columns that are used when referencing an entity using it's unique properties.
    • setUniquePropertyQuality

      public void setUniquePropertyQuality(UniquePropertyQuality uniquePropertyQuality)
      Indicates what kind of properties are used for referencing an entity with its unique properties.
    • setPreferSequenceCurentValue

      public void setPreferSequenceCurentValue(boolean preferSequenceCurentValue)
      Indiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking for unique properties.
    • setWriteRelativeIds

      public void setWriteRelativeIds(boolean writeRelativeIds)
      Indicates that we write into a schema that is not empty. By default we write all IDs as absolute values and change the sequences / table generators at the end. But this would crash if there is data in the database already that uses the same IDs. So in the case of incremental updates, one should set this setting to true - which will generate relative IDs which respect the existing IDs.
    • setWriteNullValues

      public void setWriteNullValues(boolean writeNullValues)
      Indicates to include null values in statements.
    • setQuoteAllIdentifiers

      public void setQuoteAllIdentifiers(boolean quoteAllIdentifiers)
      Indicates to quote all identifiers. Otherwise only those identifiers are quoted, which are surrounded by '"' or '`'.
    • setDefaultSequenceGenerators

      public void setDefaultSequenceGenerators(Map<String,SequenceIdGenerator> defaultSequenceGenerators)
      The default sequence generator, if none is explicitly specified in a GeneratedValue.
    • setDefaultTableGenerators

      public void setDefaultTableGenerators(Map<String,TableIdGenerator> defaultTableGenerators)
      The default table generator, if none is explicitly specified in a GeneratedValue.
    • setContextModelListeners

      public void setContextModelListeners(List<ContextModelListener> contextModelListeners)
      All listeners of this context.