Search in sources :

Example 1 with NoJpqlEntityModel

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

Aggregations

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 EntityPointer (com.haulmont.cuba.core.sys.jpql.pointer.EntityPointer)1 NoPointer (com.haulmont.cuba.core.sys.jpql.pointer.NoPointer)1 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RecognitionException (org.antlr.runtime.RecognitionException)1