Class: CPEE::ModelManagement::Create

Inherits:
Riddl::Implementation
  • Object
show all
Defined in:
lib/cpee-model-management/implementation.rb

Overview

}}}

Instance Method Summary collapse

Instance Method Details

#responseObject

{{{



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/cpee-model-management/implementation.rb', line 384

def response
  models = @a[4]
  views = @a[1]
  conns = @a[2]
  templates = @a[3]
  default_view = @a[5]
  if  @p.first.name == 'stage'
    where = @a[0] == :main ? '' : @r.map{ |d| Riddl::Protocols::Utils::unescape(d) }.join('/')
    stage = @p.shift.value
    stage = templates.keys.include?(stage) ? stage : default_view
    name = @p[0].value
    source = templates[stage] ? templates[stage] : 'testset.xml'
    fname = File.join(models,where,name + '.xml')
  else
    where = @r[0..-2].map{ |d| Riddl::Protocols::Utils::unescape(d) }.join('/')
    stage = nil
    name = @r[-1].sub(/\.xml$/,'')
    source = File.join(models,where,name + '.xml')
    where = @p[0].value
    if @p.length > 1
      fname = File.join(models,where,@p[1].value + '.xml')
    else
      fname = File.join(models,where,name + '.xml')
    end
  end

  attrs = JSON::load File.open(source + '.attrs') rescue {}
  stage = attrs['design_stage'] if stage.nil? && attrs['design_stage']
  stage = views[stage] if views && views[stage]

  counter = 0
  while File.exist?(fname)
    counter += 1
    fname = File.join(models,where,name + counter.to_s + '.xml')
  end

  dn = CPEE::ModelManagement::get_dn @h['DN']
  creator = dn['GN'] + ' ' + dn['SN']
  FileUtils.cp(source,fname)
  attrs = {}
  XML::Smart::modify(fname) do |doc|
    doc.register_namespace 'p', 'http://cpee.org/ns/properties/2.0'
    doc.find('/p:testset/p:attributes/p:info').each do |ele|
      ele.text = File.basename(fname,'.xml')
    end
    doc.find('/p:testset/p:attributes/p:creator').each do |ele|
      ele.text = creator
    end
    doc.find('/p:testset/p:attributes/p:author').each do |ele|
      ele.text = creator
    end
    doc.find('/p:testset/p:attributes/p:design_dir').each do |ele|
      ele.text = where
    end
    doc.find('/p:testset/p:attributes/p:model_uuid').each do |ele|
      ele.text = SecureRandom.uuid
    end
    if stage
      doc.find('/p:testset/p:attributes/p:design_stage').each do |ele|
        ele.text = stage
      end
    end
    attrs = doc.find('/p:testset/p:attributes/*').map do |e|
      [e.qname.name,e.text]
    end.to_h
  end
  File.write(fname + '.attrs',JSON::pretty_generate(attrs))

  CPEE::ModelManagement::op creator, 'add', models, File.join('.', where, name + '.xml')
  CPEE::ModelManagement::notify conns, 'create', models, fname
  nil
end