Moved ActiveRecord::Base.transaction to lower scope.

This commit is contained in:
Martin Edenhofer 2015-05-01 13:57:49 +02:00
parent cc641407ea
commit 8850bf299b

View file

@ -323,29 +323,41 @@ module Import::OTRS2
# create states # create states
states = load('State') states = load('State')
state(states) ActiveRecord::Base.transaction do
state(states)
end
# create priorities # create priorities
priorities = load('Priority') priorities = load('Priority')
priority(priorities) ActiveRecord::Base.transaction do
priority(priorities)
end
# create groups # create groups
queues = load('Queue') queues = load('Queue')
ticket_group(queues) ActiveRecord::Base.transaction do
ticket_group(queues)
end
# get agents groups # get agents groups
groups = load('Group') ActiveRecord::Base.transaction do
groups = load('Group')
end
# get agents roles # get agents roles
roles = load('Role') roles = load('Role')
# create agents # create agents
users = load('User') users = load('User')
user(users, groups, roles, queues) ActiveRecord::Base.transaction do
user(users, groups, roles, queues)
end
# create organizations # create organizations
organizations = load('Customer') organizations = load('Customer')
organization(organizations) ActiveRecord::Base.transaction do
organization(organizations)
end
# create customers # create customers
count = 0 count = 0
@ -517,179 +529,178 @@ module Import::OTRS2
result.each {|record| result.each {|record|
ActiveRecord::Base.transaction do # cleanup values
_cleanup(record)
# cleanup values ticket_new = {
_cleanup(record) title: '',
created_by_id: 1,
updated_by_id: 1,
}
map[:Ticket].each { |key, value|
if record[key.to_s] && record[key.to_s].class == String
ticket_new[value] = Encode.conv( 'utf8', record[key.to_s] )
else
ticket_new[value] = record[key.to_s]
end
}
ticket_old = Ticket.where( id: ticket_new[:id] ).first
ticket_new = { # find owner
title: '', if ticket_new[:owner]
user = User.lookup( login: ticket_new[:owner].downcase )
if user
ticket_new[:owner_id] = user.id
else
ticket_new[:owner_id] = 1
end
ticket_new.delete(:owner)
end
# find customer
if ticket_new[:customer]
user = User.lookup( login: ticket_new[:customer].downcase )
if user
ticket_new[:customer_id] = user.id
else
ticket_new[:customer_id] = 1
end
ticket_new.delete(:customer)
else
ticket_new[:customer_id] = 1
end
# set state types
if ticket_old
log "update Ticket.find(#{ticket_new[:id]})"
ticket_old.update_attributes(ticket_new)
else
log "add Ticket.find(#{ticket_new[:id]})"
ticket = Ticket.new(ticket_new)
ticket.id = ticket_new[:id]
ticket.save
end
record['Articles'].each { |article|
# get article values
article_new = {
created_by_id: 1, created_by_id: 1,
updated_by_id: 1, updated_by_id: 1,
} }
map[:Ticket].each { |key, value| map[:Article].each { |key, value|
if record[key.to_s] && record[key.to_s].class == String if article[key.to_s]
ticket_new[value] = Encode.conv( 'utf8', record[key.to_s] ) article_new[value] = Encode.conv( 'utf8', article[key.to_s] )
else
ticket_new[value] = record[key.to_s]
end end
} }
ticket_old = Ticket.where( id: ticket_new[:id] ).first
# find owner # create customer/sender if needed
if ticket_new[:owner] if article_new[:sender] == 'customer' && article_new[:created_by_id].to_i == 1 && !article_new[:from].empty?
user = User.lookup( login: ticket_new[:owner].downcase )
if user email = nil
ticket_new[:owner_id] = user.id begin
else email = Mail::Address.new( article_new[:from] ).address
ticket_new[:owner_id] = 1 rescue
email = article_new[:from]
if article_new[:from] =~ /<(.+?)>/
email = $1
end
end end
ticket_new.delete(:owner)
# create article user if not exists
while locks[:User][ email ]
log "user #{email} is locked"
sleep 1
end
# lock user
locks[:User][ email ] = true
user = User.where( email: email ).first
if !user
user = User.where( login: email ).first
end
if !user
begin
display_name = Mail::Address.new( article_new[:from] ).display_name ||
( Mail::Address.new( article_new[:from] ).comments && Mail::Address.new( article_new[:from] ).comments[0] )
rescue
display_name = article_new[:from]
end
# do extra decoding because we needed to use field.value
display_name = Mail::Field.new( 'X-From', display_name ).to_s
roles = Role.lookup( name: 'Customer' )
user = User.create(
login: email,
firstname: display_name,
lastname: '',
email: email,
password: '',
active: true,
role_ids: [roles.id],
updated_by_id: 1,
created_by_id: 1,
)
end
article_new[:created_by_id] = user.id
# unlock user
locks[:User][ email ] = false
end end
# find customer if article_new[:sender] == 'customer'
if ticket_new[:customer] article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id
user = User.lookup( login: ticket_new[:customer].downcase ) article_new.delete( :sender )
if user end
ticket_new[:customer_id] = user.id if article_new[:sender] == 'agent'
else article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Agent' ).id
ticket_new[:customer_id] = 1 article_new.delete( :sender )
end end
ticket_new.delete(:customer) if article_new[:sender] == 'system'
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'System' ).id
article_new.delete( :sender )
end
if article_new[:type] == 'email-external'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
article_new[:internal] = false
elsif article_new[:type] == 'email-internal'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
article_new[:internal] = true
elsif article_new[:type] == 'note-external'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
article_new[:internal] = false
elsif article_new[:type] == 'note-internal'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
article_new[:internal] = true
elsif article_new[:type] == 'phone'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'phone' ).id
article_new[:internal] = false
elsif article_new[:type] == 'webrequest'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'web' ).id
article_new[:internal] = false
else else
ticket_new[:customer_id] = 1 article_new[:type_id] = 9
end end
article_new.delete( :type )
article_old = Ticket::Article.where( id: article_new[:id] ).first
# set state types # set state types
if ticket_old if article_old
log "update Ticket.find(#{ticket_new[:id]})" log "update Ticket::Article.find(#{article_new[:id]})"
ticket_old.update_attributes(ticket_new) article_old.update_attributes(article_new)
else else
log "add Ticket.find(#{ticket_new[:id]})" log "add Ticket::Article.find(#{article_new[:id]})"
ticket = Ticket.new(ticket_new) article = Ticket::Article.new(article_new)
ticket.id = ticket_new[:id] article.id = article_new[:id]
ticket.save article.save
end end
record['Articles'].each { |article| }
# get article values
article_new = {
created_by_id: 1,
updated_by_id: 1,
}
map[:Article].each { |key, value|
if article[key.to_s]
article_new[value] = Encode.conv( 'utf8', article[key.to_s] )
end
}
# create customer/sender if needed
if article_new[:sender] == 'customer' && article_new[:created_by_id].to_i == 1 && !article_new[:from].empty?
email = nil
begin
email = Mail::Address.new( article_new[:from] ).address
rescue
email = article_new[:from]
if article_new[:from] =~ /<(.+?)>/
email = $1
end
end
# create article user if not exists
while locks[:User][ email ]
log "user #{email} is locked"
sleep 1
end
# lock user
locks[:User][ email ] = true
user = User.where( email: email ).first
if !user
user = User.where( login: email ).first
end
if !user
begin
display_name = Mail::Address.new( article_new[:from] ).display_name ||
( Mail::Address.new( article_new[:from] ).comments && Mail::Address.new( article_new[:from] ).comments[0] )
rescue
display_name = article_new[:from]
end
# do extra decoding because we needed to use field.value
display_name = Mail::Field.new( 'X-From', display_name ).to_s
roles = Role.lookup( name: 'Customer' )
user = User.create(
login: email,
firstname: display_name,
lastname: '',
email: email,
password: '',
active: true,
role_ids: [roles.id],
updated_by_id: 1,
created_by_id: 1,
)
end
article_new[:created_by_id] = user.id
# unlock user
locks[:User][ email ] = false
end
if article_new[:sender] == 'customer'
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id
article_new.delete( :sender )
end
if article_new[:sender] == 'agent'
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Agent' ).id
article_new.delete( :sender )
end
if article_new[:sender] == 'system'
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'System' ).id
article_new.delete( :sender )
end
if article_new[:type] == 'email-external'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
article_new[:internal] = false
elsif article_new[:type] == 'email-internal'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
article_new[:internal] = true
elsif article_new[:type] == 'note-external'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
article_new[:internal] = false
elsif article_new[:type] == 'note-internal'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
article_new[:internal] = true
elsif article_new[:type] == 'phone'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'phone' ).id
article_new[:internal] = false
elsif article_new[:type] == 'webrequest'
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'web' ).id
article_new[:internal] = false
else
article_new[:type_id] = 9
end
article_new.delete( :type )
article_old = Ticket::Article.where( id: article_new[:id] ).first
# set state types
if article_old
log "update Ticket::Article.find(#{article_new[:id]})"
article_old.update_attributes(article_new)
else
log "add Ticket::Article.find(#{article_new[:id]})"
article = Ticket::Article.new(article_new)
article.id = article_new[:id]
article.save
end
}
#puts "HS: #{record['History'].inspect}" #puts "HS: #{record['History'].inspect}"
ActiveRecord::Base.transaction do
record['History'].each { |history| record['History'].each { |history|
if history['HistoryType'] == 'NewTicket' if history['HistoryType'] == 'NewTicket'
#puts "HS.add( #{history.inspect} )" #puts "HS.add( #{history.inspect} )"
@ -796,8 +807,8 @@ module Import::OTRS2
created_by_id: history['CreateBy'] created_by_id: history['CreateBy']
) )
end end
} end
end }
} }
end end