package Ġcom . starting ground . cogn eb us . utilities Ċ Ċ import Ġcom . starting ground . cogn eb us . database . C ogn eb us Database Ċ import Ġcom . starting ground . cogn eb us . database . entity . Folder Ċ import Ġjavax . inject . Inject Ċ Ċ class ĠFolder Utils Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġfile DB Utils : ĠFile DB Utils , ĊĠĠĠ Ġprivate Ġval Ġdatabase : ĠC ogn eb us Database Ċ ){ ĊĊĠĠĠ Ġsuspend Ġfun Ġcopy Folders To ( folder List : ĠList < Folder >, Ġdestination Folder Id : ĠLong ? ){ ĊĠĠĠĠĠĠĠ Ġval Ġfolders In Destination Ġ= Ġdatabase . folder Database Dao . get Folders By Parent Folder Id ( destination Folder Id ) ĊĠĠĠĠĠĠĠ Ġval Ġfolder Names In Destination Ġ= Ġfolders In Destination . map Ġ{ Ġit . name Ġ} ĊĊĠĠĠĠĠĠĠ Ġfor ( folder Ġin Ġfolder List ){ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġthere Is Destination Folder With Same Name Ġ= Ġfolder Names In Destination . contains ( folder . name ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġnew Sub Destination Folder Id Ġ= Ġif Ġ( there Is Destination Folder With Same Name ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġdestination Folder With Same Name Ġ= Ġfolders In Destination . find Ġ{ Ġit . name Ġ== Ġfolder . name Ġ} !! ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdestination Folder With Same Name . folder Id ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfolder Copy Ġ= Ġfolder . copy ( folder Id Ġ= Ġ 0 L , Ġparent Folder Id Ġ= Ġdestination Folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdatabase . folder Database Dao . insert ( folder Copy ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfiles In Folder Ġ= Ġdatabase . file Database Dao . get Files By Folder Id ( folder . folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( files In Folder . isNotEmpty ()){ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfile DB Utils . copy Files To ( files In Folder , Ġnew Sub Destination Folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġsub Folder List Ġ= Ġdatabase . folder Database Dao . get Folders By Parent Folder Id ( folder . folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( sub Folder List . isEmpty ()) Ġcontinue ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġcopy Folders To ( sub Folder List , Ġnew Sub Destination Folder Id ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġsuspend Ġfun Ġis There Folder With Same Name ( folder List : ĠList < Folder >, Ġdestination Folder Id : ĠLong ?): ĠBoolean { ĊĠĠĠĠĠĠĠ Ġval Ġfolders In Destination Ġ= Ġdatabase . folder Database Dao . get Folders By Parent Folder Id ( destination Folder Id ) ĊĊĠĠĠĠĠĠĠ Ġval Ġfolder List Names Ġ= Ġfolder List . map Ġ{ Ġit . name Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġmatching Folder Ġ= Ġfolders In Destination . find Ġ{ Ġit . name Ġin Ġfolder List Names Ġ} ĊĊĠĠĠĠĠĠĠ Ġreturn Ġmatching Folder Ġ!= Ġnull ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġsuspend Ġfun Ġis There Same File Within Folders ( folder List : ĠList < Folder >, Ġdestination Folder Id : ĠLong ?): ĠBoolean { ĊĠĠĠĠĠĠĠ Ġval Ġfolders In Destination Ġ= Ġdatabase . folder Database Dao . get Folders By Parent Folder Id ( destination Folder Id ) ĊĊĠĠĠĠĠĠĠ Ġval Ġfolder List Names Ġ= Ġfolder List . map Ġ{ Ġit . name Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġmatching Destination Folders Ġ= Ġfolders In Destination . filter Ġ{ Ġit . name Ġin Ġfolder List Names Ġ} ĊĊĠĠĠĠĠĠĠ Ġmatching Destination Folders . forEach Ġ{ Ġfolder Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġorigin Folder Ġ= Ġfolder List . find Ġ{ Ġit . name Ġ== Ġfolder . name Ġ} Ġ?: Ġreturn @ forEach ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfiles Inside Origin Folder Ġ= Ġdatabase . file Database Dao . get Files By Folder Id ( origin Folder . folder Id ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġvar Ġthere Is File With Same Name Ġ= Ġfile DB Utils . is There File With Same Name ( files Inside Origin Folder , Ġfolder . folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( there Is File With Same Name ) Ġreturn Ġtrue ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfolders Inside Origin Folder Ġ= Ġdatabase . folder Database Dao . get Folders By Parent Folder Id ( origin Folder . folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthere Is File With Same Name Ġ= Ġis There Same File Within Folders ( folders Inside Origin Folder , Ġfolder . folder Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( there Is File With Same Name ) Ġreturn Ġtrue ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġreturn Ġfalse ĊĠĠĠ Ġ} Ċ }