Search in sources :

Example 1 with BasicQuery

use of org.bf2.srs.fleetmanager.util.BasicQuery in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class RegistryServiceImpl method getRegistries.

@Override
public RegistryListDto getRegistries(Integer page, Integer size, String orderBy, String search) {
    // Defaults
    var sort = Sort.by("id", Sort.Direction.Ascending);
    page = (page != null) ? page : 1;
    size = (size != null) ? size : 10;
    if (orderBy != null) {
        var order = orderBy.split(" ");
        if (order.length != 2) {
            throw new ValidationException("invalid orderBy");
        }
        if ("asc".equals(order[1])) {
            sort = Sort.by(order[0], Sort.Direction.Ascending);
        } else {
            sort = Sort.by(order[0], Sort.Direction.Descending);
        }
    }
    List<Pair<String, Object>> conditions = new ArrayList<>();
    if (search != null && !search.isEmpty()) {
        var basicQuery = new BasicQuery(search, Arrays.asList("name", "status"));
        conditions.add(Pair.of(basicQuery.getColumn(), basicQuery.getArgument()));
    }
    // list only registries from your organization or the ones the user owns
    if (isResolvable(securityIdentity)) {
        final AccountInfo accountInfo = authService.extractAccountInfo();
        String orgId = accountInfo.getOrganizationId();
        if (orgId != null) {
            conditions.add(Pair.of("orgId", orgId));
        } else {
            conditions.add(Pair.of("ownerId", accountInfo.getAccountId()));
        }
    } else {
        conditions.add(Pair.of("ownerId", OWNER_ID_PLACEHOLDER));
    }
    var query = new SearchQuery(conditions);
    PanacheQuery<RegistryData> itemsQuery = storage.executeRegistrySearchQuery(query, sort);
    var items = itemsQuery.page(Page.of(page - 1, size)).stream().map(convertRegistry::convert).collect(Collectors.toList());
    return RegistryListDto.builder().items(items).page(page).size(size).total(itemsQuery.count()).build();
}
Also used : SearchQuery(org.bf2.srs.fleetmanager.util.SearchQuery) ValidationException(javax.validation.ValidationException) BasicQuery(org.bf2.srs.fleetmanager.util.BasicQuery) ArrayList(java.util.ArrayList) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 ValidationException (javax.validation.ValidationException)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AccountInfo (org.bf2.srs.fleetmanager.spi.common.model.AccountInfo)1 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)1 BasicQuery (org.bf2.srs.fleetmanager.util.BasicQuery)1 SearchQuery (org.bf2.srs.fleetmanager.util.SearchQuery)1