Search in sources :

Example 1 with EntitiesSearchResult

use of com.haulmont.restapi.data.EntitiesSearchResult in project cuba by cuba-platform.

the class EntitiesController method loadEntitiesList.

@GetMapping("/{entityName}")
public ResponseEntity<String> loadEntitiesList(@PathVariable String entityName, @RequestParam(required = false) String view, @RequestParam(required = false) Integer limit, @RequestParam(required = false) Integer offset, @RequestParam(required = false) String sort, @RequestParam(required = false) Boolean returnNulls, @RequestParam(required = false) Boolean returnCount, @RequestParam(required = false) Boolean dynamicAttributes, @RequestParam(required = false) String modelVersion) {
    EntitiesSearchResult entitiesSearchResult = entitiesControllerManager.loadEntitiesList(entityName, view, limit, offset, sort, returnNulls, returnCount, dynamicAttributes, modelVersion);
    ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(HttpStatus.OK);
    if (BooleanUtils.isTrue(returnCount)) {
        responseBuilder.header("X-Total-Count", entitiesSearchResult.getCount().toString());
    }
    return responseBuilder.body(entitiesSearchResult.getJson());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) EntitiesSearchResult(com.haulmont.restapi.data.EntitiesSearchResult)

Example 2 with EntitiesSearchResult

use of com.haulmont.restapi.data.EntitiesSearchResult in project cuba by cuba-platform.

the class EntitiesController method searchEntitiesListGet.

@GetMapping("/{entityName}/search")
public ResponseEntity<String> searchEntitiesListGet(@PathVariable String entityName, @RequestParam String filter, @RequestParam(required = false) String view, @RequestParam(required = false) Integer limit, @RequestParam(required = false) Integer offset, @RequestParam(required = false) String sort, @RequestParam(required = false) Boolean returnNulls, @RequestParam(required = false) Boolean returnCount, @RequestParam(required = false) Boolean dynamicAttributes, @RequestParam(required = false) String modelVersion) {
    EntitiesSearchResult entitiesSearchResult = entitiesControllerManager.searchEntities(entityName, filter, view, limit, offset, sort, returnNulls, returnCount, dynamicAttributes, modelVersion);
    ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(HttpStatus.OK);
    if (BooleanUtils.isTrue(returnCount)) {
        responseBuilder.header("X-Total-Count", entitiesSearchResult.getCount().toString());
    }
    return responseBuilder.body(entitiesSearchResult.getJson());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) EntitiesSearchResult(com.haulmont.restapi.data.EntitiesSearchResult)

Example 3 with EntitiesSearchResult

use of com.haulmont.restapi.data.EntitiesSearchResult 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)

Example 4 with EntitiesSearchResult

use of com.haulmont.restapi.data.EntitiesSearchResult in project cuba by cuba-platform.

the class EntitiesControllerManager method loadEntitiesList.

public EntitiesSearchResult loadEntitiesList(String entityName, @Nullable String viewName, @Nullable Integer limit, @Nullable Integer offset, @Nullable String sort, @Nullable Boolean returnNulls, @Nullable Boolean returnCount, @Nullable Boolean dynamicAttributes, @Nullable String modelVersion) {
    entityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
    MetaClass metaClass = restControllerUtils.getMetaClass(entityName);
    checkCanReadEntity(metaClass);
    String queryString = "select e from " + entityName + " e";
    String json = _loadEntitiesList(queryString, viewName, limit, offset, sort, returnNulls, dynamicAttributes, modelVersion, metaClass, new HashMap<>());
    json = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.TO_VERSION, json);
    Long count = null;
    if (BooleanUtils.isTrue(returnCount)) {
        LoadContext ctx = LoadContext.create(metaClass.getJavaClass()).setQuery(LoadContext.createQuery(queryString));
        count = dataManager.getCount(ctx);
    }
    return new EntitiesSearchResult(json, count);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) EntitiesSearchResult(com.haulmont.restapi.data.EntitiesSearchResult)

Example 5 with EntitiesSearchResult

use of com.haulmont.restapi.data.EntitiesSearchResult in project cuba by cuba-platform.

the class EntitiesController method searchEntitiesListPost.

@PostMapping("/{entityName}/search")
public ResponseEntity<String> searchEntitiesListPost(@PathVariable String entityName, @RequestBody String requestBodyJson) {
    EntitiesSearchResult entitiesSearchResult = entitiesControllerManager.searchEntities(entityName, requestBodyJson);
    ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(HttpStatus.OK);
    JsonObject requestJsonObject = new JsonParser().parse(requestBodyJson).getAsJsonObject();
    JsonPrimitive returnCount = requestJsonObject.getAsJsonPrimitive("returnCount");
    if (returnCount != null && returnCount.getAsBoolean()) {
        responseBuilder.header("X-Total-Count", entitiesSearchResult.getCount().toString());
    }
    return responseBuilder.body(entitiesSearchResult.getJson());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JsonPrimitive(com.google.gson.JsonPrimitive) EntitiesSearchResult(com.haulmont.restapi.data.EntitiesSearchResult) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

EntitiesSearchResult (com.haulmont.restapi.data.EntitiesSearchResult)5 ResponseEntity (org.springframework.http.ResponseEntity)3 JsonObject (com.google.gson.JsonObject)2 MetaClass (com.haulmont.chile.core.model.MetaClass)2 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 RestAPIException (com.haulmont.restapi.exception.RestAPIException)1 RestFilterParseException (com.haulmont.restapi.service.filter.RestFilterParseException)1 RestFilterParseResult (com.haulmont.restapi.service.filter.RestFilterParseResult)1