Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
#
# Copyright (c) 2007 Joyent Inc.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'app/models/sync_log'
require 'app/controllers/slingshot_controller'

module Slingshot
  
  module XMLDeSerialize
    
    def self.included(base)
      base.extend(ClassMethods)
    end
  
    # XML Deserializer for ActiveRecord
    # by Wayne Robinson and Dominic Orchard
    module ClassMethods
      def from_xml(xml)
        if xml.class == String
            # If passed a string, convert to XML object, and set root
            xml = REXML::Document.new(xml) 
            root = xml.elements[1]
        else
            # If already passed an XML object, then set root to XML object
            root = xml
        end
      
        if  ((root.name.underscore != self.class_name.underscore) and 
               (root.name.underscore != self.class_name.pluralize.underscore))
                # Check the top level is actual refering to the class
                # e.g. , for class Customer
               return 'nil'
        end
      
        # Deal with XML data containing many record instances
          if (root.name.underscore == self.class_name.pluralize.underscore and self.class_name.pluralize.underscore!=self.class_name.underscore) or root.name==root.elements[1].name
          
              root.elements.inject([]) do |instances, element|

                  elem = self.from_xml(element)
               
               
                  instances.push(elem)
              end

          else
              # Try to retrieve from ID in
              # XML data and update this record or start a new record
              # Find an id element in the elements
              id_element = root.elements.inject(nil) do |found, element|
                    if element.name=="id"
                          element
                    else
                          found
                    end
              end
              # if we haven't found the ID element
              if id_element.nil?
                    new_record = self.new
              else
                    # Retrieve from XML
                    begin            
                        new_record = self.find(id_element.text.to_i)                  
                    rescue
                        # If that record in fact didn't exist... start a new one
                        new_record = self.new
                    end
              end
            
              # Iterate through elements
              root.elements.each do | element |
                  sym = element.name.underscore.to_sym
      
                  # An association
                if element.has_elements?
  
                  setter = (sym.to_s+"=")
                  # Check the setter is an instance method
                  if self.instance_methods.member?(setter)
                        klass = self.reflect_on_association(sym).klass
                       from = klass.from_xml(element)
                       new_record.__send__(setter.to_sym, from)
                       puts new_record.addresses[0].country_name
                  end
    
                # An attribute
                else
                    # Check that the attribute is actual part of the record
                    if new_record.attributes.member?(sym.to_s) || sym==:id
                        if element.text.nil?              
                              col = new_record.column_for_attribute(sym)
                              # Handle an empty element with a not null column
                              if !col.null
                                  # Use default value 
                                  new_record[sym] = col.default
                                
                              end
                        else                      
                              new_record[sym] = element.text
                        end
                    end
               end
            end
        
            new_record
          end
       end
       
     end
     
  end
  
  
end