use of io.apiman.manager.api.beans.search.OrderByBean in project apiman by apiman.
the class AbstractJpaStorage method applySearchCriteriaToQuery.
/**
* Applies the criteria found in the {@link SearchCriteriaBean} to the JPA query.
* @param criteria
* @param builder
* @param query
* @param from
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <T> void applySearchCriteriaToQuery(SearchCriteriaBean criteria, CriteriaBuilder builder, CriteriaQuery<?> query, Root<T> from, boolean countOnly) {
List<SearchCriteriaFilterBean> filters = criteria.getFilters();
if (filters != null && !filters.isEmpty()) {
List<Predicate> predicates = new ArrayList<>();
for (SearchCriteriaFilterBean filter : filters) {
if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
Path<Object> path = from.get(filter.getName());
Class<?> pathc = path.getJavaType();
if (pathc.isAssignableFrom(String.class)) {
predicates.add(builder.equal(path, filter.getValue()));
} else if (pathc.isEnum()) {
predicates.add(builder.equal(path, Enum.valueOf((Class) pathc, filter.getValue())));
}
} else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
predicates.add(builder.equal(from.<Boolean>get(filter.getName()), Boolean.valueOf(filter.getValue())));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.gt) {
predicates.add(builder.greaterThan(from.<Long>get(filter.getName()), new Long(filter.getValue())));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.gte) {
predicates.add(builder.greaterThanOrEqualTo(from.<Long>get(filter.getName()), new Long(filter.getValue())));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.lt) {
predicates.add(builder.lessThan(from.<Long>get(filter.getName()), new Long(filter.getValue())));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.lte) {
predicates.add(builder.lessThanOrEqualTo(from.<Long>get(filter.getName()), new Long(filter.getValue())));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.neq) {
predicates.add(builder.notEqual(from.get(filter.getName()), filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
predicates.add(builder.like(builder.upper(from.<String>get(filter.getName())), filter.getValue().toUpperCase().replace('*', '%')));
}
}
query.where(predicates.toArray(new Predicate[predicates.size()]));
}
OrderByBean orderBy = criteria.getOrderBy();
if (orderBy != null && !countOnly) {
if (orderBy.isAscending()) {
query.orderBy(builder.asc(from.get(orderBy.getName())));
} else {
query.orderBy(builder.desc(from.get(orderBy.getName())));
}
}
}
use of io.apiman.manager.api.beans.search.OrderByBean 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.OrderByBean 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.OrderByBean 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;
}
use of io.apiman.manager.api.beans.search.OrderByBean in project apiman by apiman.
the class JpaStorage method auditEntity.
/**
* {@inheritDoc}
*/
@Override
public <T> SearchResultsBean<AuditEntryBean> auditEntity(String organizationId, String entityId, String entityVersion, Class<T> type, PagingBean paging) throws StorageException {
SearchCriteriaBean criteria = new SearchCriteriaBean();
if (paging != null) {
criteria.setPaging(paging);
} else {
criteria.setPage(1);
criteria.setPageSize(20);
}
criteria.setOrder("id", false);
if (organizationId != null) {
criteria.addFilter("organizationId", organizationId, SearchCriteriaFilterOperator.eq);
}
if (entityId != null) {
criteria.addFilter("entityId", entityId, SearchCriteriaFilterOperator.eq);
}
if (entityVersion != null) {
criteria.addFilter("entityVersion", entityVersion, SearchCriteriaFilterOperator.eq);
}
if (type != null) {
AuditEntityType entityType = null;
if (type == OrganizationBean.class) {
entityType = AuditEntityType.Organization;
} else if (type == ClientBean.class) {
entityType = AuditEntityType.Client;
} else if (type == ApiBean.class) {
entityType = AuditEntityType.Api;
} else if (type == PlanBean.class) {
entityType = AuditEntityType.Plan;
}
if (entityType != null) {
criteria.addFilter("entityType", entityType.name(), SearchCriteriaFilterOperator.eq);
}
}
return find(criteria, List.of(new OrderByBean(true, AuditEntryBean_.ID)), AuditEntryBean.class, true);
}
Aggregations