Skip to content

Instantly share code, notes, and snippets.

@kardeiz
Created November 11, 2014 16:33
Show Gist options
  • Save kardeiz/c8ab990614dbbcb31213 to your computer and use it in GitHub Desktop.
Save kardeiz/c8ab990614dbbcb31213 to your computer and use it in GitHub Desktop.
script to output DSpace resource policy info
#!/bin/env jruby
require 'csv'
DSPACE_CFG = 'path to your dspace.cfg'
DSPACE_DIR = File.expand_path('../..', DSPACE_CFG)
DSPACE_JARS = Dir[File.join(DSPACE_DIR, 'lib/*.jar')]
ADMIN_EMAIL = 'your admin email'
OUTPUT = '/tmp/rp.csv'
DSPACE_JARS.each {|jar| require jar }
ConfigurationManager = org.dspace.core.ConfigurationManager
EPerson = org.dspace.eperson.EPerson
Context = org.dspace.core.Context
HandleManager = org.dspace.handle.HandleManager
AuthorizeManager = org.dspace.authorize.AuthorizeManager
Item = org.dspace.content.Item
DSpaceObject = org.dspace.content.DSpaceObject
ResourcePolicy = org.dspace.authorize.ResourcePolicy
TableRow = org.dspace.storage.rdbms.TableRow
ConfigurationManager.load_config DSPACE_CFG
DatabaseManager = org.dspace.storage.rdbms.DatabaseManager
def admin_context
@admin_context ||= Context.new.tap do |o|
o.current_user = EPerson.find_by_email(o, ADMIN_EMAIL)
end
end
class ResourcePolicy
field_accessor :myContext
def resource
DSpaceObject.find(myContext, resource_type, resource_id)
end
def self.find_all(context)
csr = ResourcePolicy.java_class.declared_constructor(Context, TableRow)
csr.accessible = true
q = "SELECT * FROM resourcepolicy"
rows = DatabaseManager.queryTable(context, "resourcepolicy", q)
[].tap do |o|
while row = rows.next do
o << csr.new_instance(context, row).to_java
end
end
end
def fields_for_audit
resource, eperson, group = self.resource, self.eperson, self.group
{
name: rp_name,
type: rp_type,
start_date: ( start_date && start_date.to_s ),
end_date: ( end_date && end_date.to_s ),
resource_type: ( resource && resource.type_text ),
resource_id: ( resource && resource.id ),
eperson: ( eperson && eperson.name ),
group: ( group && group.name )
}
end
end
CSV.open(OUTPUT, 'w') do |csv|
rps = ResourcePolicy.find_all(admin_context)
csv << keys = [:name, :type, :start_date, :end_date, :resource_type, :resource_id, :eperson, :group]
rps.each do |rp|
csv << rp.fields_for_audit.values_at(*keys)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment