Class: Hyrax::Workflow::WorkflowImporter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
app/services/hyrax/workflow/workflow_importer.rb

Overview

Responsible for loading workflows from a data source.

Defined Under Namespace

Modules: SchemaValidator

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, permission_template:, schema: default_schema, validator: default_validator, logger: default_logger) ⇒ WorkflowImporter

Returns a new instance of WorkflowImporter.

Parameters:

  • data (#deep_symbolize_keys)
    • the configuration information from which we will generate all the data entries

  • permission_template (Hyrax::PermissionTemplate)
    • the permission_template that will be associated with each of these entries

  • schema (#call) (defaults to: default_schema)
    • The schema in which you will validate the data

  • validator (#call) (defaults to: default_validator)
    • The validation service for the given data and schema

  • logger (#debug, #info, #fatal, #warning) (defaults to: default_logger)
    • The logger to capture any meaningful output



92
93
94
95
96
97
98
99
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 92

def initialize(data:, permission_template:, schema: default_schema, validator: default_validator, logger: default_logger)
  self.data = data
  self.schema = schema
  self.validator = validator
  self.permission_template = permission_template
  @logger = logger
  validate!
end

Class Attribute Details

.load_errorsObject

Returns the value of attribute load_errors.



18
19
20
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 18

def load_errors
  @load_errors
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



125
126
127
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 125

def errors
  @errors
end

Class Method Details

.clear_load_errors!Object



14
15
16
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 14

def clear_load_errors!
  self.load_errors = []
end

.generate_from_hash(data:, permission_template:, **keywords) ⇒ Array<Sipity::Workflow>

Responsible for generating the work type and corresponding processing entries based on given pathname or JSON document.

Parameters:

  • data (#deep_symbolize_keys)
    • the configuration information from which we will generate all the data entries

  • permission_template (Hyrax::PermissionTemplate)
    • the permission_template that will be associated with each of these entries

Returns:



79
80
81
82
83
84
85
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 79

def self.generate_from_hash(data:, permission_template:, **keywords)
  importer = new(data: data, permission_template: permission_template, **keywords)
  workflows = importer.call
  self.load_errors ||= []
  load_errors.concat(importer.errors)
  workflows
end

.generate_from_json_file(path:, permission_template:, **keywords) ⇒ Array<Sipity::Workflow>

Responsible for generating the work type and corresponding processing entries based on given pathname or JSON document.

Parameters:

  • path (#read or String)
    • the location on the file system that can be read

  • permission_template (Hyrax::PermissionTemplate)
    • the permission_template that will be associated with each of these entries

Returns:



66
67
68
69
70
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 66

def self.generate_from_json_file(path:, permission_template:, **keywords)
  contents = path.respond_to?(:read) ? path.read : File.read(path)
  data = JSON.parse(contents)
  generate_from_hash(data: data, permission_template: permission_template, **keywords)
end

.load_workflow_for(permission_template:, logger: default_logger) ⇒ TrueClass, FalseClass

Note:

I’d like to deprecate .load_workflows but for now that is beyond the scope of what I’m after. So I will use its magic instead

Load all of the workflows for the given permission_template

Parameters:

  • permission_template (Hyrax::PermissionTemplate)
  • logger (#info, #debug, #warning, #fatal) (defaults to: default_logger)
    • By default this is Hyrax::Workflow::WorkflowImporter.default_logger

Returns:

  • (TrueClass)

    if one or more workflows were loaded

  • (FalseClass)

    if no workflows were loaded



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 33

def self.load_workflow_for(permission_template:, logger: default_logger)
  workflow_config_filenames = Dir.glob(path_to_workflow_files)
  if workflow_config_filenames.none?
    logger.info("Unable to load workflows for #{permission_template.class} ID=#{permission_template.id}. No workflows were found in #{path_to_workflow_files}")
    return false
  end
  workflow_config_filenames.each do |config|
    logger.info "Loading permission_template ID=#{permission_template.id} with workflow config #{config}"
    generate_from_json_file(path: config, permission_template: permission_template, logger: default_logger)
  end
  true
end

.load_workflows(permission_templates: Hyrax::PermissionTemplate.all, **kwargs) ⇒ TrueClass

Load all the workflows in config/workflows/*.json for each of the permission templates

Parameters:

  • permission_templates (#each) (defaults to: Hyrax::PermissionTemplate.all)
    • An enumerator of permission templates (by default Hyrax::PermissionTemplate.all)

Returns:

  • (TrueClass)


51
52
53
54
55
56
57
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 51

def self.load_workflows(permission_templates: Hyrax::PermissionTemplate.all, **kwargs)
  clear_load_errors!
  Array.wrap(permission_templates).each do |permission_template|
    load_workflow_for(permission_template: permission_template, **kwargs)
  end
  true
end

Instance Method Details

#callObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/services/hyrax/workflow/workflow_importer.rb', line 127

def call
  self.errors = []
  Array.wrap(data.fetch(:workflows)).map do |configuration|
    begin
      find_or_create_from(configuration: configuration)
    rescue InvalidStateRemovalException => e
      e.states.each do |state|
        error = I18n.t('hyrax.workflow.load.state_error', workflow_name: state.workflow.name, state_name: state.name, entity_count: state.entities.count)
        Rails.logger.error(error)
        errors << error
      end
      Sipity::Workflow.find_by(name: configuration[:name])
    end
  end
end