# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 if Object.const_defined?("Gem") && rtmgem = Gem.loaded_specs["rtm-ontopia"] require 'rtm/javatmapi' else javatmapi_path = File.expand_path(File.join(File.dirname(__FILE__), "../../../rtm-javatmapi/lib")) if File.directory?(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.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 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 Java::OrgTmapiCore::TopicMap.register_java_implementation net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl module Java::OrgTmapiCore::Topic register_java_implementation net.ontopia.topicmaps.impl.tmapi2.TopicImpl end module Java::OrgTmapiCore::Association register_java_implementation net.ontopia.topicmaps.impl.tmapi2.AssociationImpl end module Java::OrgTmapiCore::Name register_java_implementation net.ontopia.topicmaps.impl.tmapi2.NameImpl end module Java::OrgTmapiCore::Occurrence register_java_implementation net.ontopia.topicmaps.impl.tmapi2.OccurrenceImpl end module Java::OrgTmapiCore::Variant register_java_implementation net.ontopia.topicmaps.impl.tmapi2.VariantImpl end module Java::OrgTmapiCore::Role register_java_implementation net.ontopia.topicmaps.impl.tmapi2.RoleImpl end module Java::OrgTmapiCore::Scoped 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 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 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 register_java_implementation net.ontopia.topicmaps.impl.tmapi2.ReifiableImpl end module Java::OrgTmapiCore::Locator 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