2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
module Import
|
|
|
|
class Exchange
|
|
|
|
class ItemAttributes
|
|
|
|
|
|
|
|
def self.extract(resource)
|
|
|
|
new(resource).extract
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(resource)
|
|
|
|
@resource = resource
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract
|
2018-04-12 14:57:37 +00:00
|
|
|
@extract ||= begin
|
2017-08-14 11:56:23 +00:00
|
|
|
properties = @resource.get_all_properties!
|
|
|
|
result = normalize(properties)
|
|
|
|
flattened = flatten(result)
|
2019-06-28 11:38:49 +00:00
|
|
|
booleanize_values(flattened)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def booleanize_values(properties)
|
2020-09-30 09:07:01 +00:00
|
|
|
booleans = %w[true false]
|
2017-08-14 11:56:23 +00:00
|
|
|
properties.each do |key, value|
|
2020-07-13 12:46:08 +00:00
|
|
|
case value
|
|
|
|
when String
|
2020-09-30 09:07:01 +00:00
|
|
|
next if booleans.exclude?(value)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
properties[key] = value == 'true'
|
2020-07-13 12:46:08 +00:00
|
|
|
when Hash
|
2017-08-14 11:56:23 +00:00
|
|
|
properties[key] = booleanize_values(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(properties)
|
2017-09-15 16:16:12 +00:00
|
|
|
properties.each_with_object({}) do |(key, value), result|
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
next if key == :body
|
|
|
|
|
|
|
|
if value[:text]
|
|
|
|
result[key] = value[:text]
|
|
|
|
elsif value[:attribs]
|
|
|
|
result[key] = value[:attribs]
|
|
|
|
elsif value[:elems]
|
|
|
|
result[key] = sub_elems(value[:elems])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sub_elems(elems)
|
2017-09-15 16:16:12 +00:00
|
|
|
elems.each_with_object({}) do |elem, result|
|
2017-08-14 11:56:23 +00:00
|
|
|
if elem[:entry]
|
|
|
|
result.merge!( sub_elem_entry( elem[:entry] ) )
|
|
|
|
else
|
|
|
|
result.merge!( normalize(elem) )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sub_elem_entry(entry)
|
|
|
|
entry_value = {}
|
|
|
|
if entry[:elems]
|
|
|
|
entry_value = sub_elems(entry[:elems])
|
|
|
|
end
|
|
|
|
|
|
|
|
if entry[:text]
|
|
|
|
entry_value[:text] = entry[:text]
|
|
|
|
end
|
|
|
|
|
|
|
|
if entry[:attribs].present?
|
|
|
|
entry_value.merge!(entry[:attribs])
|
|
|
|
end
|
|
|
|
|
|
|
|
entry_key = entry_value.delete(:key)
|
|
|
|
{
|
|
|
|
entry_key => entry_value
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def flatten(properties, prefix: nil)
|
2020-09-30 09:07:01 +00:00
|
|
|
keys = %i[text id]
|
2017-09-15 16:16:12 +00:00
|
|
|
properties.each_with_object({}) do |(key, value), result|
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
result_key = key
|
|
|
|
if prefix
|
2020-09-30 09:07:01 +00:00
|
|
|
result_key = if keys.include?(key) && ( !result[result_key] || result[result_key] == value )
|
2017-08-14 11:56:23 +00:00
|
|
|
prefix
|
|
|
|
else
|
2020-08-21 08:18:31 +00:00
|
|
|
:"#{prefix}.#{key}"
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
result_key = result_key.to_s.downcase
|
|
|
|
|
|
|
|
if value.is_a?(Hash)
|
|
|
|
sub_result = flatten(value, prefix: result_key)
|
|
|
|
result.merge!(sub_result)
|
|
|
|
else
|
|
|
|
result[result_key] = value.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|