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



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
# File 'backend/app/model/mixins/representative_file_version.rb', line 13

def sequel_to_jsonmodel(objs, opts = {})
  jsons = super
  return jsons unless AppConfig[:enable_representative_file_version]
  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"])
        json["representative_file_version"] = DigitalObject.to_jsonmodel(id)["representative_file_version"]
      end
      if json["representative_file_version"].nil? && representative_instance
        DigitalObjectComponent.filter(:root_record_id => id).select(:id).each do |row|
          # unlikely to scale when the tree is large
          if rep_fv = DigitalObjectComponent.to_jsonmodel(row[:id])["representative_file_version"]
            json["representative_file_version"] = rep_fv
            break
          end
        end
      end

    when "DigitalObject", "DigitalObjectComponent"
      # Compute representative_file_version property, ANW-1493
      fvs = json[:file_versions]

      published_representative_fv = fvs.select { |fv| (fv["publish"] == true || fv["publish"] == 1) && (fv["is_representative"] == true || fv["is_representative"] == 1) }

      published_image_thumbnail_fvs = fvs.select { |fv| (fv["publish"] == true || fv["publish"] == 1) && fv["is_representative"] != true && fv["is_representative"] != 1 && fv["use_statement"] == 'image-thumbnail' }

      published_fvs = fvs.select { |fv| (fv["publish"] == true || fv["publish"] == 1) && fv["is_representative"] != true && fv["is_representative"] != 1 && fv["use_statement"] != 'image-thumbnail' }

      if published_representative_fv.count > 0
        json["representative_file_version"] = published_representative_fv.first
      elsif published_image_thumbnail_fvs.count > 0
        json["representative_file_version"] = published_image_thumbnail_fvs.first
      elsif published_fvs.count > 0
        json["representative_file_version"] = published_fvs.first
      end
    end

    # this could get very ugly
    if json["representative_file_version"].nil? && self.name == "Resource"
      ArchivalObject.filter(:root_record_id => json.id).select(:id).each do |row|
        if rep_fv = ArchivalObject.to_jsonmodel(row[:id])["representative_file_version"]
          json["representative_file_version"] = rep_fv
          break
        end
      end
    end
  end

  jsons
end