Class: SubjectHandler

Inherits:
Handler show all
Defined in:
backend/app/lib/bulk_import/subject_handler.rb

Constant Summary

Constants inherited from Handler

Handler::DISAMB_STR

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Method Summary collapse

Methods inherited from Handler

#clear, #save, #search

Methods included from BulkImportMixins

#ao_save, #archival_object_from_ref, #archival_object_from_ref_or_uri, #archival_object_from_uri, #create_date, #created, #find_top_container, #handle_notes, #indicator_and_type_exist_for_resource?, #resolves, #resource_from_ref, #resource_match, #sub_container_from_barcode, #test_exceptions, #valid, #value_check

Methods included from CrudHelpers

#handle_create, #handle_delete, #handle_listing, #handle_raw_listing, #handle_unlimited_listing, #handle_update, scoped_dataset, with_record_conflict_reporting, #with_record_conflict_reporting

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Constructor Details

#initialize(current_user, validate_only = false) ⇒ SubjectHandler

Returns a new instance of SubjectHandler.



6
7
8
9
10
11
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 6

def initialize(current_user, validate_only = false)
  super
  @subject_term_types = CvList.new("subject_term_type", @current_user)
  @subject_sources = CvList.new("subject_source", @current_user)
  @subjects = {} # will track both confirmed ids, and newly created ones.
end

Instance Method Details

#build(id, term, type, source) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 31

def build(id, term, type, source)
  {
    :id => id,
    :term => term || (id ? I18n.t("bulk_import.unfound_id", :id => id, :type => "subject [#{type}] [#{source}]") : nil),
    :type => type,
    :source => source,
    :id_but_no_term => id && !term,
  }
end

#create_subj(subject) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 93

def create_subj(subject)
  begin
    term = JSONModel(:term).new._always_valid!
    term.term = subject[:term]
    term.term_type = subject[:type]
    term.vocabulary = "/vocabularies/1"  # we're making a gross assumption here
    subj = JSONModel(:subject).new._always_valid!
    subj.terms.push term
    subj.source = subject[:source]
    subj.vocabulary = "/vocabularies/1"  # we're making a gross assumption here
    subj = save(subj, Subject)
  rescue Exception => e
    raise BulkImportException.new(I18n.t("bulk_import.error.no_create", :why => e.message))
  end
  subj
end

#get_db_subj(subject, has_source, report) ⇒ Object



110
111
112
113
114
115
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 110

def get_db_subj(subject, has_source, report)
  s_params = {}
  s_params[:q] = "title:\"#{subject[:term]}\" AND term_type_enum_s:\"#{subject[:type]}\""
  s_params[:q] = "#{s_params[:q]} AND source_enum_s:\"#{subject[:source]}\"" if has_source
  ret_subj = search(nil, s_params, :subject, "subject", "title:#{subject[:term]}", report)
end

#get_or_create(id, term, type, source, repo_id, report) ⇒ Object



41
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 41

def get_or_create(id, term, type, source, repo_id, report)
  has_source = !source.nil?
  errs = []
  subject = validate_subject(id, term, type, source, errs)

  subject_key = key_for(subject)
  if !(subj = stored(@subjects, subject[:id], subject_key))
    unless subject[:id].nil?
      begin
        subj = Subject.get_or_die(Integer(subject[:id]))
      rescue Exception => e
        if !e.message.end_with?("not found")
          raise BulkImportException.new(I18n.t("bulk_import.error.no_create", :why => e.message))
        end
      end
    end

    begin
      if !subj
        # we do this here in case there's a valid ID, even with bad source & type
        raise Exception.new(errs.join("; ")) if !errs.empty?
        begin
          subj = get_db_subj(subject, has_source, report)
        rescue Exception => e
          if e.is_a?(BulkImportDisambigException)
            disam = subject[:term] + DISAMB_STR
            report.add_info(I18n.t("bulk_import.warn.disam", :which => subject[:term], :name => disam))
            subject[:term] = disam
          else
            raise e
          end
        end
      end
      if !subj
        subj = create_subj(subject)
        report.add_info(I18n.t(@create_key, :what => "#{I18n.t("bulk_import.subj")} [#{subject[:term]}]", :id => subj.uri))
      end
    rescue Exception => e
      raise BulkImportException.new(I18n.t("bulk_import.error.no_create", :why => e.message))
    end
    if subj
      if subj[:id_but_no_term]
        @subjects[subject[:id].to_s] = subj
      else
        @subjects[subj.id.to_s] = subj
      end
      @subjects[subject_key] = subj
    end
  end
  subj
end

#key_for(subject) ⇒ Object



19
20
21
22
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 19

def key_for(subject)
  key = "#{subject[:term]} #{subject[:source]}: #{subject[:type]}"
  key
end

#renewObject



13
14
15
16
17
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 13

def renew
  clear(@subject_term_types)
  clear(@subject_sources)
  @subjects = {}
end

#stored(hash, id, key) ⇒ Object

Deal with the fact that an ID without a term may not be enough of a disambiguation



26
27
28
29
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 26

def stored(hash, id, key)
  ret_obj = super(hash, nil, key)
  ret_obj
end

#validate_subject(id, term, type, source, errs) ⇒ Object



117
118
119
120
121
# File 'backend/app/lib/bulk_import/subject_handler.rb', line 117

def validate_subject(id, term, type, source, errs)
  type = type.nil? ? "topical" : value_check(@subject_term_types, type, errs)
  source = source.nil? ? "ingest" : value_check(@subject_sources, source, errs)
  build(id, term, type, source)
end