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);
}
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);
}
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;
}
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;
}
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"));
}
Aggregations