63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/xml_active.rb', line 63
def one_from_xml(xml, options = [])
if xml.is_a? String
doc = Nokogiri::XML xml
current_node = doc.children.first
else
current_node = xml
end
if xml_node_matches_class(current_node)
pk_value = 0
pk_node = current_node.xpath self.primary_key.to_s
if pk_node
begin
ar = find pk_node.text
pk_value = pk_node.text
rescue
if options.include?(:sync) or options.include?(:create)
ar = self.new
else
return nil
end
end
else
if options.include?(:sync) or options.include?(:create)
ar = self.new
else
return nil
end
end
self.reflect_on_all_associations.each do |association|
if ActiveRecord::Reflection::AssociationReflection.method_defined? :foreign_key
foreign_key = association.foreign_key
elsif ActiveRecord::Reflection::AssociationReflection.method_defined? :primary_key_name
foreign_key = association.primary_key_name
else
raise "Unsupported version of ActiveRecord. Unable to identify the foreign key."
end
case
when association.macro == :has_many, association.macro == :has_and_belongs_to_many
if pk_value == 0
containers = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_node.text}]/#{association.name}")
else
containers = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_value}]/#{association.name}")
end
if containers.count > 0
container = containers[0]
klass = association.klass
child_ids = []
container.element_children.each do |single_obj|
child_ids[child_ids.length] = single_obj.xpath(self.primary_key.to_s).text
new_record = klass.one_from_xml(single_obj, options)
if (new_record != nil)
ar.__send__(container.name.underscore.to_sym) << new_record
end
end
if pk_value != 0
if options.include?(:sync)
if child_ids.length > 0
klass.destroy_all [klass.primary_key.to_s + " not in (?) and #{foreign_key} = ?", child_ids.collect, pk_value]
end
elsif options.include?(:destroy)
if child_ids.length > 0
klass.destroy_all [klass.primary_key.to_s + " not in (?) and #{foreign_key} = ?", child_ids.collect, pk_value]
else
klass.destroy_all
end
end
end
end
when association.macro == :has_one
single_objects = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_value}]/#{association.name}")
klass = association.klass
record = klass.where(foreign_key => pk_value).all
if single_objects.count == 1
if record.count == 1
db_pk_value = record[0][klass.primary_key]
xml_pk_value = Integer(single_objects[0].element_children.xpath("//#{self.name.underscore}/#{klass.primary_key}").text)
if db_pk_value != xml_pk_value
if options.include?(:sync) or options.include?(:destroy)
klass.destroy(record[0][klass.primary_key])
end
end
elsif record.count > 1
raise "Too many records for one to one association in the database. Found #{record.count} records of '#{association.name}' for association with '#{self.name}'"
end
if options.include?(:create) or options.include?(:update) or options.include?(:sync)
new_record = klass.one_from_xml(single_objects[0], options)
if new_record != nil
new_record[foreign_key.to_sym] = ar[self.primary_key]
new_record.save!
end
end
elsif single_objects.count > 1
raise "Too many records for one to one association in the provided XML. Found #{single_objects.count} records of '#{association.name}' for association with '#{self.name}'"
else
if record.count > 0 and options.include?(:sync) or options.include?(:destroy)
klass.destroy_all("#{foreign_key} = #{pk_value}")
end
end
when association.macro == :belongs_to
else
raise "unsupported association #{association.macro} for #{association.name } on #{self.name}"
end
end
if options.include? :update or options.include? :sync or options.include? :create
current_node.element_children.each do |node|
node_name = node.name.underscore.to_sym
association = self.reflect_on_association node_name
if association.nil?
if node.attributes['nil'].try(:value)
ar[node_name] = nil
else
ar[node_name] = node.text
end
end
end
end
if options.include? :sync
ar.save
elsif options.include?(:create) and ar.new_record?
ar.save
elsif options.include?(:update) and not ar.new_record?
ar.save
end
ar
else
raise "The supplied XML (#{current_node.name}) cannot be mapped to this class (#{self.name})"
end
end
|