Class: Document

Inherits:
Kithe::Work
  • Object
show all
Includes:
ActiveModel::Validations, AttrJson::Record::QueryScopes
Defined in:
app/models/document.rb,
app/models/document/reference.rb,
app/models/document/bbox_validator.rb,
app/models/document/date_validator.rb,
app/models/document/geom_validator.rb,
app/models/document/controlled_lists.rb,
app/models/document/date_range_validator.rb

Overview

Date Range Validation

Allow: YYYY-YYYY, *-YYYY, YYYY-*, Start YYYY == End YYYY Disallow: YYYX-YYYY, YYYY-, 2000-1999, YYYY-YYYY?

Defined Under Namespace

Classes: BboxValidator, ControlledLists, DateRangeValidator, DateValidator, GeomValidator, Reference

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skip_callbacksObject

Returns the value of attribute skip_callbacks.



15
16
17
# File 'app/models/document.rb', line 15

def skip_callbacks
  @skip_callbacks
end

Instance Method Details

#a_downloadable_resource?Boolean

Downloadable Resouce

Returns:

  • (Boolean)


56
57
58
# File 'app/models/document.rb', line 56

def a_downloadable_resource?
  references_json.include?("downloadUrl")
end

#access_jsonObject



238
239
240
241
242
# File 'app/models/document.rb', line 238

def access_json
  access = {}
  access_urls.each { |au| access[au.institution_code] = au.access_url }
  access.to_json
end

#access_urlsObject

Institutional Access URLs



312
313
314
# File 'app/models/document.rb', line 312

def access_urls
  DocumentAccess.where(friendlier_id: friendlier_id).order(institution_code: :asc)
end

#apply_downloads(references) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/document.rb', line 91

def apply_downloads(references)
  dct_downloads = references["http://schema.org/downloadUrl"]
  # Make sure downloads exist!
  if document_downloads.present?
    multiple_downloads = multiple_downloads_array
    if dct_downloads.present?
      multiple_downloads << {label: download_text(send(GeoblacklightAdmin::Schema.instance.solr_fields[:format])),
                              url: dct_downloads}
    end
    references[:"http://schema.org/downloadUrl"] = multiple_downloads
  end
  references
end

#available?Boolean

GBL SolrDocument convience methods

Returns:

  • (Boolean)


139
140
141
# File 'app/models/document.rb', line 139

def available?
  public? || same_institution?
end

#checked_endpoint(type) ⇒ Object

Provides a convenience method to access a SolrDocument’s References endpoint url without having to check and see if it is available :type => a string which if its a Geoblacklight::Constants::URI key

will return a coresponding Geoblacklight::Reference


227
228
229
230
# File 'app/models/document.rb', line 227

def checked_endpoint(type)
  type = references.send(type)
  type.endpoint if type.present?
end

#created_at_dtObject



244
245
246
# File 'app/models/document.rb', line 244

def created_at_dt
  created_at&.utc&.iso8601
end

#current_versionObject



306
307
308
309
# File 'app/models/document.rb', line 306

def current_version
  # Will return 0 if no PaperTrail version exists yet
  versions&.last&.index || 0
end

#data_dictionary_downloadObject



193
194
195
# File 'app/models/document.rb', line 193

def data_dictionary_download
  references.data_dictionary.to_hash if references.data_dictionary.present?
end

#date_range_jsonObject



257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/models/document.rb', line 257

def date_range_json
  date_ranges = []
  unless send(GeoblacklightAdmin::Schema.instance.solr_fields[:date_range]).all?(&:blank?)
    send(GeoblacklightAdmin::Schema.instance.solr_fields[:date_range]).each do |date_range|
      start_d, end_d = date_range.split("-")
      start_d = "*" if start_d == "YYYY" || start_d.nil?
      end_d = "*" if end_d == "YYYY" || end_d.nil?
      date_ranges << "[#{start_d} TO #{end_d}]" if start_d.present?
    end
  end
  date_ranges
end

#dct_references_s_to_csv(key, destination) ⇒ Object



298
299
300
301
302
303
304
# File 'app/models/document.rb', line 298

def dct_references_s_to_csv(key, destination)
  send(destination).detect do |ref|
    ref.category == GeoblacklightAdmin::Schema.instance.dct_references_mappings[key]
  end.value
rescue NoMethodError
  nil
end

#dct_title_sObject

Ensures a manually created “title” makes it into the attr_json “title”



253
254
255
# File 'app/models/document.rb', line 253

def dct_title_s
  title
end

#derive_dcat_bboxObject

Convert GEOM for Solr Indexing



349
350
351
352
353
354
355
356
357
# File 'app/models/document.rb', line 349

def derive_dcat_bbox
  if send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).present?
    # "W,S,E,N" convert to "ENVELOPE(W,E,N,S)"
    w, s, e, n = send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).split(",")
    "ENVELOPE(#{w},#{e},#{n},#{s})"
  else
    ""
  end
end

#derive_dcat_centroidObject



359
360
361
362
363
364
365
366
# File 'app/models/document.rb', line 359

def derive_dcat_centroid
  if send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).present?
    w, s, e, n = send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).split(",")
    "#{(n.to_f + s.to_f) / 2},#{(e.to_f + w.to_f) / 2}"
  else
    ""
  end
end

#derive_locn_geometryObject



316
317
318
319
320
321
322
323
324
# File 'app/models/document.rb', line 316

def derive_locn_geometry
  if send(GeoblacklightAdmin::Schema.instance.solr_fields[:geometry]).present?
    send(GeoblacklightAdmin::Schema.instance.solr_fields[:geometry])
  elsif send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).present?
    derive_polygon
  else
    ""
  end
end

#derive_polygonObject

Convert BBOX to GEOM Polygon



327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'app/models/document.rb', line 327

def derive_polygon
  if send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).present?
    # Guard against a whole world polygons
    if send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]) == "-180,-90,180,90"
      "ENVELOPE(-180,180,90,-90)"
    else
      # "W,S,E,N" convert to "POLYGON((W N, E N, E S, W S, W N))"
      w, s, e, n = send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).split(",")
      "POLYGON((#{w} #{n}, #{e} #{n}, #{e} #{s}, #{w} #{s}, #{w} #{n}))"
    end
  else
    ""
  end
end

#direct_downloadObject



168
169
170
# File 'app/models/document.rb', line 168

def direct_download
  references.download.to_hash if references.download.present?
end

#display_noteObject



172
173
174
# File 'app/models/document.rb', line 172

def display_note
  send(Settings.FIELDS.DISPLAY_NOTE) || ""
end

#download_text(format) ⇒ Object

Wraps download text with proper_case_format



124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/document.rb', line 124

def download_text(format)
  download_format = proper_case_format(format)
  prefix = "Original "
  begin
    format = download_format
  rescue
    # Need to rescue if format doesn't exist
  end
  value = prefix + format.to_s
  value.html_safe
end

#downloadable?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'app/models/document.rb', line 164

def downloadable?
  (direct_download || download_types.present? || iiif_download) && available?
end

#external_urlObject



197
198
199
# File 'app/models/document.rb', line 197

def external_url
  references.url&.endpoint
end

#file_formatObject



218
219
220
# File 'app/models/document.rb', line 218

def file_format
  send(Settings.FIELDS.FORMAT) || ""
end

#gbl_mdModified_dtObject



248
249
250
# File 'app/models/document.rb', line 248

def gbl_mdModified_dt
  updated_at&.utc&.iso8601
end

#geom_fieldObject



205
206
207
# File 'app/models/document.rb', line 205

def geom_field
  send(Settings.FIELDS.GEOMETRY) || ""
end

#geometryObject



209
210
211
212
# File 'app/models/document.rb', line 209

def geometry
  # @TODO
  # @geometry ||= Geoblacklight::Geometry.new(geom_field)
end

#hgl_downloadObject



176
177
178
# File 'app/models/document.rb', line 176

def hgl_download
  references.hgl.to_hash if references.hgl.present?
end

#iiif_downloadObject



189
190
191
# File 'app/models/document.rb', line 189

def iiif_download
  references.iiif.to_hash if references.iiif.present?
end

#iso_language_mappingObject

Convert three char language code to proper string



369
370
371
372
373
374
375
376
377
378
# File 'app/models/document.rb', line 369

def iso_language_mapping
  mapping = []

  if send(GeoblacklightAdmin::Schema.instance.solr_fields[:language]).present?
    send(GeoblacklightAdmin::Schema.instance.solr_fields[:language]).each do |lang|
      mapping << GeoblacklightAdmin::IsoLanguageCodes.call[lang]
    end
  end
  mapping
end

#item_viewerObject



11
12
13
# File 'app/models/document.rb', line 11

def item_viewer
  GeoblacklightAdmin::ItemViewer.new(references)
end

#itemtypeObject



201
202
203
# File 'app/models/document.rb', line 201

def itemtype
  "http://schema.org/Dataset"
end

#local?Boolean

Returns:

  • (Boolean)


151
152
153
154
# File 'app/models/document.rb', line 151

def local?
  local = send(Settings.FIELDS.PROVIDER) || ""
  local.casecmp(Settings.INSTITUTION_LOCAL_NAME)&.zero?
end

#local_restricted?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/document.rb', line 147

def local_restricted?
  local? && restricted?
end

#multiple_downloads_arrayObject



105
106
107
# File 'app/models/document.rb', line 105

def multiple_downloads_array
  document_downloads.collect { |d| {label: d.label, url: d.value} }
end

#oembedObject



180
181
182
# File 'app/models/document.rb', line 180

def oembed
  references.oembed.endpoint if references.oembed.present?
end

#proper_case_format(format) ⇒ Object

From GBL

Looks up properly formatted names for formats



113
114
115
116
117
118
119
# File 'app/models/document.rb', line 113

def proper_case_format(format)
  if I18n.exists?("geoblacklight.formats.#{format.to_s.parameterize(separator: "_")}")
    I18n.t("geoblacklight.formats.#{format.to_s.parameterize(separator: "_")}")
  else
    format
  end
end

#public?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/models/document.rb', line 143

def public?
  rights_field_data.present? && rights_field_data.casecmp("public").zero?
end

#referencesObject

Index Transformations - *_json functions



79
80
81
82
83
84
85
# File 'app/models/document.rb', line 79

def references
  references = ActiveSupport::HashWithIndifferentAccess.new
  send(GeoblacklightAdmin::Schema.instance.solr_fields[:reference]).each do |ref|
    references[Document::Reference::REFERENCE_VALUES[ref.category.to_sym][:uri]] = ref.value
  end
  apply_downloads(references)
end

#references_jsonObject



87
88
89
# File 'app/models/document.rb', line 87

def references_json
  references.to_json
end

#restricted?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/document.rb', line 156

def restricted?
  rights_field_data.blank? || rights_field_data.casecmp("restricted").zero?
end

#rights_field_dataObject



160
161
162
# File 'app/models/document.rb', line 160

def rights_field_data
  send(Settings.FIELDS.ACCESS_RIGHTS) || ""
end

#same_institution?Boolean

Returns:

  • (Boolean)


184
185
186
187
# File 'app/models/document.rb', line 184

def same_institution?
  institution = send(Settings.FIELDS.PROVIDER) || ""
  institution.casecmp(Settings.INSTITUTION.downcase).zero?
end

#set_geometryObject



342
343
344
345
346
# File 'app/models/document.rb', line 342

def set_geometry
  return unless locn_geometry.blank? && self&.dcat_bbox&.present?

  self.locn_geometry = derive_polygon
end

#solr_year_jsonObject Also known as: gbl_indexYear_im



270
271
272
273
274
275
# File 'app/models/document.rb', line 270

def solr_year_json
  return [] if send(GeoblacklightAdmin::Schema.instance.solr_fields[:date_range]).blank?

  start_d, _end_d = send(GeoblacklightAdmin::Schema.instance.solr_fields[:date_range]).first.split("-")
  [start_d] if start_d.presence
end

#state_machineObject



37
38
39
# File 'app/models/document.rb', line 37

def state_machine
  @state_machine ||= DocumentStateMachine.new(self, transition_class: DocumentTransition)
end

#thumbnailObject

End / From GBL



234
235
236
# File 'app/models/document.rb', line 234

def thumbnail
  members.find { |m| m.respond_to?(:thumbnail) }
end

#to_csvObject

Export Transformations - to_*



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/models/document.rb', line 279

def to_csv
  attributes = GeoblacklightAdmin::Schema.instance.exportable_fields
  attributes.map do |key, value|
    if value[:delimited]
      send(value[:destination])&.join("|")
    elsif value[:destination] == "dct_references_s"
      dct_references_s_to_csv(key, value[:destination])
    elsif value[:destination] == "b1g_publication_state_s"
      send(:current_state)
    else
      send(value[:destination])
    end
  end
end

#to_trajectObject



294
295
296
# File 'app/models/document.rb', line 294

def to_traject
  Kithe::Model.find_by_friendlier_id(friendlier_id).update_index(writer: Traject::DebugWriter.new({}))
end

#wxs_identifierObject



214
215
216
# File 'app/models/document.rb', line 214

def wxs_identifier
  send(Settings.FIELDS.WXS_IDENTIFIER) || ""
end