iab-InsuranceBizLogic 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,135 @@
1
+ module BDBXMLJE
2
+ include Java
3
+ require 'date'
4
+ require 'pathname'
5
+ require 'lib/db.jar'
6
+ require 'lib/dbxml.jar'
7
+
8
+ # Include all the Java and JE classes that we need.
9
+
10
+ include_class 'java.io.File' do |p, c|
11
+ "J#{c}"
12
+ end
13
+
14
+ include_class ['com.sleepycat.db.Environment','com.sleepycat.db.EnvironmentConfig'] do |p, c|
15
+ "J#{c}"
16
+ end
17
+ include_class 'com.sleepycat.db.Cursor'
18
+ include_class 'com.sleepycat.db.Database'
19
+ include_class 'com.sleepycat.db.DatabaseConfig'
20
+ include_class 'com.sleepycat.db.DatabaseType'
21
+ include_class 'com.sleepycat.db.DatabaseEntry'
22
+ include_class 'com.sleepycat.db.OperationStatus'
23
+ include_class 'com.sleepycat.db.Transaction'
24
+
25
+ include_class 'com.sleepycat.bind.tuple.IntegerBinding'
26
+ include_class 'com.sleepycat.bind.tuple.StringBinding'
27
+
28
+ include_class 'com.sleepycat.dbxml.XmlManager'
29
+ include_class 'com.sleepycat.dbxml.XmlManagerConfig'
30
+ include_class 'com.sleepycat.dbxml.XmlDocument'
31
+ include_class 'com.sleepycat.dbxml.XmlTransaction'
32
+
33
+ #setup DB_ROOT to point to IABDB location
34
+ unless defined?(DB_ROOT)
35
+ db_path = APP_CONFIG['dbpath']
36
+ db_path = Pathname.new(db_path).cleanpath(true).to_s
37
+ DB_ROOT = db_path
38
+ DB_NAME = APP_CONFIG['dbcontainer']
39
+ end
40
+
41
+ def setup_env
42
+ #setup UUID state file
43
+ UUID.config(:state_file => "#{DB_ROOT}/uuid.state",
44
+ :sequence => rand(0x100000000),
45
+ :mac_addr => '00:19:e3:36:60:f5')
46
+
47
+ begin
48
+ envConf = JEnvironmentConfig.new()
49
+ envConf.setAllowCreate(true)
50
+ envConf.setTransactional(true)
51
+ envConf.setInitializeCache(true);
52
+
53
+ f = JFile.new(DB_ROOT)
54
+ $env = JEnvironment.new(f, envConf);
55
+
56
+ manConf = XmlManagerConfig.new()
57
+ manConf.setAllowExternalAccess(true)
58
+ $man = XmlManager.new($env,manConf)
59
+
60
+ if (!File.exist?("#{DB_ROOT}/#{DB_NAME}"))
61
+ $con = $man.createContainer(DB_NAME)
62
+ else
63
+ $con = $man.openContainer(DB_NAME)
64
+ end
65
+
66
+ $base = "."
67
+ rescue NativeException => e
68
+ puts "Native exception's cause: #{e.cause}"
69
+ raise
70
+ end
71
+ end
72
+
73
+ def create_key_and_doc(msg)
74
+ key = UUID.new
75
+ create_doc(msg,key)
76
+ key
77
+ end
78
+
79
+ def read_doc(key)
80
+ $con.getDocument(key).getContentAsString()
81
+ end
82
+
83
+ def create_doc(msg,key)
84
+ #removed the transaction bit since causes error when running inside Tomcat under JRuby
85
+ #txn = $man.createTransaction()
86
+ a = $man.createDocument()
87
+ a.setContent(msg)
88
+ a.setName(key)
89
+ $con.putDocument(key,msg)
90
+ #$con.putDocument(txn,key,msg)
91
+ #txn.commit()
92
+ end
93
+
94
+ def replace_doc(msg,key)
95
+ #removed the transaction bit since causes error when running inside Tomcat under JRuby
96
+ #txn = $man.createTransaction()
97
+ #a = $con.getDocument(txn,key)
98
+ a = $con.getDocument(key)
99
+ a.setContent(msg)
100
+ #$con.updateDocument(txn,a)
101
+ $con.updateDocument(a)
102
+ #txn.commit
103
+ end
104
+
105
+ def find(query)
106
+ cxt = $man.createQueryContext()
107
+ results = $man.query(query, cxt)
108
+ if (results.size() == 0) then
109
+ return nil
110
+ end
111
+ #results is essentially an array of result lines
112
+ #a single result will span a number of output lines, e.g. outer joined
113
+ #MTA info will appear with a CRLF separator
114
+ #the following code makes up single logical result rows based on the lines between
115
+ #successive pairs of {} braces
116
+ #
117
+ #it should be noted that the result set from the xqueries is intended to be
118
+ #a valid ruby block
119
+ #this allows the result to be used to instantiate an active record object
120
+ #with no further manipulation required
121
+ rows = []
122
+ i = 0
123
+ while results.hasNext()
124
+ if rows[i] == nil
125
+ rows[i] = ""
126
+ end
127
+ r = results.next().asString()
128
+ rows[i] << r.chomp
129
+ if (r.match(']}'))
130
+ i = i + 1
131
+ end
132
+ end
133
+ rows
134
+ end
135
+ end
@@ -0,0 +1,50 @@
1
+ # Copyright (c) 2007-2008 Orangery Technology Limited
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+ $:.unshift(File.dirname(__FILE__))
5
+ require 'rubygems'
6
+ require 'uuid'
7
+ require 'rexml/document'
8
+ require 'singleton'
9
+ require 'modbdbxml' unless defined?(JRUBY_VERSION)
10
+ require 'modbdbxmlje' if defined?(JRUBY_VERSION)
11
+
12
+ class Persist
13
+ include BDBXML unless defined?(JRUBY_VERSION)
14
+ include BDBXMLJE if defined?(JRUBY_VERSION)
15
+
16
+ include Singleton
17
+
18
+ def initialize
19
+ setup_env
20
+ end
21
+
22
+ def put(key,request)
23
+ begin
24
+ if (key == 'UUID') then
25
+ key = UUID.new
26
+ create_doc(request,key)
27
+ else
28
+ replace_doc(request,key)
29
+ end
30
+ key
31
+ end
32
+ end
33
+
34
+ def get(key)
35
+ read_doc(key)
36
+ end
37
+
38
+ #get with the insistence the document will be there
39
+ #if not then create it
40
+ def get!(key,request)
41
+ result=""
42
+ begin
43
+ result = read_doc(key)
44
+ rescue Exception => e #document not found
45
+ create_doc(request,key)
46
+ result = read_doc(key)
47
+ end
48
+ return result
49
+ end
50
+ end
@@ -0,0 +1,59 @@
1
+ class Hash
2
+
3
+ def deep_merge0(hash)
4
+ target = dup
5
+
6
+ hash.keys.each do |key|
7
+ if hash[key].is_a? Hash and self[key].is_a? Hash
8
+ target[key] = target[key].deep_merge(hash[key])
9
+ next
10
+ end
11
+
12
+ target[key] = hash[key]
13
+ end
14
+
15
+ target
16
+ end
17
+
18
+ def deep_merge!(second)
19
+ second.each_pair do |k,v|
20
+ if self[k].is_a?(Hash) and second[k].is_a?(Hash)
21
+ self[k].deep_merge!(second[k])
22
+ else
23
+ self[k] = second[k]
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ def deep_merge2(other)
30
+ deep_proc = Proc.new { |k, s, o|
31
+ if s.kind_of?(Hash) && o.kind_of?(Hash)
32
+ next s.merge(o, &deep_proc)
33
+ end
34
+ next o
35
+ }
36
+ merge(other, &deep_proc)
37
+ end
38
+
39
+
40
+ def deep_merge(second)
41
+ merger = proc { |key,v1,v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
42
+ self.merge(second, &merger)
43
+ end
44
+
45
+
46
+ def keep_merge(hash)
47
+ target = dup
48
+ hash.keys.each do |key|
49
+ if hash[key].is_a? Hash and self[key].is_a? Hash
50
+ target[key] = target[key].keep_merge(hash[key])
51
+ next
52
+ end
53
+ #target[key] = hash[key]
54
+ target.update(hash) { |key, *values| values.flatten.uniq }
55
+ end
56
+ target
57
+ end
58
+
59
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (c) 2007-2008 Orangery Technology Limited
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+ require 'processengine/engine'
5
+
6
+ class Communicator
7
+
8
+ @@instance = Communicator.new
9
+
10
+ def self.instance
11
+ @@instance
12
+ end
13
+
14
+ def handle(session,process,params)
15
+ eng = PEngine.new
16
+ eng.push(session,process,params)
17
+ end
18
+ end
@@ -0,0 +1,171 @@
1
+ # Copyright (c) 2007-2008 Orangery Technology Limited
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+ require 'models/persist'
5
+ require 'cgi'
6
+ require 'net/http'
7
+
8
+ require 'servicebroker/broker'
9
+ require 'Marshaller'
10
+
11
+ require 'bizLogic/applymta'
12
+ require 'bizLogic/search'
13
+ require 'bizLogic/payment'
14
+ require 'bizLogic/diff'
15
+ require 'bizLogic/continualQNB'
16
+ require 'bizLogic/VPMSHelper'
17
+
18
+ require 'processengine/HashEnhancement'
19
+
20
+ class PEngine
21
+ include XMLDiff
22
+ include Marshaller
23
+
24
+ include ApplyMTA
25
+ include Search
26
+ include Payment
27
+ include RefineQuote
28
+ include VPMSHelper
29
+
30
+ PRODUCTMODELS = Hash.new
31
+
32
+ def deriveActiveRecordDefinitionOfProduct(product)
33
+ if (PRODUCTMODELS.has_key?(product))
34
+ productModel = PRODUCTMODELS[product.to_sym]
35
+ else
36
+ require 'ProductInterpreter'
37
+ oilfile = File.join("#{DY_MODELS}/#{product}/DSL/product.oil")
38
+ open(oilfile) {|f| @contents = f.read }
39
+ dsl = @contents.to_s
40
+ if (!dsl.include?("product :#{product}"))
41
+ raise "#{DY_MODELS}/#{product}/DSL/product.oil does NOT contain a product defintion for #{product}"
42
+ end
43
+ productModel = ProductInterpreter.execute(dsl)
44
+ PRODUCTMODELS[product.to_sym] = productModel
45
+ end
46
+ productModel
47
+ end
48
+
49
+ def addDefaults(p1)
50
+ #adds default values where no value currently exists
51
+ #these values should be extracted from the data model!!
52
+ p2 = {"ContentsCoverCoverDetailOtherContentsSumInsured" => {"Amount" => "1000000"},
53
+ "ContentsCoverCoverDetailOtherStockSumInsured" => {"Amount" => "1000000"},
54
+ "ContentsCoverCoverDetailTargetStockSumInsured" => {"Amount" => "1000000"},
55
+ "BuildingsCoverCoverDetailShopFrontCoverDetailSumInsured" => {"Amount" => "1000000"},
56
+ "BuildingsCoverCoverDetailTenantsImprovementsCoverDetailSumInsured" => {"Amount" => "1000000"},
57
+ "BookDebtsCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
58
+ "BusinessInterruptionCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
59
+ "EmployersLiabilityCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
60
+ "GlassCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
61
+ "LossOfLicenceCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
62
+ "MoneyCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
63
+ "ProductLiabilityCoverCoverDetailSumInsured" => {"Amount" => "1000000"},
64
+ "PublicLiabilityCoverCoverDetailSumInsured" => {"Amount" => "1000000"}}
65
+
66
+ p2.each do |k,v|
67
+ if p1.has_key?(k)
68
+ v.each do |ik,iv|
69
+ if (!p1[k].has_key?(ik) or p1[k][ik].length == 0)
70
+ #puts "replacing value for:#{k},#{ik}"
71
+ p1[k][ik] = iv
72
+ end
73
+ end
74
+ else
75
+ #puts "replacing value for:#{k}"
76
+ p1[k] = v
77
+ end
78
+ end
79
+ return p1
80
+ end
81
+
82
+ def push(session,process,params)
83
+ package = 'CommercialProperty'
84
+ persist = Persist.instance
85
+
86
+ case process
87
+ when "ProcessDefinition"
88
+ require 'RailsProcessInterpreter'
89
+ open("#{PRODUCT_ROOT.gsub(/%product/,session[:product])}/DSL/processes.oil") {|f| @contents = f.read }
90
+ dsl = @contents.to_s
91
+ action_methods = RailsProcessInterpreter.execute(dsl)
92
+ action_methods
93
+
94
+ when "GetNBQuote"
95
+ eval(deriveActiveRecordDefinitionOfProduct(session[:product]))
96
+ xml = createXMLMessage(session[:product],params,false) { |k,v| "<#{k}>#{v}</#{k}>" }
97
+ key = persist.put("UUID",xml)
98
+ session[:policyKey] = key
99
+
100
+ if APP_CONFIG['use_rating_engine']
101
+ nvpxml = buildVPMSMessage(session[:product],package,session[:brand],params)
102
+ response = callVPMS(nvpxml)
103
+ parseVPMSresponse(response,session[:product])
104
+ xml = mergeIntoXMLDoc(xml,@premiums)
105
+ end
106
+ prepareModels(session[:product],xml)
107
+ #cmd = SBroker.RequestRatingService("NB",session[:product],true,false,false)
108
+ #quote = cmd.call(xml)
109
+
110
+ when "RefineNBQuote"
111
+ eval(deriveActiveRecordDefinitionOfProduct(session[:product]))
112
+ origxml = persist.get(session[:policyKey])
113
+ h = prepareModels(session[:product],origxml)
114
+ params = h.deep_merge(params)
115
+ combinedxml = createXMLMessage(session[:product],params,false) { |k,v| "<#{k}>#{v}</#{k}>" }
116
+ key = persist.put(session[:policyKey],combinedxml)
117
+
118
+ if APP_CONFIG['use_rating_engine']
119
+ nvpxml = buildVPMSMessage(session[:product],package,session[:brand],params)
120
+ response = callVPMS(nvpxml)
121
+ parseVPMSresponse(response,session[:product])
122
+ combinedxml = mergeIntoXMLDoc(combinedxml,@premiums)
123
+ end
124
+
125
+ prepareModels(session[:product],combinedxml)
126
+
127
+ when "Search"
128
+ executeSearch(session[:product],params)
129
+
130
+ when "FindPolicyOrQuote"
131
+ xml = persist.get(params[:choosen][:one])
132
+ prepareModels(session[:product],xml)
133
+
134
+ when "SectionRating"
135
+ "1553.25"
136
+
137
+ when "MTAReason"
138
+ xml = persist.get(session[:policyKey])
139
+ prepareModels(session[:product],xml)
140
+
141
+ when "GetMTAQuote"
142
+ #db = Persist.instance
143
+ origImage = persist.get(session[:policyKey])
144
+ #TODO: the origImage will need any MTAs layering on there
145
+ xml = createXMLMessage(session[:product],params,false) { |k,v| "<#{k}>#{v}</#{k}>" }
146
+ applyMTA(session[:mtaStartDate],session[:mtaEndDate],session[:policyKey],origImage,xml)
147
+ prepareModels(session[:product],xml)
148
+
149
+ when "PolicyDocumentation"
150
+ #get XML quote/policy document
151
+ xml = persist.get(session[:policyKey])
152
+ #call the rating engine (again!) until we've preserved the quote
153
+ cmd = SBroker.RequestRatingService("NB",session[:product],true,false,false)
154
+ quote = cmd.call(xml)
155
+ prepareModels(session[:product],xml)
156
+
157
+ when "Checkout"
158
+ next_url = setup_payment(params)
159
+ next_url
160
+
161
+ when "ConfirmPayment"
162
+ get_gateway_payment_details(params)
163
+
164
+ when "CompletedPayment"
165
+ complete_payment(params)
166
+
167
+ else
168
+ puts "**********> #{process} logic performed once it has been written <**********"
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,74 @@
1
+ # Copyright (c) 2007-2008 Orangery Technology Limited
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+ require 'net/http'
5
+ require 'RatingEngineResolver'
6
+
7
+ class SBroker
8
+ SERVICE_BROKER_ROOT = File.dirname(__FILE__)
9
+ XSL_ROOT = File.join(SERVICE_BROKER_ROOT, 'xsl')
10
+ MOCK_ROOT = File.join(SERVICE_BROKER_ROOT, 'mocks')
11
+
12
+ def self.RequestRatingService(process,product,full,partial,knockout)
13
+ open("#{PRODUCTS_ROOT}/#{product}/dsl.rb") {|f| @contents = f.read }
14
+ script = @contents.to_s
15
+ mycmd = RatingEngineResolver.execute(script)
16
+ cmd = eval(mycmd)
17
+ cmd
18
+ end
19
+
20
+ def self.InvokeRTE(msg)
21
+ Net::HTTP.start(APP_CONFIG['rte_restful_ip'], 80) do |http|
22
+ response = http.post('/RestfulXRTE.ashx/quote',msg)
23
+ response.body
24
+ end
25
+ end
26
+
27
+ def self.ApplyTransform(xml,transform,code)
28
+ require 'xml/xslt'
29
+
30
+ xslt = XML::XSLT.new()
31
+ xslt.xml = xml
32
+ xslt.xsl = File.join(XSL_ROOT, transform)
33
+ if (code != nil) then
34
+ xslt.parameters = { "code" => "#{code}" }
35
+ end
36
+ transformed_xml = xslt.serve()
37
+ transformed_xml
38
+ end
39
+
40
+ def self.ExtractSectionPremium(xml,response_xml)
41
+ begin
42
+ #expects a standard format message with a code value in
43
+ #will be ok since all the schemas are derived from the
44
+ #formal polaris library
45
+ transformed_xml = SBroker.ApplyTransform(xml,"extractCode.xsl",nil)
46
+ if (transformed_xml == nil)
47
+ transformed_xml = SBroker.ApplyTransform(xml,"extractDescription.xsl",nil)
48
+ end
49
+ msg = transformed_xml.strip
50
+ vals = msg.split('>')
51
+ #get some odd initial character here...quick hack to remove it
52
+ #need to figure out what it is
53
+ code = vals[1][1,vals[1].length]
54
+
55
+ #now extract the premium from the response
56
+ #this approach is taken so that the xslt can hide the nasties of a none XML dictionary
57
+ #TODO: provide a XSLT conversation on the way back from thre RTE
58
+ transformed_xml = SBroker.ApplyTransform(response_xml,"extractPremium.xsl",code)
59
+ msg = transformed_xml.strip
60
+
61
+ vals = msg.split('>')
62
+ #get some odd initial character here...quick hack to remove it
63
+ #need to figure out what it is
64
+ premium = vals[1][1,vals[1].length]
65
+ rescue Exception => e
66
+ puts "THE SECTION PREMIUM ERROR'ED WITH:#{e.message}"
67
+ end
68
+ end
69
+
70
+ def self.GetMockRatingResponse(mockFile)
71
+ quote = open("#{MOCK_ROOT}/" + mockFile) {|f| f.read }
72
+ quote.to_s
73
+ end
74
+ end