package Ġcom . example . employee administration . services Ċ Ċ import Ġcom . example . employee administration . model . aggreg ates . Aggregate State Ċ import Ġcom . example . employee administration . model . aggreg ates . Department Ċ import Ġcom . example . employee administration . model . dto . Department Dto Ċ import Ġcom . example . employee administration . model . dto . Department Sync Ċ import Ġcom . example . employee administration . repositories . Department Repository Ċ import Ġcom . example . employee administration . repositories . Employee Repository Ċ import Ġcom . fasterxml . jackson . databind . ObjectMapper Ċ import Ġorg . springframework . stereotype . Service Ċ import Ġorg . springframework . transaction . annotation . Transactional Ċ Ċ @ Service Ċ class ĠDepartment Service ( ĊĠĠĠĠĠĠĠ Ġval Ġdepartment Repository : ĠDepartment Repository , ĊĠĠĠĠĠĠĠ Ġval Ġemployee Repository : ĠEmployee Repository , ĊĠĠĠĠĠĠĠ Ġval Ġworkflow Service : ĠWorkflow Service , ĊĠĠĠĠĠĠĠ Ġval ĠobjectMapper : ĠObjectMapper Ċ ): ĠWorkflow Persistence Service < Department >, ĠMapping Service < Department , ĠDepartment Dto >, ĠSync Mapping Service < Department , ĠDepartment Sync > Ġ{ ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġoverride Ġfun Ġsave Aggregate With Workflow ( aggregate : ĠDepartment , Ġcomp ensation Aggregate : ĠDepartment ?): ĠDepartment Ġ{ ĊĠĠĠĠĠĠĠ Ġvar Ġvariables Map Ġ= Ġempty Map < String , ĠString >() ĊĠĠĠĠĠĠĠ Ġ// ĠIf Ġthe Ġemployee Ġwas Ġcreated Ġno Ġcomp ensation Ġdata Ġis Ġnecessary ĊĠĠĠĠĠĠĠ Ġif Ġ( comp ensation Aggregate Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġvariables Map Ġ= Ġvariables Map . plus (" comp ensation Department " Ġto ĠobjectMapper . writeValue AsString ( map To Sync Dto ( comp ensation Aggregate ))) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġaggregate . state Ġ= ĠAggregate State . PENDING ĊĠĠĠĠĠĠĠ Ġval Ġsaved Department Ġ= Ġdepartment Repository . save ( aggregate ) ĊĠĠĠĠĠĠĠ Ġvariables Map Ġ= Ġvariables Map . plus (" department " Ġto ĠobjectMapper . writeValue AsString ( map To Sync Dto ( saved Department ))) ĊĠĠĠĠĠĠĠ Ġworkflow Service . create Workflow Instance ( variables Map , Ġ" synchron ize - department ") ĊĠĠĠĠĠĠĠ Ġreturn Ġsaved Department ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġmap To Dto ( aggregate : ĠDepartment ): ĠDepartment Dto Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠDepartment Dto ( aggregate . id !! , Ġaggregate . name , Ġaggregate . state ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġmap To Entity ( dto : ĠDepartment Dto ): ĠDepartment Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠDepartment ( dto . id , Ġdto . name ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġmap To Sync Dto ( aggregate : ĠDepartment ): ĠDepartment Sync Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠDepartment Sync ( aggregate . id !! , Ġaggregate . name , Ġaggregate . deleted , Ġaggregate . state ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun ĠgetAll Depart ments (): ĠList < Department Dto > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġdepartment Repository . getAll By Deleted False (). map Ġ{ Ġmap To Dto ( it ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Department ById ( id : ĠLong ): ĠDepartment Dto Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġdepartment Repository . getById And Deleted False ( id ). map Ġ{ Ġmap To Dto ( it ) Ġ}. orElse Throw () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġfun Ġcreate Department ( department Dto : ĠDepartment Dto ): ĠDepartment Dto Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġdepartment Repository . get ByName And Deleted False ( department Dto . name ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġmap To Dto ( it ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. orElse Get Ġ{ Ġmap To Dto ( save Aggregate With Workflow ( map To Entity ( department Dto ), Ġnull )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġfun Ġupdate Department ( department Dto : ĠDepartment Dto ): ĠDepartment Dto Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġdepartment Ġ= Ġdepartment Repository . findById ( department Dto . id !! ). orElse Throw () ĊĠĠĠĠĠĠĠ Ġval Ġcomp ensation Department Ġ= Ġdepartment . copy () ĊĠĠĠĠĠĠĠ Ġif Ġ( department . state Ġ== ĠAggregate State . PENDING ) Ġthrow ĠRuntimeException (" Department Ġis Ġstill Ġpending ") ĊĠĠĠĠĠĠĠ Ġif Ġ( department . name Ġ!= Ġdepartment Dto . name ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdepartment . rename Department ( department Dto . name ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġmap To Dto ( save Aggregate With Workflow ( department , Ġcomp ensation Department )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġfun Ġdelete Department ( id : ĠLong ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( employee Repository . getAll By Department _ Id And Deleted False ( id ). isEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġdepartment Ġ= Ġdepartment Repository . getById And Deleted False ( id ). orElse Throw Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠException (" The Ġdepartment Ġyou Ġare Ġtrying Ġto Ġdelete Ġdoes Ġnot Ġexist ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( department . state Ġ== ĠAggregate State . PENDING ) Ġthrow ĠRuntimeException (" Department Ġis Ġstill Ġpending ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġcomp ensation Department Ġ= Ġdepartment . copy () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdepartment . delete Aggregate () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsave Aggregate With Workflow ( department , Ġcomp ensation Department ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠException (" The Ġdepartment Ġhas Ġemployees Ġassigned Ġto Ġit Ġand Ġcannot Ġbe Ġdeleted .") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ }