Class: BulkOps::Relationship

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/bulk_ops/relationship.rb

Constant Summary collapse

RELATIONSHIP_FIELDS =
['parent','child','order','next','collection']

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Relationship

Returns a new instance of Relationship.



8
9
10
11
12
13
14
# File 'lib/bulk_ops/relationship.rb', line 8

def initialize *args
  super *args
  
  # Attempt to resolve the relationship immediately
  # which might work in the case of updates
#    resolve!
end

Instance Method Details

#findObjectObject



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
# File 'lib/bulk_ops/relationship.rb', line 16

def findObject
  case (identifier_type || "").downcase
  when "id"
    begin
    object = ActiveFedora::Base.find(object_identifier)
    rescue Ldp::Gone
      return false
    end
    return object || false
  when "title"
    #          TODO clean up solr query and add work type to it
    query = "{!field f=title_tesim}#{object_identifier}"
    objects = ActiveFedora::SolrService.instance.conn.get(ActiveFedora::SolrService.select_path,
                                                          params: { fq: query, rows: 100})["response"]["docs"]
    if objects.present?
      return ActiveFedora::Base.find(objects.first["id"])
    elsif (relationship_type || "").downcase == "collection"
      return Collection.create(title: [object_identifier])
    else
      return false
    end
  when "identifier"
    query = "{!field f=identifier_tesim}#{object_identifier}"
    objects = ActiveFedora::SolrService.instance.conn.get(ActiveFedora::SolrService.select_path,params: { fq: query, rows: 100})["response"]["docs"]
    return false if objects.blank?
    return ActiveFedora::Base.find(objects.first["id"])
  when "row"
    object_proxy = BulkOps::WorkProxy.find_by(operation_id: work_proxy.operation_id, 
                                              row_number: (object_identifier.to_i))
    ActiveFedora::Base.find(object_proxy.work_id)
  when "proxy_id"
    return false unless (proxy = BulkOps::WorkProxy.find(proxy_id))
    return false unless proxy.work_id.present?
    ActiveFedora::Base.find(proxy.work_id)
  end
end

#implement_relationship!(type, subject, object) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bulk_ops/relationship.rb', line 85

def implement_relationship!(type,subject,object)
  case (type || "").downcase
  when "parent"
    unless object.member_ids.include? subject.id
      object.reload
      object.save
      object.ordered_members = insert_among_children(object, subject)
      object.save
    end
  when "child"
    #CAVEAT ordering not fully implemented in this case
    unless subject.member_ids.include? object.id
      subject.ordered_members << object
      subject.save
    end
  when "order"
    #TODO - implement this - related to ordering of filesets
    
  end
  update(status: "complete")
end

#insert_among_children(object, new_member) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bulk_ops/relationship.rb', line 61

def insert_among_children(object,new_member)
  return nil unless ["parent"].include?((relationship_type || "").downcase)
  prev_sib_id = previous_sibling
  # This is the id of the WorkProxy associate with the most recent sibling work
  # that might be fully ingested. If is it not fully ingested, we will move on 
  # to the preceding sibling.
  while prev_sib_id.present? 
    prev_sib_proxy = BulkOps::WorkProxy.find(prev_sib_id)
    # Check if the previous sibling is fully ingested 
    # and get its index among its siblings (if it has been successfully attached to the parent)
    prev_sib_index = object.ordered_member_ids.index(prev_sib_proxy.work_id) if prev_sib_proxy.work_id.present?
    # Insert the new member among its siblings if we found the right place
    return object.ordered_members.to_a.insert(prev_sib_index+1, new_member) if prev_sib_index.present?
    # Otherwise, pull up the sibling's relationship field to check if it sibling has a sibling before it
    sib_relationship = prev_sib_proxy.relationships.find{|rel| rel.findObject.id == object.id }
    # If we can't find an ingested sibling among the ordered members,
    # break this loop and make this work the first member.
    break unless sib_relationship.present?
    prev_sib_id = sib_relationship.previous_sibling
  end
  #If we never found an existing previous sibling already attached, put this one at the front
  return  [new_member]+object.ordered_members.to_a
end

#resolve!Object



53
54
55
56
57
58
59
# File 'lib/bulk_ops/relationship.rb', line 53

def resolve!
  unless subject = work_proxy.work and object = self.findObject
    wait!
    return
  end
  implement_relationship! relationship_type, subject, object
end