package Ġat . cp ick l . g ads u . appointment Ċ Ċ import Ġat . cp ick l . g ads u . appointment . g cal . G Cal Event Ċ import Ġat . cp ick l . g ads u . appointment . g cal . G Cal Event Meta Ċ import Ġat . cp ick l . g ads u . appointment . g cal . G Cal Service Ċ import Ġat . cp ick l . g ads u . appointment . g cal . G Cal Update Event Ċ import Ġat . cp ick l . g ads u . client . Client Ċ import Ġat . cp ick l . g ads u . client . Client Repository Ċ import Ġat . cp ick l . g ads u . service . Clock Ċ import Ġat . cp ick l . g ads u . service . LOG Ċ import Ġcom . google . common . event bus . EventBus Ċ import Ġorg . joda . time . DateTime Ċ import Ġjavax . inject . Inject Ċ Ċ // fun Ġmain ( args : ĠArray < String >) Ġ{ Ċ //// ĠĠĠ Ġval Ġkotlin Hack Ġ= Ġwhen Ġ( validate ()) Ġ{ Ċ //// ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Ok Ġ-> Ġprintln (" Ok ") Ċ //// ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Too Long Ġ-> Ġprintln (" No pe ") Ċ //// ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Miss Format Ġ-> Ġprintln (" No pe ") Ċ //// ĠĠĠ Ġ} Ċ // ĠĠĠ Ġval Ġresult Ġ= Ġvalidate () Ċ // ĠĠĠ Ġval Ġkotlin Hack Ġ= Ġwhen Ġ( result ) Ġ{ Ċ // ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Ok Ġ-> Ġprintln (" Ok ") Ċ // ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Miss Format Ġ-> Ġprintln (" No pe , Ġformat : Ġ${ result . input }") Ċ // Ċ // ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Too Long Ġ-> Ġprintln (" No pe , Ġtoo Ġlong : Ġ${ result . length }") Ġ// Ġmust Ġcome Ġbefore Ġabstract Ċ // ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Too Short Ġ-> Ġprintln (" No pe , Ġtoo Ġshort : Ġ${ result . length }") Ċ // ĠĠĠĠĠĠĠ Ġis ĠValidation Result . Size Invalid Ġ-> Ġthrow ĠRuntimeException (" not Ġreachable !") Ċ // ĠĠĠ Ġ} Ċ //} Ċ // fun Ġvalidate (): ĠValidation Result Ġ{ Ċ // ĠĠĠ Ġreturn ĠValidation Result . Too Long ( 4 2 ) Ċ //} Ċ // sealed Ġclass ĠValidation Result Ġ{ Ċ // ĠĠĠ Ġclass ĠOk Ġ: ĠValidation Result () Ċ // ĠĠĠ Ġabstract Ġclass ĠSize Invalid ( val Ġlength : ĠInt ) Ġ: ĠValidation Result () Ċ // ĠĠĠ Ġclass ĠToo Long ( length : ĠInt ) Ġ: ĠSize Invalid ( length ) Ċ // ĠĠĠ Ġclass ĠToo Short ( length : ĠInt ) Ġ: ĠSize Invalid ( length ) Ċ // ĠĠĠ Ġclass ĠM iss Format ( val Ġinput : ĠString ) Ġ: ĠValidation Result () Ċ //} Ċ Ċ interface ĠAppointment Service Ġ{ ĊĠĠĠ Ġfun ĠfindAll Future For ( client : ĠClient ): ĠList < Appointment > ĊĠĠĠ Ġfun Ġinsert OrUpdate ( appointment : ĠAppointment ): ĠAppointment ĊĠĠĠ Ġfun Ġdelete All ( client : ĠClient ) ĊĠĠĠ Ġfun Ġdelete ( appointment : ĠAppointment ) ĊĊĠĠĠ Ġfun Ġupcoming Appointment For ( client : ĠClient ): ĠAppointment ? ĊĠĠĠ Ġfun Ġupcoming Appointment For ( clientId : ĠString ): ĠAppointment ? ĊĠĠĠ Ġfun ĠfindAll Between ( range : ĠPair < DateTime , ĠDateTime >): ĠList < Appointment > Ċ } Ċ Ċ class ĠAppointment ServiceImpl Ġ@ Inject Ġconstructor ( ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġrepository : ĠAppointment Repository , ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġbus : ĠEvent Bus , ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġclock : ĠClock , ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġg cal : ĠG Cal Service , ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġclient Repository : ĠClient Repository Ċ ) Ġ: ĠAppointment Service Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġlog Ġ= ĠLOG ( java Class ) ĊĊĠĠĠ Ġoverride Ġfun ĠfindAll Future For ( client : ĠClient ): ĠList < Appointment > Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . debug (" findAll Future For ( client ={ })", Ġclient ) ĊĠĠĠĠĠĠĠ Ġreturn Ġrepository . findAll Start After ( clock . now (), Ġclient ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun ĠfindAll Between ( range : ĠPair < DateTime , ĠDateTime >): ĠList < Appointment > Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . debug (" findAll Between ( range ={ })", Ġrange ) ĊĠĠĠĠĠĠĠ Ġreturn Ġrepository . findAll Between ( range ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġinsert OrUpdate ( appointment : ĠAppointment ): ĠAppointment Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . debug (" insert OrUpdate ( appointment ={ })", Ġappointment ) ĊĊĠĠĠĠĠĠĠ Ġval Ġclient Ġ= Ġclient Repository . findById ( appointment . clientId ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( appointment . yet Pers isted ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġupdate Only ( appointment , Ġclient ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġappointment ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinsert Only ( appointment , Ġclient ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġprivate Ġfun Ġinsert Only ( appointment : ĠAppointment , Ġclient : ĠClient ): ĠAppointment Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . trace (" insert Only (.. )") ĊĠĠĠĠĠĠĠ Ġval Ġsaved Appointment Ġ= Ġif Ġ( appointment . g cal Id Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġyet Ġpersisted Ġin Ġg cal ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġsaved Appointment Ġ= Ġrepository . insert ( appointment ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġg cal . update Event ( G Cal Update Event . createNew ( saved Appointment . id !! , Ġappointment , Ġclient )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsaved Appointment ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġinsert Ġnew Ġg cal Ġevent ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġbase Event Ġ= ĠG Cal Event . createNew ( appointment , Ġclient ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġmaybe Created Event : ĠG Cal Event Meta ? Ġ= Ġg cal . create Event ( base Event ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġsaved Appointment Ġ= Ġrepository . insert ( appointment . copy ( g cal Id Ġ= Ġmaybe Created Event ?. id , Ġg cal Url Ġ= Ġmaybe Created Event ?. url )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( maybe Created Event Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġg cal . update Event ( maybe Created Event . copy For Update ( saved Appointment . id !! , Ġbase Event , Ġappointment . clientId )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsaved Appointment ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġbus . post ( Appointment Saved Event ( saved Appointment )) ĊĠĠĠĠĠĠĠ Ġreturn Ġsaved Appointment ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġupdate Only ( appointment : ĠAppointment , Ġclient : ĠClient ) Ġ{ ĊĠĠĠĠĠĠĠ Ġrepository . update ( appointment ) ĊĊĠĠĠĠĠĠĠ Ġif Ġ( appointment . g cal Id Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġg cal . update Event ( G Cal Update Event . createNew ( appointment . id !! , Ġappointment , Ġclient )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġbus . post ( Appointment Changed Event ( appointment )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġdelete ( appointment : ĠAppointment ) Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . debug (" delete ( appointment ={ })", Ġappointment ) ĊĠĠĠĠĠĠĠ Ġrepository . delete ( appointment ) ĊĠĠĠĠĠĠĠ Ġif Ġ( appointment . g cal Id Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġg cal . delete Event ( appointment . g cal Id ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġbus . post ( Appointment Deleted Event ( appointment )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġdelete All ( client : ĠClient ) Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . debug (" delete All ( client ={ })", Ġclient ) ĊĠĠĠĠĠĠĠ Ġrepository . delete All ( client ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġupcoming Appointment For ( client : ĠClient ) Ġ= Ġupcoming Appointment For ( client . id !! ) ĊĊĠĠĠ Ġoverride Ġfun Ġupcoming Appointment For ( clientId : ĠString ): ĠAppointment ? Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . debug (" up coming Appointment For ( clientId ={ })", ĠclientId ) ĊĠĠĠĠĠĠĠ Ġreturn Ġrepository . findAll Start After ( clock . now (), ĠclientId ). sorted (). first OrNull () ĊĠĠĠ Ġ} Ċ Ċ } Ċ Ċ fun ĠG Cal Event . Companion . createNew ( appointment : ĠAppointment , Ġclient : ĠClient ) Ġ= ĠG Cal Event ( ĊĠĠĠĠĠĠĠ Ġid Ġ= Ġnull , ĊĠĠĠĠĠĠĠ Ġg ads u Id Ġ= Ġnull , Ġ// Ġwill Ġbe Ġupdated Ġlater Ġon ĊĠĠĠĠĠĠĠ ĠclientId Ġ= Ġappointment . clientId , ĊĠĠĠĠĠĠĠ Ġsummary Ġ= Ġclient . preferred Name , ĊĠĠĠĠĠĠĠ Ġdescription Ġ= Ġappointment . note , ĊĠĠĠĠĠĠĠ Ġstart Ġ= Ġappointment . start , ĊĠĠĠĠĠĠĠ Ġend Ġ= Ġappointment . end , ĊĠĠĠĠĠĠĠ Ġurl Ġ= Ġnull Ċ ) Ċ Ċ fun ĠG Cal Update Event . Companion . createNew ( g ads u Id : ĠString , Ġappointment : ĠAppointment , Ġclient : ĠClient ) Ġ= ĊĠĠĠĠĠĠĠ ĠG Cal Update Event ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġid Ġ= Ġappointment . g cal Id !! , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġg ads u Id Ġ= Ġg ads u Id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠclientId Ġ= Ġappointment . clientId , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġsummary Ġ= Ġclient . preferred Name , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdescription Ġ= Ġappointment . note , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġstart Ġ= Ġappointment . start , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġend Ġ= Ġappointment . end ĊĠĠĠĠĠĠĠ Ġ) Ċ