use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean 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.SearchCriteriaFilterBean 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.SearchCriteriaFilterBean in project apiman by apiman.
the class NotificationRepositoryImpl method searchNotificationsByUser.
@Override
public SearchResultsBean<NotificationEntity> searchNotificationsByUser(String recipientUserId, SearchCriteriaBean searchCriteria) throws StorageException {
var recipientFilter = new SearchCriteriaFilterBean().setName("recipient").setOperator(SearchCriteriaFilterOperator.eq).setValue(recipientUserId);
searchCriteria.getFilters().add(recipientFilter);
return super.find(searchCriteria, List.of(new OrderByBean(true, NotificationEntity_.ID)), NotificationEntity.class, true);
}
use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.
the class NotificationRepositoryImpl method getLatestNotificationsByRecipientId.
@Override
public SearchResultsBean<NotificationEntity> getLatestNotificationsByRecipientId(String recipientUserId, @Nullable PagingBean paging) throws StorageException {
var filter = new SearchCriteriaFilterBean().setName("recipient").setOperator(SearchCriteriaFilterOperator.eq).setValue(recipientUserId);
var searchCriteria = new SearchCriteriaBean().setFilters(List.of(filter)).setPaging(paging).setOrder("id", false);
return super.find(searchCriteria, List.of(new OrderByBean(true, NotificationEntity_.ID)), NotificationEntity.class, true);
}
use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.
the class AbstractJpaStorage method applySearchCriteriaToQuery.
/**
* Applies the criteria found in the {@link SearchCriteriaBean} to the JPA query.
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <T> CriteriaBuilder<T> applySearchCriteriaToQuery(String rootAlias, SearchCriteriaBean criteria, CriteriaBuilder<T> cb, boolean countOnly) {
List<SearchCriteriaFilterBean> filters = criteria.getFilters();
if (filters != null && !filters.isEmpty()) {
for (SearchCriteriaFilterBean filter : filters) {
final String name = cb.getPath(filter.getName()).getPath();
if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
com.blazebit.persistence.Path path = cb.getPath(filter.getName());
Class<?> pathKlazz = path.getJavaType();
if (pathKlazz.isEnum()) {
cb = cb.where(name).eq(Enum.valueOf((Class) pathKlazz, filter.getValue()));
} else {
cb = cb.where(name).eq(filter.getValue());
}
} else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
cb = cb.where(name).eq(Boolean.valueOf(filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.gt) {
cb = cb.where(name).gt(Long.valueOf(filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.gte) {
cb = cb.where(name).ge(Long.valueOf(filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.lt) {
cb = cb.where(name).lt(Long.valueOf(filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.lte) {
cb = cb.where(name).le(Long.valueOf(filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.neq) {
cb = cb.where(name).notEq(Long.valueOf(filter.getValue()));
} else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
cb = cb.where(name).like(false).value(filter.getValue().toUpperCase().replace('*', '%')).noEscape();
}
}
}
OrderByBean orderBy = criteria.getOrderBy();
if (orderBy != null && !countOnly) {
if (orderBy.isAscending()) {
cb = cb.orderByAsc(orderBy.getName());
} else {
cb = cb.orderByDesc(orderBy.getName());
}
}
return cb;
}
Aggregations