Class: Dor::PublicXmlService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/public_xml_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ PublicXmlService

Returns a new instance of PublicXmlService.



5
6
7
# File 'lib/dor/services/public_xml_service.rb', line 5

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/dor/services/public_xml_service.rb', line 3

def object
  @object
end

Instance Method Details

#public_content_metadataNokogiri::XML::Document

Returns sanitized for public consumption.

Returns:



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
# File 'lib/dor/services/public_xml_service.rb', line 64

def 
  return Nokogiri::XML::Document.new unless object.datastreams['contentMetadata']
  @public_content_metadata ||= begin
    result = object.datastreams['contentMetadata'].ng_xml.clone

    # remove any resources or attributes that are not destined for the public XML
    result.xpath('/contentMetadata/resource[not(file[(@deliver="yes" or @publish="yes")]|externalFile)]').each(&:remove)
    result.xpath('/contentMetadata/resource/file[not(@deliver="yes" or @publish="yes")]'                ).each(&:remove)
    result.xpath('/contentMetadata/resource/file').xpath('@preserve|@shelve|@publish|@deliver'          ).each(&:remove)
    result.xpath('/contentMetadata/resource/file/checksum'                                              ).each(&:remove)

    # support for dereferencing links via externalFile element(s) to the source (child) item - see JUMBO-19
    result.xpath('/contentMetadata/resource/externalFile').each do |externalFile|
      # enforce pre-conditions that resourceId, objectId, fileId are required
      src_resource_id = externalFile['resourceId']
      src_druid = externalFile['objectId']
      src_file_id = externalFile['fileId']
      fail ArgumentError, "Malformed externalFile data: #{externalFile.inspect}" if [src_resource_id, src_file_id, src_druid].map(&:blank?).any?

      # grab source item
      src_item = Dor.find(src_druid)

      # locate and extract the resourceId/fileId elements
      doc = src_item.datastreams['contentMetadata'].ng_xml
      src_resource = doc.at_xpath("//resource[@id=\"#{src_resource_id}\"]")
      src_file = src_resource.at_xpath("file[@id=\"#{src_file_id}\"]")
      src_image_data = src_file.at_xpath('imageData')

      # always use title regardless of whether a child label is present
      src_label = doc.create_element('label')
      src_label.content = src_item.full_title

      # add the extracted label and imageData
      externalFile.add_previous_sibling(src_label)
      externalFile << src_image_data unless src_image_data.nil?
    end

    result
  end
end

#public_identity_metadataObject



55
56
57
58
59
60
61
# File 'lib/dor/services/public_xml_service.rb', line 55

def 
  @public_identity_metadata ||= begin
    im = object.datastreams['identityMetadata'].ng_xml.clone
    im.search('//release').each(&:remove) # remove any <release> tags from public xml which have full history
    im
  end
end

#public_relationshipsObject



47
48
49
# File 'lib/dor/services/public_xml_service.rb', line 47

def public_relationships
  @public_relationships ||= object.public_relationships.clone
end

#public_rights_metadataObject



51
52
53
# File 'lib/dor/services/public_xml_service.rb', line 51

def 
  @public_rights_metadata ||= object.datastreams['rightsMetadata'].ng_xml.clone
end

#release_xmlString

Generate XML structure for inclusion to Purl

Returns:

  • (String)

    The XML release node as a string, with ReleaseDigest as the root document



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dor/services/public_xml_service.rb', line 34

def release_xml
  @release_xml ||= begin
      builder = Nokogiri::XML::Builder.new do |xml|
      xml.releaseData {
        object.released_for.each do |project, released_value|
          xml.release(released_value['release'], :to => project)
        end
      }
    end
    Nokogiri::XML(builder.to_xml)
  end
end

#to_xmlObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dor/services/public_xml_service.rb', line 9

def to_xml
  pub = Nokogiri::XML('<publicObject/>').root
  pub['id'] = object.pid
  pub['published'] = Time.now.utc.xmlschema
  pub['publishVersion'] = 'dor-services/' + Dor::VERSION

  pub.add_child(.root) # add in modified identityMetadata datastream
  pub.add_child(.root) if .xpath('//resource').any?
  pub.add_child(.root)

  pub.add_child(public_relationships.root) unless public_relationships.nil? # TODO: Should never be nil in practice; working around an ActiveFedora quirk for testing
  pub.add_child(object.generate_dublin_core.root)
  pub.add_child(Nokogiri::XML(object.generate_public_desc_md).root)
  pub.add_child(release_xml.root) unless release_xml.xpath('//release').children.size == 0 # If there are no release_tags, this prevents an empty <releaseData/> from being added
  # Note we cannot base this on if an individual object has release tags or not, because the collection may cause one to be generated for an item,
  # so we need to calculate it and then look at the final result.s
  pub.add_child(Nokogiri("<thumb>#{object.thumb}</thumb>").root) unless object.thumb.nil?

  new_pub = Nokogiri::XML(pub.to_xml) { |x| x.noblanks }
  new_pub.encoding = 'UTF-8'
  new_pub.to_xml
end