Class: Download::DwcArchive::Complete
- Inherits:
-
Download::DwcArchive
- Object
- Download
- Download::DwcArchive
- Download::DwcArchive::Complete
- Defined in:
- app/models/download/dwc_archive/complete.rb
Overview
Only one per project. Includes the complete current contents of DwCOccurrences.
Direct Known Subclasses
Class Method Summary collapse
- .api_buildable? ⇒ Boolean
-
.process_complete_download_request(project) ⇒ Download
Raises TaxonWorks::Error on error.
- .project_api_access_token_destroyed ⇒ Object
Instance Method Summary collapse
-
#build ⇒ Object
private
Builds the complete DwC-A export by enqueuing a job.
- #has_eml_without_stubs ⇒ Object private
-
#normalized_predicate_extensions(predicates) ⇒ Object
private
predicate_extensions may have been initialized from query parameters with string keys and string values.
- #sync_expires_with_preferences ⇒ Object private
Class Method Details
.api_buildable? ⇒ Boolean
24 25 26 |
# File 'app/models/download/dwc_archive/complete.rb', line 24 def self.api_buildable? true end |
.process_complete_download_request(project) ⇒ Download
Raises TaxonWorks::Error on error.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/download/dwc_archive/complete.rb', line 30 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 |
.project_api_access_token_destroyed ⇒ Object
147 148 149 150 |
# File 'app/models/download/dwc_archive/complete.rb', line 147 def self.project_api_access_token_destroyed # May not be necessary if the download doesn't include media extension, but we're doing it anyway. Download::DwcArchive::Complete.destroy_all end |
Instance Method Details
#build ⇒ Object (private)
Builds the complete DwC-A export by enqueuing a job.
Constructs the export parameters from project preferences and enqueues DwcaCreateDownloadJob to generate the archive.
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 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/download/dwc_archive/complete.rb', line 61 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 || [] taxonworks_extensions = project.complete_dwc_download_internal_values || [] by_id = Current.user_id || project.complete_dwc_download_default_user_id 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), taxonworks_extensions:, project_id:, user_id: by_id ) end |
#has_eml_without_stubs ⇒ Object (private)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/download/dwc_archive/complete.rb', line 108 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.
129 130 131 132 133 134 135 136 |
# File 'app/models/download/dwc_archive/complete.rb', line 129 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_preferences ⇒ Object (private)
138 139 140 141 142 143 144 145 |
# File 'app/models/download/dwc_archive/complete.rb', line 138 def sync_expires_with_preferences max_age = project.complete_dwc_download_max_age return if max_age.nil? # Guarantees that a GBIF call (every 7 days) will occur after max_age and # before the existing download expires. self.expires = Time.zone.now + max_age.days + 7.day + 1.day end |