Search in sources :

Example 1 with EntitySerializationOption

use of com.haulmont.cuba.core.app.serialization.EntitySerializationOption in project cuba by cuba-platform.

the class EntitiesControllerManager method loadEntity.

public String loadEntity(String entityName, String entityId, @Nullable String viewName, @Nullable Boolean returnNulls, @Nullable Boolean dynamicAttributes, @Nullable String modelVersion) {
    entityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
    MetaClass metaClass = restControllerUtils.getMetaClass(entityName);
    checkCanReadEntity(metaClass);
    LoadContext<Entity> ctx = new LoadContext<>(metaClass);
    Object id = getIdFromString(entityId, metaClass);
    ctx.setId(id);
    if (!Strings.isNullOrEmpty(viewName)) {
        View view = restControllerUtils.getView(metaClass, viewName);
        ctx.setView(view);
    }
    ctx.setLoadDynamicAttributes(BooleanUtils.isTrue(dynamicAttributes));
    Entity entity = dataManager.load(ctx);
    checkEntityIsNotNull(entityName, entityId, entity);
    List<EntitySerializationOption> serializationOptions = new ArrayList<>();
    serializationOptions.add(EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
    if (BooleanUtils.isTrue(returnNulls))
        serializationOptions.add(EntitySerializationOption.SERIALIZE_NULLS);
    restControllerUtils.applyAttributesSecurity(entity);
    String json = entitySerializationAPI.toJson(entity, null, serializationOptions.toArray(new EntitySerializationOption[0]));
    json = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.TO_VERSION, json);
    return json;
}
Also used : EntitySerializationOption(com.haulmont.cuba.core.app.serialization.EntitySerializationOption) MetaClass(com.haulmont.chile.core.model.MetaClass) JsonObject(com.google.gson.JsonObject) EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView)

Example 2 with EntitySerializationOption

use of com.haulmont.cuba.core.app.serialization.EntitySerializationOption in project cuba by cuba-platform.

the class QueriesControllerManager method _executeQuery.

protected String _executeQuery(String entityName, String queryName, @Nullable Integer limit, @Nullable Integer offset, @Nullable String viewName, @Nullable Boolean returnNulls, @Nullable Boolean dynamicAttributes, @Nullable String version, Map<String, String> params) {
    LoadContext<Entity> ctx;
    entityName = restControllerUtils.transformEntityNameIfRequired(entityName, version, JsonTransformationDirection.FROM_VERSION);
    try {
        ctx = createQueryLoadContext(entityName, queryName, limit, offset, params);
    } catch (ClassNotFoundException | ParseException e) {
        throw new RestAPIException("Error on executing the query", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
    ctx.setLoadDynamicAttributes(BooleanUtils.isTrue(dynamicAttributes));
    // override default view defined in queries config
    if (!Strings.isNullOrEmpty(viewName)) {
        MetaClass metaClass = restControllerUtils.getMetaClass(entityName);
        restControllerUtils.getView(metaClass, viewName);
        ctx.setView(viewName);
    }
    List<Entity> entities = dataManager.loadList(ctx);
    entities.forEach(entity -> restControllerUtils.applyAttributesSecurity(entity));
    List<EntitySerializationOption> serializationOptions = new ArrayList<>();
    serializationOptions.add(EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
    if (BooleanUtils.isTrue(returnNulls))
        serializationOptions.add(EntitySerializationOption.SERIALIZE_NULLS);
    String json = entitySerializationAPI.toJson(entities, ctx.getView(), serializationOptions.toArray(new EntitySerializationOption[0]));
    json = restControllerUtils.transformJsonIfRequired(entityName, version, JsonTransformationDirection.TO_VERSION, json);
    return json;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntitySerializationOption(com.haulmont.cuba.core.app.serialization.EntitySerializationOption) MetaClass(com.haulmont.chile.core.model.MetaClass) RestAPIException(com.haulmont.restapi.exception.RestAPIException) ParseException(java.text.ParseException)

Example 3 with EntitySerializationOption

use of com.haulmont.cuba.core.app.serialization.EntitySerializationOption in project cuba by cuba-platform.

the class EntitiesControllerManager method _loadEntitiesList.

protected String _loadEntitiesList(String queryString, @Nullable String viewName, @Nullable Integer limit, @Nullable Integer offset, @Nullable String sort, @Nullable Boolean returnNulls, @Nullable Boolean dynamicAttributes, @Nullable String modelVersion, MetaClass metaClass, Map<String, Object> queryParameters) {
    LoadContext<Entity> ctx = new LoadContext<>(metaClass);
    if (!Strings.isNullOrEmpty(sort)) {
        boolean descSortOrder = false;
        if (sort.startsWith("-")) {
            descSortOrder = true;
            sort = sort.substring(1);
        } else if (sort.startsWith("+")) {
            sort = sort.substring(1);
        }
        queryString += " order by e." + sort + (descSortOrder ? " desc" : "");
    }
    LoadContext.Query query = new LoadContext.Query(queryString);
    if (limit != null) {
        query.setMaxResults(limit);
    } else {
        query.setMaxResults(persistenceManagerClient.getMaxFetchUI(metaClass.getName()));
    }
    if (offset != null) {
        query.setFirstResult(offset);
    }
    if (queryParameters != null) {
        query.setParameters(queryParameters);
    }
    ctx.setQuery(query);
    View view = null;
    if (!Strings.isNullOrEmpty(viewName)) {
        view = restControllerUtils.getView(metaClass, viewName);
        ctx.setView(view);
    }
    ctx.setLoadDynamicAttributes(BooleanUtils.isTrue(dynamicAttributes));
    List<Entity> entities = dataManager.loadList(ctx);
    entities.forEach(entity -> restControllerUtils.applyAttributesSecurity(entity));
    List<EntitySerializationOption> serializationOptions = new ArrayList<>();
    serializationOptions.add(EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
    if (BooleanUtils.isTrue(returnNulls))
        serializationOptions.add(EntitySerializationOption.SERIALIZE_NULLS);
    String json = entitySerializationAPI.toJson(entities, view, serializationOptions.toArray(new EntitySerializationOption[0]));
    json = restControllerUtils.transformJsonIfRequired(metaClass.getName(), modelVersion, JsonTransformationDirection.TO_VERSION, json);
    return json;
}
Also used : EntitySerializationOption(com.haulmont.cuba.core.app.serialization.EntitySerializationOption) EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView)

Aggregations

EntitySerializationOption (com.haulmont.cuba.core.app.serialization.EntitySerializationOption)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 EntityImportView (com.haulmont.cuba.core.app.importexport.EntityImportView)2 JsonObject (com.google.gson.JsonObject)1 Entity (com.haulmont.cuba.core.entity.Entity)1 RestAPIException (com.haulmont.restapi.exception.RestAPIException)1 ParseException (java.text.ParseException)1