Search in sources :

Example 1 with EntityPointer

use of com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer in project cuba by cuba-platform.

the class HintProvider method hintFieldName.

private HintResponse hintFieldName(String lastWord, String input, int caretPosition, Set<InferredType> expectedTypes) throws RecognitionException {
    QueryTreeAnalyzer queryAnalyzer = new QueryTreeAnalyzer();
    try {
        queryAnalyzer.prepare(model, input, false);
    } catch (RecognitionException | JPA2RecognitionException e) {
        List<String> errorMessages = new ArrayList<>();
        errorMessages.add(e.getMessage());
        return new HintResponse("Query error", errorMessages);
    }
    List<ErrorRec> errorRecs = queryAnalyzer.getInvalidIdVarNodes();
    QueryVariableContext root = queryAnalyzer.getRootQueryVariableContext();
    if (root == null) {
        List<String> errorMessages = prepareErrorMessages(errorRecs);
        errorMessages.add(0, "Query variable context is null");
        return new HintResponse("Query error", errorMessages);
    }
    QueryVariableContext queryVC = root.getContextByCaretPosition(caretPosition);
    EntityPath path = EntityPath.parseEntityPath(lastWord);
    Pointer pointer = path.resolvePointer(model, queryVC);
    if (pointer instanceof NoPointer) {
        List<String> errorMessages = prepareErrorMessages(errorRecs);
        errorMessages.add(0, "Cannot parse [" + lastWord + "]");
        return new HintResponse("Query error", errorMessages);
    }
    if (pointer instanceof CollectionPointer) {
        List<String> errorMessages = prepareErrorMessages(errorRecs);
        errorMessages.add(0, "Cannot get attribute of collection [" + lastWord + "]");
        return new HintResponse("Query error", errorMessages);
    }
    if (!(pointer instanceof EntityPointer)) {
        List<String> errorMessages = prepareErrorMessages(errorRecs);
        return new HintResponse("Query error", errorMessages);
    }
    List<Option> options = new ArrayList<>();
    JpqlEntityModel targetEntity = ((EntityPointer) pointer).getEntity();
    if (targetEntity instanceof NoJpqlEntityModel)
        return new HintResponse(options, path.lastEntityFieldPattern);
    List<Attribute> attributes = targetEntity.findAttributesStartingWith(path.lastEntityFieldPattern, expectedTypes);
    for (Attribute attribute : attributes) {
        options.add(new Option(attribute.getName(), attribute.getUserFriendlyName()));
    }
    return new HintResponse(options, path.lastEntityFieldPattern);
}
Also used : JPA2RecognitionException(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException) Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) NoPointer(com.haulmont.cuba.core.sys.jpql.pointer.NoPointer) ArrayList(java.util.ArrayList) NoJpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel) CollectionPointer(com.haulmont.cuba.core.sys.jpql.pointer.CollectionPointer) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) EntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer) NoPointer(com.haulmont.cuba.core.sys.jpql.pointer.NoPointer) NoJpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) EntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer) ArrayList(java.util.ArrayList) List(java.util.List) RecognitionException(org.antlr.runtime.RecognitionException) JPA2RecognitionException(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException) CollectionPointer(com.haulmont.cuba.core.sys.jpql.pointer.CollectionPointer)

Example 2 with EntityPointer

use of com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer in project cuba by cuba-platform.

the class IdentificationVariableNode method deductFields.

public void deductFields(QueryVariableContext queryVC, CommonTree node, DomainModel model) {
    List children = node.getChildren();
    CommonTree T_SELECTED_ITEMS_NODE = (CommonTree) children.get(0);
    for (Object o : T_SELECTED_ITEMS_NODE.getChildren()) {
        o = ((SelectedItemNode) o).getChild(0);
        if (!(o instanceof PathNode)) {
            throw new RuntimeException("Not a path node");
        }
        PathNode pathNode = (PathNode) o;
        Pointer pointer = pathNode.resolvePointer(model, queryVC);
        if (pointer instanceof NoPointer) {
            queryVC.setEntity(NoJpqlEntityModel.getInstance());
            return;
        }
        if (pointer instanceof SimpleAttributePointer) {
            SimpleAttributePointer saPointer = (SimpleAttributePointer) pointer;
            queryVC.getEntity().addAttributeCopy(saPointer.getAttribute());
        } else if (pointer instanceof EntityPointer) {
            if (T_SELECTED_ITEMS_NODE.getChildren().size() != 1) {
                // todo implement
                throw new UnsupportedOperationException("Unimplemented variant with returned array");
            } else {
                queryVC.setEntity(((EntityPointer) pointer).getEntity());
            }
        }
    }
}
Also used : SimpleAttributePointer(com.haulmont.cuba.core.sys.jpql.pointer.SimpleAttributePointer) EntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer) CommonTree(org.antlr.runtime.tree.CommonTree) NoPointer(com.haulmont.cuba.core.sys.jpql.pointer.NoPointer) List(java.util.List) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) SimpleAttributePointer(com.haulmont.cuba.core.sys.jpql.pointer.SimpleAttributePointer) EntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer) NoPointer(com.haulmont.cuba.core.sys.jpql.pointer.NoPointer)

Aggregations

EntityPointer (com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer)2 NoPointer (com.haulmont.cuba.core.sys.jpql.pointer.NoPointer)2 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)2 List (java.util.List)2 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)1 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)1 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)1 NoJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel)1 CollectionPointer (com.haulmont.cuba.core.sys.jpql.pointer.CollectionPointer)1 SimpleAttributePointer (com.haulmont.cuba.core.sys.jpql.pointer.SimpleAttributePointer)1 ArrayList (java.util.ArrayList)1 RecognitionException (org.antlr.runtime.RecognitionException)1 CommonTree (org.antlr.runtime.tree.CommonTree)1