Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #444 from sul-dlss/process-service
Browse files Browse the repository at this point in the history
Extract ProcessService from Processable
  • Loading branch information
jcoyne committed Dec 21, 2018
2 parents 86e3d7d + 0f1fabf commit 27d0828
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 330 deletions.
21 changes: 9 additions & 12 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-12-20 22:37:55 -0600 using RuboCop version 0.60.0.
# on 2018-12-21 10:32:47 -0600 using RuboCop version 0.60.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -25,12 +25,10 @@ Lint/AssignmentInCondition:
- 'lib/dor/datastreams/workflow_definition_ds.rb'
- 'lib/dor/utils/predicate_patch.rb'

# Offense count: 5
# Offense count: 3
Lint/HandleExceptions:
Exclude:
- 'lib/dor/indexers/processable_indexer.rb'
- 'lib/dor/models/concerns/contentable.rb'
- 'lib/dor/models/concerns/processable.rb'
- 'script/console'
- 'spec/support/foxml_helper.rb'

Expand Down Expand Up @@ -74,7 +72,7 @@ Lint/Void:
Metrics/AbcSize:
Max: 122

# Offense count: 190
# Offense count: 195
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Expand All @@ -85,16 +83,16 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 253

# Offense count: 18
# Offense count: 17
Metrics/CyclomaticComplexity:
Max: 25

# Offense count: 82
# Offense count: 79
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 58

# Offense count: 6
# Offense count: 5
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 236
Expand All @@ -104,7 +102,7 @@ Metrics/ModuleLength:
Metrics/ParameterLists:
Max: 7

# Offense count: 18
# Offense count: 17
Metrics/PerceivedComplexity:
Max: 25

Expand Down Expand Up @@ -182,7 +180,7 @@ Naming/UncommunicativeMethodParamName:
- 'lib/dor/models/concerns/editable.rb'
- 'lib/dor/models/concerns/rightsable.rb'

# Offense count: 18
# Offense count: 16
# Configuration parameters: EnforcedStyle.
# SupportedStyles: snake_case, camelCase
Naming/VariableName:
Expand All @@ -194,7 +192,6 @@ Naming/VariableName:
- 'spec/datastreams/content_metadata_ds_spec.rb'
- 'spec/datastreams/workflow_ds_spec.rb'
- 'spec/indexers/processable_indexer_spec.rb'
- 'spec/models/concerns/processable_spec.rb'
- 'spec/models/concerns/publishable_spec.rb'

# Offense count: 11
Expand Down Expand Up @@ -261,7 +258,7 @@ Style/ConditionalAssignment:
Style/DateTime:
Exclude:
- 'lib/dor/indexers/processable_indexer.rb'
- 'lib/dor/models/concerns/processable.rb'
- 'lib/dor/services/status_service.rb'

# Offense count: 76
Style/Documentation:
Expand Down
39 changes: 20 additions & 19 deletions lib/dor-services.rb
Expand Up @@ -149,35 +149,36 @@ def logger

# Services
autoload_under 'services' do
autoload :SearchService
autoload :ShelvingService
autoload :IndexingService
autoload :MetadataService
autoload :RegistrationService
autoload :SuriService
autoload :WorkflowService
autoload :DatastreamBuilder
autoload :DigitalStacksService
autoload :SdrIngestService
autoload :CleanupResetService
autoload :CleanupService
autoload :CreateWorkflowService
autoload :ProvenanceMetadataService
autoload :TechnicalMetadataService
autoload :CreativeCommonsLicenseService
autoload :DatastreamBuilder
autoload :DigitalStacksService
autoload :FileMetadataMergeService
autoload :SecondaryFileNameService
autoload :IndexingService
autoload :MetadataService
autoload :MergeService
autoload :ReleaseTagService
autoload :ResetWorkspaceService
autoload :CleanupResetService
autoload :Ontology
autoload :OpenDataLicenseService
autoload :ProvenanceMetadataService
autoload :PublicDescMetadataService
autoload :PublicXmlService
autoload :PublishMetadataService
autoload :RegistrationService
autoload :ReleaseTagService
autoload :ResetWorkspaceService
autoload :SdrIngestService
autoload :SearchService
autoload :SecondaryFileNameService
autoload :ShelvingService
autoload :StatusService
autoload :SuriService
autoload :TagService
autoload :TechnicalMetadataService
autoload :ThumbnailService
autoload :Ontology
autoload :CreativeCommonsLicenseService
autoload :OpenDataLicenseService
autoload :VersionService
autoload :WorkflowService
end

# Workflow Classes
Expand Down
25 changes: 14 additions & 11 deletions lib/dor/indexers/processable_indexer.rb
Expand Up @@ -7,13 +7,14 @@ class ProcessableIndexer
attr_reader :resource
def initialize(resource:)
@resource = resource
@status_service = StatusService.new(resource)
end

# @return [Hash] the partial solr document for processable concerns
def to_solr
{}.tap do |solr_doc|
add_versions(solr_doc)
add_milestones(solr_doc, resource.milestones)
add_milestones(solr_doc)
solr_doc['modified_latest_dttsi'] = resource.modified_date.to_datetime.utc.strftime('%FT%TZ')
add_solr_value(solr_doc, 'rights', resource.rights, :string, [:symbol]) if resource.respond_to? :rights
add_status(solr_doc)
Expand All @@ -22,6 +23,8 @@ def to_solr

private

attr_reader :status_service

def current_version
@current_version ||= begin
resource.versionMetadata.current_version_id
Expand All @@ -31,36 +34,36 @@ def current_version
end

def add_status(solr_doc)
solr_doc['status_ssi'] = resource.status # status is singular (i.e. the current one)
status_info_hash = resource.status_info
solr_doc['status_ssi'] = status_service.status # status is singular (i.e. the current one)
status_info_hash = status_service.status_info
status_code = status_info_hash[:status_code]
add_solr_value(solr_doc, 'processing_status_text', simplified_status_code_disp_txt(status_code), :string, [:stored_sortable])
solr_doc['processing_status_code_isi'] = status_code
end

def add_milestones(solr_doc, milestones)
milestones.each do |milestone|
def add_milestones(solr_doc)
status_service.milestones.each do |milestone|
timestamp = milestone[:at].utc.xmlschema
milestone[:version] ||= current_version
solr_doc['lifecycle_ssim'] ||= []
solr_doc['lifecycle_ssim'] << milestone[:milestone]
add_solr_value(solr_doc, 'lifecycle', "#{milestone[:milestone]}:#{timestamp};#{milestone[:version]}", :symbol)
end

add_sortable_milestones(solr_doc, milestones)
add_sortable_milestones(solr_doc)
end

def sortable_milestones(milestones)
def sortable_milestones
sortable = {}
milestones.each do |milestone|
status_service.milestones.each do |milestone|
sortable[milestone[:milestone]] ||= []
sortable[milestone[:milestone]] << milestone[:at].utc.xmlschema
end
sortable
end

def add_sortable_milestones(solr_doc, milestones)
sortable_milestones(milestones).each do |milestone, unordered_dates|
def add_sortable_milestones(solr_doc)
sortable_milestones.each do |milestone, unordered_dates|
dates = unordered_dates.sort
# create the published_dttsi and published_day fields and the like
dates.each do |date|
Expand Down Expand Up @@ -90,7 +93,7 @@ def add_versions(solr_doc)
# @return [String] text translation of the status code, minus any trailing parenthetical explanation
# e.g. 'In accessioning (described)' and 'In accessioning (described, published)' both return 'In accessioning'
def simplified_status_code_disp_txt(status_code)
Processable::STATUS_CODE_DISP_TXT[status_code].gsub(/\(.*\)$/, '').strip
StatusService::STATUS_CODE_DISP_TXT[status_code].gsub(/\(.*\)$/, '').strip
end
end
end
92 changes: 8 additions & 84 deletions lib/dor/models/concerns/processable.rb
Expand Up @@ -16,33 +16,6 @@ module Processable
autocreate: true
end

# verbiage we want to use to describe an item when it has completed a particular step
STATUS_CODE_DISP_TXT = {
0 => 'Unknown Status', # if there are no milestones for the current version, someone likely messed up the versioning process.
1 => 'Registered',
2 => 'In accessioning',
3 => 'In accessioning (described)',
4 => 'In accessioning (described, published)',
5 => 'In accessioning (described, published, deposited)',
6 => 'Accessioned',
7 => 'Accessioned (indexed)',
8 => 'Accessioned (indexed, ingested)',
9 => 'Opened'
}.freeze

# milestones from accessioning and the order they happen in
STEPS = {
'registered' => 1,
'submitted' => 2,
'described' => 3,
'published' => 4,
'deposited' => 5,
'accessioned' => 6,
'indexed' => 7,
'shelved' => 8,
'opened' => 9
}.freeze

# The ContentMetadata and DescMetadata robot are allowed to build the
# datastream by reading a file from the /dor/workspace that matches the
# datastream name. This allows assembly or pre-assembly to prebuild the
Expand Down Expand Up @@ -73,55 +46,23 @@ def cleanup
CleanupService.cleanup(self)
end

def milestones
@milestones ||= Dor::Config.workflow.client.get_milestones('dor', pid)
end

# @return [Hash{Symbol => Object}] including :current_version, :status_code and :status_time
def status_info
current_version = '1'
begin
current_version = versionMetadata.current_version_id
rescue StandardError
end

current_milestones = []
# only get steps that are part of accessioning and part of the current version. That can mean they were archived with the current version
# number, or they might be active (no version number).
milestones.each do |m|
if STEPS.key?(m[:milestone]) && (m[:version].nil? || m[:version] == current_version)
current_milestones << m unless m[:milestone] == 'registered' && current_version.to_i > 1
end
end

status_code = 0
status_time = nil
# for each milestone in the current version, see if it comes after the current 'last' step, if so, make it the last and record the date/time
current_milestones.each do |m|
m_name = m[:milestone]
m_time = m[:at].utc.xmlschema
next unless STEPS.key?(m_name) && (!status_time || m_time > status_time)

status_code = STEPS[m_name]
status_time = m_time
end

{ current_version: current_version, status_code: status_code, status_time: status_time }
StatusService.status_info(self)
end
deprecation_deprecate status_info: 'Use StatusService.status_info instead'

# @param [Boolean] include_time
# @return [String] single composed status from status_info
def status(include_time = false)
status_info_hash = status_info
current_version = status_info_hash[:current_version]
status_code = status_info_hash[:status_code]
status_time = status_info_hash[:status_time]
StatusService.status(self, include_time)
end
deprecation_deprecate status: 'Use StatusService.status instead'

# use the translation table to get the appropriate verbage for the latest step
result = "v#{current_version} #{STATUS_CODE_DISP_TXT[status_code]}"
result += " #{format_date(status_time)}" if include_time
result
def milestones
StatusService.new(self).milestones
end
deprecation_deprecate status_info: 'Use StatusService#milestones instead'

# 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
Expand All @@ -138,22 +79,5 @@ def initialize_workflow(name, create_ds = true, priority = 0)
warn 'WARNING: initialize_workflow is deprecated, use create_workflow instead'
create_workflow(name, create_ds, priority)
end

private

# handles formating utc date/time to human readable
# XXX: bad form to hardcode TZ here. Code smell abounds.
def format_date(datetime)
d =
if datetime.is_a?(Time)
datetime
else
DateTime.parse(datetime).in_time_zone(ActiveSupport::TimeZone.new('Pacific Time (US & Canada)'))
end
I18n.l(d).strftime('%Y-%m-%d %I:%M%p')
rescue StandardError
d = datetime.is_a?(Time) ? datetime : Time.parse(datetime.to_s)
d.strftime('%Y-%m-%d %I:%M%p')
end
end
end

0 comments on commit 27d0828

Please sign in to comment.