Class: CaTissue::Annotation::EntityFacade

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/catissue/database/annotation/entity_facade.rb

Overview

EntityFacade is the caRuby substitute for the broken caTissue EntityManager. EntityManager is the caTissue singleton Winnebago object for doing lots of things with dynamic extensions. The EntityManager defects are listed in the various Rubydoc quirks.

Instance Method Summary collapse

Constructor Details

#initializeEntityFacade

Initializes the id generator and a SQL executor. The id generator and executor are used for the caTissue bug work-arounds described in the method docs and IdGenerator.



17
18
19
20
21
22
23
24
# File 'lib/catissue/database/annotation/entity_facade.rb', line 17

def initialize
  # the work-around id generator
  @idgen = IdGenerator.new
  # a general-purpose SQL executor for calling the work-arounds
  @executor = CaTissue::Database.current.executor
  # the primary entity class => entity id hash
  @pr_eid_hash = {}
end

Instance Method Details

#annotation_entity_id(klass, validate = true) ⇒ Integer

Returns the caTissue entity id for the class.

Parameters:

  • klass (Class)

    the CaTissue::Annotation primary class

  • validate (Boolean) (defaults to: true)

    flag indicating whether to raise an exception if the class is not primary

Returns:

  • (Integer)

    the caTissue entity id for the class

Raises:

  • (AnnotationError)

    if the validate flag is set and the class is not primary



52
53
54
55
56
# File 'lib/catissue/database/annotation/entity_facade.rb', line 52

def annotation_entity_id(klass, validate=true)
  eid = @pr_eid_hash[klass] ||= recursive_annotation_entity_id(klass)
  if eid.nil? and validate then raise AnnotationError.new("Entity not found for annotation #{klass}") end
  eid
end

#annotation_table_for_entity_id(eid) ⇒ String

Parameters:

  • obj (Annotation)

    the annotation object

  • the (Integer)

    annotation entity identifier

Returns:

  • (String)

    the entity table name

  • (String)

    the entity table name



104
105
106
107
108
109
110
# File 'lib/catissue/database/annotation/entity_facade.rb', line 104

def annotation_table_for_entity_id(eid)
  result = @executor.query(TABLE_NAME_SQL, eid).first
  if result.nil? then raise AnnotationError.new("Table not found for annotation entity id #{eid}") end
  tbl = result[0]
  logger.debug { "Annotation entity with id #{eid} has table #{tbl}." }
  tbl
end

#associated_entity_id(eid, name) ⇒ Integer

Returns the referenced CaTissue::Annotation class entity id.

Parameters:

  • eid (Integer)

    the referencing entity id

  • name (String)

    the association property name

Returns:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/catissue/database/annotation/entity_facade.rb', line 82

def associated_entity_id(eid, name)
  # The caTissue role is capitalized.
  role = name.capitalize_first
  ref_eid = recursive_associated_entity_id(eid, role)
  if ref_eid then
    logger.debug { "Entity id #{eid} is associated with property #{name} via entity id #{ref_eid}." }
  else
    logger.debug { "Entity id #{eid} is not associated with property #{name}." }
  end
  ref_eid
end

#container_id(eid) ⇒ Integer

Obtains the undocumented caTisue container id for the given primary entity id.

Returns:

  • (Integer)

    eid the primary entity id



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/catissue/database/annotation/entity_facade.rb', line 135

def container_id(eid)
  # The following call is broken in caTissue 1.1.2 (see method doc).
  # EntityManager.instance.get_container_id_for_entity(eid)
  # Work-around this caTissue bug with a direct query.
  result = @executor.query(CTR_ID_SQL, eid).first
  if result.nil? then
    logger.debug("Dynamic extension container id not found for annotation with entity id #{eid}")
    return
  end
  cid = result[0].to_i
  logger.debug { "Annotation with entity id #{eid} has container id #{cid}." }
  cid
end

#hook_entity_id(klass) ⇒ Integer

Returns the class entity id.

Parameters:

Returns:

  • (Integer)

    the class entity id



67
68
69
70
71
72
73
# File 'lib/catissue/database/annotation/entity_facade.rb', line 67

def hook_entity_id(klass)
  result = @executor.query(HOOK_ENTITY_ID_SQL, klass.java_class.name).first
  if result.nil? then raise AnnotationError.new("Entity id not found for static hook class #{klass.qp}") end
  eid = result[0].to_i
  logger.debug { "Static hook class #{klass.qp} has entity id #{eid}." }
  eid
end

#next_identifier(annotation) ⇒ Integer

Returns a new identifier for the given annotation object.

Parameters:

Returns:

  • (Integer)

    a new identifier for the given annotation object



28
29
30
31
32
33
34
# File 'lib/catissue/database/annotation/entity_facade.rb', line 28

def next_identifier(annotation)
  # The entity table name, which will be a cryptic value like DE_E_1283.
  eid = annotation_entity_id(annotation.class)
  aeid = common_ancestor_entity_id(eid)
  tbl = annotation_table_for_entity_id(aeid)
  next_identifier_for_table(tbl)
end

#next_identifier_for_table(table) ⇒ Integer

Returns the next identifier to use when creating a table record.

Parameters:

  • the (String)

    table name

Returns:

  • (Integer)

    the next identifier to use when creating a table record



38
39
40
41
# File 'lib/catissue/database/annotation/entity_facade.rb', line 38

def next_identifier_for_table(table)
  # delegate to id generator
  @idgen.next_identifier(table)
end

#parent_entity_id(eid) ⇒ Integer?

Returns the parent entity id, if any.

Parameters:

  • eid (Integer)

    the referencing entity id

  • name (String)

    the association property name

Returns:

  • (Integer, nil)

    the parent entity id, if any



114
115
116
117
# File 'lib/catissue/database/annotation/entity_facade.rb', line 114

def parent_entity_id(eid)
  result = @executor.query(PARENT_ENTITY_ID_SQL, eid).first
  result[0].to_i if result and result[0]
end

#primary?(eid) ⇒ Boolean

Returns whether the entity is primary.

Parameters:

  • eid (Integer)

    the entity id to check

Returns:

  • (Boolean)

    whether the entity is primary



60
61
62
63
# File 'lib/catissue/database/annotation/entity_facade.rb', line 60

def primary?(eid)
  result = @executor.query(IS_PRIMARY_SQL, eid).first
  not result.nil?
end