Improved import of objects.

This commit is contained in:
Martin Edenhofer 2013-02-17 21:59:57 +01:00
parent 9e6cf750b3
commit 024720e515
2 changed files with 55 additions and 7 deletions

View file

@ -7,6 +7,11 @@ class ApplicationModel < ActiveRecord::Base
after_update :cache_delete after_update :cache_delete
after_destroy :cache_delete after_destroy :cache_delete
# for import other objects, remove 'id'
def self.attributes_protected_by_default
['type']
end
def self.param_cleanup(params) def self.param_cleanup(params)
data = {} data = {}
self.new.attributes.each {|item| self.new.attributes.each {|item|

View file

@ -97,16 +97,21 @@ module Import::OTRS
result = result.reverse result = result.reverse
Thread.abort_on_exception = true Thread.abort_on_exception = true
thread_count = 2 thread_count = 4
threads = {} threads = {}
(1..thread_count).each {|thread| (1..thread_count).each {|thread|
threads[thread] = Thread.new { threads[thread] = Thread.new {
sleep thread * 3 sleep thread * 3
puts "Started import thread# #{thread}..." puts "Started import thread# #{thread} ..."
while true run = true
while run
ticket_ids = result.pop(20) ticket_ids = result.pop(20)
return if ticket_ids.empty? if !ticket_ids.empty?
self.ticket(ticket_ids) self.ticket(ticket_ids)
else
puts "... thread# #{thread}, no more work."
run = false
end
end end
} }
} }
@ -117,6 +122,41 @@ module Import::OTRS
return return
end end
def self.diff
puts 'Start diff...'
# check if system is in import mode
if !Setting.get('import_mode')
raise "System is not in import mode!"
end
# create states
ticket_state
# create priorities
ticket_priority
# create groups
ticket_group
# create agents
user
self.ticket_diff()
return
end
def self.ticket_diff()
url = "public.pl?Action=Export;Type=TicketDiff;Limit=30"
response = request( url )
return if !response
return if response.code.to_s != '200'
result = json(response)
self._ticket_result(result)
end
def self.ticket(ticket_ids) def self.ticket(ticket_ids)
url = "public.pl?Action=Export;Type=Ticket;" url = "public.pl?Action=Export;Type=Ticket;"
ticket_ids.each {|ticket_id| ticket_ids.each {|ticket_id|
@ -127,6 +167,10 @@ module Import::OTRS
return if response.code.to_s != '200' return if response.code.to_s != '200'
result = json(response) result = json(response)
self._ticket_result(result)
end
def self._ticket_result(result)
# puts result.inspect # puts result.inspect
map = { map = {
:Ticket => { :Ticket => {
@ -605,9 +649,7 @@ module Import::OTRS
result.each { |user| result.each { |user|
# puts 'USER: ' + user.inspect # puts 'USER: ' + user.inspect
# if user['UserID'] != 1
_set_valid(user) _set_valid(user)
# puts 'USER: ' + user.inspect
role = Role.lookup( :name => 'Agent' ) role = Role.lookup( :name => 'Agent' )
# get new attributes # get new attributes
@ -631,6 +673,7 @@ module Import::OTRS
if user_old if user_old
puts "update User.find(#{user_new[:id]})" puts "update User.find(#{user_new[:id]})"
# puts 'Update User' + user_new.inspect # puts 'Update User' + user_new.inspect
user_new.delete( :role_ids )
user_old.update_attributes(user_new) user_old.update_attributes(user_new)
else else
puts "add User.find(#{user_new[:id]})" puts "add User.find(#{user_new[:id]})"