package Ġru . coding 4 fun . intellij . database . ui . form . security . login . database Ċ Ċ import Ġcom . intellij . ui . Checkbox Tree Listener Ċ import Ġcom . intellij . ui . Checked TreeNode Ċ import Ġru . coding 4 fun . intellij . database . model . property . security . login . Ms Database Of Login Ċ import Ġru . coding 4 fun . intellij . database . model . property . security . login . Ms Database Role Membership Ċ import Ġru . coding 4 fun . intellij . database . model . property . security . login . Ms Login Model Ċ import Ġru . coding 4 fun . intellij . database . ui . Speed Search Tree Ċ import Ġru . coding 4 fun . intellij . database . ui . form . common . Model Modification Ċ import Ġru . coding 4 fun . intellij . database . ui . form . common . Modification Tracker Ċ import Ġjavax . swing . J ScrollPane Ċ Ċ class ĠDatabase Mediator ( Ċ ĉ tree Panel : ĠJ ScrollPane , Ċ ĉ model : ĠMs Login Model Ċ ) Ġ{ Ċ ĉ private Ġclass ĠUser In Db ( Ċĉ ĉ var Ġis Selected : ĠBoolean , Ċĉ ĉ val Ġroles : ĠList < Ms Database Role Membership >, Ċĉ ĉ val Ġuser In Db : ĠMs Database Of Login , Ċĉ ĉ val Ġpublic Role Node : ĠChecked TreeNode , Ċĉ ĉ var Ġselected Roles Count : ĠInt Ġ= Ġ 0 Ċ ĉ ) Ġ{ Ċĉ ĉ val Ġrole Mods Ġ= Ġhash Map Of < String , ĠMs Database Role Membership >() ĊĊĉ ĉ init Ġ{ Ċĉĉ ĉ selected Roles Count Ġ= Ġroles . count Ġ{ Ġit . isSelected Ġ} Ċĉ ĉ } ĊĊĉ ĉ fun Ġinverse Selection () Ġ{ Ċĉĉ ĉ isSelected Ġ= Ġ! isSelected Ċĉĉ ĉ public Role Node . isChecked Ġ= Ġis Selected Ċĉĉ ĉ if Ġ(! isSelected ) Ġde select User () Ċĉ ĉ } ĊĊĉ ĉ fun Ġtoggle Role ( role : ĠMs Database Role Membership , Ġis Checked : ĠBoolean ) Ġ{ Ċĉĉ ĉ selected Roles Count Ġ+= Ġif Ġ( isChecked ) Ġ 1 Ġelse Ġ- 1 ĊĊĉĉ ĉ if Ġ( role . isSelected Ġ== Ġis Checked ) Ġrole Mods . remove ( role . name ) Ċĉĉ ĉ else Ġrole Mods [ role . name ] Ġ= Ġrole . copy ( isSelected Ġ= Ġis Checked ) Ċĉ ĉ } ĊĊĉ ĉ fun Ġde select User () Ġ{ Ċĉĉ ĉ roles . as Sequence (). filter Ġ{ Ġit . isSelected Ġ}. forEach Ġ{ Ġit . isSelected Ġ= Ġfalse Ġ} Ċĉĉ ĉ role Mods . clear () Ċĉĉ ĉ selected Roles Count Ġ= Ġ 0 Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġval Ġusers Ġ= Ġhash Map Of < String , ĠUser In Db >() ĊĊ ĉ init Ġ{ Ċĉ ĉ val Ġroles Of D bs Ġ= Ġmodel . db Roles . groupBy Ġ{ Ġit . database Name Ġ} Ċĉ ĉ val Ġroot Node Ġ= ĠChecked TreeNode () ĊĊĉ ĉ for Ġ( db Ġin Ġmodel . login Databases ) Ġ{ Ċĉĉ ĉ val Ġdb Node Ġ= ĠChecked TreeNode (). also Ġ{ Ċĉĉĉ ĉ it . user Object Ġ= Ġdb Ċĉĉĉ ĉ it . isChecked Ġ= Ġdb . isSelected Ċĉĉĉ ĉ root Node . add ( it ) Ċĉĉ ĉ } Ċĉĉ ĉ val Ġroles Ġ= Ġroles Of D bs [ db . name ] !! ĊĊĉĉ ĉ var Ġpublic Node : ĠChecked TreeNode ? Ġ= Ġnull Ċĉĉ ĉ var Ġselected Roles Count Ġ= Ġ 0 Ċĉĉ ĉ for Ġ( role Ġin Ġroles ) Ġ{ Ċĉĉĉ ĉ Checked TreeNode (). also Ġ{ Ċĉĉĉĉ ĉ it . user Object Ġ= Ġrole Ċĉĉĉĉ ĉ it . isChecked Ġ= Ġrole . isSelected Ċĉĉĉĉ ĉ db Node . add ( it ) Ċĉĉĉĉ ĉ if Ġ(" public ". equals ( role . name , Ġtrue )) Ġpublic Node Ġ= Ġit . also Ġ{ Ċĉĉĉĉĉ ĉ it . isChecked Ġ= Ġdb . isSelected Ċĉĉĉĉĉ ĉ it . isEnabled Ġ= Ġfalse Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ if Ġ( role . isSelected ) Ġ++ selected Roles Count Ċĉĉĉ ĉ } Ċĉĉ ĉ } ĊĊĉĉ ĉ val Ġuser In Db Ġ= ĠUser In Db ( db . isSelected , Ġroles , Ġdb , Ġpublic Node !! , Ġselected Roles Count ) Ċĉĉ ĉ users [ db . name ] Ġ= Ġuser In Db Ċĉ ĉ } ĊĊĊĉ ĉ val ĠcheckBox Tree Ġ= ĠSpeed Search Tree ( root Node , Ġthis :: getText For Node ) Ċĉ ĉ tree Panel . viewport . add ( checkBox Tree ) ĊĊĉ ĉ checkBox Tree . add Checkbox Tree Listener ( Ċĉĉ ĉ object Ġ: ĠCheckbox Tree Listener Ġ{ Ċĉĉĉ ĉ override Ġfun Ġnode StateChanged ( node : ĠChecked TreeNode ) Ġ{ Ċĉĉĉĉ ĉ when Ġ( val Ġuser Object Ġ= Ġnode . user Object ) Ġ{ Ċĉĉĉĉĉ ĉ is ĠMs Database Of Login Ġ-> Ġupdate Db User ( user Object , Ġnode ) Ċĉĉĉĉĉ ĉ is ĠMs Database Role Membership Ġ-> Ġupdate Role ( user Object , Ġnode ) Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ċĉĉ ĉ }) Ċ ĉ } ĊĊ ĉ private Ġfun ĠgetText For Node ( value : ĠAny ): ĠString Ġ{ Ċĉ ĉ if Ġ( value Ġ! is ĠChecked TreeNode ) Ġreturn Ġ"-" Ċĉ ĉ return Ġwhen Ġ( val Ġuser Object Ġ= Ġvalue . user Object ) Ġ{ Ċĉĉ ĉ is ĠMs Database Of Login Ġ-> Ġuser Object . name Ċĉĉ ĉ is ĠMs Database Role Membership Ġ-> Ġuser Object . name Ċĉĉ ĉ else Ġ-> Ġ"-" Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġupdate Db User ( db : ĠMs Database Of Login , Ġnode : ĠChecked TreeNode ) Ġ{ Ċĉ ĉ val Ġuser Ġ= Ġusers [ db . name ] !! Ċĉ ĉ if Ġ( node . isChecked Ġ!= Ġuser . isSelected ) Ġuser . inverse Selection () Ċ ĉ } ĊĊ ĉ private Ġfun Ġupdate Role ( role : ĠMs Database Role Membership , Ġnode : ĠChecked TreeNode ) Ġ{ Ċĉ ĉ val Ġuser Ġ= Ġusers [ role . database Name ] !! Ċĉ ĉ user . toggle Role ( role , Ġnode . isChecked ) Ċ ĉ } ĊĊ ĉ val Ġdb Mod Tracker Ġ= ĠDatabase Modification Tracker () Ċ ĉ val Ġdb Role Mod Tracker Ġ= ĠDatabase Role Modification Tracker () ĊĊ ĉ inner Ġclass ĠDatabase Modification Tracker Ġ: ĠMod ification Tracker < Ms Database Of Login > Ġ{ Ċĉ ĉ override Ġfun Ġget Mod ifications (): ĠList < Model Modification < Ms Database Of Login >> Ġ{ Ċĉĉ ĉ return Ġusers . values . filter Ġ{ Ġit . isSelected Ġ!= Ġit . user In Db . isSelected Ġ} Ċĉĉĉ ĉ . map Ġ{ ĠModel Modification ( it . user In Db , Ġit . user In Db . copy ( isSelected Ġ= Ġit . isSelected )) Ġ} Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ inner Ġclass ĠDatabase Role Modification Tracker Ġ: ĠMod ification Tracker < Ms Database Role Membership > Ġ{ Ċĉ ĉ override Ġfun Ġget Mod ifications (): ĠList < Model Modification < Ms Database Role Membership >> Ġ{ Ċĉĉ ĉ return Ġusers . values . filter Ġ{ Ġit . isSelected Ġ&& Ġit . role Mods . any () Ġ} Ċĉĉĉ ĉ . flatMap Ġ{ Ġuser Ġ-> Ċĉĉĉĉ ĉ user . role Mods . values . map Ġ{ Ċĉĉĉĉĉ ĉ Model Modification ( Ċĉĉĉĉĉĉ ĉ it . copy ( isSelected Ġ= Ġ! it . isSelected ), Ċĉĉĉĉĉĉ ĉ it Ċĉĉĉĉĉ ĉ ) Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } Ċ }