Skip to main content

Request and Response Events

Request Events and Response Events are key components responsible for managing data throughout the lifecycle of an operation. Request Events handle incoming data while Response Events manage the outgoing data. For each type of input defined for an operation, a corresponding request event must be created. Their main responsibility is to map the provided input DTO (Data Transfer Object) to the domain entity.

Request and Response Events should be placed inside java/com/dev/registration/company/flow/requests. Each request event corresponds to an input DTO, such as a CompanySubmitDto, and each response will typically map to an output DTO like CompanyViewDto. These classes handle what happens when one of our generated endpoints is called.

SubmitRequestEvent

Let's define a request event for the submit operation. This goes inside the flow/requests folder:

SubmitRequestEvent.java
package com.dev.registration.company.flow.requests;

import com.dev.registration.company.data.CompanyEntity;
import com.dev.registration.company.data.dm.CompanySubmitDto;
import com.dev.registration.company.document.CompanyDocument;
import com.dev.registration.company.document.Operations;
import com.dev.registration.company.document.StartOperations;
import com.dev.registration.company.document.States;
import com.dev.registration.company.document.document.events.CompanySubmitDtoRequestEvent;
import com.dev.registration.company.mappers.CompanyMapper;

import com.strategyobject.sokit.extensions.document.DocumentTransaction;
import com.strategyobject.sokit.extensions.document.api.annotations.DocumentAware;

@DocumentAware(CompanyDocument.class)
public class SubmitRequestEvent implements CompanySubmitDtoRequestEvent {

private final CompanyMapper mapper;

public SubmitRequestEvent(CompanyMapper mapper) {
this.mapper = mapper;
}

@Override
public void execute(
DocumentTransaction<States, StartOperations, Operations, String, CompanyEntity> transaction,
CompanySubmitDto input) {
mapper.map(transaction.ctx(), input, transaction.entity());
}
}

Since it's possible to have multiple documents, we use @DocumentAware to indicate which one this request is targeting.
This class implements CompanySubmitDtoRequestEvent, a generated interface that connects the specific operation and DTO with our event logic.

Again, remember that if you want to use an other DTO and map the request event to the input, for example for the "approve" operatin, you must create an ApproveRequestEvent.java file and implement it how we just did for the submit operation.

Same thing for the response event. Here below you can see how we implement the ViewResponseEvent:

package com.dev.registration.company.flow.responses;

import com.dev.registration.company.data.CompanyEntity;
import com.dev.registration.company.data.dm.CompanyViewDto;
import com.dev.registration.company.document.CompanyDocument;
import com.dev.registration.company.document.Operations;
import com.dev.registration.company.document.StartOperations;
import com.dev.registration.company.document.States;
import com.dev.registration.company.document.document.events.CompanyViewDtoResponseEvent;
import com.dev.registration.company.mappers.CompanyMapper;

import com.strategyobject.sokit.extensions.document.DocumentTransaction;
import com.strategyobject.sokit.extensions.document.api.annotations.DocumentAware;

@DocumentAware(CompanyDocument.class)
public class ViewResponseEvent implements CompanyViewDtoResponseEvent {

private final CompanyMapper mapper;

public ViewResponseEvent(CompanyMapper mapper) {
this.mapper = mapper;
}

@Override
public CompanyViewDto execute(
DocumentTransaction<States, StartOperations, Operations, String, CompanyEntity> transaction) {
CompanyViewDto companyViewDto = mapper.mapView(transaction.entity());
companyViewDto.setCreatorSubject(null);
return companyViewDto;
}
}

You can notice that the creatorSubject field is backend-only and not included in the JSON model shown previously. Here, we explicitly hide it before returning the DTO in the view response event.


In the next sections, we will explore how to build the mapper layer that is currently use by the above classes.