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


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::Item.find primary_druid
  @secondary_pids = secondary_pids
  @secondary_objs = secondary_pids.map {|pid|  Dor::Item.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 MergeService.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.decomission_secondaries
  # kick off commonAccessioning for the primary?
end

Instance Method Details

#check_objects_editableObject


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

def check_objects_editable
  unless @primary.allows_modification?
    raise Dor::Exception.new "Primary object is not editable: #{@primary.pid}"
  end
  if ( non_editable = (@secondary_objs.detect {|obj| ! obj.allows_modification? } ))
    raise Dor::Exception.new "Secondary object is not editable: #{non_editable.pid}"
  end
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

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

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 {|id| id.value }.each do |file_id|
        copy_path = sec_druid.find_content file_id
        new_name = secondary.new_secondary_file_name(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

#decomission_secondariesObject


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

def decomission_secondaries
  @secondary_objs.each do |secondary_obj|
    begin
      @current_secondary = secondary_obj
      @current_secondary.decomission @tag
      @current_secondary.save

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

#move_metadata_and_contentObject


35
36
37
38
39
# File 'lib/dor/services/merge_service.rb', line 35

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

#unpublishObject

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


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

def unpublish
  @current_secondary.
end

#unshelveObject

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


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

def unshelve
  DigitalStacksService.prune_stacks_dir @current_secondary.pid
end