Module: Marshaller

Defined in:
lib/Marshaller.rb

Instance Method Summary collapse

Instance Method Details

#buildXMLDocFromRailsParms(nodeName, params, nvp_format, myagg, info, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/Marshaller.rb', line 36

def buildXMLDocFromRailsParms(nodeName,params,nvp_format,myagg,info,&block)
    if (myagg.length == 0)
      xml =  createNode(nodeName.to_s,params,nvp_format,myagg,&block)
    else
      xml =  createNode(myagg.to_s,params,nvp_format,myagg,&block)
    end
    xml << expandRelationships(:has_one,nodeName,params,nvp_format,myagg,info,&block)
    xml << expandRelationships(:has_many,nodeName,params,nvp_format,myagg,info,&block)

    xml = "" if !(xml.length > 0)
    xml = "<#{nodeName}>#{xml}</#{nodeName}>" if (xml.length > 0 and !nvp_format)
    xml
end

#collectValue(child, en, pn) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/Marshaller.rb', line 215

def collectValue(child,en,pn)
 #there are issues with this approach to multi-occ returns
 #this will get the client stuff running
 #will now switch to using the cardinalities in the model
 #by stepping up one level in the tree and reviewing all cardinalties of
 #contained relations which will include the current subject
 if (!@pHashes.has_key?(en))
   @pHashes[en] = HashWithIndifferentAccess.new
   @pHashes[en][pn] = unescape(child.to_s).strip
 else
   if @pHashes[en].is_a?(Array)
     if (!@pHashes[en][@pHashes[en].length-1].has_key?(pn))
       @pHashes[en][@pHashes[en].length-1][pn] = unescape(child.to_s).strip
     else
       h = HashWithIndifferentAccess.new
       h[pn] = unescape(child.to_s).strip
       @pHashes[en].push(h)
     end
   elsif @pHashes[en][pn]
     existingHash = @pHashes[en]
     @pHashes[en] = Array.new
     @pHashes[en].push(existingHash)
     h = HashWithIndifferentAccess.new
     h[pn] = unescape(child.to_s).strip
     @pHashes[en].push(h)
   else
     @pHashes[en][pn] = unescape(child.to_s).strip
   end
 end
end

#createNode(table, params, nvp_format, myagg, &block) ⇒ Object



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
# File 'lib/Marshaller.rb', line 50

def createNode(table,params,nvp_format,myagg,&block)
  xml=""
  values = params[table]
  if ( values != nil) then
    fullName = expandedName(myagg)
    if (values.is_a?(String))
        if (nvp_format)
          xml << block.call("#{fullName}Value",values.strip)
        else
          xml << block.call("Value",values.strip)
        end
    else
      values.each_key do |key|
        if (values[key].length > 0)
          if (nvp_format)
            xml << block.call("#{fullName}#{key}",values[key])
          else
            xml << block.call(key,values[key])
          end
        end
      end
    end
  end
  xml 
end

#createXMLMessage(relation, params, nvp_format, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/Marshaller.rb', line 28

def createXMLMessage(relation,params,nvp_format,&block)
  @pqbNodes = Array.new
  #dump the hash table rails builds for use in RTE message building test harness
  #open('garyparams','w') { |f| YAML.dump(params, f) }
  xml = buildXMLDocFromRailsParms(relation,params,nvp_format,Array.new,"",&block)
  xml
end

#expandedName(myagg) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/Marshaller.rb', line 76

def expandedName(myagg)
    fullName = ""
    myagg.each do |a|
      fullName << "#{a.to_s.gsub(/[0-9]/,"")}_"
    end
    fullName
end

#expandRelationships(cardinality, nodeName, params, nvp_format, myagg, info, &block) ⇒ Object



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
# File 'lib/Marshaller.rb', line 96

def expandRelationships(cardinality,nodeName,params,nvp_format,myagg,info,&block)
  xml = ""
  associations = {}
  #strip out subscripts when accessing the generated data dictionary
  if (myagg.length == 0)
   entityName = nodeName.to_s.gsub(/[0-9]/,"")
  else
   entityName = myagg.to_s.gsub(/[0-9]/,"")
  end
  begin
    associations = eval("#{entityName}.reflect_on_all_associations(:#{cardinality.id2name})")
  rescue
    raise "#{entityName} does not exist - please check the entity and coverage library definitions\nPerhaps you intended:#{info}"
  end
  
  associations.each do |ass|
    klass = eval(ass.name.id2name)
    if (myagg.last.to_s.include?("PremiumQuoteBreakdown") and (klass.nodeName == "GrossAmount"))
      @pqbNodes.push("#{expandedName(myagg)}#{klass.nodeName}")
    end
    if (myagg.to_s == "PremiumQuoteBreakdown" and
          (klass.nodeName == "Amount" or klass.nodeName == "IPTAmount" or klass.nodeName == "IPTPercent" ) )
      @pqbNodes.push("#{expandedName(myagg)}#{klass.nodeName}")
    end
    myagg.push(klass.nodeName)
    #puts "THE NODENAME:#{nodeN}\n AND THE REAL HASHNAME: #{ass.name.id2name}\n OUR DERIVED EQUIVALENT:#{myagg}"
    if (cardinality.id2name == "has_one")
      xml << buildXMLDocFromRailsParms(klass.nodeName,params,nvp_format,myagg,ass.name.id2name,&block)
    else
      #make this call as many times as there are instances of this has_many type
      #make sure we add in the subscript each time into the myagg string first though
      #use findDimension to look inside the rails dispatcher generated hash table of the http paramaters
      #to determine the number of entries for a given multiply occuring complex type
      findDimension(params,myagg).times do |i|
        myagg.push(myagg.pop.to_s.gsub(/[0-9]/,"")+"#{i}")
        xml << buildXMLDocFromRailsParms(klass.nodeName,params,nvp_format,myagg,ass.name.id2name,&block)
      end
    end
    myagg.pop
  end
  xml
end

#findDimension(params, myagg) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/Marshaller.rb', line 139

def findDimension(params,myagg)   
  low = 0
  high = 50

  while (high-low) > 1
    middle = (low + high) / 2
    str = "#{myagg}#{middle}"
    if hashEntryExists(params,str)
      low = middle
    else
      high = middle
    end
  end
  #indexing starts at 0 so need to add 1 to calculated dimension
  low = low + 1
  #puts "DIMENSION for #{myagg} is #{low}"
  low
end

#getRAILSClassRequiresForProductModelFromOilDef(product) ⇒ Object

generates ruby require statements for each of the classes for the data model this is then eval’ed in the code to introduce the models into the sessions



17
18
19
20
21
22
23
24
25
26
# File 'lib/Marshaller.rb', line 17

def getRAILSClassRequiresForProductModelFromOilDef(product)
    require 'ProductInterpreter'
    oilfile = File.join("#{DY_MODELS}/#{product}/DSL/product.oil")
    open(oilfile) {|f| @contents = f.read }
    dsl = @contents.to_s
    if (!dsl.include?("product :#{product}"))
      raise "#{DY_MODELS}/#{product}/DSL/product.oil does NOT contain a product defintion for #{product}"
    end
    productModel = ProductInterpreter.execute(dsl)
end

#hashEntryExists(params, str) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/Marshaller.rb', line 158

def hashEntryExists(params,str)
  params.each do |key, value|
    if (key.match(str))
      return true
    end
  end
  return false
end

#prepareModels(product, xml) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/Marshaller.rb', line 167

def prepareModels(product,xml)
  #identify model parts for which we have data
  #and instantiate classes for it
  @pHashes = {}
  doc = REXML::Document.new(xml)   
  xml_node_to_hash([],doc.root_node,product)
  @pHashes
end

#unescape(str) ⇒ Object



246
247
248
# File 'lib/Marshaller.rb', line 246

def unescape(str)
 return CGI.unescapeHTML(str)
end

#walk_class_hierarchy(entityName) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/Marshaller.rb', line 84

def walk_class_hierarchy(entityName)
  children = []
  associations = eval("#{entityName}.reflect_on_all_associations(:has_one)")
  associations.each do |ass|
     children.push(ass.name.id2name)
  end
  associations = eval("#{entityName}.reflect_on_all_associations(:has_many)")
  associations.each do |ass|
     children.push(ass.name.id2name)
  end
  children
end

#xml_node_to_hash(path, node, product, parent_namespaces = {}) ⇒ Object

:nodoc



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
# File 'lib/Marshaller.rb', line 176

def xml_node_to_hash(path, node,product, parent_namespaces={}) #:nodoc
   this_node = {}
   namespaces = parent_namespaces.dup
   node.attributes.each do |name, value|
     case name
     when 'xmlns'
       (namespaces['@xmlns'] ||= {})['$'] = value
     when /^xmlns:(.*)/
       (namespaces['@xmlns'] ||= {})[$1] = value
     else
       this_node["@#{name}"] = value
     end
   end
   node.each_child do |child|
     case child.node_type
     when :element
       key, value = child.expanded_name, xml_node_to_hash(path.clone.push(child.expanded_name),child, product, namespaces)
     when :text
       if unescape(child.to_s).strip.length > 0
         pn,en = "#{path.pop}","#{path}".gsub(product,'')
         #puts "VALUES are:#{en},#{pn}=#{unescape(child.to_s).strip}"
         collectValue(child,en,pn)
       end
       key, value = '$', unescape(child.to_s).strip
       next if value.empty?
     end
     current = this_node[key]
     case current
     when Array
       this_node[key] << value
     when nil
       this_node[key] = value
     else
       this_node[key] = [current.dup, value]
     end
   end
   return this_node.merge(namespaces)
end