Search in sources :

Example 1 with PagingBean

use of io.apiman.manager.api.beans.search.PagingBean in project apiman by apiman.

the class UserResourceImpl method getActivity.

/**
 * @see IUserResource#getActivity(java.lang.String, int, int)
 */
@Override
public SearchResultsBean<AuditEntryBean> getActivity(String userId, int page, int pageSize) throws NotAuthorizedException {
    securityContext.checkIfUserIsCurrentUser(userId);
    if (page <= 1) {
        page = 1;
    }
    if (pageSize == 0) {
        pageSize = 20;
    }
    try {
        SearchResultsBean<AuditEntryBean> rval;
        PagingBean paging = new PagingBean();
        paging.setPage(page);
        paging.setPageSize(pageSize);
        rval = query.auditUser(userId, paging);
        return rval;
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PagingBean(io.apiman.manager.api.beans.search.PagingBean) AuditEntryBean(io.apiman.manager.api.beans.audit.AuditEntryBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 2 with PagingBean

use of io.apiman.manager.api.beans.search.PagingBean in project apiman by apiman.

the class EsStorage method find.

/**
 * Finds entities using a generic search criteria bean.
 * @param criteria
 * @param type
 * @param unmarshaller
 * @throws StorageException
 */
private <T> SearchResultsBean<T> find(SearchCriteriaBean criteria, String type, IUnmarshaller<T> unmarshaller) throws StorageException {
    try {
        SearchResultsBean<T> rval = new SearchResultsBean<>();
        // Set some default in the case that paging information was not included in the request.
        PagingBean paging = criteria.getPaging();
        if (paging == null) {
            paging = new PagingBean();
            paging.setPage(1);
            paging.setPageSize(20);
        }
        int page = paging.getPage();
        int pageSize = paging.getPageSize();
        int start = (page - 1) * pageSize;
        SearchSourceBuilder builder = new SearchSourceBuilder().size(pageSize).from(start).fetchSource(true);
        // Sort order
        OrderByBean orderBy = criteria.getOrderBy();
        if (orderBy != null) {
            String name = orderBy.getName();
            // Get the index definition so that we can see whether a '.keyword' is available. If so, use it.
            EsIndexProperties esIndex = getEsIndices().get(type);
            if (esIndex.hasProperty(name) && esIndex.getProperty(name).isKeywordMultiField()) {
                name = name + ".keyword";
            }
            if (orderBy.isAscending()) {
                builder.sort(name, SortOrder.ASC);
            } else {
                builder.sort(name, SortOrder.DESC);
            }
        }
        // Now process the filter criteria
        List<SearchCriteriaFilterBean> filters = criteria.getFilters();
        QueryBuilder q = null;
        if (filters != null && !filters.isEmpty()) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            List<QueryBuilder> andFilter = boolQuery.filter();
            int filterCount = 0;
            for (SearchCriteriaFilterBean filter : filters) {
                String propertyName = filter.getName();
                if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
                    andFilter.add(QueryBuilders.termQuery(propertyName, filter.getValue()));
                    filterCount++;
                } else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
                    q = QueryBuilders.wildcardQuery(propertyName, filter.getValue().toLowerCase().replace('%', '*'));
                } else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
                    // $NON-NLS-1$
                    andFilter.add(QueryBuilders.termQuery(propertyName, "true".equals(filter.getValue())));
                    filterCount++;
                }
            // TODO implement the other filter operators here!
            }
            if (filterCount > 0) {
                q = boolQuery;
            }
        }
        builder.query(q);
        String fullIndexName = getFullIndexName(type);
        SearchResponse response = getClient().search(new SearchRequest(fullIndexName).source(builder), RequestOptions.DEFAULT);
        SearchHits thehits = response.getHits();
        rval.setTotalSize((int) thehits.getTotalHits().value);
        for (SearchHit hit : thehits.getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            T bean = unmarshaller.unmarshal(sourceAsMap);
            rval.getBeans().add(bean);
        }
        return rval;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) INDEX_MANAGER_POSTFIX_CONTRACT(io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CONTRACT) INDEX_MANAGER_POSTFIX_CLIENT(io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CLIENT) PagingBean(io.apiman.manager.api.beans.search.PagingBean) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResultsBean(io.apiman.manager.api.beans.search.SearchResultsBean) SearchHits(org.elasticsearch.search.SearchHits) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean) SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 3 with PagingBean

use of io.apiman.manager.api.beans.search.PagingBean in project apiman by apiman.

the class OrganizationResourceImpl method getPlanVersionActivity.

/**
 * @see IOrganizationResource#getPlanVersionActivity(java.lang.String, java.lang.String, java.lang.String, int, int)
 */
@Override
public SearchResultsBean<AuditEntryBean> getPlanVersionActivity(String organizationId, String planId, String version, int page, int pageSize) throws PlanVersionNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.planView, organizationId);
    if (page <= 1) {
        page = 1;
    }
    if (pageSize == 0) {
        pageSize = 20;
    }
    try {
        SearchResultsBean<AuditEntryBean> rval;
        PagingBean paging = new PagingBean();
        paging.setPage(page);
        paging.setPageSize(pageSize);
        rval = query.auditEntity(organizationId, planId, version, PlanBean.class, paging);
        return rval;
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PagingBean(io.apiman.manager.api.beans.search.PagingBean) AuditEntryBean(io.apiman.manager.api.beans.audit.AuditEntryBean) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean) NewPlanBean(io.apiman.manager.api.beans.plans.NewPlanBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) UpdatePlanBean(io.apiman.manager.api.beans.plans.UpdatePlanBean) ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 4 with PagingBean

use of io.apiman.manager.api.beans.search.PagingBean in project apiman by apiman.

the class OrganizationResourceImpl method getPlanActivity.

/**
 * @see IOrganizationResource#getPlanActivity(java.lang.String, java.lang.String, int, int)
 */
@Override
public SearchResultsBean<AuditEntryBean> getPlanActivity(String organizationId, String planId, int page, int pageSize) throws PlanNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.planView, organizationId);
    if (page <= 1) {
        page = 1;
    }
    if (pageSize == 0) {
        pageSize = 20;
    }
    try {
        SearchResultsBean<AuditEntryBean> rval;
        PagingBean paging = new PagingBean();
        paging.setPage(page);
        paging.setPageSize(pageSize);
        rval = query.auditEntity(organizationId, planId, null, PlanBean.class, paging);
        return rval;
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PagingBean(io.apiman.manager.api.beans.search.PagingBean) AuditEntryBean(io.apiman.manager.api.beans.audit.AuditEntryBean) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean) NewPlanBean(io.apiman.manager.api.beans.plans.NewPlanBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) UpdatePlanBean(io.apiman.manager.api.beans.plans.UpdatePlanBean) ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 5 with PagingBean

use of io.apiman.manager.api.beans.search.PagingBean in project apiman by apiman.

the class AbstractJpaStorage method find.

/**
 * Get a list of entities based on the provided criteria and entity type.
 * @param criteria
 * @param type
 * @throws StorageException if a storage problem occurs while storing a bean
 */
protected <T> SearchResultsBean<T> find(SearchCriteriaBean criteria, List<OrderByBean> uniqueOrderIdentifiers, Consumer<CriteriaBuilder<T>> builderCallback, Class<T> type, String typeAlias, boolean paginate) throws StorageException {
    try {
        // Set some default in the case that paging information was not included in the request.
        PagingBean paging = criteria.getPaging();
        if (paging == null) {
            paging = PagingBean.create(1, 20);
        }
        int page = paging.getPage();
        int pageSize = paging.getPageSize();
        int start = (page - 1) * pageSize;
        CriteriaBuilder<T> cb = criteriaBuilderFactory.create(getActiveEntityManager(), type).from(type, typeAlias);
        // Apply filters from user-provided criteria.
        cb = applySearchCriteriaToQuery(typeAlias, criteria, cb, false);
        // Allow caller to modify the query, for example to add permissions constraints.
        builderCallback.accept(cb);
        if (paginate) {
            PaginatedCriteriaBuilder<T> paginatedCb = cb.page(start, pageSize);
            /*
                 * Add an orderBy of unique identifiers *last* in the query; this is required for pagination to work properly.
                 *
                 * The tuple formed by the fields in this orderBy clause MUST be unique, otherwise BlazePersistence will throw an exception.
                 *
                 * Without a unique tuple, the ordering may be unstable, which can cause pagination to behave unpredictably.
                 */
            for (OrderByBean order : uniqueOrderIdentifiers) {
                paginatedCb = paginatedCb.orderBy(order.getName(), order.isAscending());
            }
            PagedList<T> resultList = paginatedCb.getResultList();
            return new SearchResultsBean<T>().setTotalSize(Math.toIntExact(resultList.getTotalSize())).setBeans(resultList);
        } else {
            // (x,y) IN (select x,y ... subquery) which works in all DBs except H2. Beware...
            for (OrderByBean order : uniqueOrderIdentifiers) {
                cb = cb.orderBy(order.getName(), order.isAscending());
            }
            List<T> resultList = cb.getResultList();
            return new SearchResultsBean<T>().setTotalSize(Math.toIntExact(resultList.size())).setBeans(resultList);
        }
    } catch (Throwable t) {
        LOGGER.error(t.getMessage(), t);
        throw new StorageException(t);
    }
}
Also used : PagingBean(io.apiman.manager.api.beans.search.PagingBean) SearchResultsBean(io.apiman.manager.api.beans.search.SearchResultsBean) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Aggregations

PagingBean (io.apiman.manager.api.beans.search.PagingBean)12 StorageException (io.apiman.manager.api.core.exceptions.StorageException)11 AuditEntryBean (io.apiman.manager.api.beans.audit.AuditEntryBean)8 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)8 IOException (java.io.IOException)4 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)3 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)3 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)3 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)3 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)3 ApiVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException)3 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)3 ClientAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException)3 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)3 ClientVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException)3 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)3 ContractAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException)3 ContractNotFoundException (io.apiman.manager.api.rest.exceptions.ContractNotFoundException)3 EntityStillActiveException (io.apiman.manager.api.rest.exceptions.EntityStillActiveException)3 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)3