package Ġtw ink le . agri ledger . flows Ċ Ċ import Ġco . parallel universe . fib ers . Suspend able Ċ import Ġnet . cord a . core . C ord a RuntimeException Ċ import Ġnet . cord a . core . contracts . Command Ċ import Ġnet . cord a . core . contracts . State And Contract Ċ import Ġnet . cord a . core . contracts . State And Ref Ċ import Ġnet . cord a . core . contracts . require That Ċ import Ġnet . cord a . core . flows .* Ċ import Ġnet . cord a . core . transactions . Signed Transaction Ċ import Ġnet . cord a . core . transactions . Transaction Builder Ċ import Ġnet . cord a . core . utilities . Progress Tracker Ċ import Ġtw ink le . agri ledger . contracts . Asset Contract Ċ import Ġtw ink le . agri ledger . states . Asset Container State Ċ import Ġtw ink le . agri ledger . states . Location State Ċ import Ġtw ink le . agri ledger . states . O bl igation State Ċ import Ġutils . get Asset Container By Physical Container Id Ċ import Ġutils . getLocation By Physical Container Id Ċ import Ġutils . get O bl igation By Physical Container Id Ċ import Ġjava . lang . RuntimeException Ċ import Ġjava . util .* ĊĊ Ċ @ Initi ating Flow Ċ @ Start able By RPC Ċ class ĠMerge Asset Containers Flow ( val Ġphysical Container IDs : ĠList < String >) Ġ: ĠFlow Logic < Signed Transaction >() Ġ{ ĊĠĠĠ Ġoverride Ġval Ġprogress Tracker Ġ= ĠProgress Tracker () ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġoverride Ġfun Ġcall (): ĠSigned Transaction Ġ{ ĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 4 . ĠGet Ġa Ġreference Ġto Ġa Ġtransaction Ġbuilder . ĊĠĠĠĠĠĠĠ Ġval Ġnot ary Ġ= Ġservice Hub . network Map Cache . not ary Id entities . first () ĊĠĠĠĠĠĠĠ Ġval Ġbuilder Ġ= ĠTransaction Builder ( not ary Ġ= Ġnot ary ) ĊĊĠĠĠĠĠĠĠ Ġvar Ġasset State And Ref : ĠState And Ref < Asset Container State >? Ġ= Ġnull ĊĠĠĠĠĠĠĠ Ġvar Ġlocation State And Ref : ĠState And Ref < Location State >? Ġ= Ġnull ĊĠĠĠĠĠĠĠ Ġvar Ġoblig ation State And Ref : ĠState And Ref < O bl igation State >? Ġ= Ġnull ĊĊĠĠĠĠĠĠĠ Ġval Ġlocations : ĠMutable List < State And Ref < Location State >> Ġ= Ġmutable ListOf () ĊĠĠĠĠĠĠĠ Ġphysical Container IDs . forEach Ġ{ physical Container ID Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 1 . ĠRetrieve ĠStates Ġspecified Ġby Ġlinear Id Ġfrom Ġthe Ġvault . ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġphysical Container UUID Ġ= ĠUUID . fromString ( physical Container ID ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġasset State And Ref Ġ= Ġget Asset Container By Physical Container Id ( physical Container UUID , Ġservice Hub ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠC ord a RuntimeException (" state Ġwith Ġsuch Ġphysical Container ID Ġdoes Ġnot Ġexists ") ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġlocation State And Ref Ġ= Ġget Location By Physical Container Id ( physical Container UUID , Ġservice Hub ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠC ord a RuntimeException (" state Ġwith Ġsuch Ġphysical Container ID Ġdoes Ġnot Ġexists ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlocations . add ( location State And Ref !! ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġoblig ation State And Ref Ġ= Ġget O bl igation By Physical Container Id ( physical Container UUID , Ġservice Hub ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠC ord a RuntimeException (" state Ġwith Ġsuch Ġphysical Container ID Ġdoes Ġnot Ġexists ") ĊĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġbuilder . with Items ( asset State And Ref !! , Ġlocation State And Ref !! , Ġoblig ation State And Ref !! ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġis Same Location ( locations ) ĊĊĠĠĠĠĠĠĠ Ġval Ġinput Asset Ġ= Ġasset State And Ref !!. state . data ĊĠĠĠĠĠĠĠ Ġval Ġinput Location Ġ= Ġlocation State And Ref !!. state . data ĊĠĠĠĠĠĠĠ Ġval Ġinput O bl igation Ġ= Ġoblig ation State And Ref !!. state . data ĊĊĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 3 . ĠCreate Ġthe Ġtransfer Ġcommand . ĊĠĠĠĠĠĠĠ Ġval Ġsign ers Ġ= Ġinput Asset . participants . map Ġ{ Ġit . own ing Key Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġtransfer Command Ġ= ĠCommand ( Asset Contract . Commands . Transfer (), Ġsign ers ) ĊĊĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 2 . ĠCreate Ġthe Ġnew ĠParent Ġand ĠChild Ġstates . ĊĠĠĠĠĠĠĠ Ġval Ġphysical Container ID Ġ= ĠUUID . randomUUID () ĊĠĠĠĠĠĠĠ Ġbuilder . with Items ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠState And Contract ( input Asset . with New Physical Container ID ( physical Container ID ), ĠAsset Contract . ID ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠState And Contract ( input Location . with New Physical Container ID ( physical Container ID ), ĠAsset Contract . ID ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠState And Contract ( input O bl igation . with New Physical Container ID ( physical Container ID ), ĠAsset Contract . ID ) ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 5 . ĠCreate Ġthe Ġtransaction Ġwhich Ġcompri ses Ġinputs , Ġoutputs Ġand Ġone Ġcommand . ĊĠĠĠĠĠĠĠ Ġbuilder . with Items ( transfer Command ) ĊĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 6 . ĠVerify Ġand Ġsign Ġthe Ġtransaction . ĊĠĠĠĠĠĠĠ Ġbuilder . verify ( service Hub ) ĊĠĠĠĠĠĠĠ Ġval Ġp tx Ġ= Ġservice Hub . sign Initial Transaction ( builder ) ĊĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 7 . ĠCollect Ġsignature Ġand Ġadd Ġit Ġto Ġthe Ġtransaction . ĊĠĠĠĠĠĠĠ Ġ// ĠThis Ġalso Ġverifies Ġthe Ġtransaction Ġand Ġchecks Ġthe Ġsignatures . ĊĠĠĠĠĠĠĠ Ġval Ġcounter party Ġ= Ġif Ġ( service Hub . my Info . legal Id entities . first () Ġ== Ġinput O bl igation . ob lig ation . bene ficiary ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinput O bl igation . ob lig ation . owner ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinput O bl igation . ob lig ation . bene ficiary ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġsessions Ġ= ĠlistOf ( init iate Flow ( counter party )) ĊĠĠĠĠĠĠĠ Ġval Ġst x Ġ= Ġsub Flow ( Collect Signatures Flow ( pt x , Ġsessions )) ĊĊĠĠĠĠĠĠĠ Ġ// ĠStage Ġ 8 . ĠNot ar ise Ġand Ġrecord Ġthe Ġtransaction Ġin Ġour Ġvault s . ĊĠĠĠĠĠĠĠ Ġval Ġnor ar ized Tx Ġ= Ġsub Flow ( Final ity Flow ( st x , Ġsessions )) ĊĠĠĠĠĠĠĠ Ġreturn Ġnor ar ized Tx ĊĠĠĠ Ġ} Ċ } Ċ Ċ @ Initi ated By ( Merge Asset Containers Flow :: class ) Ċ class ĠMerge Asset Containers Flow Responder ( val Ġflow Session : ĠFlow Session ) Ġ: ĠFlow Logic < Signed Transaction >() Ġ{ ĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġoverride Ġfun Ġcall (): ĠSigned Transaction Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġsigned Transaction Flow Ġ= Ġobject Ġ: ĠSign Transaction Flow ( flow Session ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcheck Transaction ( st x : ĠSigned Transaction ) Ġ= Ġrequire That Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġsome Ġrequirements Ġshould Ġbe Ġhere ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġtx We Just Signed Id Ġ= Ġsub Flow ( signed Transaction Flow ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġsub Flow ( Receive Final ity Flow ( other Side Session Ġ= Ġflow Session , Ġexpected Tx Id Ġ= Ġtx We Just Signed Id . id )) ĊĠĠĠ Ġ} Ċ } Ċ Ċ private Ġfun Ġis Same Location ( location : ĠMutable List < State And Ref < Location State >> ): ĠBoolean { ĊĠĠĠ Ġvar Ġlongitude Ġ= Ġlocation . get ( 0 ). state . data . gps . longitude ĊĠĠĠ Ġvar Ġlatitude Ġ= Ġlocation . get ( 0 ). state . data . gps . latitude ĊĠĠĠ Ġlocation . remove At ( 0 ) ĊĠĠĠ Ġlocation . forEach Ġ{ location State Ġ-> ĊĠĠĠĠĠĠĠ Ġif Ġ( location State . state . data . gps . longitude Ġ!= Ġlongitude Ġ|| ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġlocation State . state . data . gps . latitude Ġ!= Ġlatitude ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠRuntimeException (" Assets Ġnot Ġin Ġthe Ġsame Ġplace ") ĊĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġtrue Ċ } ĊĊ