|
# !/usr/bin/env ruby |
|
|
|
# In general this script does the following, assuming that it is run within a Rails context: |
|
# ```ruby |
|
# work = ActiveFedora::Base.find(patent_pid, cast: true) |
|
# collection = LibraryCollection(collection_pid) |
|
# return true if work.library_collection_ids.include?(collection.id) |
|
# work.library_collection_ids += collection.id |
|
# work.save! |
|
# ``` |
|
|
|
require 'optparse' |
|
config = { |
|
environment: 'development' |
|
} |
|
|
|
command_name = File.basename(__FILE__) |
|
|
|
OptionParser.new do |options| |
|
# This banner is the first line of your help documentation. |
|
options.set_banner "Usage: bundle exec rails runner #{command_name} --collection_id [PID] [files]\n\n" \ |
|
"Associate items identified by PIDs in the source files with the given --collection_id.\n\n" \ |
|
"Each line of the given file is assumed to be a single pid\n\n " \ |
|
"Examples:\n" \ |
|
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_work_pids.txt\n" \ |
|
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt\n" \ |
|
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt | more\n" \ |
|
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt > output.txt\n" \ |
|
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt second_list_of_pids.txt\n" \ |
|
"\t$ echo 'und:abcdefg' | bundle exec rails runner #{command_name} -c 'und:123456789'" |
|
|
|
# Separator just adds a new line with the specified text. |
|
options.separator "" |
|
options.separator "Specific options:" |
|
|
|
# Note this is provided to "parrot" the parameters of rails runner |
|
options.on("-e", "--environment=name", String, "Specifies the environment for the runner to operate under (test/development/production).", "Default: #{config[:environment]}") do |environment| |
|
config[:environment] = environment |
|
end |
|
|
|
options.on("-c", "--collection_pid [PID]", String, "The full PID of the target Collection (REQUIRED)") do |collection_pid| |
|
config[:collection_pid] = collection_pid |
|
end |
|
|
|
options.on_tail("-h", "--help", "You're looking at it!") do |
|
$stderr.puts options |
|
exit 1 |
|
end |
|
end.parse! |
|
|
|
# Responsible for associating the PID to the config assigned collection |
|
def associate_pid(pid, config) |
|
pid = pid.strip |
|
collection = config.fetch(:collection) |
|
work = nil |
|
begin |
|
work = ActiveFedora::Base.find(pid, cast: true) |
|
rescue ActiveFedora::ObjectNotFoundError |
|
$stderr.puts "Unable to find ActiveFedora::Base ID='#{pid}' and cannot add to the #{collection.class} ID='#{collection.id}'" |
|
return false |
|
end |
|
|
|
if work.library_collection_ids.include?(collection.id) |
|
$stdout.puts "#{work.class} ID='#{work.id}' already associated with #{collection.class} ID='#{collection.id}'" |
|
return true |
|
else |
|
work.library_collection_ids += [collection.id] |
|
work.save! |
|
$stdout.puts "Added #{work.class} ID='#{work.id}' to #{collection.class} ID='#{collection.id}'" |
|
return true |
|
end |
|
end |
|
|
|
unless config.key?(:collection_pid) |
|
$stderr.puts "Error: #{command_name} requires a --collection_pid option\nSee '#{command_name} -h' for more information" |
|
exit 2 |
|
end |
|
|
|
begin |
|
$stdout.puts "Finding LibraryCollection ID='#{config.fetch(:collection_pid)}'" |
|
config[:collection] = LibraryCollection.find(config.fetch(:collection_pid)) |
|
rescue ActiveFedora::ObjectNotFoundError |
|
$stderr.puts "Unable to find LibraryCollection ID='#{config.fetch(:collection_pid)}'" |
|
exit 3 |
|
end |
|
|
|
# Keep reading lines of input as long as they're coming. |
|
while input = ARGF.gets |
|
input.each_line do |pid| |
|
begin |
|
associate_pid(pid, config) |
|
rescue Errno::EPIPE |
|
# sysexits(3) specifies that exit code 74 represent an IO error, |
|
# which is the likely situation |
|
exit(74) |
|
end |
|
end |
|
end |