Class: Download::DwcArchive::Complete

Inherits:
Download::DwcArchive show all
Defined in:
app/models/download/dwc_archive/complete.rb

Overview

Only one per project. Includes the complete current contents of DwCOccurrences.

Direct Known Subclasses

PupalComplete

Constant Summary

Constants inherited from Download

STORAGE_PATH

Instance Attribute Summary

Attributes inherited from Download

#description, #expires, #filename, #is_public, #name, #project_id, #request, #times_downloaded, #total_records, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Download

#delete_file, #dir_path, #expired?, #file, #file_path, #ready?, #save_file, #set_sha2, #source_file_path=, storage_path

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Class Method Details

.api_buildable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/download/dwc_archive/complete.rb', line 23

def self.api_buildable?
  true
end

.process_complete_download_request(project) ⇒ Download

Raises TaxonWorks::Error on error.

Returns:

  • (Download)

    the complete download to be served



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/download/dwc_archive/complete.rb', line 29

def self.process_complete_download_request(project)
  # !! Note Current.user_id may not be set here !!
  download = Download.where(
    type: 'Download::DwcArchive::Complete', project_id: project.id
  ).first

  return nil if download.nil?

  if download.ready?
    max_age = project.complete_dwc_download_max_age # in days
    download_age = Time.current - download.created_at
    by_id = Current.user_id || project.complete_dwc_download_default_user_id
    if max_age && download_age.to_f / 1.day > max_age
      # Create a fresh download that will replace the existing one when
      # ready.
      Download::DwcArchive::PupalComplete.create(by: by_id) # don't raise if one already exists
    end

    download.increment!(:times_downloaded)
    return download
  else
    raise TaxonWorks::Error, 'The existing download is not ready yet'
  end
end

Instance Method Details

#buildObject (private)



55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/download/dwc_archive/complete.rb', line 55

def build
  project_params = { project_id: }
  record_scope = ::DwcOccurrence.where(project_params)
  eml_dataset,  = project.complete_dwc_eml_preferences
  predicates = project.complete_dwc_download_predicates || []
  extensions = project.complete_dwc_download_extensions || []
  biological_associations_scope = extensions.include?('resource_relationships') ?
    {
      core_params: project_params, # all dwc_occurrences for this project
      collection_objects_query: ::Queries::BiologicalAssociation::Filter.new(
        collection_object_query: ::Queries::CollectionObject::Filter.new(
          dwc_occurrence_query: project_params
        ).params
      ).all.to_sql
    } : nil
  media_scope = extensions.include?('media') ?
    {
      collection_objects: ::Queries::CollectionObject::Filter.new(
        dwc_occurrence_query: project_params
      ).all.to_sql,

      field_occurrences: ::Queries::FieldOccurrence::Filter.new(
        dwc_occurrence_query: project_params
      ).all.to_sql
    } : nil

  ::DwcaCreateDownloadJob.perform_later(
    id,
    core_scope: record_scope.to_sql,
    eml_data: {
      dataset: eml_dataset,
      additional_metadata: 
    },
    extension_scopes: {
      biological_associations: biological_associations_scope,
      media: media_scope
    },
    predicate_extensions: normalized_predicate_extensions(predicates),
    project_id:
  )
end

#has_eml_without_stubsObject (private)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/download/dwc_archive/complete.rb', line 97

def has_eml_without_stubs
  eml_dataset,  = project.complete_dwc_eml_preferences
  # dataset has required fields for eml GBIF validation, additional metadata
  # does not.

  # TODO: require the required dataset EML fields that GBIF requires.
  if eml_dataset.nil? || eml_dataset.empty?
    errors.add(:base, 'Non-empty dataset xml is required')
  end

  if eml_dataset.include?('STUB')
    errors.add(:base, "EML dataset cannot contain 'STUB'")
  end

  if &.include?('STUB')
    errors.add(:base, "EML additional metadata cannot contain 'STUB'")
  end
end

#normalized_predicate_extensions(predicates) ⇒ Object (private)

predicate_extensions may have been initialized from query parameters with string keys and string values.



118
119
120
121
122
123
124
125
# File 'app/models/download/dwc_archive/complete.rb', line 118

def normalized_predicate_extensions(predicates)
  return {} if !predicates&.is_a?(Hash)

  predicates.inject({}) do |h, (k, v)|
    h[k.to_sym] = v.map(&:to_i)
    h
  end
end

#sync_expires_with_preferencesObject (private)



127
128
129
130
131
132
# File 'app/models/download/dwc_archive/complete.rb', line 127

def sync_expires_with_preferences
  max_age = project.complete_dwc_download_max_age
  return if max_age.nil?

  self.expires = Time.zone.now + max_age.days + 1.day
end