Search in sources :

Example 1 with Pointer

use of com.haulmont.cuba.core.sys.jpql.pointer.Pointer 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 Pointer

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

the class EntitiesFinder method resolveEntityNames.

protected void resolveEntityNames(Set<String> entityNames, PathNode node, DomainModel model, QueryVariableContext queryVariableContext) {
    List<Pointer> pointers = node.resolveTransitionalPointers(model, queryVariableContext);
    pointers.stream().filter(pointer -> pointer instanceof HasEntityPointer).forEach(pointer -> {
        JpqlEntityModel entityModel = ((HasEntityPointer) pointer).getEntity();
        if (entityModel != null) {
            entityNames.add(entityModel.getName());
        }
    });
}
Also used : HashSet(java.util.HashSet) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) CommonTree(org.antlr.runtime.tree.CommonTree) List(java.util.List) IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) Set(java.util.Set) HasEntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer) TreeVisitorAction(org.antlr.runtime.tree.TreeVisitorAction) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode) ArrayList(java.util.ArrayList) HasEntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) HasEntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 3 with Pointer

use of com.haulmont.cuba.core.sys.jpql.pointer.Pointer 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)

Example 4 with Pointer

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

the class EntityPath method resolveTransitionalPointers.

public List<Pointer> resolveTransitionalPointers(DomainModel model, QueryVariableContext queryVC) {
    List<Pointer> pointers = new ArrayList<>();
    Pointer pointer = EntityPointer.create(queryVC, topEntityVariableName);
    pointers.add(pointer);
    for (String traversedField : traversedFields) {
        pointer = pointer.next(model, traversedField);
        pointers.add(pointer);
    }
    return pointers;
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) EntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer)

Aggregations

Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)4 EntityPointer (com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)2 NoPointer (com.haulmont.cuba.core.sys.jpql.pointer.NoPointer)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)1 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)1 NoJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel)1 CollectionPointer (com.haulmont.cuba.core.sys.jpql.pointer.CollectionPointer)1 HasEntityPointer (com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer)1 SimpleAttributePointer (com.haulmont.cuba.core.sys.jpql.pointer.SimpleAttributePointer)1 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)1 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 RecognitionException (org.antlr.runtime.RecognitionException)1 TreeVisitorAction (org.antlr.runtime.tree.TreeVisitorAction)1