Module: RepresentativeFileVersion::ClassMethods

Defined in:
backend/app/model/mixins/representative_file_version.rb

Instance Method Summary collapse

Instance Method Details

#sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'backend/app/model/mixins/representative_file_version.rb', line 40

def sequel_to_jsonmodel(objs, opts = {})
  jsons = super

  jsons.each do |json|
    case self.name
    when "Resource", "Accession", "ArchivalObject"
      if representative_instance = json.instances.select {|i| i["is_representative"] == true && i["instance_type"] == "digital_object" }.first
        id = JSONModel(:digital_object).id_for(representative_instance["digital_object"]["ref"])
        digital_object = DigitalObject.to_jsonmodel(id, opts)
        if digital_object["representative_file_version"]
          json["representative_file_version"] = digital_object["representative_file_version"]
                                                  .merge("derived_from" => digital_object.uri)
                                                  .reject { |k, _| k == "link_uri" }
        else
          digital_object_component_set = DigitalObjectComponent
                                           .left_join(:file_version, digital_object_component_id: :digital_object_component__id)
                                           .filter(root_record_id: digital_object.id)
                                           .select(Sequel.as(:digital_object_component__id, :digital_object_component_id),
                                                   Sequel.as(:digital_object_component__parent_id, :digital_object_component_parent_id),
                                                   Sequel.as(:digital_object_component__position, :digital_object_component_position),
                                                   Sequel.as(:file_version__id, :file_version_id),
                                                   Sequel.as(:file_version__publish, :file_version_publish),
                                                   Sequel.as(:file_version__use_statement_id, :file_version_use_statement_id),
                                                   Sequel.as(:file_version__is_representative, :file_version_is_representative))
                                           .order(:digital_object_component_position)

          if (digital_object_component_id = find_representative_in_digital_object_tree(digital_object_component_set))
            digital_object_component = DigitalObjectComponent.to_jsonmodel(digital_object_component_id, opts)
            if digital_object_component['representative_file_version']
              json['representative_file_version'] = digital_object_component['representative_file_version'].merge("derived_from" => digital_object_component.uri)
            end
          end
        end
      end
    when "DigitalObject", "DigitalObjectComponent"
      fvs = json[:file_versions]
      fv_pairs = []
      last_pair = nil
      fvs.each do |fv|
        # all done if:
        break if fv_pairs[0] && fv_pairs[0].size == 2
        # ANW-1721 - if the fv in last_pair ends up being selected, this will be its link target
        if (last_pair && last_pair.size == 1)
          last_pair << fv
        end
        # ANW-1209 REQ-3
        if fv_pairs[0].nil? && fv["publish"] && fv["is_representative"]
          last_pair = fv_pairs[0] = [fv]
        # ANW-1209 REQ-3.1
        elsif fv_pairs[1].nil? && fv["publish"] && fv["use_statement"] == 'image-thumbnail'
          last_pair = fv_pairs[1] = [fv]
        # The older logic for selecting an image to show via `process_file_versions`
        # in public/app/controllers/concerns/result_info.rb
        elsif fv_pairs[2].nil? && fv["publish"] \
          && fv["file_uri"].start_with?('http') && fv["xlink_show_attribute"] == 'embed'

          last_pair = fv_pairs[2] = [fv]
        end
      end
      # now we select the best candidate pair based on order
      if (fvp = fv_pairs.compact.first)
        json["representative_file_version"] = fvp[0]
        if fvp[1] && fvp[1]["publish"]
          json["representative_file_version"]["link_uri"] = fvp[1]["file_uri"]
        end
      end

    end

    # if we still don't have a representative and are dealing with a Resource
    # that has a tree under it, we will now look in the tree
    if json["representative_file_version"].nil? && self.name == "Resource"

      archival_object_set = ArchivalObject
                              .left_join(:instance, :archival_object_id => :archival_object__id)
                              .left_join(:instance_do_link_rlshp, :instance_id => :instance__id)
                              .left_join(:digital_object, :id => :instance_do_link_rlshp__digital_object_id)
                              .filter(archival_object__root_record_id: json.id)
                              .filter(archival_object__publish: true)
                              .select(Sequel.as(:archival_object__id, :archival_object_id),
                                      Sequel.as(:archival_object__position, :archival_object_position),
                                      Sequel.as(:archival_object__parent_id, :archival_object_parent_id),
                                      Sequel.as(:digital_object__id, :digital_object_id),
                                      Sequel.as(:instance__is_representative, :digital_object_is_representative))
                              # .order(:archival_object_position)

      if (digital_object_id = find_representative_in_resource_tree(archival_object_set))
        digital_object = DigitalObject.to_jsonmodel(digital_object_id, opts)
        if digital_object["representative_file_version"]
          json["representative_file_version"] = digital_object['representative_file_version'].merge("derived_from" => digital_object.uri)
        else
          digital_object_component_set = DigitalObjectComponent
                                           .left_join(:file_version, digital_object_component_id: :digital_object_component__id)
                                           .filter(root_record_id: digital_object.id)
                                           .select(Sequel.as(:digital_object_component__id, :digital_object_component_id),
                                                   Sequel.as(:digital_object_component__parent_id, :digital_object_component_parent_id),
                                                   Sequel.as(:digital_object_component__position, :digital_object_component_position),
                                                   Sequel.as(:file_version__id, :file_version_id),
                                                   Sequel.as(:file_version__publish, :file_version_publish),
                                                   Sequel.as(:file_version__use_statement_id, :file_version_use_statement_id),
                                                   Sequel.as(:file_version__is_representative, :file_version_is_representative))
                                           .order(:digital_object_component_position)

          if (digital_object_component_id = find_representative_in_digital_object_tree(digital_object_component_set))
            digital_object_component = DigitalObjectComponent.to_jsonmodel(digital_object_component_id, opts)
            if digital_object_component['representative_file_version']
              json['representative_file_version'] = digital_object_component['representative_file_version'].merge("derived_from" => digital_object_component.uri)
            end
          end
        end
      end

    end
  end
  jsons
end