Class: EntityType

Inherits:
Object
  • Object
show all
Defined in:
lib/ObjectModel/AnEntity/EntityType.rb

Class Method Summary collapse

Class Method Details

.create_copy(e) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 42

def create_copy e
  e.entity_id.should_not! :be_nil
  
  c = EntityCopy.new
  c.om_version, c.entity_id, c.name = e.om_version, e.entity_id, e.name
  c.parent = e.instance_variable_get "@parent"
  c.updated!           
  
  # Attributes
  e.meta.attributes.each do |n, m|
    m.type.initialize_copy m, e, c
  end
  
  # Children
  e.meta.children.each do |n, m|
    m.type.initialize_copy m, e, c
  end
  
  # References
  e.meta.references.each do |n, m|
    m.type.initialize_copy m, e, c
  end      
  
  BackReferences.initialize_copy e, c            
  
  return c
end

.custom_initialization(e, outdated = nil) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 278

def custom_initialization e, outdated = nil
  e.meta.attributes.each do |n, m|
    next if m.initialize == NotDefined or (outdated and !outdated.include?(n))
    value = if m.initialize.is_a? Proc
      e.instance_eval &m.initialize
    else
      m.initialize
    end
    e.send m.name.to_writer, value 
  end
end

.delete(e, storage) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 252

def delete e, storage
  entity_id = e.entity_id
  
  # Entity
  storage[:entities].filter(:entity_id => entity_id).delete 
  storage[:entities_content].filter(:entity_id => entity_id).delete
  
  # Attributes     
  e.meta.attributes.each do |n, m|     
    m.type.respond_to :delete, e, m, storage                    
  end            
  
  # Children
  e.meta.children.each do |n, m|
    m.type.respond_to :delete, e, m, storage                      
  end
  
  # References
  e.meta.references.each do |n, m|
    m.type.respond_to :delete, e, m, storage                        
  end
  
  # BackReferences
  BackReferences.delete e, storage
end

.delete_all_children(e) ⇒ Object



328
329
330
331
332
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 328

def delete_all_children e           
  e.meta.children.each do |n, m| 
    m.type.delete_all_children e, m
  end
end

.delete_all_references_to(tr, e) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 334

def delete_all_references_to tr, e
  c = tr.copies[e.entity_id]
  processed = Set.new
  BackReferences.delete_all_references_to e, c do |referrer_entity_id|
    next if processed.include? referrer_entity_id
    processed << referrer_entity_id
    
    referrer = tr.resolve referrer_entity_id
    referrer.meta.references.each do |n, m|
      m.type.delete_all_references_to referrer, e, m
    end
  end
end

.delete_backreference(transaction, entity, reference) ⇒ Object



356
357
358
359
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 356

def delete_backreference transaction, entity, reference
  reference_copy = transaction.copy_get! reference
  BackReferences.delete_backreference entity, reference, reference_copy
end

.delete_from_parent(e, parent) ⇒ Object



348
349
350
351
352
353
354
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 348

def delete_from_parent e, parent    
  e.should! :be_a, Entity
  parent.should! :be_a, Entity
  parent.meta.children.each do |n, m|
    m.type.delete_from_parent e, parent, m
  end
end

.dump_id(id) ⇒ Object



390
391
392
393
394
395
396
397
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 390

def dump_id id      
  if id == nil 
    "nil" 
  else
    id.should! :be_a, String
    id
  end
end

.dump_id!(id) ⇒ Object



404
405
406
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 404

def dump_id! id
  [dump_id(id), "ENTITY_ID"]
end

.each_attribute(e, &b) ⇒ Object



366
367
368
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 366

def each_attribute e, &b
  e.meta.attributes.each{|n, m| b.call e.send(n)}
end

.each_child(e, &b) ⇒ Object



370
371
372
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 370

def each_child e, &b
  e.meta.children.each{|n, m| m.type.each e, m, &b}
end

.each_entity_in_repository(repository, &b) ⇒ Object



378
379
380
381
382
383
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 378

def each_entity_in_repository repository, &b
  repository.storage[:entities].each do |row|
    e = repository.by_id row[:entity_id]
    b.call e
  end
end

.each_reference(e, &b) ⇒ Object



374
375
376
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 374

def each_reference e, &b
  e.meta.references.each{|n, m| m.type.each e, m, &b}
end

.initialize_new_entity(e, eid, entity_id, tr) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 70

def initialize_new_entity e, eid, entity_id, tr
  new_entity_id = if entity_id
    entity_id.should! :be_a, String
    #       path_index = tr.repository._index_manager.get_index_without_transaction_check(:path)
    #       path_index.get_entity_id(entity_id).should! :be_nil                
    raise_without_self "Not Unique :entity_id ('#{entity_id}')!", ObjectModel if storage_include? entity_id, tr.repository.storage
    
    tr.copies.should_not! :include, entity_id
    entity_id
  else
    tr.repository.storage.generate(:entity_id)
  end
  
  unless eid
    eid = "eid_" + tr.repository.storage.generate(:name)
  end
  
  e.instance_variable_set "@entity_id", new_entity_id            
  e.instance_variable_set "@om_repository", tr.repository
  e.instance_variable_set "@om_version", 0
  
  # Attributes
  e.meta.attributes.each do |n, m|       
    value = m.type.initial_value m, e
    e.instance_variable_set m.ivname, value
  end
  
  # Children
  e.meta.children.each do |n, m|
    value = m.type.initial_value m, e
    e.instance_variable_set m.ivname, value
  end
  
  # References
  e.meta.references.each do |n, m|
    value = m.type.initial_value m, e
    e.instance_variable_set m.ivname, value
  end            
  
  BackReferences.initialize_entity e
  
  tr.event_processor.fire_before e, :new
  
  e.name = eid # after :new
end

.initialize_storage(db) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 5

def initialize_storage db     
  db.create_table :entities do
    column :entity_id, :text
    column :name, :text
    column :class, :text
    
    column :om_version, :text       
    column :parent_id, :text
    
    primary_key :entity_id
#       index :entity_id
  end
  
  db.create_table :entities_content do
    column :entity_id, :text
    
    column :name, :text
    
    column :value, :text        
    column :class, :text
    
    primary_key :entity_id, :attr_name, :key
#       index :entity_id
  end            
end

.load(entity_id, repository, storage) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 116

def load entity_id, repository, storage
  entity_id.should! :be_a, String
  row = storage[:entities][:entity_id => entity_id]
  unless row
    raise NotFoundError, "Entity with entity_id '#{entity_id}' not found!"
    #       raise_without_self NotFoundError, "Entity with Path '#{path}' not found!", ObjectModel 
  end
  
  # Entity
  klass_name = row[:class]
  begin
    klass = eval klass_name, TOPLEVEL_BINDING, __FILE__, __LINE__
    e = klass.new nil, nil, "original_new"
    e.should! :be_a, Entity
  rescue NameError => err
    log.error "Can't find '#{klass}' Class for '#{entity_id}' Entity!"
    raise LoadError.new(err)
  end    
  
  e.instance_variable_set "@entity_id", entity_id
  e.instance_variable_set "@name", row[:name].should_not!(:be_nil).should_not!(:be_empty)
  e.instance_variable_set "@om_version", row[:om_version].should_not!(:be_nil).to_i
  e.instance_variable_set "@parent", load_id(row[:parent_id].should_not!(:be_nil))
  e.instance_variable_set "@om_repository", repository
  
  outdated = []
  
  # Attributes
  e.meta.attributes.each do |n, m|
    value = begin
      m.type.load m, e, storage            
    rescue LoadError
      outdated << n
      log.warn "Can't load '#{entity_id}.#{n}' Attribute, default value will be used!"
      value = m.type.initial_value m, e
      e.instance_variable_set m.ivname, value
    end
  end            
  
  # Children
  e.meta.children.each do |n, m|
    value = begin
      m.type.load m, e, storage          
    rescue LoadError
      log.warn "Can't load '#{entity_id}.#{n}' Child, default value will be used!"
      value = m.type.initial_value m, e
      e.instance_variable_set m.ivname, value
    end
  end
  
  # References
  e.meta.references.each do |n, m|
    value = begin
      m.type.load m, e, storage
    rescue LoadError
      log.warn "Can't load '#{entity_id}.#{n}' Reference, default value will be used!"         
      value = m.type.initial_value m, e
      e.instance_variable_set m.ivname, value
    end       
  end
  
  # BackReferences
  BackReferences.load e, storage     
  
  # Data Migration
  custom_initialization e, outdated if outdated.size > 0
  
  return e
end

.load_id(data) ⇒ Object

Raises:



385
386
387
388
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 385

def load_id data      
  raise LoadError if data == nil
  data == "nil" ? nil : data
end

.load_id!(data, klass) ⇒ Object

Raises:



399
400
401
402
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 399

def load_id! data, klass
  raise LoadError unless klass == "ENTITY_ID"
  load_id data
end

.new_backreference(transaction, entity, reference) ⇒ Object



361
362
363
364
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 361

def new_backreference transaction, entity, reference
  reference_copy = transaction.copy_get! reference
  BackReferences.new_backreference entity, reference, reference_copy
end

.persist(c, e, storage) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 218

def persist c, e, storage
  entity_id = e.entity_id
  
  # Entity
  entities = storage[:entities]
  entities.filter(:entity_id => entity_id).delete 
  entities.insert(
                  :entity_id => entity_id.should!(:be_a, String).should_not!(:be_empty),
  :name => e.name.should!(:be_a, String).should_not!(:be_empty),
  :class => e.class.name,
  :om_version => c.om_version,
  :parent_id => AnEntity::EntityType.dump_id(c.parent)
  )
  storage[:entities_content].filter(:entity_id => entity_id).delete
  
  # Attributes     
  e.meta.attributes.each do |n, m|
    m.type.persist c, entity_id, m, storage                   
  end            
  
  # Children
  e.meta.children.each do |n, m|
    m.type.persist c, entity_id, m, storage       
  end
  
  # References
  e.meta.references.each do |n, m|
    m.type.persist c, entity_id, m, storage       
  end
  
  # BackReferences
  BackReferences.persist c, entity_id, storage
end


31
32
33
34
35
36
37
38
39
40
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 31

def print_storage db, name
  if name == nil or name == :entities
    puts "\nEntities:"
    db[:entities].print
  end
  if name == nil or name == :entities_content
    puts "EntitiesContent:"
    db[:entities_content].print
  end
end

.storage_include?(entity_id, storage) ⇒ Boolean



186
187
188
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 186

def storage_include? entity_id, storage
  storage[:entities][:entity_id => entity_id] != nil
end

.validate_attribute(e, name, new, old) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 290

def validate_attribute e, name, new, old
  m = e.class.meta.attributes[name]
  
  unless m.type.validate_type new
    raise_without_self Errors::ValidationError, 
    "Invalid Value Type '#{new}' for Attribute '#{e.class.name}.#{name}'!", 
    ObjectModel
  end
  
  v = m.validate     
  if v != nil
    v.should! :be_a, Proc
    begin
      v.call new
    rescue RuntimeError => err
      raise Errors::ValidationError, err.message, err.backtrace
    end
    #       unless v.call new
    #         raise_without_self Errors::ValidationError, 
    #         "Invalid Value '#{new}' for Attribute '#{e.class.name}.#{name}'!", 
    #         ObjectModel
    #       end
  end                  
end

.validate_entity(e) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 315

def validate_entity e
  begin
    e.class.meta.validation.validate e
  rescue RuntimeError => err
    raise Errors::ValidationError, err.message, err.backtrace
  end
  #      unless e.class.meta.validation.validate e
  #        raise_without_self Errors::ValidationError, 
  #        "Invalid Entity #{e}!",
  #        ObjectModel
  #      end
end

.write_back(c, e) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ObjectModel/AnEntity/EntityType.rb', line 190

def write_back c, e
  c.should! :be_a, EntityCopy
  
  # Entity
  c.om_version += 1
  e.instance_variable_set "@om_version", c.om_version
  e.instance_variable_set "@parent", c.parent
  e.instance_variable_set "@name", c.name
  
  # Attributes
  e.meta.attributes.each do |n, m|
    m.type.write_back c, e, m
  end            
  
  # Children
  e.meta.children.each do |n, m|
    m.type.write_back c, e, m       
  end
  
  # References
  e.meta.references.each do |n, m|
    m.type.write_back c, e, m       
  end
  
  # BackReferences     
  BackReferences.write_back c, e
end