Here is an Import class that can be used to import records from Lotus Notes.
In this example, you would have a Notes view called ‘pages’ and ‘adverts’ that have view columns that match the table columns for your page and advert ActiveRecord model and tables.
For instance, if you have a pages table in MySQL with a key and body column, this would need to have a notes view with a column called ‘key’ and one called ‘body’. Also it assumes you follow the naming convention for ActiveRecord and a pages table would have a Page model class.
Rake task
namespace :db do desc "Import from the notes database" task :import_notes => :environment do import = Import.new import.load_obj("pages", Page) import.load_obj("adverts", Advert) end end
import.rb
class Import
BASEHOST = "yourserver.com"
DBPATH = "/path/database.nsf"
BASEURL = "http://#{BASEHOST}#{DBPATH}"
def load_obj(name, class_name, get_image=false, directory="cards")
@url = "#{BASEURL}/#{name}?ReadViewEntries&Count=5000"
@content = Net::HTTP.get(URI.parse(@url))
doc = REXML::Document.new(@content)
my_hash = Hash.new
REXML::XPath.each(doc,"/viewentries/viewentry") { |xml|
my_hash[:unid] = xml.attributes['unid']
REXML::XPath.each(xml, 'entrydata') { |col|
process_entrydata(my_hash, col)
}
@obj = class_name.find_by_unid(my_hash[:unid])
@obj = class_name.new() unless @obj
@obj.update_attributes(my_hash)
@obj.save!
if get_image
if @obj.image
download_image(@obj.unid, @obj.image, directory)
end
end
}
end
def process_entrydata(some_hash, col)
if col.elements['text']
some_hash[col.attributes['name'].downcase.to_sym] = col.elements['text'].text
elsif col.elements['textlist']
list_element = col.elements['textlist']
list_array = Array.new
REXML::XPath.each(list_element, 'text') { |txt|
list_array << txt.text.chomp if txt.text
}
some_hash[col.attributes['name'].downcase.to_sym] = list_array
elsif col.elements['numberlist']
list_element = col.elements['numberlist']
list_array = Array.new
REXML::XPath.each(list_element, 'number') { |txt|
list_array << txt.text.to_f
}
some_hash[col.attributes['name'].downcase.to_sym] = list_array
elsif col.elements['datetime']
datetime = col.elements['datetime'].text
year = datetime.slice(0,4)
month = datetime.slice(4,2)
day = datetime.slice(6,2)
some_hash[col.attributes['name'].downcase.to_sym] = Time.local(year, month, day, 0, 0, 0)
else
some_hash[col.attributes['name'].downcase.to_sym] = col.elements['number'].text.to_f
end
end
end
Here is an Import class that can be used to import records from Lotus Notes.
In this example, you would have a Notes view called ‘pages’ and ‘adverts’ that have view columns that match the table columns for your page and advert ActiveRecord model and tables.
For instance, if you have a pages table in MySQL with a key and body column, this would need to have a notes view with a column called ‘key’ and one called ‘body’. Also it assumes you follow the naming convention for ActiveRecord and a pages table would have a Page model class.
Rake task
namespace :db do desc "Import from the notes database" task :import_notes => :environment do import = Import.new import.load_obj("pages", Page) import.load_obj("adverts", Advert) end end
import.rb
class Import
BASEHOST = "yourserver.com"
DBPATH = "/path/database.nsf"
BASEURL = "http://#{BASEHOST}#{DBPATH}"
def load_obj(name, class_name, get_image=false, directory="cards")
@url = "#{BASEURL}/#{name}?ReadViewEntries&Count=5000"
@content = Net::HTTP.get(URI.parse(@url))
doc = REXML::Document.new(@content)
my_hash = Hash.new
REXML::XPath.each(doc,"/viewentries/viewentry") { |xml|
my_hash[:unid] = xml.attributes['unid']
REXML::XPath.each(xml, 'entrydata') { |col|
process_entrydata(my_hash, col)
}
@obj = class_name.find_by_unid(my_hash[:unid])
@obj = class_name.new() unless @obj
@obj.update_attributes(my_hash)
@obj.save!
if get_image
if @obj.image
download_image(@obj.unid, @obj.image, directory)
end
end
}
end
def process_entrydata(some_hash, col)
if col.elements['text']
some_hash[col.attributes['name'].downcase.to_sym] = col.elements['text'].text
elsif col.elements['textlist']
list_element = col.elements['textlist']
list_array = Array.new
REXML::XPath.each(list_element, 'text') { |txt|
list_array << txt.text.chomp if txt.text
}
some_hash[col.attributes['name'].downcase.to_sym] = list_array
elsif col.elements['numberlist']
list_element = col.elements['numberlist']
list_array = Array.new
REXML::XPath.each(list_element, 'number') { |txt|
list_array << txt.text.to_f
}
some_hash[col.attributes['name'].downcase.to_sym] = list_array
elsif col.elements['datetime']
datetime = col.elements['datetime'].text
year = datetime.slice(0,4)
month = datetime.slice(4,2)
day = datetime.slice(6,2)
some_hash[col.attributes['name'].downcase.to_sym] = Time.local(year, month, day, 0, 0, 0)
else
some_hash[col.attributes['name'].downcase.to_sym] = col.elements['number'].text.to_f
end
end
end