Class: ProjectUnification::SpecialHandlers::ControlledVocabularyTermHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/project_unification/special_handlers.rb

Overview

Handler for ControlledVocabularyTerm with rename-on-conflict and merge registry.

When a source CVT conflicts on name/definition/uri with a target CVT, and the two are semantically equivalent, the source is renamed with a sentinel suffix so it can be saved, then registered for Phase 2 collapse via Shared::Unify. The sentinel is deliberately conspicuous — if Phase 2 cleanup ever fails the user can find orphaned records by searching "UNIFICATION TO PROJECT".

Constant Summary collapse

SENTINEL_PREFIX =
'UNIFICATION TO PROJECT'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_project_id:, target_project_id:, options: {}) ⇒ ControlledVocabularyTermHandler

Returns a new instance of ControlledVocabularyTermHandler.



483
484
485
486
# File 'lib/project_unification/special_handlers.rb', line 483

def initialize(source_project_id:, target_project_id:, options: {})
  @source_project_id = source_project_id
  @target_project_id = target_project_id
end

Instance Attribute Details

#source_project_idObject (readonly)

Returns the value of attribute source_project_id.



479
480
481
# File 'lib/project_unification/special_handlers.rb', line 479

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



479
480
481
# File 'lib/project_unification/special_handlers.rb', line 479

def target_project_id
  @target_project_id
end

Instance Method Details

#conflict_fields(record) ⇒ Object (private)



568
569
570
571
572
# File 'lib/project_unification/special_handlers.rb', line 568

def conflict_fields(record)
  record.errors.details
    .select { |_, details| details.any? { |d| d[:error] == :taken } }
    .keys
end

#cvts_semantically_equivalent?(record, target_cvt) ⇒ Boolean (private)

Returns:

  • (Boolean)


574
575
576
577
578
579
# File 'lib/project_unification/special_handlers.rb', line 574

def cvts_semantically_equivalent?(record, target_cvt)
  record.name == target_cvt.name &&
    record.definition == target_cvt.definition &&
    record.uri_relation == target_cvt.uri_relation &&
    (record.uri.blank? || record.uri == target_cvt.uri)
end

#find_conflicting_target_cvts(record) ⇒ Object (private)



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/project_unification/special_handlers.rb', line 581

def find_conflicting_target_cvts(record)
  matches = {}

  name_match = ControlledVocabularyTerm.find_by(
    type: record.type, name: record.name, project_id: target_project_id
  )
  matches[:name] = name_match if name_match

  if record.definition.present?
    definition_match = ControlledVocabularyTerm.find_by(
      definition: record.definition, project_id: target_project_id
    )
    matches[:definition] = definition_match if definition_match
  end

  if record.uri.present?
    uri_match = ControlledVocabularyTerm.find_by(
      uri: record.uri, uri_relation: record.uri_relation, project_id: target_project_id
    )
    matches[:uri] = uri_match if uri_match
  end

  matches
end

#migrateObject



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/project_unification/special_handlers.rb', line 488

def migrate
  stats = { track: :special, model: 'ControlledVocabularyTerm', migrated: 0,
            conflicts: [], errors: [], merge_registry: [] }
  sentinel = "#{SENTINEL_PREFIX} #{target_project_id}"

  records = ControlledVocabularyTerm.where(project_id: source_project_id)
  return stats unless records.exists?

  records.find_each do |record|
    record.no_cached = true if record.respond_to?(:no_cached=)
    record.project_id = target_project_id

    if record.valid?
      record.save!
      stats[:migrated] += 1
    elsif uniqueness_error?(record)
      target_cvts  = find_conflicting_target_cvts(record)
      unique_targets = target_cvts.values.uniq(&:id)
      sole_target  = unique_targets.first if unique_targets.one?

      if sole_target && sole_target.type == record.type && cvts_semantically_equivalent?(record, sole_target)
        record.errors.details.each do |field, errors|
          next unless errors.any? { |e| e[:error] == :taken }
          case field
          when :name       then record.name       = unique_cvt_name(record.name, record.type, sentinel)
          when :definition then record.definition  = unique_cvt_definition(record.definition, sentinel)
          when :uri        then record.uri         = unique_cvt_uri(record.uri, record.uri_relation, sentinel)
          end
        end

        record.save!
        stats[:migrated] += 1
        stats[:merge_registry] << { model: record.class.name, renamed_id: record.id, target_id: sole_target.id }
      else
        primary_target = sole_target || unique_targets.first
        stats[:conflicts] << {
          id: record.id,
          model: 'ControlledVocabularyTerm',
          conflict_fields: conflict_fields(record),
          errors: record.errors.full_messages,
          source_cvt: {
            project_id: source_project_id,
            cvt_type: record.type,
            name: record.name,
            definition: record.definition,
            uri: record.uri,
            uri_relation: record.uri_relation
          },
          target_cvt: primary_target && {
            id: primary_target.id,
            project_id: target_project_id,
            cvt_type: primary_target.type,
            name: primary_target.name,
            definition: primary_target.definition,
            uri: primary_target.uri,
            uri_relation: primary_target.uri_relation
          },
          target_cvts: target_cvts.transform_values { |cvt|
            { id: cvt.id, project_id: target_project_id, cvt_type: cvt.type,
              name: cvt.name, definition: cvt.definition, uri: cvt.uri, uri_relation: cvt.uri_relation }
          }
        }
      end
    else
      stats[:errors] << { id: record.id, model: 'ControlledVocabularyTerm',
                          error: record.errors.full_messages.join('; ') }
    end
  rescue => e
    stats[:errors] << { id: record.id, model: 'ControlledVocabularyTerm', error: e.message }
  end

  stats
end

#unique_cvt_definition(original_definition, sentinel) ⇒ Object (private)



616
617
618
619
620
621
622
623
624
# File 'lib/project_unification/special_handlers.rb', line 616

def unique_cvt_definition(original_definition, sentinel)
  candidate = "#{original_definition} [#{sentinel}]"
  counter = 2
  while ControlledVocabularyTerm.exists?(definition: candidate, project_id: target_project_id)
    candidate = "#{original_definition} [#{sentinel} #{counter}]"
    counter += 1
  end
  candidate
end

#unique_cvt_name(original_name, cvt_type, sentinel) ⇒ Object (private)



606
607
608
609
610
611
612
613
614
# File 'lib/project_unification/special_handlers.rb', line 606

def unique_cvt_name(original_name, cvt_type, sentinel)
  candidate = "#{original_name} [#{sentinel}]"
  counter = 2
  while ControlledVocabularyTerm.exists?(type: cvt_type, name: candidate, project_id: target_project_id)
    candidate = "#{original_name} [#{sentinel} #{counter}]"
    counter += 1
  end
  candidate
end

#unique_cvt_uri(original_uri, uri_relation, sentinel) ⇒ Object (private)



626
627
628
629
630
631
632
633
634
635
# File 'lib/project_unification/special_handlers.rb', line 626

def unique_cvt_uri(original_uri, uri_relation, sentinel)
  no_space_sentinel = sentinel.tr(' ', '-')
  candidate = "#{original_uri}/#{no_space_sentinel}"
  counter = 2
  while ControlledVocabularyTerm.exists?(uri: candidate, uri_relation: uri_relation, project_id: target_project_id)
    candidate = "#{original_uri}/#{no_space_sentinel}-#{counter}"
    counter += 1
  end
  candidate
end

#uniqueness_error?(record) ⇒ Boolean (private)

Returns:

  • (Boolean)


564
565
566
# File 'lib/project_unification/special_handlers.rb', line 564

def uniqueness_error?(record)
  record.errors.details.values.flatten.any? { |e| e[:error] == :taken }
end