Search Event
In SOKit, search operations are customizable thanks to the Search Event mechanism. This allows developers to control how search requests are handled and how results are prepared and returned to the client.
A search event is a class that implements a search-specific interface for the generated DTO, such as CompanySearchableDtoSearchEvent. This interface extends a SearchEvent interface which defines three key methods: pre, map, and post.
The pre method is executed before the actual search query is run. It can be used to:
- Apply dynamic filters (e.g., restrict search by user role)
- Modify pagination or sorting rules
- Add security checks
The defualt behavior is to returns the existing filter unchanged.
The map method takes the result entity and transforms it into the final SearchableDto that the client will receive.
Some use cases can be:
- Hide or transform data
- Enrich DTOs with derived values
- Apply formatting rules
The post method is executed after the entity has been mapped into the DTO. It can be helpful to:
- Modify the final
searchableresult before it is sent - Add additional fields, metadata, or dynamic values
By default it does nothig.
In our case we have implment the following DefaultSearcheEvent:
package com.dev.registration.company.flow.search;
import static com.dev.registration.company.document.security.SecurityAll.OFFICER;
import com.dev.registration.company.data.CompanyEntity;
import com.dev.registration.company.data.dm.CompanySearchableDto;
import com.dev.registration.company.document.CompanyDocument;
import com.dev.registration.company.document.document.events.CompanySearchableDtoSearchEvent;
import com.dev.registration.company.mappers.CompanyMapper;
import java.util.Optional;
import com.strategyobject.sokit.extensions.core.context.SOContext;
import com.strategyobject.sokit.extensions.core.search.Pageable;
import com.strategyobject.sokit.extensions.document.api.annotations.DocumentAware;
import com.strategyobject.sokit.extensions.document.api.dm.DocumentMetadata;
import com.strategyobject.sokit.extensions.document.api.dm.DocumentSearchable;
import com.strategyobject.sokit.extensions.qdsl.QueryAst;
@DocumentAware(CompanyDocument.class)
public class DefaultSearchEvent implements CompanySearchableDtoSearchEvent {
private final CompanyMapper mapper;
public DefaultSearchEvent(CompanyMapper mapper) {
this.mapper = mapper;
}
@Override
public Optional<QueryAst> pre(SOContext ctx, Pageable pageable, Optional<QueryAst> filter) {
return filter;
}
@Override
public CompanySearchableDto map(SOContext ctx, DocumentMetadata metadata, CompanyEntity entity) {
return mapper.mapSearchable(entity);
}
@Override
public void post(SOContext ctx, DocumentSearchable<CompanySearchableDto> searchable) {}
}