Skip to main content

Search a Company

Enabling Search on the Document

In the CompanyDocument interface, we specified that the document is searchable using the @Search annotation. To make this work properly, we also need to declare a corresponding Searchable DTO on the main entity. This DTO will represent the output of the search.

We do this by adding another @Dto annotation on CompanyEntity, like so:

CompanyEntity.java
@Entity
@Table(name = "TBL_COMPANY")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Dto(variant = "View", altVariants = "Entry")
@Dto(variant = "Searchable", altVariants = "Entry")
@Dto(variant = "Submit", altVariants = "Entry")
@Dto(variant = "Approve", altVariants = "Entry", includeAll = false)
@Dto(variant = "Reject", altVariants = "Entry", includeAll = false)
public class CompanyEntity extends DocumentEntity implements Serializable {
// ...
}

In addition, in the filter package, we define the available filters using a custom class like DefaultFilter. This class extends SearchFilter and uses the fields() method to declare which fields can be used in search queries, for example, filtering by establishedDate or the current document state in our case.

DefaultFilter.java
package com.dev.registration.company.document.filters;

import static com.strategyobject.sokit.extensions.qdsl.QueryAst.QueryExpression.QueryField.ofDate;
import static com.strategyobject.sokit.extensions.qdsl.QueryAst.QueryExpression.QueryField.ofString;

import java.util.HashSet;
import java.util.Set;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import com.strategyobject.sokit.extensions.core.context.SOContext;
import com.strategyobject.sokit.extensions.qdsl.QueryAst;
import com.strategyobject.sokit.extensions.qdsl.dm.SearchFilter;

@Schema(
name = "DefaultFilter",
title = "Default Filter",
description = DefaultFilter.DESCRIPTION,
implementation = String.class)
public class DefaultFilter extends SearchFilter {

public static final String DESCRIPTION =
"""
Default Search Filter:

__Available filters__

- *metadata.current.state* The current state of the registration document of the company.
- *data.establishedDate* The established date of the company.
""";

public DefaultFilter(String filter) {
super(filter);
}

@Override
public Set<QueryAst.QueryExpression.QueryField<?>> fields(String id, SOContext ctx) {
final var set = new HashSet<QueryAst.QueryExpression.QueryField<?>>();
set.add(ofString("metadata.current.state"));
set.add(ofDate("data.establishedDate"));
return set;
}
}

What's Next

In the next sections, we’ll see the security part and how everything we’ve built so far results in auto-generated code, including ready-to-use endpoints that we can call directly in our application.