This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jl.old
725 lines (656 loc) · 22.5 KB
/
app.jl.old
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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
module App
using GenieFramework
using JSON, DataStructures, Dates, SearchLight, TimeZones
using LifeInsuranceContracts
import LifeInsuranceContracts: tostruct, find, SQLWhereExpression,
connect, get_contracts, get_partners, get_ids, get_products, serialize, deserialize, create_component!, update_component!, update_entity!, commit_workflow!, rollback_workflow!, persistModelStateContract,
ContractPartnerRole, TariffItemRole, TariffItemPartnerRole,
Contract, Partner, Product, Tariff, Workflow,
ContractPartnerRefRevision,
ContractSection, PartnerSection, ProductItemSection, TariffItemSection, ProductSection, TariffSection,
csection, psection, pisection, prsection, tsection,
get_contracts, get_partners, get_products, history_forest,
get_tariff_interface, persist_tariffs, compareModelStateContract, compareRevisions, treeview, get_node_by_label, load_role
@genietools
include("treeedit.jl")
CS_UNDO = Stack{Dict{String,Any}}()
function validate_calcargs(args::Dict{String,Any})
fn = args["calculation_target"]["selected"]
mapreduce(def -> !(isnothing(def.second["value"]) || def.second["value"] == ""), &, args["calculation_target"][fn])
end
@app begin
@out activetxn::Bool = false
@in command::String = ""
@in prompt_new_txn::Bool = false
@in prompt_create_contract::Bool = false
@out contracts::Vector{Contract} = []
@out contract_ids::Dict{Int64,Int64} = Dict{Int64,Int64}()
@out current_contract::Contract = Contract()
@in selected_contract_idx::Integer = -1
@out current_workflow::Workflow = Workflow()
@out partners::Vector{Partner} = []
@out partner_ids::Vector{Dict{String,Any}} = []
@out current_partner::Partner = Partner()
@in selected_partner_idx::Integer = -1
@out products::Vector{Product} = []
@out product_ids::Vector{Dict{String,Any}} = []
@out product_names::Dict{Integer,String} = Dict()
@out current_product::Product = Product()
@in selected_product_idx::Integer = -1
@in new_contract_partner_role::Integer = 0
@in new_contract_partner::Integer = 0
@in selected_contractpartner_idx::Integer = -1
@in selected_productitem_idx::Integer = -1
@in selected_tariffitem_idx::Integer = -1
@in tariffitem_selections::Dict{Integer,Dict{Integer,Bool}} = Dict()
@in new_tariffitem_partner::Dict{Integer,Integer} = Dict()
@in selected_version::String = ""
@out current_version::Integer = 0
@out txn_time::ZonedDateTime = now(tz"UTC")
@in newreftime::String = ""
@out ref_time::ZonedDateTime = now(tz"UTC")
@out histo::Vector{Dict{String,Any}} = Dict{String,Any}[]
@in cs::Dict{String,Any} = Dict{String,Any}("loaded" => "false")
@out cs_persisted = Dict{String,Any}()
@out ps::Dict{String,Any} = Dict{String,Any}("loaded" => "false")
@out prs::Dict{String,Any} = Dict{String,Any}("loaded" => "false")
@out prs0::ProductSection = ProductSection()
@in selected_product_part_idx::Integer = 0
@in tab::String = "csection"
@in leftDrawerOpen::Bool = false
@in show_contract_partners::Bool = false
@in show_product_items::Bool = false
@in new_product_reference::Integer = 0
@in productpartnerroles::Dict{Integer,Integer} = Dict()
@out partnerrolemap::Dict{Integer,PartnerSection} = Dict()
@out tpidrolemap::Dict{Integer,Integer} = Dict{Integer,Integer}()
@in show_tariff_item_partners::Bool = false
@in show_tariff_items::Bool = false
@out d::Dict{String,Any} = Dict()
@in tree::Vector{Dict{Symbol,Any}} = []
@out rolesContractPartner::Vector{Dict{String,Any}} = []
@out rolesTariffItem::Vector{Dict{String,Any}} = []
@out rolesTariffItemPartner::Vector{Dict{String,Any}} = []
# tariff calculations
@in tariffcalculation::Dict{String,Any} = get_tariff_interface(0).calls
@in calculate::Bool = false
@in sync::Bool = false
@in loadca::Bool = false
@out validated::Bool = false
@in opendialogue::Bool = false
@out tariff_interface_id::Integer = 0
@onchange isready begin
connect()
rolesContractPartner = load_role(ContractPartnerRole)
rolesTariffItem = load_role(TariffItemRole)
rolesTariffItemPartner = load_role(TariffItemPartnerRole)
@show rolesContractPartner
@show rolesTariffItem
@show rolesTariffItemPartner
cs["loaded"] = "false"
prs = Dict{String,Any}("loaded" => "false")
ps = Dict{String,Any}("loaded" => "false")
partners = get_partners()
push!(partner_ids, Dict("value" => 0, "label" => "nobody"))
partner_ids = get_ids(partners)
for p in get_products()
rev = prsection(p.id.value, now(tz"UTC"), now(tz"UTC"), 0).revision
push!(product_ids, Dict("label" => rev.description, "value" => rev.id.value))
product_names[rev.id.value] = rev.description
end
push!(product_ids, Dict("label" => "none", "value" => 0))
try
@info "vor tariffcalculation tariff_interface_id"
@push
@show partner_ids
@show product_ids
@show "App is loaded"
tab = "contracts"
catch err
println("wassis shief gegangen ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
@onchange cs begin
@info "cs changed"
end
"""
@onchange prompt_new_txn
starting a new txn on the current contract
"""
@onchange prompt_new_txn begin
@info "prompt_new_txn pressed"
@show prompt_new_txn
if !prompt_new_txn
@show newreftime
@show ref_time
if newreftime == ""
@info "cancelled"
else
n = replace(newreftime, "/" => "-")
ref_time = ZonedDateTime(DateTime(n), tz"UTC")
current_workflow = Workflow(
type_of_entity="Contract",
tsw_validfrom=ref_time,
ref_history=current_contract.ref_history
)
txntime = current_workflow.tsdb_validfrom
update_entity!(current_workflow)
activetxn = true
cs = JSON.parse(serialize(csection(current_contract.id.value, now(tz"UTC"), ref_time, activetxn ? 1 : 0)))
cs["loaded"] = "true"
cs_persisted = deepcopy(cs)
@info "cs==cs_persisted?"
@show cs == cs_persisted
@show cs
@show cs_persisted
push!(CS_UNDO, cs_persisted)
end
else
@info "öffnen"
end
end
"""
@onchange prompt_create_contract
creating a new contract whereby a txn on the new contract is started
"""
@onchange prompt_create_contract begin
@info "prompt_create_contract pressed"
@show prompt_create_contract
if !prompt_create_contract
activetxn = true
w1 = Workflow(
type_of_entity="Contract",
tsw_validfrom=ref_time,
tsdb_validfrom=now(tz"UTC")
)
create_entity!(w1)
c = Contract()
cr = ContractRevision(description="contract creation properties")
@info "before create component"
create_component!(c, cr, w1)
@info "after create component"
current_workflow = w1
current_contract = c
histo = map(treeview, history_forest(current_contract.ref_history.value).shadowed)
cs = JSON.parse(serialize(csection(current_contract.id.value, now(tz"UTC"), ref_time, 1)))
cs["loaded"] = "true"
cs_persisted = deepcopy(cs)
@info "cs==cs_persisted?"
@show cs == cs_persisted
push!(CS_UNDO, cs_persisted)
@push
end
end
"""
@onchange new_product_reference
select a product reference for the first product item productpartnerroles is initialized for the selected product
"""
@onchange new_product_reference begin
@show new_product_reference
if new_product_reference > 0
@show current_workflow
prs0 = prsection(new_product_reference, now(tz"UTC"), ref_time)
@show prs0
productpartnerroles = Dict()
map(prs0.parts) do pt
for r in pt.ref.partner_roles
productpartnerroles[r.ref_role.value] = 0
end
end
@show productpartnerroles
@push
end
end
@onchange productpartnerroles begin
@info "productpartnerroles"
@show productpartnerroles
end
@onchange selected_contract_idx begin
if (selected_contract_idx >= 0)
@show "selected_contract_idx"
@show selected_contract_idx
@info "enter selected_contract_idx"
try
current_contract = contracts[selected_contract_idx+1]
activetxn = length(find(ValidityInterval, SQLWhereExpression("ref_history=? and is_committed=0", current_contract.ref_history))) == 1
@show current_contract
@show activetxn
if activetxn
current_workflow = find(Workflow, SQLWhereExpression("ref_history=? and is_committed=0", current_contract.ref_history))[1]
ref_time = current_workflow.tsw_validfrom
histo = map(treeview, history_forest(current_contract.ref_history.value).shadowed)
cs = JSON.parse(serialize(csection(current_contract.id.value, now(tz"UTC"), ref_time, activetxn ? 1 : 0)))
cs["loaded"] = "true"
new_product_reference = 0
else
ref_time = MaxDate - Day(1)
histo = map(treeview, history_forest(current_contract.ref_history.value).shadowed)
cs = JSON.parse(serialize(csection(current_contract.id.value, now(tz"UTC"), ref_time, activetxn ? 1 : 0)))
cs["loaded"] = "true"
end
for pi in 1:length(cs["product_items"])
tariffitem_selections[pi] = Dict()
for ti in 1:length(cs["product_items"][pi]["tariff_items"])
tariffitem_selections[pi][ti] = false
end
end
selected_tariffitem_idx = -1
selected_productitem_idx = -1
@push
@show current_workflow
cs_persisted = deepcopy(cs)
@info "cs==cs_persisted?"
@show cs == cs_persisted
@show cs
@show cs_persisted
push!(CS_UNDO, cs_persisted)
tab = "csection"
selected_contract_idx = -1
@info "contract loaded"
@show cs_persisted
catch err
println("wassis shief gegangen ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
end
@onchange selected_partner_idx begin
@show selected_partner_idx
@info "selected_partner_idx"
if (selected_partner_idx >= 0)
@show selected_partner_idx
@info "enter selected_partner_idx"
try
current_partner = partners[selected_partner_idx+1]
# histo = map(treeview, history_forest(current_contract.ref_history.value).shadowed)
ps = JSON.parse(serialize(psection(current_partner.id.value, now(tz"UTC"), now(tz"UTC"), activetxn ? 1 : 0)))
ps["loaded"] = "true"
selected_partner_idx = -1
ps["loaded"] = "true"
@show ps["loaded"]
tab = "partner"
@show tab
catch err
println("wassis shief gegangen ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
end
@onchange selected_product_idx begin
@show selected_product_idx
@info "selected_product_idx"
if (selected_product_idx >= 0)
@show selected_product_idx
@info "enter selected_product_idx"
try
current_product = products[selected_product_idx+1]
# histo = map(treeview, history_forest(current_contract.ref_history.value).shadowed)
prs = JSON.parse(serialize(prsection(current_product.id.value, now(tz"UTC"), now(tz"UTC"), activetxn ? 1 : 0)))
selected_product_idx = -1
prs["loaded"] = "true"
@show prs["loaded"]
tab = "product"
@show tab
catch err
println("wassis shief gegangen ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
end
@onchange selected_contractpartner_idx begin
if selected_contractpartner_idx != -1
@show selected_contractpartner_idx
end
end
@onchange selected_productitem_idx begin
if selected_productitem_idx != -1
@show selected_productitem_idx
end
end
@onchange selected_tariffitem_idx begin
if selected_tariffitem_idx != -1
@info "gleich tariff_interface_id=@="
@show selected_tariffitem_idx
for p in keys(tariffitem_selections)
for t in keys(tariffitem_selections[p])
tariffitem_selections[p][t] = false
end
end
@info "nach reset"
@show tariffitem_selections
tariffitem_selections[selected_productitem_idx+1][selected_tariffitem_idx+1] = true
@show tariffitem_selections
@info "setting tariff_interface_id"
tariff_interface_id = cs["product_items"][selected_productitem_idx+1]["tariff_items"][selected_tariffitem_idx+1]["tariff_ref"]["ref"]["revision"]["interface_id"]
@show tariff_interface_id
tariffcalculation = get_tariff_interface(Val(tariff_interface_id)).calls
@show tariffcalculation
@push
end
end
@onchange new_contract_partner begin
@show new_contract_partner
end
@onchange new_contract_partner_role begin
@show new_contract_partner_role
end
@onchange command begin
if command != ""
try
@show command
"""
command clear contract
"""
if command == "clear contract"
current_contract = Contract()
cs = Dict{String,Any}("loaded" => "false")
end
"""
command add productitem
"""
if command == "add productitem"
@show command
@show productpartnerroles
@show values(productpartnerroles)
partnerrolemap::Dict{Integer,PartnerSection} = Dict()
for key in keys(productpartnerroles)
partnerrolemap[key] = psection(productpartnerroles[key], now(tz"UTC"), ref_time)
end
@show partnerrolemap
@info "before instantiation"
pis = instantiate_product(prs0, partnerrolemap)
@info "productitem created"
pisj = JSON.parse(serialize(pis))
@show pisj
cs["product_items"] = [pisj]
@show cs["product_items"]
@push
command = ""
end
if command == "add contractpartner"
@show command
@show cs["partner_refs"]
@show new_contract_partner_role
@show new_contract_partner
cprj = JSON.parse(serialize(ContractPartnerReference(
rev=ContractPartnerRefRevision(ref_role=DbId(new_contract_partner_role), ref_partner=DbId(new_contract_partner)),
ref=PartnerSection())))
new_contract_partner_role = 0
new_contract_partner = 0
append!(cs["partner_refs"], [cprj])
@show cs["partner_refs"]
@info "anzahl prefs= "
@info length(cs["partner_refs"])
@push
command = ""
end
if command == "start transaction"
activetxn = true
w1 = Workflow(
type_of_entity="Contract",
ref_history=current_contract.ref_history,
tsw_validfrom=ref_time,
)
update_entity!(w1)
current_workflow = w1
cs = JSON.parse(serialize(csection(current_contract.id.value, now(tz"UTC"), ref_time, activetxn ? 1 : 0)))
cs["loaded"] = "true"
@push
@show command
command = ""
end
if startswith(command, "delete_contract_partner")
@show command
@show first(CS_UNDO)["partner_refs"]
idx = parse(Int64, chopprefix(command, "delete_contract_partner:"))
@show cs["partner_refs"][idx+1]["rev"]
if isnothing(cs["partner_refs"][idx+1]["rev"]["id"]["value"])
deleteat!(cs["partner_refs"], idx + 1)
@info "after delete new cp"
else
cs["partner_refs"][idx+1]["rev"]["ref_invalidfrom"]["value"] = current_workflow.ref_version
@show cs["partner_refs"][idx+1]["rev"]
@info "after delete persisted cp"
end
@push
end
if command == "pop"
@info "before pop"
@show first(CS_UNDO)["partner_refs"]
cs = pop!(CS_UNDO)
@push
@info "after pop"
@show cs["partner_refs"]
end
if command == "push"
push!(CS_UNDO, deepcopy(cs))
@show first(CS_UNDO)["partner_refs"]
end
if command == "compare"
@show command
@show cs_persisted
deltas = compareModelStateContract(cs_persisted, cs, current_workflow)
@info "showing deltas"
@show deltas
end
if command == "persist"
@show command
@show cs_persisted
persistModelStateContract(cs_persisted, cs, current_workflow, current_contract)
cs = JSON.parse(serialize(csection(current_contract.id.value, txn_time, ref_time, 1)))
cs["loaded"] = "true"
@push
end
if command == "validate"
@show command
for pij in cs["product_items"]
validate(tostruct(ProductItemSection, pij))
end
end
if command == "commit"
@show command
@show current_workflow
commit_workflow!(current_workflow)
activetxn = 0
current_workflow = Workflow()
end
if command == "rollback"
@show command
@show current_workflow
rollback_workflow!(current_workflow)
activetxn = 0
command = "clear contract"
end
command = ""
catch err
println("wassis shief gegangen ")
@error "ERROR: " exception = (err, catch_backtrace())
command = ""
end
end
end
@onchange selected_version begin
@info "version handler"
@show selected_version
if selected_version != ""
@show tab
try
node = get_node_by_label(histo, selected_version)
@info "node"
@show node
activetxn = (node["interval"]["is_committed"] == 0 ? true : false)
txn_time = node["interval"]["tsdb_validfrom"]
ref_time = node["interval"]["tsworld_validfrom"]
current_version = parse(Int, selected_version)
@show activetxn
@show txn_time
@show ref_time
@show current_version
@info "vor csection"
cs = JSON.parse(serialize(csection(current_contract.id.value, txn_time, ref_time, activetxn ? 1 : 0)))
cs["loaded"] = "true"
@info "vor tab "
tab = "csection"
catch err
println("wassis shief gegangen ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
end
@onchange opendialogue begin
@info "opendialogue"
tariffcalculation = get_tariff_interface(tariff_interface_id).calls
validated = false
@show tariffcalculation
@push
end
@onchange tariffcalculation begin
if haskey(tariffcalculation, "calculation_target")
let fn = tariffcalculation["calculation_target"]["selected"]
if fn != "none"
validated = validate_calcargs(tariffcalculation)
@show validated
end
end
end
end
@onchange sync begin
if sync
@info "sync called"
ca = cs["product_items"][selected_productitem_idx+1]["tariff_items"][selected_tariffitem_idx+1]["contract_attributes"]
tc = tariffcalculation["calculation_target"]
@show tc
tcsel = tc["selected"]
@show tcsel
args = tc[tcsel]
@show args
for k in keys(args)
@info ("key= " * k)
if haskey(ca, k)
@show ca[k]
if args[k]["type"] == "Int"
@info "int"
@show parse(Int, args[k]["value"])
ca[k]["value"] = parse(Int, args[k]["value"])
else
@info "not int"
@show args[k]["value"]
ca[k]["value"] = args[k]["value"]
end
end
end
if haskey(ca, tcsel)
ca[tcsel]["value"] = Int(round(tariffcalculation["result"]["value"]))
end
@show ca
@push
sync = false
end
end
@onchange loadca begin
try
if loadca
@info "loadca called"
ca = cs["product_items"][selected_productitem_idx+1]["tariff_items"][selected_tariffitem_idx+1]["contract_attributes"]
tc = tariffcalculation["calculation_target"]
@show tc
tcsel = tc["selected"]
@show tcsel
if tcsel != "none"
args = tc[tcsel]
@show args
@show ca
for k in keys(args)
@info k
if haskey(ca, k) && !isnothing(ca[k])
@show ca[k]
if args[k]["type"] == "Int"
@info "int"
@show ca[k]
args[k]["value"] = string(ca[k]["value"])
else
@info "not int"
@show(ca[k])
args[k]["value"] = string(ca[k]["value"])
end
end
end
validated = validate_calcargs(tariffcalculation)
@show ca
@show args
loadca = false
@push
end
end
catch err
@info("Exception calculating ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
@onchange calculate begin
if calculate
calculate = false
@info "calculating"
try
@show tariff_interface_id
calculator = get_tariff_interface(tariff_interface_id).calculator
@show selected_productitem_idx
@show selected_tariffitem_idx
ti = tostruct(TariffItemSection, cs["product_items"][selected_productitem_idx+1]["tariff_items"][selected_tariffitem_idx+1])
@info "vor calc"
@show tariffcalculation
calculator(ti, tariffcalculation)
@push
catch err
@info("Exception calculating ")
@error "ERROR: " exception = (err, catch_backtrace())
end
end
end
@onchange tab begin
@show tab
if tab == "contracts"
current_contract = Contract()
contracts = get_contracts()
let df = SearchLight.query("select distinct c.id, w.is_committed from contracts c join histories h on c.ref_history = h.id join workflows w on w.ref_history = h.id ")
for p in Pair.(df.id, df.is_committed)
println(p)
if haskey(contract_ids, p.first)
if p.second == 0
contract_ids[p.first] = 0
end
else
contract_ids[p.first] = p.second
end
end
end
@show contract_ids
@push
@info "contractsModel pushed"
end
if (tab == "partners")
partners = get_partners()
@info "read partners"
end
if (tab == "products")
@info "products"
products = get_products()
@show products
@push
@info "read products"
end
if (tab == "csection")
@show tab
end
if (tab == "product")
@show tab
end
if (tab == "partner")
@show tab
end
@info "leave tab handler"
end
end
@page("/", "app.jl.html")
end