class Port

notes: key_id lookup has been hardcoded to use @current_key. users will not be ported, but must be added as new users in new system.

Public Class Methods

new(source_db, target_db, target_db_postgresql, logger=nil, source_database_type=:sql_server) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 26
def initialize(source_db, target_db, target_db_postgresql, logger=nil, source_database_type=:sql_server)
  @user_params = {id: 0, user_name: "root"}
  @current_user = Schema::User.new(@user_params)
  @current_key = @current_user
  @logger = logger
  log "initialize"
  if source_database_type == :sql_server
    # sql server:
    @sql_true = "1"
    @sql_allow_order_bys_in_sub_select = "top 1000000000"
    @left_bracket = "["
    @right_bracket = "]"
    @convert_nvarchar_beg = "convert(nvarchar, "
    @convert_nvarchar_end = ")"
  else
    # postgresql:
    @sql_true = "true"
    @sql_allow_order_bys_in_sub_select = ""
    @left_bracket = ""
    @right_bracket = ""
    @convert_nvarchar_beg = ""
    @convert_nvarchar_end = "::text"
  end

  @source_db = source_db
  @target_db = target_db
  @target_db_postgresql = target_db_postgresql
  @lookup = Lookup.new(@target_db_postgresql)

  if @target_db.respond_to? :log_path
    Dir.mkdir @target_db.log_path unless Dir.exist? @target_db.log_path
    @error_log_file_name = "#{@target_db.log_path}/error_#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.log"
  else
    Dir.mkdir "../logs" unless Dir.exist? "../logs"
    @error_log_file_name = "../logs/error_#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.log"
  end
end

Public Instance Methods

create_db_and_schema() click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 81
def create_db_and_schema
  log 'create_db_and_schema'
  ZCreateDb.run @target_db.postgresql
end
create_log_table() click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 69
def create_log_table
  drop_table Controller::Log
  create_table Controller::Log
  @log_id = -1
end
db_log(msg, type, db) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 162
def db_log(msg, type, db)
  @log_id += 1
  Controller::Log.new(@current_user, @current_key, {id: @log_id, name: "zport_data", type: type, item: msg, timestamp: Time.now.utc}, db).insert if db
end
initial_data() click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 86
def initial_data
  log 'initial_data', @target_db_postgresql
  reset_table Controller::User
  reset_table Controller::Role
  reset_table Controller::UserRole
  ZBootstrap.root_data(@target_db.postgresql)
end
log(msg, db=nil) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 156
def log(msg, db=nil)
  puts "log info: #{msg}"
  @logger.info(msg)
  db_log(msg, 'info', db) if db
end
log_error(msg, db=nil) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 144
def log_error(msg, db=nil)
  puts "log error: #{msg}"
  @logger.error(msg)
  db_log(msg, 'error', db) if db
end
log_warn(msg, db=nil) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 150
def log_warn(msg, db=nil)
  puts "log warn: #{msg}"
  @logger.warn(msg)
  db_log(msg, 'warn', db) if db
end
reset_db() click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 64
def reset_db
  log 'reset_db'
  JunglePath::Gen::DB.reset!(@target_db.postgresql)
end
set_log_id() click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 75
def set_log_id
  log_id = @target_db_postgresql.get_max_id_for_table(:log)
  log_id = -1 if log_id == nil
  @log_id = log_id
end
user() click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 94
def user
  log 'user', @target_db_postgresql
  drop_table Controller::Contact
  create_table Controller::Contact
  sql = JunglePath::SQL::Helpers.sql("
    select
      a.ROW_ID as siebel_id,
      b.FST_NAME as first_name,
      b.LAST_NAME as last_name,
      lower(b.EMAIL_ADDR) as email,
      b.CELL_PH_NUM as phone,
      lower(a.LOGIN) as user_name,
      a.PASSWORD as password,
      a.CREATED as created_at,
      a.CREATED_BY as created_by_siebel_id,
      a.LAST_UPD as updated_at,
      a.LAST_UPD_BY as updated_by_siebel_id
    from dbo.S_USER a
    join dbo.S_CONTACT b on a.ROW_ID = b.ROW_ID
    order by a.ROW_ID
  ")
  ds = @source_db.base[sql]
  results = ds.all
  id = @target_db_postgresql.get_max_id_for_table(:user)
  results.each do |item|
    id += 1
    item[:user_id] = id
    item[:contact_id] = id
  end
  count = 0
  @target_db.transaction do
    results.each do |h|
      h = hash_values_nil_if_blank(h)
      count += 1
      puts "user: #{count} of #{results.length}."
      h[:email] = nil unless h[:email] and h[:email].include?('@')
      h[:password] = JunglePath::Authentication::Helpers.generate_api_key('generated_password_') #generate a random password for ported users. They will not have their password unless it is changed, so they will not be able to log on directly.
      #also, defaulting portal users to active for now...:
      hash = {id: h[:user_id], siebel_id: h[:siebel_id], user_name: h[:user_name], email: h[:email], first_name: h[:first_name], last_name: h[:last_name], password: h[:password], active: true}
      user = Controller::User.new(@current_user, @current_key, hash, @target_db).insert
      h[:password] = nil
      Controller::SiebelUser.new(@current_user, @current_key, h, @target_db).insert
      Controller::Contact.new(@current_user, @current_key, h, @target_db).insert
      Controller::SiebelContact.new(@current_user, @current_key, h, @target_db).insert
    end
  end
  @target_db_postgresql.reset_sequence_for_table(:user)
  log "user: processed #{results.length} rows.", @target_db_postgresql
end

Private Instance Methods

correlated_lookup(table, value, search_column, target_column) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 169
def correlated_lookup table, value, search_column, target_column
  @lookup.value(table, value, search_column, target_column)
end
create_table(controller_class) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 195
def create_table(controller_class)
  controller_class.new(@current_user, @current_key, {}, @target_db).create_table
end
drop_table(controller_class) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 191
def drop_table(controller_class)
  controller_class.new(@current_user, @current_key, {}, @target_db).drop_table
end
hash_values_nil_if_blank(hash) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 173
def hash_values_nil_if_blank(hash)
  hash.each do |key, value|
    #puts "hash[#{key}] = '#{value}'" if value and value.to_s[0] == "\t"
    hash[key] = nil_if_blank(value)
  end
  hash
end
nil_if_blank(value) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 181
def nil_if_blank(value)
  if value.class == String
    v = value.strip
    v = nil if v.empty? or v == 'No Match Row Id'
    v
  else
    value
  end
end
reset_table(controller_class) click to toggle source
# File lib/jungle_path/app/ztools/zport_data.rb, line 199
def reset_table(controller_class)
  drop_table controller_class
  create_table controller_class
end