Module: Cul::Hydra::StructMetadataHelperBehavior

Defined in:
app/helpers/cul/hydra/struct_metadata_helper_behavior.rb

Constant Summary collapse

METS_NS =
{'mets' => 'http://www.loc.gov/METS/'}

Instance Method Summary collapse

Instance Method Details

#html_class_for_filename(filename) ⇒ Object



16
17
18
19
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 16

def html_class_for_filename(filename)
  mime = mime_for_name(filename) || 'application/octet-stream'
  mime.sub(/\//,'_')
end

#mime_for_name(filename) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 3

def mime_for_name(filename)
  ext = File.extname(filename).downcase
  mt = MIME::Types.type_for(ext)
  if mt.is_a? Array
    mt = mt.first
  end
  unless mt.nil?
    return mt.content_type
  else
    return nil
  end
end

#struct_metadata(doc) ⇒ Object



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
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 60

def (doc)
  members = get_members(doc)

  node_map = {}
  ds = (doc)
  ds.divs_with_attribute(true,'CONTENTIDS').each do |node|
    node_map[node['CONTENTIDS']] = node
  end
  
  map = {}
  Rails.logger.warn("No members for doc #{doc[:id]}") unless members.size > 0
  members.each do |member|
    ids = ((member[:identifier_ssim] || []) + (member[:dc_identifier_ssim] || []))
    ids.uniq!
    ids.delete(member[:id])
    node_map.each do |cid, node|
      if ids.include? cid
        node['pid'] = member[:id]
        map[member[:id]] = member
        Rails.logger.info("Mapped child node in structMap: '#{member[:id]}'' -> '#{cid}'")
        break
      end
    end
    if map[member[:id]].nil?
      Rails.logger.warn("Unmapped child node in structMap: #{ids.inspect}")
    end
  end
  [ds, map]
end

#struct_metadata_ds(doc) ⇒ Object



21
22
23
24
25
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 21

def (doc)
  pid = base_id_for(doc)
  xml = rubydora.datastream_dissemination(:pid=>pid, :dsid=>'structMetadata')
  Cul::Hydra::Datastreams::StructMetadata.from_xml(xml)
end

#struct_metadata_file_system(doc) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 29

def (doc)
  ds = (doc)
  doc = Nokogiri::XML(ds.content)
  node = doc.xpath('mets:structMap',METS_NS).first
  (:ul, nil, class: 'file-system') do
    children_content = node.xpath('mets:div',METS_NS).reduce('') do |content, child|
      content << (child)
    end
    children_content.html_safe
  end
end

#struct_metadata_file_system_cache_key(doc) ⇒ Object



26
27
28
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 26

def (doc)
  File.join(doc['id'],'structMetadata','fs-html',doc['system_modified_dtsi'])
end

#struct_metadata_node(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/cul/hydra/struct_metadata_helper_behavior.rb', line 40

def (node)
  children = node.xpath('mets:div',METS_NS)
  if children.length == 0
    # file
    (:li,nil, class: ['fs-file',html_class_for_filename(node['LABEL'])]) do
      (:a, node['LABEL'], href: '#', 'data-id'=>node['CONTENTIDS'])
    end
  else
    # folder
    (:li, nil, class: 'fs-directory') do
      (:a, node['LABEL'], href: '#') +
      (:ul, nil, class: 'fs-children') do
        children_content = children.reduce('') do |content, child|
          content << (child)
        end
        children_content.html_safe
      end
    end
  end
end