Class: Dor::Workflow::Document

Inherits:
Object
  • Object
show all
Includes:
OM::XML::Document, SolrDocHelper
Defined in:
lib/dor/workflow/document.rb

Constant Summary collapse

@@definitions =
{}

Instance Method Summary collapse

Methods included from SolrDocHelper

#add_solr_value

Constructor Details

#initialize(node) ⇒ Document


21
22
23
# File 'lib/dor/workflow/document.rb', line 21

def initialize node
  self.ng_xml = Nokogiri::XML(node)
end

Instance Method Details

#[](value) ⇒ Object


60
61
62
# File 'lib/dor/workflow/document.rb', line 60

def [](value)
  self.processes.find { |p| p.name == value }
end

#active?Boolean


34
35
36
# File 'lib/dor/workflow/document.rb', line 34

def active?
  processes.any? { |proc| !proc.version }
end

#definitionObject


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dor/workflow/document.rb', line 38

def definition
  @definition ||= begin
    if @@definitions.has_key? self.workflowId.first
      @@definitions[self.workflowId.first]
    else
    wfo = Dor::WorkflowObject.find_by_name(self.workflowId.first)
    wf_def=wfo ? wfo.definition : nil
    @@definitions[self.workflowId.first] = wf_def
    wf_def
    end
  end
end

#expedited?Boolean

is this an incomplete workflow with steps that have a priority > 0


25
26
27
# File 'lib/dor/workflow/document.rb', line 25

def expedited?
  processes.any? { |proc| !proc.completed? && proc.priority.to_i > 0 }
end

#graph(parent = nil, dir = nil) ⇒ Object


51
52
53
54
55
56
57
58
# File 'lib/dor/workflow/document.rb', line 51

def graph(parent=nil, dir=nil)
  wf_definition = self.definition
  result = wf_definition ? Workflow::Graph.from_processes(wf_definition.repo, wf_definition.name, self.processes, parent) : nil
  unless result.nil?
    result['rankdir'] = dir || 'TB'
  end
  result
end

#inspectObject


126
127
128
# File 'lib/dor/workflow/document.rb', line 126

def inspect
  "#<#{self.class.name}:#{self.object_id}>"
end

#priorityInteger


30
31
32
# File 'lib/dor/workflow/document.rb', line 30

def priority
  processes.map {|proc| proc.priority.to_i }.detect(0) {|p| p > 0}
end

#processesObject


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dor/workflow/document.rb', line 64

def processes
  #if the workflow service didnt return any processes, dont return any processes from the reified wf
  if ng_xml.search("/workflow/process").length == 0
    return []
  end
  @processes ||= if self.definition
    self.definition.processes.collect do |process|
      node = ng_xml.at("/workflow/process[@name = '#{process.name}']")
      process.update!(node,self) unless node.nil?
      process
    end
  else
    self.find_by_terms(:workflow, :process).collect do |x|
      pnode = Dor::Workflow::Process.new(self.repository, self.workflowId, {})
      pnode.update!(x,self)
      pnode
    end.sort_by(&:datetime)
  end
end

#to_solr(solr_doc = Hash.new, *args) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dor/workflow/document.rb', line 84

def to_solr(solr_doc=Hash.new, *args)
  wf_name = self.workflowId.first
  repo=self.repository.first
  add_solr_value(solr_doc, 'wf', wf_name, :string, [:facetable])
  add_solr_value(solr_doc, 'wf_wps', wf_name, :string, [:facetable])
  add_solr_value(solr_doc, 'wf_wsp', wf_name, :string, [:facetable])
  status = processes.empty? ? 'empty' : (processes.all?(&:completed?) ? 'completed' : 'active')
  errors = processes.select(&:error?).count
  add_solr_value(solr_doc, 'workflow_status', [wf_name,status,errors,repo].join('|'), :string, [:displayable])

  processes.each do |process|
    if process.status.present?
      #add a record of the robot having operated on this item, so we can track robot activity
      if process.date_time and process.status and (process.status == 'completed' || process.status == 'error')
        add_solr_value(solr_doc, "wf_#{wf_name}_#{process.name}", process.date_time+'Z', :date)
      end
      add_solr_value(solr_doc, 'wf_error', "#{wf_name}:#{process.name}:#{process.error_message}", :string, [:facetable,:displayable]) if process.error_message #index the error message without the druid so we hopefully get some overlap
      add_solr_value(solr_doc, 'wf_wsp', "#{wf_name}:#{process.status}", :string, [:facetable])
      add_solr_value(solr_doc, 'wf_wsp', "#{wf_name}:#{process.status}:#{process.name}", :string, [:facetable])
      add_solr_value(solr_doc, 'wf_wps', "#{wf_name}:#{process.name}", :string, [:facetable])
      add_solr_value(solr_doc, 'wf_wps', "#{wf_name}:#{process.name}:#{process.status}", :string, [:facetable])
      add_solr_value(solr_doc, 'wf_swp', "#{process.status}", :string, [:facetable])
      add_solr_value(solr_doc, 'wf_swp', "#{process.status}:#{wf_name}", :string, [:facetable])
      add_solr_value(solr_doc, 'wf_swp', "#{process.status}:#{wf_name}:#{process.name}", :string, [:facetable])
      if process.state != process.status
        add_solr_value(solr_doc, 'wf_wsp', "#{wf_name}:#{process.state}:#{process.name}", :string, [:facetable])
        add_solr_value(solr_doc, 'wf_wps', "#{wf_name}:#{process.name}:#{process.state}", :string, [:facetable])
        add_solr_value(solr_doc, 'wf_swp', "#{process.state}", :string, [:facetable])
        add_solr_value(solr_doc, 'wf_swp', "#{process.state}:#{wf_name}", :string, [:facetable])
        add_solr_value(solr_doc, 'wf_swp', "#{process.state}:#{wf_name}:#{process.name}", :string, [:facetable])
      end
    end
  end

  solr_doc['wf_wps_facet'].uniq!    if solr_doc['wf_wps_facet']
  solr_doc['wf_wsp_facet'].uniq!    if solr_doc['wf_wsp_facet']
  solr_doc['wf_swp_facet'].uniq!    if solr_doc['wf_swp_facet']
  solr_doc['workflow_status'].uniq! if solr_doc['workflow_status']

  solr_doc
end