2018-04-26 08:55:53 +00:00
|
|
|
require_dependency 'mixin/rails_logger'
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
module Import
|
|
|
|
class Exchange
|
|
|
|
class Folder
|
|
|
|
include ::Mixin::RailsLogger
|
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
DEFAULT_ROOTS = %i[root msgfolderroot publicfoldersroot].freeze
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
def initialize(connection)
|
|
|
|
@connection = connection
|
|
|
|
@lookup_map = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_folder_map
|
|
|
|
@id_folder_map ||= all.collect do |folder|
|
|
|
|
[folder.id, folder]
|
|
|
|
end.to_h
|
|
|
|
|
|
|
|
# duplicate object to avoid errors where keys get
|
|
|
|
# added via #get_folder while iterating over
|
|
|
|
# the result of this method
|
|
|
|
@lookup_map = @id_folder_map.dup
|
|
|
|
@id_folder_map
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(id)
|
2018-08-13 07:10:24 +00:00
|
|
|
(@lookup_map[id] ||= @connection.get_folder(id)) ||
|
|
|
|
id_folder_map[id]
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
2018-07-10 05:32:24 +00:00
|
|
|
@all ||= children(*DEFAULT_ROOTS)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
def children(*parents)
|
|
|
|
return [] if parents.empty?
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
direct_descendants = parents.map(&method(:request_children))
|
|
|
|
.flatten.uniq.compact
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
direct_descendants | children(*direct_descendants)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_path(folder)
|
2018-09-03 01:36:24 +00:00
|
|
|
display_name = folder.display_name&.utf8_encode(fallback: :read_as_sanitized_binary)
|
2017-08-14 11:56:23 +00:00
|
|
|
parent_folder = find(folder.parent_folder_id)
|
|
|
|
|
2018-08-13 07:10:24 +00:00
|
|
|
return display_name if parent_folder.blank?
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2018-08-13 07:10:24 +00:00
|
|
|
"#{display_path(parent_folder)} -> #{display_name}"
|
2017-08-14 11:56:23 +00:00
|
|
|
rescue Viewpoint::EWS::EwsError
|
2018-08-13 07:10:24 +00:00
|
|
|
display_name
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
def request_children(parent)
|
|
|
|
parent = parent.id if parent.respond_to?(:id) # type coercion
|
|
|
|
@connection.folders(root: parent)
|
2017-08-14 11:56:23 +00:00
|
|
|
rescue Viewpoint::EWS::EwsFolderNotFound => e
|
2018-07-10 05:32:24 +00:00
|
|
|
logger.warn(e) && return
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|