Module: Dor::Processable

Extended by:
ActiveSupport::Concern
Includes:
Upgradable, SolrDocHelper
Included in:
AdminPolicyObject, BasicItem, Collection, Set, Versionable
Defined in:
lib/dor/models/processable.rb

Instance Method Summary collapse

Methods included from Upgradable

add_upgrade_callback, included, run_upgrade_callbacks, #upgrade!

Methods included from SolrDocHelper

#add_solr_value

Instance Method Details

#build_datastream(datastream, force = false, is_required = false) ⇒ Object

Takes the name of a datastream, as a string (fooMetadata). Builds that datastream using the content of a file if such a file exists and is newer than the object’s current datastream; otherwise, builds the datastream by calling build_fooMetadata_datastream.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dor/models/processable.rb', line 48

def build_datastream(datastream, force = false, is_required = false)
  # See if the datastream exists as a file and if the file's
  # timestamp is newer than the datastream's timestamp.
  ds       = datastreams[datastream]
  filename = (datastream)
  use_file = filename && (ds.createDate.nil? || File.mtime(filename) >= ds.createDate)
  # Build datastream.
  if use_file
    content = File.read(filename)
    ds.content = content
    ds.ng_xml = Nokogiri::XML(content) if ds.respond_to?(:ng_xml)
    ds.save unless ds.digital_object.new?
  elsif force or empty_datastream?(ds)
    meth = "build_#{datastream}_datastream".to_sym
    if respond_to?(meth)
      content = self.send(meth, ds)
      ds.save unless ds.digital_object.new?
    end
  end
  # Check for success.
  if is_required && empty_datastream?(ds)
    raise "Required datastream #{datastream} could not be populated!"
  end
  return ds
end

#cleanupObject



74
75
76
# File 'lib/dor/models/processable.rb', line 74

def cleanup()
  CleanupService.cleanup(self)
end

#empty_datastream?(datastream) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/dor/models/processable.rb', line 26

def empty_datastream?(datastream)
  if datastream.new?
    true
  elsif datastream.class.respond_to?(:xml_template)
    datastream.content.to_s.empty? or EquivalentXml.equivalent?(datastream.content, datastream.class.xml_template)
  else
    datastream.content.to_s.empty?
  end
end

#find_metadata_file(datastream) ⇒ Object

Takes the name of a datastream, as a string. Tries to find a file for the datastream. Returns the path to it or nil.



39
40
41
42
# File 'lib/dor/models/processable.rb', line 39

def (datastream)
  druid = DruidTools::Druid.new(pid, Dor::Config.stacks.local_workspace_root)
  return druid.("#{datastream}.xml")
end

#initialize_workflow(name, repo = 'dor', create_ds = true, priority = 0) ⇒ Object

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

Parameters:

  • name (String)

    of the workflow to be initialized

  • repo (String) (defaults to: 'dor')

    name of the repository to create workflow for

  • create_ds (Boolean) (defaults to: true)

    create a ‘workflows’ datastream in Fedora for the object



207
208
209
210
211
212
# File 'lib/dor/models/processable.rb', line 207

def initialize_workflow(name, repo='dor', create_ds=true, priority=0)
  priority = workflows.current_priority if priority == 0
  opts = { :create_ds => create_ds }
  opts[:priority] = priority if(priority > 0)
  Dor::WorkflowService.create_workflow(repo, self.pid, name, Dor::WorkflowObject.initial_workflow(name), opts)
end

#milestonesObject



78
79
80
# File 'lib/dor/models/processable.rb', line 78

def milestones
  Dor::WorkflowService.get_milestones('dor',self.pid)
end

#set_workflows_datastream_locationObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dor/models/processable.rb', line 14

def set_workflows_datastream_location
  # This is a work-around for some strange logic in ActiveFedora that
  # don't allow self.workflows.new? to work if we load the object using
  # .load_instance_from_solr.
  return if self.respond_to? :inner_object and self.inner_object.is_a? ActiveFedora::SolrDigitalObject

  if self.workflows.new?
    workflows.mimeType = 'application/xml'
    workflows.dsLocation = File.join(Dor::Config.workflow.url,"dor/objects/#{self.pid}/workflows")
  end
end

#status(include_time = false) ⇒ Object



81
82
83
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dor/models/processable.rb', line 81

def status(include_time=false)
    current_version='1'
    begin
      current_version = self..current_version_id
    rescue
    end
    status = 0
    version = ''
    #verbage we want to use to describe an item when it has completed a particular step
    status_hash={
      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'
    }
    #milestones from accesioning and the order they happen in
    steps={
      'registered' => 1,
      'submitted' => 2,
      'described' => 3,
      'published' => 4,
      'deposited' => 5,
      'accessioned' => 6,
      'indexed' => 7,
      'shelved' => 8,
      'opened' => 1
    }
    status_time=nil

    current=false
    versions=[]
    result=""
    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.keys.include?(m[:milestone]) and (m[:version].nil? or m[:version] == current_version)
        current_milestones << m unless m[:milestone] == 'registered' and current_version.to_i > 1
      end
    end
    status = 0
    status_time = ''
    #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|
      name=m[:milestone]
      time=m[:at].utc.xmlschema
      if steps.keys.include? name
        if steps[name] > status
          status = steps[name]
          status_time=time
        end
      end
    end
    #use the translation table to get the appropriate verbage for the latest step
    result='v'+current_version.to_s+' '+status_hash[status].to_s
    result +=" #{format_date(status_time)}" if include_time
    result
end

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dor/models/processable.rb', line 145

def to_solr(solr_doc=Hash.new, *args)
  super(solr_doc, *args)
  sortable_milestones = {}
  current_version='1'
  begin
    current_version = self..current_version_id
  rescue
  end
  current_version_num=current_version.to_i

  if self.respond_to?('versionMetadata')
    #add an entry with version id, tag and description for each version
    while current_version_num > 0
      add_solr_value(solr_doc, 'versions', current_version_num.to_s + ';' + self..tag_for_version(current_version_num.to_s) + ';' + self..description_for_version(current_version_num.to_s), :string, [:displayable])
      current_version_num -= 1
    end
  end

  self.milestones.each do |milestone|
    timestamp = milestone[:at].utc.xmlschema
    sortable_milestones[milestone[:milestone]] ||= []
    sortable_milestones[milestone[:milestone]] << timestamp
    add_solr_value(solr_doc, 'lifecycle', milestone[:milestone], :string, [:searchable, :facetable])
    unless milestone[:version]
      milestone[:version]=current_version
    end
    add_solr_value(solr_doc, 'lifecycle', "#{milestone[:milestone]}:#{timestamp};#{milestone[:version]}", :string, [:displayable])
  end

  sortable_milestones.each do |milestone, unordered_dates|
    dates = unordered_dates.sort
    #create the published_dt and published_day fields and the like
    add_solr_value(solr_doc, milestone+'_day', DateTime.parse(dates.last).beginning_of_day.utc.xmlschema.split('T').first, :string, [:searchable, :facetable])
    add_solr_value(solr_doc, milestone, dates.first, :date, [:searchable, :facetable])

    #fields for OAI havester to sort on
    add_solr_value(solr_doc, "#{milestone}_earliest_dt", dates.first, :date, [:sortable])
    add_solr_value(solr_doc, "#{milestone}_latest_dt", dates.last, :date, [:sortable])

    #for future faceting
    add_solr_value(solr_doc, "#{milestone}_earliest", dates.first, :date, [:searchable, :facetable])
    add_solr_value(solr_doc, "#{milestone}_latest", dates.last, :date, [ :searchable, :facetable])

  end
  add_solr_value(solr_doc,"status",status,:string, [:displayable])

  if sortable_milestones['opened']
    #add a facetable field for the date when the open version was opened
    opened_date=sortable_milestones['opened'].sort.last
    add_solr_value(solr_doc, "version_opened", DateTime.parse(opened_date).beginning_of_day.utc.xmlschema.split('T').first, :string, [ :searchable, :facetable])
  end
  add_solr_value(solr_doc, "current_version", current_version.to_s, :string, [ :displayable , :facetable])
  add_solr_value(solr_doc, "last_modified_day", self.modified_date.to_s.split('T').first, :string, [ :facetable ])
  add_solr_value(solr_doc, "rights", rights, :string, [:facetable]) if self.respond_to? :rights
  solr_doc
end