Module: Dor::Editable

Extended by:
ActiveSupport::Concern
Included in:
AdminPolicyObject
Defined in:
lib/dor/models/concerns/editable.rb

Overview

This is basically used just by APOs. Arguably “editable” is the wrong name.

Constant Summary collapse

CREATIVE_COMMONS_USE_LICENSES =

these hashes map short (“machine”) license codes to their corresponding URIs and human readable titles. they also allow for deprecated entries (via optional :deprecation_warning). clients that use these maps are advised to only display undeprecated entries, except where a deprecated entry is already in use by an object. e.g., an APO that already specifies “by_sa” for its default license code could continue displaying that in a list of license options for editing, preferably with the deprecation warning. but other deprecated entries would be omitted in such a selectbox. TODO: seems like Editable is not the most semantically appropriate place for these mappings? though they’re used by methods that live in Editable. TODO: need some way to do versioning. for instance, what happens when a new version of an existing license comes out, since it will presumably use the same license code, but a different title and URI?

{
  'by' => { :human_readable => 'Attribution 3.0 Unported',
            :uri => 'https://creativecommons.org/licenses/by/3.0/' },
  'by-sa' => { :human_readable => 'Attribution Share Alike 3.0 Unported',
               :uri => 'https://creativecommons.org/licenses/by-sa/3.0/' },
  'by_sa' => { :human_readable => 'Attribution Share Alike 3.0 Unported',
               :uri => 'https://creativecommons.org/licenses/by-sa/3.0/',
               :deprecation_warning => 'license code "by_sa" was a typo in argo, prefer "by-sa"' },
  'by-nd' => { :human_readable => 'Attribution No Derivatives 3.0 Unported',
               :uri => 'https://creativecommons.org/licenses/by-nd/3.0/' },
  'by-nc' => { :human_readable => 'Attribution Non-Commercial 3.0 Unported',
               :uri => 'https://creativecommons.org/licenses/by-nc/3.0/' },
  'by-nc-sa' => { :human_readable => 'Attribution Non-Commercial Share Alike 3.0 Unported',
                  :uri => 'https://creativecommons.org/licenses/by-nc-sa/3.0/' },
  'by-nc-nd' => { :human_readable => 'Attribution Non-Commercial, No Derivatives 3.0 Unported',
                  :uri => 'https://creativecommons.org/licenses/by-nc-nd/3.0/' },
  'pdm' => { :human_readable => 'Public Domain Mark 1.0',
             :uri => 'https://creativecommons.org/publicdomain/mark/1.0/' }
}.freeze
OPEN_DATA_COMMONS_USE_LICENSES =
{
  'pddl' => { :human_readable => 'Open Data Commons Public Domain Dedication and License 1.0',
              :uri => 'http://opendatacommons.org/licenses/pddl/1.0/' },
  'odc-by' => { :human_readable => 'Open Data Commons Attribution License 1.0',
                :uri => 'http://opendatacommons.org/licenses/by/1.0/' },
  'odc-odbl' => { :human_readable => 'Open Data Commons Open Database License 1.0',
                  :uri => 'http://opendatacommons.org/licenses/odbl/1.0/' }
}.freeze

Instance Method Summary collapse

Instance Method Details

#add_default_collection(val) ⇒ Object

Add a collection to the listing of collections for items governed by this apo.

Parameters:

  • val (String)

    pid of the collection, ex. druid:ab123cd4567



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dor/models/concerns/editable.rb', line 103

def add_default_collection(val)
  xml = .ng_xml
  .ng_xml_will_change!
  reg = xml.search('//administrativeMetadata/registration').first
  unless reg
    reg = Nokogiri::XML::Node.new('registration', xml)
    xml.search('/administrativeMetadata').first.add_child(reg)
  end
  node = Nokogiri::XML::Node.new('collection', xml)
  node['id'] = val
  reg.add_child(node)
end

#add_roleplayer(role, entity, type = :workgroup) ⇒ Object

Adds a person or group to a role in the APO role metadata datastream

Parameters:

  • role (String)

    the role the group or person will be filed under, ex. dor-apo-manager

  • entity (String)

    the name of the person or group, ex dlss:developers or sunetid:someone

  • type (Symbol) (defaults to: :workgroup)

    :workgroup for a group or :person for a person



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dor/models/concerns/editable.rb', line 55

def add_roleplayer(role, entity, type = :workgroup)
  xml = .ng_xml
  .ng_xml_will_change!
  group = type == :workgroup ? 'group' : 'person'
  nodes = xml.search('/roleMetadata/role[@type=\'' + role + '\']')
  if nodes.length > 0
    group_node = Nokogiri::XML::Node.new(group, xml)
    id_node = Nokogiri::XML::Node.new('identifier', xml)
    group_node.add_child(id_node)
    id_node.content = entity
    id_node['type'] = type.to_s
    nodes.first.add_child(group_node)
  else
    node = Nokogiri::XML::Node.new('role', xml)
    node['type'] = role
    group_node = Nokogiri::XML::Node.new(group, xml)
    node.add_child group_node
    id_node = Nokogiri::XML::Node.new('identifier', xml)
    group_node.add_child(id_node)
    id_node.content = entity
    id_node['type'] = type.to_s
    xml.search('/roleMetadata').first.add_child(node)
  end
end

#agreementObject



346
347
348
# File 'lib/dor/models/concerns/editable.rb', line 346

def agreement
  agreement_object ? agreement_object.pid : ''
end

#agreement=(val) ⇒ Object



350
351
352
353
354
# File 'lib/dor/models/concerns/editable.rb', line 350

def agreement=(val)
  fail ArgumentError, 'agreement must have a valid druid' if val.blank?

  self.agreement_object = Dor.find val.to_s, :cast => true
end


155
156
157
# File 'lib/dor/models/concerns/editable.rb', line 155

def copyright_statement
  defaultObjectRights.copyright.first
end


159
160
161
# File 'lib/dor/models/concerns/editable.rb', line 159

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

#creative_commons_licenseObject



163
164
165
# File 'lib/dor/models/concerns/editable.rb', line 163

def creative_commons_license
  defaultObjectRights.creative_commons.first
end

#creative_commons_license=(use_license_machine) ⇒ Object



200
201
202
203
204
# File 'lib/dor/models/concerns/editable.rb', line 200

def creative_commons_license=(use_license_machine)
  defaultObjectRights.initialize_term!(:creative_commons)
  defaultObjectRights.creative_commons = use_license_machine
  defaultObjectRights.creative_commons.uri = CREATIVE_COMMONS_USE_LICENSES[use_license_machine][:uri]
end

#creative_commons_license_humanObject



167
168
169
# File 'lib/dor/models/concerns/editable.rb', line 167

def creative_commons_license_human
  defaultObjectRights.creative_commons_human.first
end

#creative_commons_license_human=(use_license_human) ⇒ Object



206
207
208
209
# File 'lib/dor/models/concerns/editable.rb', line 206

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

#default_collectionsArray

get all collections listed for this APO, used during registration

Returns:

  • (Array)

    array of pids



97
98
99
# File 'lib/dor/models/concerns/editable.rb', line 97

def default_collections
  .term_values(:registration, :default_collection)
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



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
275
276
277
278
# File 'lib/dor/models/concerns/editable.rb', line 244

def default_rights
  xml = defaultObjectRights.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'
  else
    # if none of the above, the rights xml structure is unsupported/unintelligible
    nil
  end
end

#default_rights=(rights) ⇒ Object

Set the rights in default object rights

Parameters:

  • rights (String)

    Stanford, World, Dark, or None

Raises:

  • (ArgumentError)


282
283
284
285
286
287
288
289
# File 'lib/dor/models/concerns/editable.rb', line 282

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

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

#default_workflow=(wf) ⇒ Object

set a single default workflow

Parameters:

  • wf (String)

    the name of the workflow, ex. ‘digitizationWF’



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/dor/models/concerns/editable.rb', line 325

def default_workflow=(wf)
  fail ArgumentError, 'Must have a valid workflow for default' if wf.blank?

  xml = .ng_xml
  .ng_xml_will_change!
  nodes = xml.search('//registration/workflow')
  if nodes.first
    nodes.first['id'] = wf
  else
    nodes = xml.search('//registration')
    unless nodes.first
      reg_node = Nokogiri::XML::Node.new('registration', xml)
      xml.root.add_child(reg_node)
    end
    nodes = xml.search('//registration')
    wf_node = Nokogiri::XML::Node.new('workflow', xml)
    wf_node['id'] = wf
    nodes.first.add_child(wf_node)
  end
end

#default_workflowsArray

List of default workflows, used to provide choices at registration

Returns:

  • (Array)

    and array of pids, ex [‘druid:ab123cd4567’]



319
320
321
# File 'lib/dor/models/concerns/editable.rb', line 319

def default_workflows
  .term_values(:registration, :workflow_id)
end

#desc_metadata_formatObject



291
292
293
# File 'lib/dor/models/concerns/editable.rb', line 291

def 
  ..first
end

#desc_metadata_format=(format) ⇒ Object



295
296
297
298
299
300
301
302
# File 'lib/dor/models/concerns/editable.rb', line 295

def (format)
  # create the node if it isnt there already
  unless ..first
    .ng_xml_will_change!
    .add_child_node(.ng_xml.root, :metadata_format)
  end
  .update_values({ [:metadata_format] => format })
end

#desc_metadata_sourceObject



304
305
306
# File 'lib/dor/models/concerns/editable.rb', line 304

def 
  ..first
end

#desc_metadata_source=(source) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/dor/models/concerns/editable.rb', line 308

def (source)
  # create the node if it isnt there already
  unless ..first
    .ng_xml_will_change!
    .add_child_node(.ng_xml.root, :metadata_source)
  end
  .update_values({ [:metadata_source] => format })
end

#metadata_sourceObject



135
136
137
# File 'lib/dor/models/concerns/editable.rb', line 135

def 
  ..first
end

#metadata_source=(val) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/dor/models/concerns/editable.rb', line 139

def (val)
  if ..nil?
    .ng_xml_will_change!
    .add_child_node(, :descMetadata)
  end
  .update_values({ [:descMetadata, :source] => val })
end

#mods_titleObject



87
88
89
# File 'lib/dor/models/concerns/editable.rb', line 87

def mods_title
  .term_values(:title_info, :main_title).first
end

#mods_title=(val) ⇒ Object



91
92
93
# File 'lib/dor/models/concerns/editable.rb', line 91

def mods_title=(val)
  .update_values({ [:title_info, :main_title] => val })
end

#open_data_commons_licenseObject



171
172
173
# File 'lib/dor/models/concerns/editable.rb', line 171

def open_data_commons_license
  defaultObjectRights.open_data_commons.first
end

#open_data_commons_license=(use_license_machine) ⇒ Object



211
212
213
214
215
# File 'lib/dor/models/concerns/editable.rb', line 211

def open_data_commons_license=(use_license_machine)
  defaultObjectRights.initialize_term!(:open_data_commons)
  defaultObjectRights.open_data_commons = use_license_machine
  defaultObjectRights.open_data_commons.uri = OPEN_DATA_COMMONS_USE_LICENSES[use_license_machine][:uri]
end

#open_data_commons_license_humanObject



175
176
177
# File 'lib/dor/models/concerns/editable.rb', line 175

def open_data_commons_license_human
  defaultObjectRights.open_data_commons_human.first
end

#open_data_commons_license_human=(use_license_human) ⇒ Object



217
218
219
220
# File 'lib/dor/models/concerns/editable.rb', line 217

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

#purge_rolesObject

remove all people groups and roles from the APO role metadata datastream



81
82
83
84
85
# File 'lib/dor/models/concerns/editable.rb', line 81

def purge_roles
  .ng_xml.search('/roleMetadata/role').each do |node|
    node.remove
  end
end

#remove_default_collection(val) ⇒ Object



116
117
118
119
120
# File 'lib/dor/models/concerns/editable.rb', line 116

def remove_default_collection(val)
  xml = .ng_xml
  .ng_xml_will_change!
  xml.search('//administrativeMetadata/registration/collection[@id=\'' + val + '\']').remove
end

#rolesHash

Get all roles defined in the role metadata, and the people or groups in those roles. Groups are prefixed with ‘workgroup:’

Returns:

  • (Hash)

    role => [‘person’,‘group’] ex. {“dor-apo-manager” => [“workgroup:dlss:developers”, “sunetid:lmcrae”]



124
125
126
127
128
129
130
131
132
133
# File 'lib/dor/models/concerns/editable.rb', line 124

def roles
  roles = {}
  .ng_xml.search('/roleMetadata/role').each do |role|
    roles[role['type']] = []
    role.search('identifier').each do |entity|
      roles[role['type']] << entity['type'] + ':' + entity.text
    end
  end
  roles
end

#use_licenseObject



179
180
181
182
183
184
# File 'lib/dor/models/concerns/editable.rb', line 179

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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/dor/models/concerns/editable.rb', line 224

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
    defaultObjectRights.update_term!(:creative_commons, ' ')
    defaultObjectRights.update_term!(:creative_commons_human, ' ')
    defaultObjectRights.update_term!(:open_data_commons, ' ')
    defaultObjectRights.update_term!(:open_data_commons_human, ' ')
  elsif CREATIVE_COMMONS_USE_LICENSES.include? use_license_machine
    self.creative_commons_license = use_license_machine
    self.creative_commons_license_human = CREATIVE_COMMONS_USE_LICENSES[use_license_machine][:human_readable]
  elsif OPEN_DATA_COMMONS_USE_LICENSES.include? use_license_machine
    self.open_data_commons_license = use_license_machine
    self.open_data_commons_license_human = OPEN_DATA_COMMONS_USE_LICENSES[use_license_machine][:human_readable]
  else
    fail ArgumentError, "'#{use_license_machine}' is not a valid license code"
  end
end

#use_license_humanObject



193
194
195
196
197
198
# File 'lib/dor/models/concerns/editable.rb', line 193

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



186
187
188
189
190
191
# File 'lib/dor/models/concerns/editable.rb', line 186

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

  nil
end

#use_statementObject



147
148
149
# File 'lib/dor/models/concerns/editable.rb', line 147

def use_statement
  defaultObjectRights.use_statement.first
end

#use_statement=(val) ⇒ Object



151
152
153
# File 'lib/dor/models/concerns/editable.rb', line 151

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