use of io.apiman.manager.api.beans.search.SearchResultsBean in project apiman by apiman.
the class JpaStorage method findPlans.
/**
* @see io.apiman.manager.api.core.IStorageQuery#findPlans(java.lang.String, io.apiman.manager.api.beans.search.SearchCriteriaBean)
*/
@Override
public SearchResultsBean<PlanSummaryBean> findPlans(String organizationId, SearchCriteriaBean criteria) throws StorageException {
criteria.addFilter("organization.id", organizationId, SearchCriteriaFilterOperator.eq);
SearchResultsBean<PlanBean> result = find(criteria, PlanBean.class);
SearchResultsBean<PlanSummaryBean> rval = new SearchResultsBean<>();
rval.setTotalSize(result.getTotalSize());
List<PlanBean> plans = result.getBeans();
rval.setBeans(new ArrayList<>(plans.size()));
for (PlanBean plan : plans) {
PlanSummaryBean summary = new PlanSummaryBean();
OrganizationBean organization = plan.getOrganization();
summary.setId(plan.getId());
summary.setName(plan.getName());
summary.setDescription(plan.getDescription());
summary.setOrganizationId(plan.getOrganization().getId());
summary.setOrganizationName(organization.getName());
rval.getBeans().add(summary);
}
return rval;
}
use of io.apiman.manager.api.beans.search.SearchResultsBean 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.SearchResultsBean in project apiman by apiman.
the class BlobResourceInterceptorProviderTest method simple_nested_structures_with_collection_traversal.
@Test
public void simple_nested_structures_with_collection_traversal() throws IOException {
ApiBean ab = new ApiBean();
ab.setImage("blobaroni/foo-bar-baz_jpeg");
ApiVersionBean avb = new ApiVersionBean();
avb.setApi(ab);
SearchResultsBean<ApiVersionBean> searchResults = new SearchResultsBean<ApiVersionBean>().setTotalSize(1).setBeans(List.of(avb));
BlobResourceInterceptorProvider interceptorProvider = new BlobResourceInterceptorProvider();
interceptorProvider.aroundWriteTo(new DummyWriterContext(searchResults));
assertThat(avb.getApi().getImage()).isEqualTo("blobs/blobaroni/foo-bar-baz_jpeg");
}
use of io.apiman.manager.api.beans.search.SearchResultsBean in project apiman by apiman.
the class JpaStorage method findOrganizations.
/**
* {@inheritDoc}
*/
@Override
public SearchResultsBean<OrganizationSummaryBean> findOrganizations(SearchCriteriaBean criteria, PermissionConstraint permissionConstraint) throws StorageException {
Consumer<CriteriaBuilder<OrganizationBean>> constraintFunc = builder -> {
};
if (permissionConstraint.isConstrained()) {
// With constraint, first allow the user's explicitly permitted orgs, plus orgs with discoverable APIs.
constraintFunc = (builder) -> builder.whereOr().where(OrganizationBean_.ID).in(permissionConstraint.getPermittedOrgs()).where("org.id").in().from(DiscoverabilityEntity.class, "d").select("d.orgId").where("d.discoverability").isNotNull().where("d.discoverability").in(permissionConstraint.getAllowedDiscoverabilities()).end().endOr();
}
SearchResultsBean<OrganizationBean> orgs = find(criteria, List.of(new OrderByBean(true, OrganizationBean_.ID)), constraintFunc, OrganizationBean.class, "org", true);
SearchResultsBean<OrganizationSummaryBean> rval = new SearchResultsBean<>();
rval.setTotalSize(orgs.getTotalSize());
List<OrganizationBean> beans = orgs.getBeans();
for (OrganizationBean bean : beans) {
OrganizationSummaryBean osb = new OrganizationSummaryBean();
osb.setId(bean.getId());
osb.setName(bean.getName());
osb.setDescription(bean.getDescription());
rval.getBeans().add(osb);
}
return rval;
}
use of io.apiman.manager.api.beans.search.SearchResultsBean in project apiman by apiman.
the class JpaStorage method findPlans.
/**
* {@inheritDoc}
*/
@Override
public SearchResultsBean<PlanSummaryBean> findPlans(String organizationId, SearchCriteriaBean criteria, PermissionConstraint permissionConstraint) throws StorageException {
Consumer<CriteriaBuilder<PlanBean>> constraintFunc = builder -> {
};
if (permissionConstraint.isConstrained()) {
constraintFunc = builder -> builder.whereOr().where("plan.organization.id").in(permissionConstraint.getPermittedOrgs()).where("plan.id").in().from(DiscoverabilityEntity.class, "d").select("d.planId").where("d.orgId").eq(organizationId).where("d.planId").isNotNull().where("d.discoverability").in(permissionConstraint.getAllowedDiscoverabilities()).end().endOr();
}
SearchResultsBean<PlanBean> result = find(criteria, List.of(new OrderByBean(true, PlanBean_.ID), new OrderByBean(true, "organization.id")), constraintFunc, PlanBean.class, "plan", true);
// TODO(msavy): replace with projection or mapping
SearchResultsBean<PlanSummaryBean> rval = new SearchResultsBean<>();
rval.setTotalSize(result.getTotalSize());
List<PlanBean> plans = result.getBeans();
rval.setBeans(new ArrayList<>(plans.size()));
for (PlanBean plan : plans) {
PlanSummaryBean summary = new PlanSummaryBean();
OrganizationBean organization = plan.getOrganization();
summary.setId(plan.getId());
summary.setName(plan.getName());
summary.setDescription(plan.getDescription());
summary.setOrganizationId(plan.getOrganization().getId());
summary.setOrganizationName(organization.getName());
rval.getBeans().add(summary);
}
return rval;
}
Aggregations