Class: LangHandler

Inherits:
Handler show all
Defined in:
backend/app/lib/bulk_import/lang_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, #stored

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) ⇒ LangHandler

Returns a new instance of LangHandler.



2
3
4
5
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 2

def initialize(current_user)
  @language_types = CvList.new("language_iso639_2", current_user)
  @script_types = CvList.new("script_iso15924", current_user)
end

Instance Method Details

#create_language(lang_val, script, langmaterial, publish, report) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 12

def create_language(lang_val, script, langmaterial, publish, report)
  langs = []
  have_lang = !lang_val.nil?
  have_note = !langmaterial.nil?
  if have_lang || have_note
    lang_code = nil
    if !have_lang
      begin
        lang_code = @language_types.value(langmaterial)
        have_note = false
      rescue Exception => n
        #we know that the note isn't just the language
      end
    else
      begin
        lang_code = @language_types.value(lang_val)
      rescue Exception => n
        report.add_errors(I18n.t("bulk_import.error.lang_code", :lang => lang_val))
      end
    end
    if lang_code
      langscript = JSONModel(:language_and_script).new
      langscript.language = lang_code
      if !script.nil?
        begin
          langscript.script = @script_types.value(script)
        rescue Exception => n
          report.add_errors(I18n.t("bulk_import.error.script_code", :script => script))
        end
      end
      lang = JSONModel(:lang_material).new
      lang.language_and_script = langscript
      langs.push lang
    end
    if have_note
      lang = JSONModel(:lang_material).new
      content = langmaterial
      begin
        wellformed(content)
        note = JSONModel(:note_langmaterial).new
        if publish.class.name == "String"
          publish = publish == "1"
        end
        note.publish = publish
        note.type = "langmaterial"
        note.content.push content if !content.nil?
        lang.notes.push note
        langs.push lang
      rescue Exception => e
        report.add_errors(I18n.t("bulk_import.error.bad_note", :type => "langmaterial", :msg => e.message))
      end
    end
  end
  langs
end

#renewObject



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

def renew
  clear(@language_types)
  clear(@script_types)
end

#wellformed(note) ⇒ Object

currently a repeat from the controller



69
70
71
72
73
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 69

def wellformed(note)
  if note.match("</?[a-zA-Z]+>")
    frag = Nokogiri::XML("<root xmlns:xlink='https://www.w3.org/1999/xlink'>#{note}</root>") { |config| config.strict }
  end
end