Class: Handler

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/bulk_import/handler.rb

Constant Summary collapse

DISAMB_STR =
" DISAMBIGUATE ME!"

Instance Method Summary collapse

Constructor Details

#initialize(current_user, validate_only) ⇒ Handler

Returns a new instance of Handler.



21
22
23
24
25
# File 'backend/app/lib/bulk_import/handler.rb', line 21

def initialize(current_user, validate_only)
  @current_user = current_user
  @validate_only = validate_only
  @create_key = @validate_only ? "bulk_import.could_be" : "bulk_import.created"
end

Instance Method Details

#clear(enum_list) ⇒ Object



97
98
99
# File 'backend/app/lib/bulk_import/handler.rb', line 97

def clear(enum_list)
  enum_list.renew
end

#save(obj, model) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'backend/app/lib/bulk_import/handler.rb', line 27

def save(obj, model)
  test_exceptions(obj)
  if !@validate_only
    saved = model.create_from_json(obj)
    objs = model.sequel_to_jsonmodel([saved])
    revived = objs.empty? ? nil : objs[0] if !objs.empty?
  else
    obj
  end
end

#search(repo_id, params, sym, type = "", match = "", report = nil) ⇒ Object

if repo_id is nil, do a global search (subject and agent) this is using archivesspace/backend/app/models/search.rb



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
81
82
83
84
85
86
87
88
89
90
# File 'backend/app/lib/bulk_import/handler.rb', line 40

def search(repo_id, params, sym, type = "", match = "", report = nil)
  obj = nil
  search = nil
  matches = match.split(":")
  # need to add these default-y values to the params
  params[:page_size] = 10
  params[:page] = 1
  params[:sort] = ""
  unless !params[:q]
    params[:q] = "primary_type:#{type} AND #{params[:q]}" if !type.empty?
    params[:q] = "repository:\"/repositories/#{repo_id}\" AND  #{params[:q]}" if repo_id
  end
  begin
    search = Search.search(params, nil)
  rescue Exception => e
    raise e if !e.message.match("<h1>Not Found</h1>")  # global search doesn't handle this gracefully :-(
    search = { "total_hits" => 0 }
  end
  total_hits = search["total_hits"] || 0
  if total_hits == 1 #&& !search['results'].empty? # for some reason, you get a hit of '1' but still have
    obj = ASUtils.json_parse(search["results"][0]["json"])
  elsif total_hits > 1
    if matches.length == 2
      match_ct = 0
      disam = matches[1] + DISAMB_STR
      disam_obj = nil
      search["results"].each do |result|
        # if we have a disambiguate result get it
        if result[matches[0]] == disam
          disam_obj = ASUtils.json_parse(result["json"])
        elsif result[matches[0]] == matches[1]
          match_ct += 1
          obj = ASUtils.json_parse(result["json"])
        end
      end
      # if we have more than one exact match, then return disam_obj if we have one, or bail!
      if match_ct > 1
        if disam_obj
          obj = disam_obj
          report.add_info(I18n.t("bulk_import.warn.disamuse", :which => matches[1], :name => disam)) if !report.nil?
        else
          raise BulkImportDisambigException.new(I18n.t("bulk_import.error.too_many"))
        end
      end
    else
      raise BulkImportDisambigException.new(I18n.t("bulk_import.error.too_many"))
    end
  end
  obj = JSONModel(sym).from_hash(obj) if !obj.nil?
  obj
end

#stored(hash, id, key) ⇒ Object

centralize the checking for an already-found object



93
94
95
# File 'backend/app/lib/bulk_import/handler.rb', line 93

def stored(hash, id, key)
  ret_obj = (id != "" && hash.fetch(id, nil)) || hash.fetch(key, nil)
end