|
| 1 | +# frozen_string_literal: true |
| 2 | +module Mediaflux |
| 3 | + module Http |
| 4 | + # Constructs a request to mediaflux to approve a project |
| 5 | + # |
| 6 | + # @example |
| 7 | + # project = Project.first |
| 8 | + # project.save_in_mediaflux(session_id: User.first.mediaflux_session) |
| 9 | + # approve_req = Mediaflux::Http::AssetApproveRequest.new(session_token: User.first.mediaflux_session, project:) |
| 10 | + # approve_req.resolve |
| 11 | + # |
| 12 | + class AssetApproveRequest < Request |
| 13 | + attr_reader :project_metadata, :project |
| 14 | + # Constructor |
| 15 | + # @param session_token [String] the API token for the authenticated session |
| 16 | + # @param project [Project] project to approve |
| 17 | + # @param xml_namespace [String] XML namespace for the <project> element |
| 18 | + def initialize(session_token:, project:, xml_namespace: nil, xml_namespace_uri: nil) |
| 19 | + super(session_token: session_token) |
| 20 | + @project = project |
| 21 | + @project_metadata = project.metadata |
| 22 | + @xml_namespace = xml_namespace || self.class.default_xml_namespace |
| 23 | + @xml_namespace_uri = xml_namespace_uri || self.class.default_xml_namespace_uri |
| 24 | + end |
| 25 | + |
| 26 | + # Specifies the Mediaflux service to use when updating assets |
| 27 | + # @return [String] |
| 28 | + def self.service |
| 29 | + "asset.set" |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + # The generated XML mimics what we get when we issue an Aterm command as follows: |
| 35 | + # service.execute :service -name "asset.set" \ |
| 36 | + # < :id "1574" :meta -action "replace" < :tigerdata:project -xmlns:tigerdata "tigerdata" < \ |
| 37 | + # :ProjectDirectory "/td-demo-001/tigerdataNS/test-05-30-24" :Title "testing approval" :Description "I want to test the approval updates" \ |
| 38 | + # :Status "approved" :DataSponsor "cac9" :DataManager "mjc12" :Department "RDSS" :DataUser -ReadOnly "true" "la15" :DataUser "woongkim" \ |
| 39 | + # :CreatedOn "30-MAY-2024 09:11:09" :CreatedBy "cac9" :ProjectID "10.34770/tbd" |
| 40 | + # :StorageCapacity < :Size -Requested "500" -Approved "1" "1" :Unit -Requested "GB" -Approved "TB" "TB" > \ |
| 41 | + # :Performance -Requested "Standard" -Approved "Standard" "Standard" :Submission < :RequestedBy "cac9" \ |
| 42 | + # :RequestDateTime "30-MAY-2024 13:11:09" :ApprovedBy "cac9" :ApprovalDateTime "30-MAY-2024 13:12:44" \ |
| 43 | + # :EventlNote < :NoteDateTime "30-MAY-2024 13:12:44" :NoteBy "cac9" :EventType "Quota" :Message "A note"\ |
| 44 | + # > > :ProjectPurpose "Research" :SchemaVersion "0.6.1" > > > |
| 45 | + # |
| 46 | + def build_http_request_body(name:) |
| 47 | + super do |xml| |
| 48 | + xml.args do |
| 49 | + xml.id project.mediaflux_id |
| 50 | + xml.meta do |
| 51 | + xml.parent.set_attribute("action", "replace") |
| 52 | + doc = xml.doc |
| 53 | + root = doc.root |
| 54 | + # Define the namespace only if this is required |
| 55 | + root.add_namespace_definition(@xml_namespace, @xml_namespace_uri) |
| 56 | + |
| 57 | + element_name = @xml_namespace.nil? ? "project" : "#{@xml_namespace}:project" |
| 58 | + xml.send(element_name) do |
| 59 | + build_project(xml) |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + def build_project(xml) |
| 67 | + build_basic_project_meta(xml) |
| 68 | + build_departments(xml, project_metadata[:departments]) |
| 69 | + build_read_only_user(xml, project_metadata[:data_user_read_only]) |
| 70 | + build_read_write_users(xml, project_metadata[:data_user_read_write]) |
| 71 | + xml.CreatedOn self.class.format_date_for_mediaflux(project_metadata[:created_on]) |
| 72 | + xml.CreatedBy project_metadata[:created_by] |
| 73 | + xml.ProjectID project_metadata[:project_id] |
| 74 | + build_storage_capacity(xml) |
| 75 | + build_performance(xml) |
| 76 | + build_submission(xml) |
| 77 | + xml.ProjectPurpose project_metadata[:project_purpose] |
| 78 | + xml.SchemaVersion TigerdataSchema::SCHEMA_VERSION |
| 79 | + end |
| 80 | + |
| 81 | + def build_basic_project_meta(xml) |
| 82 | + xml.ProjectDirectory project_metadata[:project_directory] |
| 83 | + xml.Title project_metadata[:title] |
| 84 | + xml.Description project_metadata[:description] if project_metadata[:description].present? |
| 85 | + xml.Status project_metadata[:status] |
| 86 | + xml.DataSponsor project_metadata[:data_sponsor] |
| 87 | + xml.DataManager project_metadata[:data_manager] |
| 88 | + end |
| 89 | + |
| 90 | + def build_departments(xml, departments) |
| 91 | + return if departments.blank? |
| 92 | + |
| 93 | + departments.each do |department| |
| 94 | + xml.Department department |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + def build_read_only_user(xml, ro_users) |
| 99 | + return if ro_users.blank? |
| 100 | + |
| 101 | + ro_users.each do |ro_user| |
| 102 | + xml.DataUser do |
| 103 | + xml.parent.set_attribute("ReadOnly", true) |
| 104 | + xml.text(ro_user) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def build_read_write_users(xml, rw_users) |
| 110 | + return if rw_users.blank? |
| 111 | + |
| 112 | + rw_users.each do |rw_user| |
| 113 | + xml.DataUser rw_user |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + def build_storage_capacity(xml) |
| 118 | + xml.StorageCapacity do |
| 119 | + xml.Size do |
| 120 | + build_value(xml, project_metadata[:storage_capacity][:size][:requested], project_metadata[:storage_capacity][:size][:approved]) |
| 121 | + end |
| 122 | + xml.Unit do |
| 123 | + build_value(xml, project_metadata[:storage_capacity][:unit][:requested], project_metadata[:storage_capacity][:unit][:approved]) |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + def build_performance(xml) |
| 129 | + xml.Performance do |
| 130 | + build_value(xml, project_metadata[:storage_performance_expectations][:requested], project_metadata[:storage_performance_expectations][:approved]) |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + def build_value(xml, requested, approved) |
| 135 | + xml.parent.set_attribute("Requested", requested) |
| 136 | + xml.parent.set_attribute("Approved", approved) |
| 137 | + xml.text(approved) |
| 138 | + end |
| 139 | + |
| 140 | + def build_submission(xml) |
| 141 | + xml.Submission do |
| 142 | + xml.RequestedBy submission_event.event_person |
| 143 | + xml.RequestDateTime self.class.format_date_for_mediaflux(submission_event.created_at.iso8601) |
| 144 | + xml.ApprovedBy approval_event.event_person |
| 145 | + xml.ApprovalDateTime self.class.format_date_for_mediaflux(approval_event.created_at.iso8601) |
| 146 | + build_submission_note(xml) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + def build_submission_note(xml) |
| 151 | + return if approval_event.event_note.blank? |
| 152 | + |
| 153 | + xml.EventlNote do |
| 154 | + xml.NoteDateTime self.class.format_date_for_mediaflux(approval_event.created_at.iso8601) |
| 155 | + xml.NoteBy approval_event.event_note["note_by"] |
| 156 | + xml.EventType approval_event.event_note["event_type"] |
| 157 | + xml.Message approval_event.event_note["message"] |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + def approval_event |
| 162 | + @approval_event ||= project.provenance_events.find_by(event_type: ProvenanceEvent::APPROVAL_EVENT_TYPE) |
| 163 | + end |
| 164 | + |
| 165 | + def submission_event |
| 166 | + @submission_event ||= project.provenance_events.find_by(event_type: ProvenanceEvent::SUBMISSION_EVENT_TYPE) |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | +end |
0 commit comments