@startuml
'https://plantuml.com/sequence-diagram

autonumber

User -> ExportController: configure export using form
activate ExportController
ExportController -> ExportForm: build form
activate ExportForm

loop for every ExportElement (Filter, Aggregator)
    ExportForm -> ExportElement: `buildForm`
    activate ExportElement
    ExportElement -> ExportForm: add form to builders
    deactivate ExportElement
end

ExportForm -> ExportController
deactivate ExportForm

ExportController -> User: show form
deactivate ExportController

note left of User: Configure the export:\ncheck filters, aggregators, …

User -> ExportController: post configuration of the export
activate ExportController

ExportController -> ExportForm: `getData`
activate ExportForm
ExportForm -> ExportController: return data: list of entities, etc.
deactivate ExportForm

loop for every ExportElement (Filter, Aggregator)
    ExportController -> ExportElement: serializeData (data)
    activate ExportElement
    ExportElement -> ExportController: return serializedData (simple array with string, int, …)
    deactivate ExportElement
end

ExportController -> Database: `INSERT INTO RequestGeneration_table` (insert new entity)
ExportController -> MessageQueue: warn about a new request
activate MessageQueue
ExportController -> User: "ok, generation is in process"
deactivate ExportController

note left of User: The user see a waiting screen

MessageQueue -> MessengerConsumer: forward the message to the MessengerConsumer
deactivate MessageQueue
activate MessengerConsumer
MessengerConsumer -> Database: `SELECT * FROM RequestGeneration_table WHERE id = %s`
activate Database
Database -> MessengerConsumer: return RequestGeneration with serializedData
deactivate Database

loop for every ExportElement (Filter, Aggregator)
    MessengerConsumer -> ExportElement: deserializeData
    activate ExportElement
    ExportElement -> MessengerConsumer: return data (list of entities, etc.) from the serialized array
    deactivate ExportElement
    MessengerConsumer -> ExportElement: alter the sql query (`ExportElement::alterQuery`)
    activate ExportElement
    ExportElement -> MessengerConsumer: return the query with WHERE and GROUP BY clauses
    deactivate ExportElement
end

MessengerConsumer -> MessengerConsumer: prepare the export
MessengerConsumer -> MessengerConsumer: save the export as a stored object
MessengerConsumer -> Database: `UPDATE RequestGeneration_table SET ready = true`
deactivate MessengerConsumer

User -> ExportController: pull every 5s to know if the export is generated
activate ExportController
ExportController -> User: warn the export is generated
deactivate ExportController

User -> ExportController: download the export from object storage





@enduml
