Class: DigitalObjectHandler

Inherits:
Handler
  • Object
show all
Defined in:
backend/app/lib/bulk_import/digital_object_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, validate_only = false) ⇒ DigitalObjectHandler

Returns a new instance of DigitalObjectHandler.



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

def initialize(current_user, validate_only = false)
  super
  # we don't currently use this, but it will come in handy with enhancements
  @digital_object_types ||= CvList.new("digital_object_digital_object_type", @current_user)
end

Instance Method Details

#check_digital_id(dig_id) ⇒ Object



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

def check_digital_id(dig_id)
  params = { :q => "digital_object_id:\"#{dig_id}\"" }
  ret = search(nil, params, :digital_object, "digital_object", "", @report)
  ret = ret.nil?
end

#create(title, thumb, link, id, publish, archival_object, report, link_publish = nil, thumb_publish = nil) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'backend/app/lib/bulk_import/digital_object_handler.rb', line 18

def create(title, thumb, link, id, publish, archival_object, report, link_publish=nil, thumb_publish=nil)
  dig_o = nil
  dig_instance = nil
  link_publish = publish if link_publish.nil?
  thumb_publish = publish if thumb_publish.nil?

  # might as well check the dig_id first
  if @validate_only
    if archival_object.nil?
      archival_object = JSONModel(:archival_object).new._always_valid!
      archival_object.title = "random title"
      archival_object.ref_id = "VAL#{rand(1000000)}"
    end
  end
  osn = id || SecureRandom.hex
  if !check_digital_id(osn)
=begin
    if @validate_only
      I18n.t("bulk_import.error.dig_obj_unique", :id => osn)
      report.add_errors(I18n.t("bulk_import.error.dig_obj_unique", :id => osn))
    else
=end


    raise BulkImportException.new(I18n.t("bulk_import.error.dig_obj_unique", :id => osn))
    #      end
  end
  files = []
  if !link.nil? && link.start_with?("http")
    fv = JSONModel(:file_version).new._always_valid!
    fv.file_uri = link
    fv.publish = link_publish
    fv.xlink_actuate_attribute = "onRequest"
    fv.xlink_show_attribute = "new"
    files.push fv
  end
  if !thumb.nil? && thumb.start_with?("http")
    fv = JSONModel(:file_version).new._always_valid!
    fv.file_uri = thumb
    fv.publish = thumb_publish
    fv.xlink_actuate_attribute = "onLoad"
    fv.xlink_show_attribute = "embed"
    fv.is_representative = thumb_publish
    files.push fv
  end
  dig_o = JSONModel(:digital_object).new._always_valid!
  dig_o.title = title.nil? ? archival_object.display_string : title
  dig_o.digital_object_id = osn
  dig_o.file_versions = files if files.any?
  dig_o.publish = publish
  begin
    dig_o = save(dig_o, DigitalObject)
  rescue JSONModel::ValidationException => ve
    report.add_errors(I18n.t("bulk_import.error.dig_validation", :err => ve.errors))
    return nil
  rescue Exception => e
    raise e
  end
  report.add_info(I18n.t(@create_key, :what => I18n.t("bulk_import.dig"), :id => "'#{dig_o.title}' #{dig_o.uri} [#{dig_o.digital_object_id}]"))
  dig_instance = JSONModel(:instance).new._always_valid!
  dig_instance.instance_type = "digital_object"
  dig_instance.digital_object = { "ref" => @validate_only ? "http://x" : dig_o.uri }
  dig_instance
end

#renewObject



81
82
83
# File 'backend/app/lib/bulk_import/digital_object_handler.rb', line 81

def renew
  clear(@digital_object_types)
end