Class: ProjectMediaflux

Inherits:
Object
  • Object
show all
Defined in:
app/models/project_mediaflux.rb

Overview

Take an instance of Project and adds it to MediaFlux

Class Method Summary collapse

Class Method Details

.create!(project:, user:, xml_namespace: nil) ⇒ String

Create a project in MediaFlux

Parameters:

  • project (Project)

    the project that needs to be added to MediaFlux

  • session_id

    the session id for the user who is currently authenticated to MediaFlux

  • xml_namespace (defaults to: nil)

Returns:

  • (String)

    The id of the project that got created



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/project_mediaflux.rb', line 32

def self.create!(project:, user:, xml_namespace: nil)
  session_id = user.mediaflux_session
  store_name = Store.default(session_id: session_id).name

  # Make sure the root namespace and the required nodes below it exist.
  create_root_tree(session_id: session_id)

  # Create a namespace for the project
  # The namespace is directly under our root namespace'
  project_name = project.project_directory
  project_namespace = "#{project_name}NS"
  namespace = Mediaflux::NamespaceCreateRequest.new(namespace: project_namespace, description: "Namespace for project #{project.title}", store: store_name, session_token: session_id)
  if namespace.error?
    raise MediafluxDuplicateNamespaceError.new("Can not create the namespace #{namespace.response_error}")
  end
  # Create a collection asset under the root namespace and set its metadata
  project_parent = Mediaflux::Connection.root_collection
  create_request = Mediaflux::ProjectCreateRequest.new(session_token: session_id, namespace: project_namespace, project:, xml_namespace: xml_namespace, pid: project_parent)
  id = create_request.id
  if id.blank?
    response_error = create_request.response_error
    case response_error[:message]
    when "failed: The namespace #{project_namespace} already contains an asset named '#{project_name}'"
      raise "Project name already taken"
    when /'asset.create' failed/

      # Ensure that the metadata validations are run
      if project.valid?
        raise response_error[:message]  # something strange went wrong
      else
        raise TigerData::MissingMetadata.(schema_version: ::TigerdataSchema::SCHEMA_VERSION, errors: project.errors)
      end
    else
      raise(StandardError,"An error has occured during project creation, not related to namespace creation or collection creation")
    end
  end

  # Save the ActiveRecord to make sure the mediaflux_id is recorded
  project.mediaflux_id = id
  project.save!

  id
end

.create_root_tree(session_id:) ⇒ Object



102
103
104
105
106
107
# File 'app/models/project_mediaflux.rb', line 102

def self.create_root_tree(session_id:)
  root_ns = Rails.configuration.mediaflux["api_root_collection_namespace"]
  parent_collection = Rails.configuration.mediaflux["api_root_collection_name"]
  req = Mediaflux::RootCollectionAsset.new(session_token: session_id, root_ns:, parent_collection:)
  req.create
end

.document(project:, xml_namespace: nil) ⇒ Object



97
98
99
100
# File 'app/models/project_mediaflux.rb', line 97

def self.document(project:, xml_namespace: nil)
  xml_body = xml_payload(project:, xml_namespace:)
  Nokogiri::XML.parse(xml_body)
end

.save(project:, user:, xml_namespace: nil) ⇒ String

If the project hasn’t yet been created in mediaflux, create it. If it already exists, update it.

Returns:

  • (String)

    the mediaflux id of the project



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/project_mediaflux.rb', line 12

def self.save(project:, user:, xml_namespace: nil)
  session_id = user.mediaflux_session
  if project.mediaflux_id.nil?
    mediaflux_id = ProjectMediaflux.create!(project: project, user: user)
    ProjectAccumulator.new(project: project, session_id: session_id).create!()
    Rails.logger.debug "Project #{project.id} has been created in MediaFlux (asset id #{mediaflux_id})"
  else
    ProjectMediaflux.update(project: project, user: user)
    Rails.logger.debug "Project #{project.id} has been updated in MediaFlux (asset id #{project.mediaflux_id})"
  end
  project.reload
  project.mediaflux_id
end

.update(project:, user:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/project_mediaflux.rb', line 76

def self.update(project:, user:)
  session_id = user.mediaflux_session
  project..updated_on ||= Time.current.in_time_zone("America/New_York").iso8601
  project..updated_by ||= user.uid
  update_request = Mediaflux::ProjectUpdateRequest.new(session_token: session_id, project: project)
  update_request.resolve
  raise update_request.response_error[:message] if update_request.error?

  # Save the ActiveRecord to make sure the updated metadata is also saved in our PostgreSQL database
  project.save!
end

.xml_payload(project:, xml_namespace: nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'app/models/project_mediaflux.rb', line 88

def self.xml_payload(project:, xml_namespace: nil)
  project_name = project.project_directory
  project_namespace = "#{project_name}NS"
  project_parent = Mediaflux::Connection.root_collection

  create_request = Mediaflux::ProjectCreateRequest.new(session_token: nil, namespace: project_namespace, project:, xml_namespace: xml_namespace, pid: project_parent)
  create_request.xml_payload
end