lib/rtm/ontopia.rb in rtm-ontopia-0.2.0 vs lib/rtm/ontopia.rb in rtm-ontopia-0.2.1

- old
+ new

@@ -1,117 +1,176 @@ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 -begin +if Object.const_defined?("Gem") && rtmgem = Gem.loaded_specs["rtm-ontopia"] require 'rtm/javatmapi' -rescue LoadError +else javatmapi_path = File.expand_path(File.join(File.dirname(__FILE__), "../../../rtm-javatmapi/lib")) if File.directory?(javatmapi_path) - $:.unshift javatmapi_path + $LOAD_PATH.unshift javatmapi_path require 'rtm/javatmapi' end end Dir[File.join(File.dirname(__FILE__), 'ontopia/javalibs/*.jar')].each {|file| require file } -require (File.dirname(__FILE__) + '/ontopia/io/to_cxtm') +require File.join(File.dirname(__FILE__), '/ontopia/io/to_cxtm') module RTM class Ontopia < JavaTMAPI + require File.join(File.dirname(__FILE__), '/ontopia/rdbms/properties') identifier :ontopia def initialize(*args) super - set_tmsf(Java::NetOntopiaTopicmapsImplTmapi2::TopicMapSystemFactory.new) + if @params[:store] == :rdbms + # enhance all ontopia properties from rails-style config + ontopia_properties = RTM::Ontopia::Rdbms::Properties.rails2ontopia(@params[:store_config]) + + + # FIXME: this works only if properties was not specified as file + @params[:properties] ||= {} + ontopia_properties.each do |k,v| + @params[:properties][k] = v.to_s unless @params[:properties].key?(k) + end + end + + tmsf = Java::NetOntopiaTopicmapsImplTmapi2::TopicMapSystemFactory.new + set_tmsf(tmsf) set_properties(@params[:properties]) set_features(@params[:features]) create_system end + + # Migrate the Database to contain the Ontopia RDBMS schema. Uses the connection data provided with connect + def migrate_database + self.class.migrate_database(@params[:store_config]) + end + + # Drops the Ontopia schema from the database. This corresponds to migrating the database down. + def migrate_database_down + self.class.migrate_database_down(@params[:store_config]) + end + + # Migrate the Database to contain the Ontopia RDBMS schema. + # Uses a Rails-style database configuration hash to connect to the database. + # + # Additional hash-keys to override the defaults: + # :connection => an existing connection to be used instead of creating using given parameters + # :schema_sql => directly supply sql to execute instead of :schema_file or autodetect + # :schema_file => directly supply sql file to execute instead of autodetect + # :adapter_schema_name => directly supply the schema name (generic, h2, mysql, oracle8, oracle9i, oracle10g, postresql, sqlserver) + # :direction => :up|:down create or drop schema (defaults to :up) + # + def self.migrate_database(config) + require 'active_record' unless defined?(ActiveRecord) + require 'jdbc_adapter' unless defined?(ActiveRecord::ConnectionAdapters::Jdbc) + + # create a new anonymous class extending ActiveRecord::Base for a separate connection + anonymous_active_record_base = Class.new(ActiveRecord::Base) + + # use a supplied connection or create one using the config + connection = config[:connection] + unless connection + # connect to backend using the anonymous base class + anonymous_active_record_base.establish_connection(config) + connection = anonymous_active_record_base.connection + end + + # see if we were provided with a schema directly or get it from file + schema_sql = config[:schema_sql] + unless schema_sql + # should we migrate up or down? + direction = config[:direction] || :up + create_or_drop = direction.to_s == "up" ? "create" : "drop" + + # get the adapter name for ontopia from the config + adapter_schema_name = config[:adapter_schema_name] || config[:adapter].sub(/^jdbc/, '') + + # check if it is one of the available adapters + available_schemas = %w[generic h2 mysql oracle8 oracle9i oracle10g postresql sqlserver] + adapter_schema_name = "generic" unless available_schemas.include?(adapter_schema_name) + + # find the corresponding schema file + schema_file = config[:schema_file] || File.join(File.dirname(__FILE__), "/../../res/rdbms/setup/#{adapter_schema_name}.#{create_or_drop}.sql") + raise "Could not find schema definition file #{File.expand_path(schema_file)}." unless File.exist?(schema_file) + + # read the schema + schema_sql = File.read(schema_file) + end + + # execute the schema + connection.execute(schema_sql) + end + + # Drops the Ontopia schema from the database. This corresponds to migrating the database down. + def self.migrate_database_down(params={}) + migrate_database(params.merge(:direction => :down)) + end end end -module Java::OrgTmapiCore::TopicMap - include RTM::TopicMap - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl) - superize -end +Java::OrgTmapiCore::TopicMap.register_java_implementation net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl + module Java::OrgTmapiCore::Topic - include RTM::Topic - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.TopicImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.TopicImpl end module Java::OrgTmapiCore::Association - include RTM::Association - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.AssociationImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.AssociationImpl end module Java::OrgTmapiCore::Name - include RTM::Name - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.NameImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.NameImpl end -# there is nothing superized in occurrence -# module Java::OrgTmapiCore::Occurrence -# include RTM::Occurrence -# JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl) -# superize -# end + module Java::OrgTmapiCore::Occurrence + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl + end -# ther is nothing superized in variant -# module Java::OrgTmapiCore::Variant -# include RTM::Variant -# JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.VariantImpl) -# superize -# end +module Java::OrgTmapiCore::Variant + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.VariantImpl +end module Java::OrgTmapiCore::Role - include RTM::Role - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.RoleImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.RoleImpl end module Java::OrgTmapiCore::Scoped - include RTM::Scoped - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.ScopedImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.AssociationImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.NameImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.VariantImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.ScopedImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.AssociationImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.NameImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.VariantImpl end module Java::OrgTmapiCore::Typed - include RTM::Typed - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.AssociationImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.NameImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.RoleImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.AssociationImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.NameImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.RoleImpl end module Java::OrgTmapiCore::Construct - include RTM::Construct - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.ConstructImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.TopicImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.AssociationImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.NameImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.VariantImpl) - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.RoleImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.ConstructImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.TopicImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.AssociationImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.NameImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.VariantImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.RoleImpl end module Java::OrgTmapiCore::Reifiable - include RTM::Reifiable - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.ReifiableImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.ReifiableImpl end module Java::OrgTmapiCore::Locator - include RTM::Locator - JavaImplementations.add(self,net.ontopia.topicmaps.impl.tmapi2.LocatorImpl) - superize + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.LocatorImpl +end + +module Java::OrgTmapiCore::DatatypeAware + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.DatatypeAwareImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.VariantImpl + register_java_implementation net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl end