use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class CommitRequest method generateId.
private String generateId(String entityName) {
Metadata metadata = AppBeans.get(Metadata.NAME);
MetaClass metaClass = metadata.getSession().getClass(entityName);
MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
if (primaryKeyProp == null)
throw new UnsupportedOperationException("Cannot generate ID for " + entityName);
if (primaryKeyProp.getJavaType().equals(UUID.class)) {
UuidSource uuidSource = AppBeans.get(UuidSource.NAME);
UUID uuid = uuidSource.createUuid();
return uuid.toString();
} else if (primaryKeyProp.getJavaType().equals(Long.class)) {
NumberIdSource numberIdSource = AppBeans.get(NumberIdSource.NAME);
Long longId = numberIdSource.createLongId(entityName);
return longId.toString();
} else if (primaryKeyProp.getJavaType().equals(Integer.class)) {
NumberIdSource numberIdSource = AppBeans.get(NumberIdSource.NAME);
Integer intId = numberIdSource.createIntegerId(entityName);
return intId.toString();
} else {
throw new UnsupportedOperationException("Cannot generate ID for " + entityName);
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DataServiceController method find.
@RequestMapping(value = "/api/find.{type}", method = RequestMethod.GET)
public void find(@PathVariable String type, @RequestParam(value = "e") String entityRef, @RequestParam(value = "s") String sessionId, @RequestParam(value = "dynamicAttributes", required = false) Boolean dynamicAttributes, HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
if (!connect(sessionId, response))
return;
try {
EntityLoadInfo loadInfo = EntityLoadInfo.parse(entityRef);
if (loadInfo == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
MetaClass metaClass = loadInfo.getMetaClass();
if (!readPermitted(metaClass)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
Object objectId = loadInfo.getId();
LoadContext loadCtx = new LoadContext(metaClass);
loadCtx.setLoadDynamicAttributes(Boolean.TRUE.equals(dynamicAttributes));
loadCtx.setId(objectId);
if (loadInfo.getViewName() != null) {
loadCtx.setView(loadInfo.getViewName());
} else {
View view = metadata.getViewRepository().getView(metaClass, View.LOCAL);
loadCtx.setView(new View(view, "local-with-system-props", true));
}
Entity entity = dataService.load(loadCtx);
if (entity == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
Converter converter = conversionFactory.getConverter(type);
String result = converter.process(entity, metaClass, loadCtx.getView());
writeResponse(response, result, converter.getMimeType());
}
} catch (Throwable e) {
sendError(request, response, e);
} finally {
authentication.end();
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DataServiceController method queryByPost.
@RequestMapping(value = "/api/query", method = RequestMethod.POST)
public void queryByPost(@RequestParam(value = "s") String sessionId, @RequestHeader(value = "Content-Type") MimeType contentType, @RequestBody String requestContent, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (!authentication.begin(sessionId)) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
try {
Converter converter = conversionFactory.getConverter(contentType);
QueryRequest queryRequest = converter.parseQueryRequest(requestContent);
MetaClass metaClass = metadata.getClass(queryRequest.getEntity());
if (metaClass == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Persistent entity " + queryRequest.getEntity() + " does not exist");
return;
}
if (!entityOpPermitted(metaClass, EntityOp.READ)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
LoadContext loadCtx = new LoadContext(metaClass);
loadCtx.setLoadDynamicAttributes(Boolean.TRUE.equals(queryRequest.loadDynamicAttributes()));
LoadContext.Query query = new LoadContext.Query(queryRequest.getQuery());
for (String key : queryRequest.getParams().keySet()) {
query.setParameter(key, queryRequest.getParams().get(key));
}
loadCtx.setQuery(query);
if (queryRequest.getFirst() != null)
query.setFirstResult(queryRequest.getFirst());
if (queryRequest.getMax() != null)
query.setMaxResults(queryRequest.getMax());
if (queryRequest.getViewName() == null) {
View view = metadata.getViewRepository().getView(metaClass, View.LOCAL);
loadCtx.setView(new View(view, "local-with-system-props", true));
} else {
loadCtx.setView(queryRequest.getViewName());
}
List<Entity> entities = dataService.loadList(loadCtx);
String result = converter.process(entities, metaClass, loadCtx.getView());
writeResponse(response, result, converter.getMimeType());
} catch (RowLevelSecurityException e) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "The operation with entity " + e.getEntity() + " is denied");
} catch (Throwable e) {
sendError(request, response, e);
} finally {
authentication.end();
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class JSONConverter method parseIntoList.
protected List<Entity> parseIntoList(CommitRequest commitRequest, JSONArray nodeList) throws JSONException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException, ParseException {
List<Entity> result = new ArrayList<>(nodeList.length());
for (int j = 0; j < nodeList.length(); j++) {
JSONObject jsonObject = nodeList.getJSONObject(j);
InstanceRef ref = commitRequest.parseInstanceRefAndRegister(jsonObject.getString("id"));
MetaClass metaClass = ref.getMetaClass();
Entity instance = ref.getInstance();
asJavaTree(commitRequest, instance, metaClass, jsonObject);
result.add(instance);
}
return result;
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class JSONConverter method processServiceMethodResult.
@Override
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
MyJSONObject root = new MyJSONObject();
if (result instanceof Entity) {
Entity entity = (Entity) result;
MyJSONObject entityObject = _process(entity);
root.set("result", entityObject);
} else if (result instanceof Collection) {
if (!checkCollectionItemTypes((Collection) result, Entity.class))
throw new IllegalArgumentException("Items that are not instances of Entity found in service method result");
// noinspection unchecked
ArrayList list = new ArrayList((Collection) result);
MetaClass metaClass;
if (!list.isEmpty())
metaClass = ((Entity) list.get(0)).getMetaClass();
else
metaClass = AppBeans.get(Metadata.class).getClasses().iterator().next();
MyJSONObject.Array processed = _process(list, metaClass, null);
root.set("result", processed);
} else {
if (result != null && resultType != Void.TYPE) {
Datatype datatype = getDatatype(resultType);
root.set("result", datatype != null ? datatype.format(result) : result.toString());
} else {
root.set("result", null);
}
}
return root.toString();
}
Aggregations