#!/usr/bin/env ruby # encoding: UTF-8 require 'optparse' require 'druid-tools' def doit(druid, flags) puts "Processing #{druid.id}" begin item = Dor::Item.find(druid.druid) ap({:item => item, :collections => item.collections}) if flags[:debug] # remove all collections item.collections.dup.each {|c| item.remove_collection(c)} # add the new ones flags[:collections].each do |k, collection| item.add_collection(collection) end ap({:item => item, :collections => item.collections}) if flags[:debug] unless item.allows_modification? puts "WARNING: Item not editable: #{druid.id}" item.open_new_version end item.save item.close_version rescue ActiveFedora::ObjectNotFoundError => e puts "ERROR: #{e.message}" end end # __MAIN__ begin File.umask(002) flags = { :tmpdir => 'tmp', :verbose => false, :debug => false, :collections => {}, :workspacedir => '/var/geomdtk/current/workspace' } OptionParser.new do |opts| opts.banner = < flags}) if flags[:debug] # Validate collection druids flags[:collections].each do |druid,v| begin flags[:collections][druid] = Dor::Collection.find(druid) rescue ActiveFedora::ObjectNotFoundError => e puts "ERROR: Invalid collection #{druid}: #{e.message}" exit(-1) end end (ARGV.empty?? STDIN : ARGV).each do |pid| druid = DruidTools::Druid.new(pid.strip, flags[:workspacedir]) ap({:druid => druid}) if flags[:debug] begin doit(druid, flags) rescue Exception => e ap({:error => e}) end end rescue SystemCallError => e $stderr.puts "ERROR: #{e.message}" $stderr.puts e.backtrace exit(-1) end