Improved import of articles.

This commit is contained in:
Martin Edenhofer 2015-05-01 14:20:46 +02:00
parent 8850bf299b
commit 41d2c6c881

View file

@ -340,9 +340,7 @@ module Import::OTRS2
end end
# get agents groups # get agents groups
ActiveRecord::Base.transaction do
groups = load('Group') groups = load('Group')
end
# get agents roles # get agents roles
roles = load('Role') roles = load('Role')
@ -581,28 +579,27 @@ module Import::OTRS2
ticket.save ticket.save
end end
# utf8 encode
record['Articles'].each { |article|
article.each { |key, value|
next if !value
next if value.class != String
article[key] = Encode.conv( 'utf8', value )
}
}
# lookup customers to create first
record['Articles'].each { |article| 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 # create customer/sender if needed
if article_new[:sender] == 'customer' && article_new[:created_by_id].to_i == 1 && !article_new[:from].empty? if article['sender'] == 'customer' && article['created_by_id'].to_i == 1 && !article['from'].empty?
email = nil email = nil
begin begin
email = Mail::Address.new( article_new[:from] ).address email = Mail::Address.new( article['from'] ).address
rescue rescue
email = article_new[:from] email = article['from']
if article_new[:from] =~ /<(.+?)>/ if article['from'] =~ /<(.+?)>/
email = $1 email = $1
end end
end end
@ -622,10 +619,10 @@ module Import::OTRS2
end end
if !user if !user
begin begin
display_name = Mail::Address.new( article_new[:from] ).display_name || display_name = Mail::Address.new( article['from'] ).display_name ||
( Mail::Address.new( article_new[:from] ).comments && Mail::Address.new( article_new[:from] ).comments[0] ) ( Mail::Address.new( article['from'] ).comments && Mail::Address.new( article['from'] ).comments[0] )
rescue rescue
display_name = article_new[:from] display_name = article['from']
end end
# do extra decoding because we needed to use field.value # do extra decoding because we needed to use field.value
@ -644,11 +641,27 @@ module Import::OTRS2
created_by_id: 1, created_by_id: 1,
) )
end end
article_new[:created_by_id] = user.id article['created_by_id'] = user.id
# unlock user # unlock user
locks[:User][ email ] = false locks[:User][ email ] = false
end end
}
ActiveRecord::Base.transaction do
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
}
if article_new[:sender] == 'customer' if article_new[:sender] == 'customer'
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id
@ -697,10 +710,9 @@ module Import::OTRS2
article.id = article_new[:id] article.id = article_new[:id]
article.save article.save
end 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} )"
@ -807,8 +819,8 @@ module Import::OTRS2
created_by_id: history['CreateBy'] created_by_id: history['CreateBy']
) )
end end
end
} }
end
} }
end end