Search in sources :

Example 21 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class EntitiesControllerManager method deleteEntity.

public void deleteEntity(String entityName, String entityId, String modelVersion) {
    entityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
    MetaClass metaClass = restControllerUtils.getMetaClass(entityName);
    checkCanDeleteEntity(metaClass);
    Object id = getIdFromString(entityId, metaClass);
    Entity entity = dataManager.load(new LoadContext<>(metaClass).setId(id));
    checkEntityIsNotNull(entityName, entityId, entity);
    dataManager.remove(entity);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) JsonObject(com.google.gson.JsonObject)

Example 22 with MetaClass

use of com.haulmont.chile.core.model.MetaClass 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 23 with MetaClass

use of com.haulmont.chile.core.model.MetaClass 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 24 with MetaClass

use of com.haulmont.chile.core.model.MetaClass 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 25 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RestFilterParserTest method testEntityCondition.

@Test
public void testEntityCondition() throws Exception {
    new StrictExpectations() {

        {
            RandomStringUtils.randomAlphabetic(anyInt);
            result = "paramName1";
        }
    };
    String data = readDataFromFile("data/restFilter2.json");
    MetaClass metaClass = metadata.getClass("test$TestEntity");
    RestFilterParseResult parseResult = restFilterParser.parse(data, metaClass);
    String expectedJpqlWhere = "({E}.linkedTestEntity.id = :paramName1)";
    assertEquals(expectedJpqlWhere, parseResult.getJpqlWhere());
    Map<String, Object> queryParameters = parseResult.getQueryParameters();
    assertEquals(UUID.fromString("2de6a78f-7bef-89a7-eb5e-b725582f23af"), queryParameters.get("paramName1"));
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Test(org.junit.Test)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10