Class: Dor::DefaultObjectRightsDS

Inherits:
ActiveFedora::OmDatastream
  • Object
show all
Defined in:
lib/dor/datastreams/default_object_rights_ds.rb

Constant Summary collapse

HUMAN_XSLT =

Note that the XSL file was taken from the (apparently defunct) nokogiri-pretty project: github.com/tobym/nokogiri-pretty/blob/master/lib/indent.xsl The only modification made was to declare UTF-8 to be the encoding, instead of ISO-8859-1.

Nokogiri::XSLT(File.read(File.expand_path('human.xslt', __dir__)))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.xml_templateObject



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
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 57

def self.xml_template
  Nokogiri::XML::Builder.new do |xml|
    xml. do
      xml.access(type: 'discover') do
        xml.machine do
          xml.world
        end
      end
      xml.access(type: 'read') do
        xml.machine do
          xml.world
        end
      end
      xml.use do
        xml.human(type: 'useAndReproduction')
        xml.human(type: 'creativeCommons')
        xml.machine(type: 'creativeCommons', uri: '')
        xml.human(type: 'openDataCommons')
        xml.machine(type: 'openDataCommons', uri: '')
      end
      xml.copyright do
        xml.human
      end
    end
  end.doc
end

Instance Method Details

#contentObject



104
105
106
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 104

def content
  prettify(ng_xml).to_s
end


154
155
156
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 154

def copyright_statement
  copyright.first
end


158
159
160
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 158

def copyright_statement=(val)
  update_term!(:copyright, val.nil? ? '' : val)
end

#creative_commons_licenseObject



162
163
164
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 162

def creative_commons_license
  creative_commons.first
end

#creative_commons_license=(use_license_machine) ⇒ Object



199
200
201
202
203
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 199

def creative_commons_license=(use_license_machine)
  initialize_term!(:creative_commons)
  self.creative_commons = use_license_machine
  creative_commons.uri = CreativeCommonsLicenseService.property(use_license_machine).uri
end

#creative_commons_license_humanObject



166
167
168
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 166

def creative_commons_license_human
  creative_commons_human.first
end

#creative_commons_license_human=(use_license_human) ⇒ Object



205
206
207
208
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 205

def creative_commons_license_human=(use_license_human)
  initialize_term!(:creative_commons_human)
  self.creative_commons_human = use_license_human
end

#default_rightsString

RightsMetadataDS::RIGHTS_TYPE_CODES.keys (so this is essentially the inverse of RightsMetadataDS.upd_rights_xml_for_rights_type).

Returns:

  • (String)

    A description of the rights defined in the default object rights datastream. Can be one of



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 243

def default_rights
  xml = ng_xml
  machine_read_access = xml.search('//rightsMetadata/access[@type="read"]/machine')
  machine_discover_access = xml.search('//rightsMetadata/access[@type="discover"]/machine')

  machine_read_access_node = machine_read_access.length == 1 ? machine_read_access.first : nil
  machine_discover_access_node = machine_discover_access.length == 1 ? machine_discover_access.first : nil

  if machine_read_access_node && machine_read_access_node.search('./group[text()="Stanford" or text()="stanford"]').length == 1
    if machine_read_access_node.search('./group[@rule="no-download"]').length == 1
      'stanford-nd'
    else
      'stanford'
    end
  elsif machine_read_access_node && machine_read_access_node.search('./world').length == 1
    if machine_read_access_node.search('./world[@rule="no-download"]').length == 1
      'world-nd'
    else
      'world'
    end
  elsif machine_read_access_node && machine_read_access_node.search('./location[text()="spec"]').length == 1
    'loc:spec'
  elsif machine_read_access_node && machine_read_access_node.search('./location[text()="music"]').length == 1
    'loc:music'
  elsif machine_discover_access_node && machine_discover_access_node.search('./world').length == 1
    # if it's not stanford restricted, world readable, or location restricted, but it is world discoverable, it's "citation only"
    'none'
  elsif machine_discover_access_node && machine_discover_access_node.search('./none').length == 1
    # if it's not even discoverable, it's "dark"
    'dark'
  end
end

#default_rights=(rights) ⇒ Object

Set the rights in default object rights

Parameters:

  • rights (String)

    Stanford, World, Dark, or None

Raises:

  • (ArgumentError)


278
279
280
281
282
283
284
285
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 278

def default_rights=(rights)
  rights = rights.downcase
  raise(ArgumentError, "Unrecognized rights value '#{rights}'") unless RightsMetadataDS.valid_rights_type? rights

  rights_xml = ng_xml
  ng_xml_will_change!
  RightsMetadataDS.upd_rights_xml_for_rights_type(rights_xml, rights)
end

#initialize_term!(term) ⇒ Object

Ensures that the template is present for the given term



85
86
87
88
89
90
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 85

def initialize_term!(term)
  return unless find_by_terms(term).length < 1

  ng_xml_will_change!
  add_child_node(ng_xml.root, term)
end

#normalize!Object

Purge the XML of any empty or duplicate elements – keeps <rightsMetadata> clean



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
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 109

def normalize!
  ng_xml_will_change!
  doc = ng_xml
  if doc.xpath('/rightsMetadata/use').length > 1
    # <use> node needs consolidation
    nodeset = doc.xpath('/rightsMetadata/use')
    nodeset[1..-1].each do |node|
      node.children.each do |child|
        nodeset[0] << child # copy over to first <use> element
      end
      node.remove
    end
    raise unless doc.xpath('/rightsMetadata/use').length == 1
  end

  # Call out to the general purpose XML normalization service
  Stanford::Mods::Normalizer.new.tap do |norm|
    norm.remove_empty_attributes(doc.root)
    # cleanup ordering is important here
    doc.xpath('//machine/text()').each { |node| node.content = node.content.strip }
    doc.xpath('//human')
       .tap { |node_set| norm.clean_linefeeds(node_set) }
       .each do |node|
      norm.trim_text(node)
      norm.remove_empty_nodes(node)
    end
    doc.xpath('/rightsMetadata/copyright').each { |node| norm.remove_empty_nodes(node) }
    doc.xpath('/rightsMetadata/use').each { |node| norm.remove_empty_nodes(node) }
  end
  self.content = prettify(doc)
end

#open_data_commons_licenseObject



170
171
172
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 170

def open_data_commons_license
  open_data_commons.first
end

#open_data_commons_license=(use_license_machine) ⇒ Object



210
211
212
213
214
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 210

def open_data_commons_license=(use_license_machine)
  initialize_term!(:open_data_commons)
  self.open_data_commons = use_license_machine
  open_data_commons.uri = OpenDataLicenseService.property(use_license_machine).uri
end

#open_data_commons_license_humanObject



174
175
176
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 174

def open_data_commons_license_human
  open_data_commons_human.first
end

#open_data_commons_license_human=(use_license_human) ⇒ Object



216
217
218
219
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 216

def open_data_commons_license_human=(use_license_human)
  initialize_term!(:open_data_commons_human)
  self.open_data_commons_human = use_license_human
end

#prefixObject

maintain AF < 8 indexing behavior



288
289
290
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 288

def prefix
  ''
end

#prettify(xml_doc) ⇒ Object

Returns a nicely indented XML document.



142
143
144
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 142

def prettify(xml_doc)
  HUMAN_XSLT.apply_to(xml_doc)
end

#update_term!(term, val) ⇒ Object

Assigns the defaultObjectRights object’s term with the given value. Supports setting value to nil



93
94
95
96
97
98
99
100
101
102
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 93

def update_term!(term, val)
  ng_xml_will_change!
  if val.blank?
    update_values([term] => nil)
  else
    initialize_term! term
    update_values([term] => val)
  end
  normalize!
end

#use_licenseObject



178
179
180
181
182
183
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 178

def use_license
  return creative_commons_license unless creative_commons_license.blank?
  return open_data_commons_license unless open_data_commons_license.blank?

  nil
end

#use_license=(use_license_machine) ⇒ Object

If set to ‘:none` then Use License is removed

Parameters:

  • use_license_machine (String|Symbol)

    The machine code for the desired Use License



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 223

def use_license=(use_license_machine)
  if use_license_machine.blank? || use_license_machine == :none
    # delete use license by directly removing the XML used to define the use license
    update_term!(:creative_commons, ' ')
    update_term!(:creative_commons_human, ' ')
    update_term!(:open_data_commons, ' ')
    update_term!(:open_data_commons_human, ' ')
  elsif CreativeCommonsLicenseService.key? use_license_machine
    self.creative_commons_license = use_license_machine
    self.creative_commons_license_human = CreativeCommonsLicenseService.property(use_license_machine).label
  elsif OpenDataLicenseService.key? use_license_machine
    self.open_data_commons_license = use_license_machine
    self.open_data_commons_license_human = OpenDataLicenseService.property(use_license_machine).label
  else
    raise ArgumentError, "'#{use_license_machine}' is not a valid license code"
  end
end

#use_license_humanObject



192
193
194
195
196
197
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 192

def use_license_human
  return creative_commons_license_human unless creative_commons_license_human.blank?
  return open_data_commons_license_human unless open_data_commons_license_human.blank?

  nil
end

#use_license_uriObject



185
186
187
188
189
190
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 185

def use_license_uri
  return creative_commons.uri.first unless creative_commons.uri.blank?
  return open_data_commons.uri.first unless open_data_commons.uri.blank?

  nil
end

#use_statementObject



146
147
148
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 146

def use_statement
  super.first
end

#use_statement=(val) ⇒ Object



150
151
152
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 150

def use_statement=(val)
  update_term!(:use_statement, val.nil? ? '' : val)
end