Class: CaTissue::Annotation::ReferenceWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/catissue/database/annotation/reference_writer.rb

Overview

A ReferenceWriter saves annotations to the database. This is a helper class to work around caTissue DE API defects. This class infers a direct data mapping by navigating the caTissue DYEXT tables of the introspected Java annotation class properties.

Instance Method Summary collapse

Constructor Details

#initialize(eid, attr_md, assn_eid = nil) ⇒ ReferenceWriter

Returns a new instance of ReferenceWriter.

Parameters:

  • eid (Integer)

    the referencing annotation entity id

  • attr_md (CaRuby::AttributeMetadata)

    the annotation attribute metadata of the attribute to save

  • assn_eid (Integer, nil) (defaults to: nil)

    the referenced annotation entity id



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/catissue/database/annotation/reference_writer.rb', line 10

def initialize(eid, attr_md, assn_eid=nil)
  logger.debug { "Mapping annotation #{attr_md.declarer.qp}.#{attr_md} role attributes to database columns..." }
  efcd = EntityFacade.instance
  # the referenced annotation entity id
  assn_eid ||= associated_entity_id(eid, attr_md)
  # the referenced entity database table
  @table = efcd.annotation_table_for_entity_id(assn_eid)
  # map the attribute => column
  attr_col_hash = map_attributes(attr_md.type, assn_eid)
  logger.debug { "Annotation #{attr_md.declarer.qp} #{attr_md} maps to #{@table} as #{attr_col_hash.qp}" }
  # the mapped attributes and columns
  @attrs, cols = attr_col_hash.to_a.transpose
  # the SQL parameters clause
  params = Array.new(cols.size, '?').join(', ')
  # the create SQL
  @cr_sql = CREATE_SQL % [@table, cols.join(', '), params]
  # the update SQL
  @upd_sql = UPDATE_SQL % [@table, cols.map { |col| "#{col} = ?" }.join(', ')]
  # the superclass writer for annotations with superclass DE forms
  @parent = obtain_parent_writer(eid, attr_md)
end

Instance Method Details

#save(annotation) ⇒ Object

Parameters:

  • annotation (Annotation)

    the referenced annotation value



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/catissue/database/annotation/reference_writer.rb', line 33

def save(annotation)
  # select the SQL based on whether this is an update or a create
  sql = annotation.identifier ? @upd_sql : @cr_sql
  # allocate a new database identifier
  annotation.identifier ||= next_identifier
  # the values to bind to the SQL parameters
  values = database_parameters(annotation)
  logger.debug { "Saving #{annotation} to #{@table}..." }
  # dispatch the SQL update or create statement
  CaTissue::Database.instance.executor.execute do |dbh|
    dbh.prepare(sql) { |sth| sth.execute(*values) }
  end
  if @parent then
    logger.debug { "Saving #{annotation} parent entity attributes..." }
    @parent.save(annotation)
  end
end