Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 1.89 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'rubygems'
  2. require 'active-fedora'
  3. require 'open-uri'
  4. require File.expand_path(File.dirname(__FILE__) + '/boot')
  5.  
  6. class DeleteFedoraObjects
  7.  
  8.   attr_reader :repository
  9.   attr_reader :workflow
  10.   attr_reader :registration_robot
  11.   attr_reader :druid_list
  12.  
  13.   # Given a repository, a workflow and the name of a robot that registers objects in fedora
  14.   # we can generate a list of all the druids that were created by that robot
  15.   def initialize(repository, workflow, registration_robot)
  16.     @repository = repository
  17.     @workflow = workflow
  18.     @registration_robot = registration_robot
  19.     @druid_list = self.get_druid_list
  20.   end
  21.  
  22.   def get_druid_list
  23.     begin
  24.       druid_list = []
  25.       url_string = "#{WORKFLOW_URI}/workflow_queue?repository=#{@repository}&workflow=#{@workflow}&completed=#{@registration_robot}"
  26.       doc = Nokogiri::XML(open(url_string))
  27.       doc.xpath("//objects/object/@id").each do |id|
  28.         druid_list << id.to_s
  29.       end
  30.       return druid_list
  31.     rescue Exception => e
  32.       raise e, "Couldn't fetch druid list from #{url_string}"
  33.     end
  34.   end
  35.    
  36.     # Iterate through the druids in @druid_list and delete each of them from FEDORA
  37.     def delete_druids
  38.       connect_to_fedora
  39.       @druid_list.each do |druid|
  40.         obj = ActiveFedora::Base.load_instance(druid)
  41.         puts obj.inspect
  42.       end
  43.     end
  44.    
  45.     def connect_to_fedora
  46.       begin
  47.         if @repository == "dor"
  48.           repo_url = FEDORA_URI
  49.         elsif @repository == "sdr"
  50.           repo_url = SEDORA_URI
  51.         else
  52.           raise "I don't know how to connect to repository #{@repository}"
  53.         end
  54.         puts "connecting to #{repo_url}"
  55.         Fedora::Repository.register(repo_url)
  56.       rescue Errno::ECONNREFUSED => e
  57.        raise e, "Can't connect to Fedora at url #{repo_url}"
  58.       end
  59.     end
  60.    
  61. end
  62.  
  63. dfo = DeleteFedoraObjects.new("dor","googleScannedBookWF", "register-object")
  64. dfo.delete_druids