Search in sources :

Example 1 with MetaClass

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);
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with MetaClass

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();
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 3 with MetaClass

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();
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 4 with MetaClass

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;
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) JSONObject(org.json.JSONObject) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 5 with MetaClass

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();
}
Also used : JSONArray(org.json.JSONArray) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Datatype(com.haulmont.chile.core.datatypes.Datatype)

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