Class: Dor::MergeService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/merge_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary_druid, secondary_pids, tag, logger = nil) ⇒ MergeService

Returns a new instance of MergeService.



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

def initialize(primary_druid, secondary_pids, tag, logger = nil)
  @primary = Dor.find primary_druid
  @secondary_pids = secondary_pids
  @secondary_objs = secondary_pids.map { |pid| Dor.find pid }
  if logger.nil?
    @logger = Logger.new(STDERR)
  else
    @logger = logger
  end
  @tag = tag
end

Class Method Details

.merge_into_primary(primary_druid, secondary_druids, tag, logger = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/dor/services/merge_service.rb', line 5

def self.merge_into_primary(primary_druid, secondary_druids, tag, logger = nil)
  # TODO: test the secondary_obj to see if we've processed it already
  merge_service = Dor::MergeService.new primary_druid, secondary_druids, tag, logger
  merge_service.check_objects_editable
  merge_service.
  merge_service.decommission_secondaries
  # kick off commonAccessioning for the primary?
end

Instance Method Details

#check_objects_editableObject

Raises:



26
27
28
29
30
31
# File 'lib/dor/services/merge_service.rb', line 26

def check_objects_editable
  raise Dor::Exception, "Primary object is not editable: #{@primary.pid}" unless @primary.allows_modification?

  non_editable = @secondary_objs.detect { |obj| !obj.allows_modification? }
  raise Dor::Exception, "Secondary object is not editable: #{non_editable.pid}" if non_editable
end

#copy_workspace_contentObject

Copies the content from the secondary object workspace to the primary object’s workspace

Depends on Dor::Config.stacks.local_workspace_root


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dor/services/merge_service.rb', line 41

def copy_workspace_content
  pri_file = @primary..resource(0).file(0).id.first
  pri_druid = DruidTools::Druid.new @primary.pid, Dor::Config.stacks.local_workspace_root
  dest_path = pri_druid.find_filelist_parent 'content', pri_file
  primary_cm = @primary..ng_xml

  @secondary_objs.each do |secondary|
    sec_druid = DruidTools::Druid.new secondary.pid, Dor::Config.stacks.local_workspace_root
    secondary..ng_xml.xpath('//resource').each do |src_resource|
      primary_resource = primary_cm.at_xpath "//resource[attr[@name = 'mergedFromPid']/text() = '#{secondary.pid}' and
                                                         attr[@name = 'mergedFromResource']/text() = '#{src_resource['id']}' ]"
      sequence = primary_resource['sequence']
      src_resource.xpath('//file/@id').map(&:value).each do |file_id|
        copy_path = sec_druid.find_content file_id
        new_name = SecondaryFileNameService.create(file_id, sequence)
        # TODO: verify new_name exists in primary_cm?
        FileUtils.cp(copy_path, File.join(dest_path, "/#{new_name}"))
      end
    end
  end
end

#decommission_secondariesObject Also known as: decomission_secondaries



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dor/services/merge_service.rb', line 63

def decommission_secondaries
  @secondary_objs.each do |secondary_obj|
    begin
      @current_secondary = secondary_obj
      @current_secondary.decommission @tag
      @current_secondary.save

      unshelve
      unpublish
      Dor::CleanupService.cleanup_by_druid @current_secondary.pid
      Dor::Config.workflow.client.archive_active_workflow 'dor', @current_secondary.pid
    rescue StandardError => e
      @logger.error "Unable to decommission #{@current_secondary.pid} with primary object #{@primary.pid}: #{e.inspect}"
      @logger.error e.backtrace.join("\n")
    end
  end
end

#move_metadata_and_contentObject



33
34
35
36
37
# File 'lib/dor/services/merge_service.rb', line 33

def 
  FileMetadataMergeService.copy_file_resources @primary, @secondary_pids
  @primary.save
  copy_workspace_content
end

#unpublishObject

Withdraw item from Purl TODO: might set workflow status in future for robot to do



91
92
93
# File 'lib/dor/services/merge_service.rb', line 91

def unpublish
  @current_secondary.
end

#unshelveObject

Remove content from stacks TODO: might set workflow status in future for robot to do



85
86
87
# File 'lib/dor/services/merge_service.rb', line 85

def unshelve
  DigitalStacksService.prune_stacks_dir @current_secondary.pid
end