@startuml
autonumber
hide footbox

actor "Admin" as U

participant "RegisterDeliveryRunUI" as UI
participant "RegisterDeliveryRunController" as CTRL
participant "ApplicationPOT" as _APP
participant "app\n:ApplicationPOT" as APP
participant "session\n:UserSession" as SESSION
participant "deliveryRunService\n:DeliveryRunService" as DRS
participant "pharmacyService\n:PharmacyService" as PS
participant "orderService\n:OrderService" as OS
participant "deliveryRunDB\n:DeliveryRunDB" as DB
participant "pharmacyDB\n:PharmacyDB" as PDB
participant "orderDB\n:OrderDB" as ODB
participant "deliveryRun\n:DeliveryRun" as DR
participant "status\n:DeliveryStatus" as DS
participant "courier\n:Courier" as C
participant "Order" as A

participant "DataHandler" as DH

activate U

U -> UI : Initiates the registration of a delivery run
activate UI







deactivate CTRL


UI --> U: Asks for a list of order ids

deactivate UI




U -> UI : Gives an order id list
activate UI
UI -> CTRL : registerDeliveryRun(lstOrderId)
activate CTRL




CTRL -> DRS** : create()
activate DRS
DRS -> DB** : create()
deactivate DRS

CTRL -> PS** : create()
activate PS
PS -> PDB** : create()
deactivate PS

CTRL -> OS** : create()
activate OS
OS -> ODB** : create()

deactivate OS
CTRL -> _APP: app = getInstance()
activate _APP
deactivate _APP

CTRL -> APP : getCurrentSession()
activate APP
deactivate APP

CTRL -> SESSION : getRole()
activate SESSION
deactivate SESSION

alt if user is authorized (if it's an admin)
CTRL -> OS : lstOrder = getOrderList(lstOrderId)
activate OS
OS -> ODB : getOrder(orderId)
activate ODB
deactivate ODB

ref over ODB
SD_CONNECTIONS
end ref
deactivate ODB
deactivate OS




CTRL -> PS : courier = getSuitableCourier()
activate PS
PS -> PDB : getSuitableCourier()
activate PDB
ref over PDB
SD_CONNECTIONS
end ref
PDB -> C** : create(id,name,email,pw,nif,iban,pharmacy)
deactivate DB
deactivate PS
deactivate PDB



CTRL -> DRS : deliveryRun = newDeliveryRun(courier,lstOrder)
activate DRS
DRS -> DR** : create(courier,lstOrder)
activate DR
DR -> DS** : create()
deactivate DRS



deactivate DR

CTRL -> DRS : addNewDeliveryRun(deliveryRun)
activate DRS
DRS -> DB : addNewDeliveryRun(deliveryRun)
activate DB
deactivate DRS
ref over DB
SD_CONNECTIONS
end ref
deactivate DB


deactivate DB
deactivate DRS

deactivate CTRL

UI --> U : Registers the delivery run and informs\nabout the success of the operation
deactivate UI

else if User is not authorized


UI --> U : Informs about the unsuccess of\nthe operation
activate UI
deactivate UI
end








deactivate U


@enduml