Class ModelImpl

java.lang.Object
org.tentackle.model.impl.ModelImpl
All Implemented Interfaces:
Model

@Service(Model.class) public class ModelImpl extends Object implements Model
Model implementation.
Author:
harald
  • Constructor Details

    • ModelImpl

      public ModelImpl()
      Creates a model.
  • Method Details

    • setSchemaNameMapped

      public void setSchemaNameMapped(boolean mapSchemas)
      Description copied from interface: Model
      Sets whether schema names should be mapped to the leading part of tablenames.
      Specified by:
      setSchemaNameMapped in interface Model
      Parameters:
      mapSchemas - true if map
    • isSchemaNameMapped

      public boolean isSchemaNameMapped()
      Description copied from interface: Model
      Returns whether schema names are mapped.
      Specified by:
      isSchemaNameMapped in interface Model
      Returns:
      true if mapped
    • getEntityFactory

      public EntityFactory getEntityFactory()
      Description copied from interface: Model
      Gets the entity factory singleton.
      Specified by:
      getEntityFactory in interface Model
      Returns:
      the entity model factory
    • loadModel

      public void loadModel(String modelDir, ModelDefaults defaults, EntityAliases aliases) throws ModelException
      Description copied from interface: Model
      Loads the whole model from the model directory if not yet done.
      Specified by:
      loadModel in interface Model
      Parameters:
      modelDir - the directory containing the model files
      defaults - optional model defaults, null if none
      aliases - optional entity aliases, null if none
      Throws:
      ModelException - if the model is inconsistent
    • clearModel

      public void clearModel()
      Description copied from interface: Model
      Clears the model.
      Specified by:
      clearModel in interface Model
    • refreshModel

      public void refreshModel() throws ModelException
      Description copied from interface: Model
      Refreshes the model.
      Checks for changes and reloads if necessary.
      Specified by:
      refreshModel in interface Model
      Throws:
      ModelException - if the model is inconsistent
    • getAllEntitites

      public Collection<Entity> getAllEntitites() throws ModelException
      Description copied from interface: Model
      Gets all cached entities.
      Specified by:
      getAllEntitites in interface Model
      Returns:
      all entities loaded so far
      Throws:
      ModelException - if the model is inconsistent
    • getByFilePath

      public Entity getByFilePath(String filePath) throws ModelException
      Description copied from interface: Model
      Gets the entity by the mapfile path.
      Specified by:
      getByFilePath in interface Model
      Parameters:
      filePath - the pathname of the mapfile
      Returns:
      the entity, null if no such entity
      Throws:
      ModelException - if the model is inconsistent
    • getByEntityName

      public Entity getByEntityName(String entityName) throws ModelException
      Description copied from interface: Model
      Gets the entity by its name.
      Specified by:
      getByEntityName in interface Model
      Parameters:
      entityName - the name
      Returns:
      the entity, null if no such entity
      Throws:
      ModelException - if the model is inconsistent
    • getByTableName

      public Entity getByTableName(String tableName) throws ModelException
      Description copied from interface: Model
      Gets the entity by its table name.
      Specified by:
      getByTableName in interface Model
      Parameters:
      tableName - the table name (optional leading schema will be ignored)
      Returns:
      the entity, null if no such entity
      Throws:
      ModelException - if the model is inconsistent
    • getByClassId

      public Entity getByClassId(int classId) throws ModelException
      Description copied from interface: Model
      Gets the entity by its class id.
      Specified by:
      getByClassId in interface Model
      Parameters:
      classId - the class id
      Returns:
      the entity, null if no such entity
      Throws:
      ModelException - if the model is inconsistent
    • loadByFilePath

      public Entity loadByFilePath(String filePath, ModelDefaults defaults, EntityAliases aliases) throws ModelException
      Description copied from interface: Model
      Loads an entity for a given mapfile name.
      Entities are cached so they are loaded and parsed only once.
      Specified by:
      loadByFilePath in interface Model
      Parameters:
      filePath - the path of the mapfile
      defaults - optional model defaults, null if none
      aliases - optional entity aliases, null if none
      Returns:
      the entity
      Throws:
      ModelException - if no such mapfile or parsing error
    • loadByFilePath

      protected Entity loadByFilePath(String filePath, ModelDefaults defaults, EntityAliases aliases, boolean updateRelations) throws ModelException
      Loads an entity for a given mapfile path.
      Entities are cached so they are loaded and parsed only once.
      Parameters:
      filePath - the path of the mapfile
      defaults - optional model defaults, null if none
      aliases - optional entity aliases, null if none
      updateRelations - true if update the related entites in the model
      Returns:
      the entity
      Throws:
      ModelException - if no such mapfile or parsing error
    • parseEntity

      protected Entity parseEntity(ModelDefaults defaults, String fileName) throws ModelException
      Parses the given file and returns the created entity.
      Parameters:
      defaults - the model defaults, null if none
      fileName - the name of the file holding the model
      Returns:
      the entity
      Throws:
      ModelException - if parsing failed
    • addModelEntity

      protected void addModelEntity(ModelEntity modelEntity) throws ModelException
      Adds an entity to the model.
      Parameters:
      modelEntity - the model entity
      Throws:
      ModelException - if duplicate entities detected
    • updateRelations

      protected void updateRelations(ModelDefaults defaults, EntityAliases aliases) throws ModelException
      Updates the inheritance links and relations of all entities in the model.
      Parameters:
      defaults - the optional model defaults
      aliases - the optional entity aliases
      Throws:
      ModelException - if relations or inheritance misconfigured
    • updateRelations

      protected void updateRelations(ModelDefaults defaults, EntityAliases aliases, ModelEntity loadedModelEntity) throws ModelException
      Updates all relations.
      Parameters:
      defaults - the optional model defaults
      aliases - the optional entity aliases
      loadedModelEntity - the entity that caused the update, null if none
      Throws:
      ModelException - if relations or inheritance misconfigured
    • applyTableAlias

      protected boolean applyTableAlias(Map<String,Entity> aliasMap, Entity entity, String alias)
      Applies the table alias if unique and valid.
      Parameters:
      aliasMap - the map holding the aliases mapped to entities
      entity - the entity to apply the alias to
      alias - the alias
      Returns:
      true if applied, false if alias already exists or is not valid for at least one backend
    • isValidTableAlias

      protected boolean isValidTableAlias(String alias)
      Checks if a table alias is a valid name for all backends.
      Parameters:
      alias - the table alias
      Returns:
      true if ok
    • updateCompositePath

      protected void updateCompositePath(List<Relation> compositePath, Entity entity)
      Updates the composite path.
      Parameters:
      compositePath - the path that leads to given entity so far
      entity - the entity
    • getForeignKeys

      public Collection<ForeignKey> getForeignKeys() throws ModelException
      Description copied from interface: Model
      Gets all foreign keys.
      Specified by:
      getForeignKeys in interface Model
      Returns:
      the foreign keys
      Throws:
      ModelException - if model is inconsistent
    • createEntityFactory

      protected EntityFactory createEntityFactory()
      Creates the entity factory for this model.
      The method is invoked once from within the Model constructor.
      Returns:
      the entity factory
    • createModelDirectory

      protected ModelDirectory createModelDirectory(String modelDir, ModelDefaults defaults, EntityAliases aliases) throws ModelException
      Creates a model directory object.
      Parameters:
      modelDir - the directory name
      defaults - optional model defaults, null if none
      aliases - optional entity aliases, null if none
      Returns:
      the model directory
      Throws:
      ModelException - if directory does not exist or is not a directory
    • createModelEntity

      protected ModelEntity createModelEntity(Entity entity, String fileName) throws ModelException
      Creates a model entity wrapper.
      Parameters:
      entity - the entity
      fileName - the model file name
      Returns:
      the model entity
      Throws:
      ModelException - if creation failed
    • createReader

      protected Reader createReader(String name) throws ModelException
      Creates a reader for given name.
      Parameters:
      name - the file- or resource name
      Returns:
      the reader
      Throws:
      ModelException - if creating the reader failed (because file not found)
    • validateInheritanceHierarchy

      protected void validateInheritanceHierarchy(Entity entity, InheritanceType inheritanceType, TrackType trackType, Integrity integrity, Set<ModelError> errors)
      Verifies that the inheritance, integrity and track type is the same within a given interitance hierarchy.
      Notice that concrete classes (the leafs of the hierarchy) must have InheritanceType.NONE defined!
      Parameters:
      entity - the entity to check
      inheritanceType - the inheritance type
      trackType - the tracking type
      integrity - the integrity type
      errors - the collected model errors
    • validateComponents

      protected void validateComponents(Entity entity, Integrity integrity, Set<ModelError> errors)
      Verifies that the integrity is the same within a given composite hierarchy.
      Parameters:
      entity - the entity to check
      integrity - the integrity type
      errors - the collected model errors
    • translateAlias

      protected String translateAlias(EntityAliases aliases, String name)
      Translates an entity name if it is an alias.
      Parameters:
      aliases - the entity aliases
      name - the original name
      Returns:
      the possibly translated name