package Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . service Ċ Ċ import Ġdev . fork handles . result 4 k . Failure Ċ import Ġdev . fork handles . result 4 k . Result Ċ import Ġdev . fork handles . result 4 k . Success Ċ import Ġorg . slf 4 j . LoggerFactory Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . stereotype . Service Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . config . Manage Re calls Exception Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . config . Wrong Document Type Exception Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . controller . Status Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . db . Document Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . db . Document Category Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . db . Document Repository Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . db . Re call Repository Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . domain . Document Id Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . domain . Re call Id Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . domain . UserId Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . domain . random Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . service . V irus Scan Result . No V irus Found Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . service . V irus Scan Result . V irus Found Ċ import Ġuk . gov . just ice . digital . h mp ps . manager ec alls api . storage . S 3 Service Ċ import Ġjava . time . Clock Ċ import Ġjava . time . Offset DateTime Ċ import Ġjavax . transaction . Transactional Ċ Ċ @ Service Ċ class ĠDocument Service ( ĊĠ Ġ@ Autowired Ġprivate Ġval Ġs 3 Service : ĠS 3 Service , ĊĠ Ġ@ Autowired Ġprivate Ġval Ġrecall Repository : ĠRe call Repository , ĊĠ Ġ@ Autowired Ġprivate Ġval Ġdocument Repository : ĠDocument Repository , ĊĠ Ġ@ Autowired Ġprivate Ġval Ġvirus Scanner : ĠV irus Scanner , ĊĠ Ġ@ Autowired Ġprivate Ġval Ġclock : ĠClock Ċ ) Ġ{ ĊĠ Ġprivate Ġval Ġlog Ġ= ĠLoggerFactory . getLogger ( this :: class . java ) ĊĊĠ Ġfun Ġscan And Store Document ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġcurrent UserId : ĠUserId , ĊĠĠĠ Ġdocument Bytes : ĠByteArray , ĊĠĠĠ Ġdocument Category : ĠDocument Category , ĊĠĠĠ ĠfileName : ĠString , ĊĠĠĠ Ġdetails : ĠString ? Ġ= Ġnull ĊĠ Ġ): ĠResult < Document Id , ĠV irus Scan Result > Ġ= ĊĠĠĠ Ġfor Existing Re call ( recall Id ) Ġ{ ĊĠĠĠĠĠ Ġwhen Ġ( val Ġvirus Scan Result Ġ= Ġvirus Scanner . scan ( document Bytes )) Ġ{ ĊĠĠĠĠĠĠĠ ĠNo V irus Found Ġ-> ĠSuccess ( store Document ( recall Id , Ġcurrent UserId , Ġdocument Bytes , Ġdocument Category , ĠfileName , Ġdetails )) ĊĠĠĠĠĠĠĠ Ġis ĠV irus Found Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġlog . info ( V irus Found Event ( recall Id , Ġdocument Category , Ġvirus Scan Result . found V ir uses ). toString ()) ĊĠĠĠĠĠĠĠĠĠ ĠFailure ( vir us Scan Result ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠ Ġfun Ġstore Document ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġcurrent UserId : ĠUserId , ĊĠĠĠ Ġdocument Bytes : ĠByteArray , ĊĠĠĠ Ġdocument Category : ĠDocument Category , ĊĠĠĠ ĠfileName : ĠString , ĊĠĠĠ Ġdetails : ĠString ? Ġ= Ġnull ĊĠ Ġ): ĠDocument Id Ġ= ĊĠĠĠ Ġfor Existing Re call ( recall Id ) Ġ{ ĊĠĠĠĠĠ Ġupload ToS 3 And Save Document ( recall Id , Ġcurrent UserId , Ġdocument Category , Ġdocument Bytes , ĠfileName , Ġdetails ) ĊĠĠĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġupload ToS 3 And Save Document ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġcurrent UserId : ĠUserId , ĊĠĠĠ Ġcategory : ĠDocument Category , ĊĠĠĠ Ġdocument Bytes : ĠByteArray , ĊĠĠĠ ĠfileName : ĠString , ĊĠĠĠ Ġdetails : ĠString ? Ġ= Ġnull ĊĠ Ġ): ĠDocument Id Ġ{ ĊĠĠĠ Ġval Ġversion Ġ= Ġif Ġ( category . versioned ) Ġ{ ĊĠĠĠĠĠ Ġ( document Repository . find Latest Version ed Document By Re call Id And Category ( recall Id , Ġcategory )?. version Ġ?: Ġ 0 ) Ġ+ Ġ 1 ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ} ĊĠĠĠ Ġval Ġdocument Id Ġ= Ġ:: Document Id . random () ĊĊĠĠĠ Ġdocument Repository . save ( ĊĠĠĠĠĠ ĠDocument ( ĊĠĠĠĠĠĠĠ Ġdocument Id , ĊĠĠĠĠĠĠĠ Ġrecall Id , ĊĠĠĠĠĠĠĠ Ġcategory , ĊĠĠĠĠĠĠĠ ĠfileName , ĊĠĠĠĠĠĠĠ Ġversion , ĊĠĠĠĠĠĠĠ Ġdetails , ĊĠĠĠĠĠĠĠ ĠOffset DateTime . now ( clock ), ĊĠĠĠĠĠĠĠ Ġcurrent UserId ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġs 3 Service . upload File ( document Id , Ġdocument Bytes ) ĊĠĠĠ Ġ} Ġcatch Ġ( ex : ĠException ) Ġ{ ĊĠĠĠĠĠ Ġdocument Repository . delete By Document Id ( document Id ) ĊĠĠĠĠĠ Ġthrow Ġex ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġreturn Ġdocument Id ĊĠ Ġ} ĊĊĠ Ġfun Ġget Document ( recall Id : ĠRe call Id , Ġdocument Id : ĠDocument Id ): ĠPair < Document , ĠByteArray > Ġ= ĊĠĠĠ Ġfor Existing Re call ( recall Id ) Ġ{ ĊĠĠĠĠĠ ĠPair ( ĊĠĠĠĠĠĠĠ Ġget Re call Document ById ( recall Id , Ġdocument Id ), ĊĠĠĠĠĠĠĠ Ġs 3 Service . download File ( document Id ) ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠ Ġfun Ġget Re call Document ById ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġdocument Id : ĠDocument Id ĊĠ Ġ): ĠDocument Ġ= Ġ( ĊĠĠĠ Ġdocument Repository . getBy Re call Id And Document Id ( recall Id , Ġdocument Id ) ĊĠĠĠ Ġ) ĊĊĠ Ġfun Ġget Latest Version ed Document Content With Category ( recall Id : ĠRe call Id , Ġdocument Category : ĠDocument Category ): ĠByteArray Ġ= ĊĠĠĠ Ġget Latest Version ed Document Content With Category IfExists ( recall Id , Ġdocument Category ) ĊĠĠĠĠĠ Ġ?: Ġthrow ĠRe call Document With Category NotFoundException ( recall Id , Ġdocument Category ) ĊĊĠ Ġfun Ġget Latest Version ed Document Content With Category IfExists ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġdocument Category : ĠDocument Category ĊĠ Ġ): ĠByteArray ? Ġ= ĊĠĠĠ Ġfor Existing Re call ( recall Id ) Ġ{ ĊĠĠĠĠĠ Ġdocument Repository . find Latest Version ed Document By Re call Id And Category ( recall Id , Ġdocument Category )?. let Ġ{ ĊĠĠĠĠĠĠĠ Ġs 3 Service . download File ( it . id ()) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠ Ġprivate Ġinline Ġfun Ġ< re ified ĠT Ġ: ĠAny ?> Ġfor Existing Re call ( recall Id : ĠRe call Id , Ġfn : Ġ() Ġ-> ĠT ): ĠT Ġ{ ĊĠĠĠ Ġrecall Repository . getBy Re call Id ( recall Id ) ĊĠĠĠ Ġreturn Ġfn () ĊĠ Ġ} ĊĊĠ Ġ@ Transactional ĊĠ Ġfun Ġupdate Document Category ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġdocument Id : ĠDocument Id , ĊĠĠĠ Ġnew Category : ĠDocument Category ĊĠ Ġ): ĠDocument Ġ{ ĊĠĠĠ Ġreturn Ġfor Existing Re call ( recall Id ) Ġ{ ĊĠĠĠĠĠ Ġval Ġexisting Document Ġ= Ġget Re call Document ById ( recall Id , Ġdocument Id ) ĊĠĠĠĠĠ Ġval Ġcurrent Category Ġ= Ġexisting Document . category ĊĠĠĠĠĠ Ġif Ġ( current Category Ġ!= ĠDocument Category . UN CATEG OR I SED ) Ġ{ ĊĠĠĠĠĠĠĠ Ġthrow ĠWrong Document Type Exception ( current Category ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġdocument Repository . save ( ĊĠĠĠĠĠĠĠ Ġexisting Document ĊĠĠĠĠĠĠĠĠĠ Ġ. copy ( category Ġ= Ġnew Category , Ġversion Ġ= Ġif Ġ( new Category . versioned ) Ġ 1 Ġelse Ġnull ) ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Transactional ĊĠ Ġfun Ġdelete Document ( ĊĠĠĠ Ġrecall Id : ĠRe call Id , ĊĠĠĠ Ġdocument Id : ĠDocument Id ĊĠ Ġ) Ġ{ ĊĠĠĠ Ġval Ġrecall Ġ= Ġrecall Repository . getBy Re call Id ( recall Id ) ĊĠĠĠ Ġval Ġdocument Ġ= Ġdocument Repository . getBy Re call Id And Document Id ( recall Id , Ġdocument Id ) ĊĊĠĠĠ Ġif Ġ( recall . status () Ġ== ĠStatus . BE ING _ BOOK ED _ ON Ġ&& Ġdocument . category . uploaded ) Ġ{ ĊĠĠĠĠĠ Ġdocument Repository . delete By Document Id ( document Id ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġthrow ĠDocument Delete Exception (" Unable Ġto Ġdelete Ġdocument : ĠWrong Ġstatus Ġ[ ${ recall . status ()} ] Ġand / or Ġdocument Ġcategory Ġ[ ${ document . category }] ") ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġfun ĠgetAll Documents By Category ( recall Id : ĠRe call Id , Ġcategory : ĠDocument Category ): ĠList < Document > Ġ{ ĊĠĠĠ Ġreturn Ġdocument Repository . getAll By Re call Id And Category ( recall Id , Ġcategory ) ĊĠ Ġ} Ċ } Ċ Ċ data Ġclass ĠRe call NotFoundException ( val Ġrecall Id : ĠRe call Id ) Ġ: ĠNotFound Exception () Ċ data Ġclass ĠDocument NotFoundException ( val Ġrecall Id : ĠRe call Id , Ġval Ġdocument Id : ĠDocument Id ) Ġ: ĠNotFound Exception () Ċ data Ġclass ĠRe call Document With Category NotFoundException ( ĊĠ Ġval Ġrecall Id : ĠRe call Id , ĊĠ Ġval Ġdocument Category : ĠDocument Category Ċ ) Ġ: ĠNotFound Exception () Ċ Ċ open Ġclass ĠNotFound Exception Ġ: ĠManage Re calls Exception () Ċ class ĠV irus Found Exception Ġ: ĠManage Re calls Exception () Ċ class ĠDocument Delete Exception ( override Ġval Ġmessage : ĠString ?) Ġ: ĠManage Re calls Exception ( message ) Ċ Ċ data Ġclass ĠV irus Found Event ( ĊĠ Ġval Ġrecall Id : ĠRe call Id , ĊĠ Ġval Ġdocument Category : ĠDocument Category , ĊĠ Ġval Ġfound V ir uses : ĠMap < String , ĠCollection < String >> Ċ ) Ċ