Class: CaRuby::PersistenceService

Inherits:
Object
  • Object
show all
Defined in:
lib/caruby/database/persistence_service.rb

Overview

A PersistenceService is a database mediator which implements the #query #create, #update and #delete methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ PersistenceService

Creates a new PersistenceService with the specified application service name and options.

Parameters:

  • the (String)

    caBIG application service name

  • opts ({Symbol => Object}) (defaults to: {})

    the options

Options Hash (opts):

  • :host (String)

    the service host (default localhost)

  • :version (String)

    the caTissue version identifier



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/caruby/database/persistence_service.rb', line 21

def initialize(name, opts={})
  CaRuby::PersistenceService.import_java_classes
  @name = name
  ver_opt = opts[:version]
  @version = ver_opt.to_s.to_version if ver_opt
  @host = opts[:host] || default_host
  @port = opts[:port] || 8080
  @url = "http://#{@host}:#{@port}/#{@name}/http/remoteService"
  @timer = Jinx::Stopwatch.new
  logger.debug { "Created persistence service #{name} at #{@host}:#{@port}." }
end

Instance Attribute Details

#nameObject (readonly)

The service name.



10
11
12
# File 'lib/caruby/database/persistence_service.rb', line 10

def name
  @name
end

#timerObject (readonly)

The Stopwatch which captures the time spent in database operations performed by the application service.



13
14
15
# File 'lib/caruby/database/persistence_service.rb', line 13

def timer
  @timer
end

Instance Method Details

#app_serviceObject

Returns the ApplicationService remote instance.

Returns:

  • the CaCORE service provider wrapped by this PersistenceService



88
89
90
# File 'lib/caruby/database/persistence_service.rb', line 88

def app_service
  ApplicationService.for(@url)
end

#create(obj) ⇒ Object

Submits the create to the application service and returns the created object.



53
54
55
56
57
58
59
60
61
# File 'lib/caruby/database/persistence_service.rb', line 53

def create(obj)
  logger.debug { "Submitting create #{obj.pp_s(:single_line)} to application service #{name}..." }
  begin
    dispatch { |svc| svc.create_object(obj) }
  rescue Exception => e
    logger.error("Error creating #{obj} - #{e.message}\n#{dump(obj)}")
    raise e
  end
end

#delete(obj) ⇒ Object

Submits the delete to the application service.



75
76
77
78
79
80
81
82
83
# File 'lib/caruby/database/persistence_service.rb', line 75

def delete(obj)
  logger.debug { 'Deleting #{obj}.' }
  begin
    dispatch { |svc| svc.remove_object(obj) }
  rescue Exception => e
    logger.error("Error deleting #{obj} - #{e.message}\n#{dump(obj)}")
    raise e
  end
end

#query(template_or_hql, *path) ⇒ Object

Returns an array of objects fetched from the database which match the given template_or_hql.

If template_or_hql is a String, then the HQL is submitted to the service.

Otherwise, the template_or_hql is a query template domain object following the given attribute path. The query condition is determined by the values set in the template. Every non-nil attribute in the template is used as a select condition.



45
46
47
# File 'lib/caruby/database/persistence_service.rb', line 45

def query(template_or_hql, *path)
  String === template_or_hql ? query_hql(template_or_hql) : query_template(template_or_hql, path)
end

#update(obj) ⇒ Object

Submits the update to the application service and returns obj.



64
65
66
67
68
69
70
71
72
# File 'lib/caruby/database/persistence_service.rb', line 64

def update(obj)
   logger.debug { "Submitting update #{obj.pp_s(:single_line)} to application service #{name}..." }
   begin
     dispatch { |svc| svc.update_object(obj) }
   rescue Exception => e
     logger.error("Error updating #{obj} - #{e.message}\n#{dump(obj)}")
     raise e
   end
end