Class: Dor::Workflow::Client::WorkflowRoutes

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/workflow/client/workflow_routes.rb

Overview

Makes requests relating to a workflow

Instance Method Summary collapse

Constructor Details

#initialize(requestor:) ⇒ WorkflowRoutes

Returns a new instance of WorkflowRoutes.



8
9
10
# File 'lib/dor/workflow/client/workflow_routes.rb', line 8

def initialize(requestor:)
  @requestor = requestor
end

Instance Method Details

#all_workflows(pid:) ⇒ Workflow::Response::Workflows

Retrieves all workflows for the given object

Parameters:

  • pid (String)

    The id of the object

Returns:



104
105
106
107
# File 'lib/dor/workflow/client/workflow_routes.rb', line 104

def all_workflows(pid:)
  xml = requestor.request "objects/#{pid}/workflows"
  Workflow::Response::Workflows.new(xml: xml)
end

#create_workflow_by_name(druid, workflow_name, version:, lane_id: 'default') ⇒ Boolean

Creates a workflow for a given object in the repository. If this particular workflow for this objects exists, it will replace the old workflow. Returns true on success. Caller must handle any exceptions.

name that is known by the workflow service.

Parameters:

  • druid (String)

    The id of the object

  • workflow_name (String)

    The name of the workflow you want to create. This must correspond with a workflow

  • lane_id (String) (defaults to: 'default')

    adds laneId attribute to all process elements in the wf_xml workflow xml. Defaults to a value of 'default'

  • version (Integer)

    specifies the version so that workflow service doesn't need to query dor-services.

Returns:

  • (Boolean)

    always true



23
24
25
26
27
28
29
# File 'lib/dor/workflow/client/workflow_routes.rb', line 23

def create_workflow_by_name(druid, workflow_name, version:, lane_id: 'default')
  params = { 'lane-id' => lane_id, 'version' => version }
  requestor.request "objects/#{druid}/workflows/#{workflow_name}", 'post', '',
                    content_type: 'application/xml',
                    params: params
  true
end

#delete_all_workflows(pid:) ⇒ Boolean

Deletes all workflow steps for a particular druid

Parameters:

  • pid (String)

    The id of the object to delete the workflow from

Returns:

  • (Boolean)

    always true



152
153
154
155
# File 'lib/dor/workflow/client/workflow_routes.rb', line 152

def delete_all_workflows(pid:)
  requestor.request "objects/#{pid}/workflows", 'delete'
  true
end

#delete_workflow(druid:, workflow:, version:) ⇒ Boolean

Deletes a workflow from a particular repository and druid. This is only used by Hydrus.

Parameters:

  • druid (String)

    The id of the object to delete the workflow from

  • workflow (String)

    The name of the workflow to be deleted

  • version (Integer)

    The version of the workflow to delete

Returns:

  • (Boolean)

    always true



143
144
145
146
147
# File 'lib/dor/workflow/client/workflow_routes.rb', line 143

def delete_workflow(druid:, workflow:, version:)
  qs_args = "?version=#{version}"
  requestor.request "/objects/#{druid}/workflows/#{workflow}#{qs_args}", 'delete'
  true
end

#process(pid:, workflow_name:, process:) ⇒ Workflow::Response::Process

Parameters:

  • pid (String)

    id of object

  • workflow_name (String)

    The name of the workflow

  • process (String)

    The name of the workflow step

Returns:



134
135
136
# File 'lib/dor/workflow/client/workflow_routes.rb', line 134

def process(pid:, workflow_name:, process:)
  workflow(pid: pid, workflow_name: workflow_name).process_for_recent_version(name: process)
end

#update_error_status(druid:, workflow:, process:, error_msg:, error_text: nil) ⇒ Workflow::Response::Update

Updates the status of one step in a workflow to error. Returns true on success. Caller must handle any exceptions

Http Call

The method does an HTTP PUT to the base URL.

PUT "/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"error\" />"

Parameters:

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • process (String)

    The name of the workflow step

  • error_msg (String)

    The error message. Ideally, this is a brief message describing the error

  • error_text (String) (defaults to: nil)

    A slot to hold more information about the error, like a full stacktrace

Returns:



95
96
97
98
99
# File 'lib/dor/workflow/client/workflow_routes.rb', line 95

def update_error_status(druid:, workflow:, process:, error_msg:, error_text: nil)
  xml = create_process_xml(name: process, status: 'error', errorMessage: error_msg, error_text: error_text)
  response = requestor.request "objects/#{druid}/workflows/#{workflow}/#{process}", 'put', xml, content_type: 'application/xml'
  Workflow::Response::Update.new(json: response)
end

#update_status(druid:, workflow:, process:, status:, elapsed: 0, lifecycle: nil, note: nil, current_status: nil) ⇒ Boolean

Updates the status of one step in a workflow. Returns true on success. Caller must handle any exceptions

Http Call

The method does an HTTP PUT to the base URL. As an example:

PUT "/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"completed\" />"

Parameters:

  • repo (String)

    The repository the object resides in. The service recoginzes "dor" and "sdr" at the moment

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • process (String)

    The name of the process step

  • status (String)

    The status that you want to set -- using one of the values in VALID_STATUS

  • :elapsed (Float)

    The number of seconds it took to complete this step. Can have a decimal. Is set to 0 if not passed in.

  • :lifecycle (String)

    Bookeeping label for this particular workflow step. Examples are: 'registered', 'shelved'

  • :note (String)

    Any kind of string annotation that you want to attach to the workflow

  • :current_status (String)

    Setting this string tells the workflow service to compare the current status to this value. If the current value does not match this value, the update is not performed

Returns:

  • (Boolean)

    always true

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dor/workflow/client/workflow_routes.rb', line 50

def update_status(druid:, workflow:, process:, status:, elapsed: 0, lifecycle: nil, note: nil, current_status: nil)
  raise ArgumentError, "Unknown status value #{status}" unless VALID_STATUS.include?(status)
  raise ArgumentError, "Unknown current_status value #{current_status}" if current_status && !VALID_STATUS.include?(current_status)

  xml = create_process_xml(name: process, status: status, elapsed: elapsed, lifecycle: lifecycle, note: note)
  uri = "objects/#{druid}/workflows/#{workflow}/#{process}"
  uri += "?current-status=#{current_status}" if current_status
  response = requestor.request(uri, 'put', xml, content_type: 'application/xml')

  Workflow::Response::Update.new(json: response)
end

#workflow(pid:, workflow_name:) ⇒ Workflow::Response::Workflow

Parameters:

  • pid (String)

    id of object

  • workflow_name (String)

    The name of the workflow

Returns:



125
126
127
128
# File 'lib/dor/workflow/client/workflow_routes.rb', line 125

def workflow(pid:, workflow_name:)
  xml = fetch_workflow(druid: pid, workflow: workflow_name)
  Workflow::Response::Workflow.new(xml: xml)
end

#workflow_status(druid:, workflow:, process:) ⇒ String

Retrieves the process status of the given workflow for the given object identifier

Parameters:

  • repo (String)

    The repository the object resides in. Currently recoginzes "dor" and "sdr".

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • process (String)

    The name of the process step

Returns:

  • (String)

    status for repo-workflow-process-druid

Raises:



69
70
71
72
73
74
75
76
77
# File 'lib/dor/workflow/client/workflow_routes.rb', line 69

def workflow_status(druid:, workflow:, process:)
  workflow_md = fetch_workflow(druid: druid, workflow: workflow)
  doc = Nokogiri::XML(workflow_md)
  raise Dor::WorkflowException, "Unable to parse response:\n#{workflow_md}" if doc.root.nil?

  processes = doc.root.xpath("//process[@name='#{process}']")
  process = processes.max { |a, b| a.attr('version').to_i <=> b.attr('version').to_i }
  process&.attr('status')
end

#workflows(pid) ⇒ Array<String>

Get workflow names into an array for given PID This method only works when this gem is used in a project that is configured to connect to DOR

Examples:

client.workflows('druid:sr100hp0609')
=> ["accessionWF", "assemblyWF", "disseminationWF"]

Parameters:

  • pid (String)

    of druid

Returns:

  • (Array<String>)

    list of worklows



117
118
119
120
# File 'lib/dor/workflow/client/workflow_routes.rb', line 117

def workflows(pid)
  xml_doc = Nokogiri::XML(fetch_workflow(druid: pid, workflow: ''))
  xml_doc.xpath('//workflow').collect { |workflow| workflow['id'] }
end