Search in sources :

Example 1 with RestFilterParseResult

use of com.haulmont.restapi.service.filter.RestFilterParseResult in project cuba by cuba-platform.

the class EntitiesControllerManager method searchEntities.

public EntitiesSearchResult searchEntities(String entityName, String filterJson, @Nullable String viewName, @Nullable Integer limit, @Nullable Integer offset, @Nullable String sort, @Nullable Boolean returnNulls, @Nullable Boolean returnCount, @Nullable Boolean dynamicAttributes, @Nullable String modelVersion) {
    if (filterJson == null) {
        throw new RestAPIException("Cannot parse entities filter", "Entities filter cannot be null", HttpStatus.BAD_REQUEST);
    }
    entityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
    MetaClass metaClass = restControllerUtils.getMetaClass(entityName);
    checkCanReadEntity(metaClass);
    RestFilterParseResult filterParseResult;
    try {
        filterParseResult = restFilterParser.parse(filterJson, metaClass);
    } catch (RestFilterParseException e) {
        throw new RestAPIException("Cannot parse entities filter", e.getMessage(), HttpStatus.BAD_REQUEST, e);
    }
    String jpqlWhere = filterParseResult.getJpqlWhere().replace("{E}", "e");
    Map<String, Object> queryParameters = filterParseResult.getQueryParameters();
    String queryString = "select e from " + entityName + " e where " + jpqlWhere;
    String json = _loadEntitiesList(queryString, viewName, limit, offset, sort, returnNulls, dynamicAttributes, modelVersion, metaClass, queryParameters);
    Long count = null;
    if (BooleanUtils.isTrue(returnCount)) {
        LoadContext ctx = LoadContext.create(metaClass.getJavaClass()).setQuery(LoadContext.createQuery(queryString).setParameters(queryParameters));
        count = dataManager.getCount(ctx);
    }
    return new EntitiesSearchResult(json, count);
}
Also used : RestFilterParseResult(com.haulmont.restapi.service.filter.RestFilterParseResult) MetaClass(com.haulmont.chile.core.model.MetaClass) EntitiesSearchResult(com.haulmont.restapi.data.EntitiesSearchResult) RestAPIException(com.haulmont.restapi.exception.RestAPIException) JsonObject(com.google.gson.JsonObject) RestFilterParseException(com.haulmont.restapi.service.filter.RestFilterParseException)

Aggregations

JsonObject (com.google.gson.JsonObject)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 EntitiesSearchResult (com.haulmont.restapi.data.EntitiesSearchResult)1 RestAPIException (com.haulmont.restapi.exception.RestAPIException)1 RestFilterParseException (com.haulmont.restapi.service.filter.RestFilterParseException)1 RestFilterParseResult (com.haulmont.restapi.service.filter.RestFilterParseResult)1