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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations