2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2016-04-15 21:56:10 +00:00
|
|
|
class Transaction
|
2020-12-29 08:54:39 +00:00
|
|
|
attr_reader :options
|
2021-08-20 03:36:30 +00:00
|
|
|
attr_accessor :original_user_id, :original_interface_handle, :original_interface_context
|
2020-12-29 08:54:39 +00:00
|
|
|
|
|
|
|
def initialize(options = {})
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
start_execute
|
|
|
|
|
2016-09-06 05:51:12 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-12-29 08:54:39 +00:00
|
|
|
start_transaction
|
|
|
|
|
2017-08-13 09:54:57 +00:00
|
|
|
yield
|
2020-12-29 08:54:39 +00:00
|
|
|
ensure
|
|
|
|
finish_transaction
|
2016-09-06 05:51:12 +00:00
|
|
|
end
|
2020-12-29 08:54:39 +00:00
|
|
|
ensure
|
|
|
|
finish_execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.execute(options = {}, &block)
|
|
|
|
Transaction.new(options).execute(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def start_execute
|
|
|
|
reset_user_id_start
|
|
|
|
bulk_import_start
|
|
|
|
interface_handle_start
|
2021-08-20 03:36:30 +00:00
|
|
|
interface_context_start
|
2020-12-29 08:54:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_transaction
|
|
|
|
PushMessages.init
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish_execute
|
|
|
|
reset_user_id_finish
|
|
|
|
bulk_import_finish
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish_transaction
|
|
|
|
interface_handle_finish
|
2021-08-20 03:36:30 +00:00
|
|
|
interface_context_finish
|
2020-12-29 08:54:39 +00:00
|
|
|
|
|
|
|
TransactionDispatcher.commit(options)
|
|
|
|
PushMessages.finish
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_user_id?
|
|
|
|
options[:reset_user_id] == true
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_user_id_start
|
|
|
|
return if !reset_user_id?
|
|
|
|
|
|
|
|
self.original_user_id = UserInfo.current_user_id
|
|
|
|
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_user_id_finish
|
|
|
|
return if !reset_user_id?
|
|
|
|
|
|
|
|
UserInfo.current_user_id = original_user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_import?
|
|
|
|
options[:bulk] == true
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_import_start
|
|
|
|
return if !bulk_import?
|
|
|
|
|
|
|
|
BulkImportInfo.enable
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_import_finish
|
|
|
|
return if !bulk_import?
|
2019-02-28 14:21:59 +00:00
|
|
|
|
|
|
|
BulkImportInfo.disable
|
2016-09-06 05:51:12 +00:00
|
|
|
end
|
2020-12-29 08:54:39 +00:00
|
|
|
|
|
|
|
def interface_handle?
|
|
|
|
options[:interface_handle].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def interface_handle_start
|
|
|
|
return if !interface_handle?
|
|
|
|
|
|
|
|
self.original_interface_handle = ApplicationHandleInfo.current
|
|
|
|
|
|
|
|
ApplicationHandleInfo.current = options[:interface_handle]
|
|
|
|
end
|
|
|
|
|
|
|
|
def interface_handle_finish
|
|
|
|
return if !interface_handle?
|
|
|
|
|
|
|
|
ApplicationHandleInfo.current = original_interface_handle
|
|
|
|
end
|
2021-08-20 03:36:30 +00:00
|
|
|
|
|
|
|
def interface_context?
|
|
|
|
options[:context].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def interface_context_start
|
|
|
|
return if !interface_context?
|
|
|
|
|
|
|
|
self.original_interface_context = ApplicationHandleInfo.context
|
|
|
|
|
|
|
|
ApplicationHandleInfo.context = options[:context]
|
|
|
|
end
|
|
|
|
|
|
|
|
def interface_context_finish
|
|
|
|
return if !interface_context?
|
|
|
|
|
|
|
|
ApplicationHandleInfo.context = original_interface_context
|
|
|
|
end
|
2016-04-15 21:56:10 +00:00
|
|
|
end
|