This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #447 from sul-dlss/initiate-apo
Extract a service to create workflows
- Loading branch information
Showing
7 changed files
with
135 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dor | ||
class CreateWorkflowService | ||
# Initilizes workflow for the object in the workflow service | ||
# It will set the priorty of the new workflow to the current_priority if it is > 0 | ||
# It will set lane_id from the item's APO default workflow lane | ||
# @param [String] name of the workflow to be initialized | ||
# @param [Boolean] create_ds create a 'workflows' datastream in Fedora for the object | ||
# @param [Integer] priority the workflow's priority level | ||
def self.create_workflow(item, name:, create_ds: true, priority: 0) | ||
new(item).create_workflow(name: name, create_ds: create_ds, priority: priority) | ||
end | ||
|
||
def initialize(item) | ||
@item = item | ||
end | ||
|
||
def create_workflow(name:, create_ds: true, priority: 0) | ||
priority = item.workflows.current_priority if priority == 0 | ||
opts = { create_ds: create_ds, lane_id: default_workflow_lane } | ||
opts[:priority] = priority if priority > 0 | ||
Dor::Config.workflow.client.create_workflow(Dor::WorkflowObject.initial_repo(name), | ||
item.pid, | ||
name, | ||
Dor::WorkflowObject.initial_workflow(name), | ||
opts) | ||
item.workflows.content(true) # refresh the copy of the workflows datastream | ||
end | ||
|
||
private | ||
|
||
attr_reader :item | ||
delegate :admin_policy_object, to: :item | ||
|
||
# Returns the default lane_id from the item's APO. Will return 'default' if the item does not have | ||
# and APO, or if the APO does not have a default_lane | ||
# @return [String] the lane id | ||
def default_workflow_lane | ||
return 'default' if admin_policy_object.nil? # TODO: log warning? | ||
|
||
admin_md = admin_policy_object.administrativeMetadata | ||
return 'default' unless admin_md.respond_to?(:default_workflow_lane) # Some APOs don't have this datastream | ||
|
||
lane = admin_md.default_workflow_lane | ||
return lane unless lane.blank? | ||
|
||
'default' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Dor::CreateWorkflowService do | ||
describe '.create_workflow' do | ||
subject(:create_workflow) { described_class.create_workflow(item, name: 'accessionWF') } | ||
before do | ||
allow(item).to receive(:admin_policy_object).and_return(apo) | ||
end | ||
let(:apo) { instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject) } | ||
let(:item) { instantiate_fixture('druid:ab123cd4567', Dor::Item) } | ||
|
||
it "sets the lane_id option from the object's APO" do | ||
expect(Dor::WorkflowObject).to receive(:initial_workflow).and_return('<xml/>') | ||
expect(Dor::WorkflowObject).to receive(:initial_repo).and_return('dor') | ||
expect(Dor::Config.workflow.client).to receive(:create_workflow).with('dor', 'druid:ab123cd4567', 'accessionWF', '<xml/>', create_ds: true, lane_id: 'fast') | ||
create_workflow | ||
end | ||
end | ||
|
||
describe '#default_workflow_lane' do | ||
let(:item) { instantiate_fixture('druid:ab123cd4567', Dor::Item) } | ||
subject { described_class.new(item).send(:default_workflow_lane) } | ||
|
||
context 'when the object has an APO' do | ||
before do | ||
allow(item).to receive(:admin_policy_object).and_return(apo) | ||
end | ||
|
||
context 'with the fast lane defined in the apo' do | ||
let(:apo) { instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject) } | ||
|
||
it { is_expected.to eq 'fast' } | ||
end | ||
|
||
context 'without a lane defined' do | ||
let(:apo) { instantiate_fixture('druid:zt570tx3016', Dor::AdminPolicyObject) } | ||
|
||
it { is_expected.to eq 'default' } | ||
end | ||
|
||
context 'without administrativeMetadata' do | ||
let(:apo) { instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject) } | ||
before do | ||
allow(apo.datastreams).to receive(:[]).with('administrativeMetadata').and_return(nil) | ||
end | ||
|
||
it { is_expected.to eq 'default' } | ||
end | ||
end | ||
|
||
context 'when the object does not have an apo' do | ||
before do | ||
allow(item).to receive(:admin_policy_object).and_return(nil) | ||
end | ||
|
||
it { is_expected.to eq 'default' } | ||
end | ||
|
||
context 'when the object is newly created' do | ||
let(:item) do | ||
Dor::Item.new.tap do |i| | ||
i.admin_policy_object = instantiate_fixture('druid:zt570tx3016', Dor::AdminPolicyObject) | ||
end | ||
end | ||
it { is_expected.to eq 'default' } | ||
end | ||
end | ||
end |