Search in sources :

Example 1 with Attribute

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

use of com.haulmont.cuba.core.sys.jpql.model.Attribute in project cuba by cuba-platform.

the class EntityPointer method next.

@Override
public Pointer next(DomainModel model, String field) {
    Attribute attribute = entity.getAttributeByName(field);
    if (attribute == null) {
        return NoPointer.instance();
    }
    if (!attribute.isEntityReferenceAttribute()) {
        return new SimpleAttributePointer(entity, attribute);
    }
    String targetEntityName = attribute.getReferencedEntityName();
    try {
        JpqlEntityModel targetEntity = model.getEntityByName(targetEntityName);
        return attribute.isCollection() ? new CollectionPointer(targetEntity) : new EntityPointer(targetEntity);
    } catch (UnknownEntityNameException e) {
        return NoPointer.instance();
    }
}
Also used : Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) UnknownEntityNameException(com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 3 with Attribute

use of com.haulmont.cuba.core.sys.jpql.model.Attribute in project cuba by cuba-platform.

the class QueryTreeTransformer method getPathNodesForOrderBy.

protected List<PathNode> getPathNodesForOrderBy(PathEntityReference pathEntityReference) {
    List<PathNode> pathNodes = new ArrayList<>();
    String entityName = pathEntityReference.getPathStartingEntityName();
    PathNode pathNode = pathEntityReference.getPathNode();
    try {
        JpqlEntityModel entity = model.getEntityByName(entityName);
        String currentVariableName = pathNode.getEntityVariableName();
        PathNode currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
        for (int i = 0; i < pathNode.getChildCount(); i++) {
            String fieldName = pathNode.getChild(i).toString();
            Attribute entityAttribute = entity.getAttributeByName(fieldName);
            if (entityAttribute.isEntityReferenceAttribute() && !entityAttribute.isEmbedded()) {
                currentPathNode.addDefaultChild(fieldName);
                pathNodes.add(currentPathNode);
                currentVariableName = currentPathNode.asPathString('_');
                currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
            } else {
                currentPathNode.addDefaultChild(fieldName);
            }
            if (entityAttribute.isEntityReferenceAttribute()) {
                entityName = entityAttribute.getReferencedEntityName();
                entity = model.getEntityByName(entityName);
            }
        }
        pathNodes.add(currentPathNode);
    } catch (UnknownEntityNameException e) {
        throw new RuntimeException(String.format("Could not find entity by name %s", entityName), e);
    }
    return pathNodes;
}
Also used : Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) UnknownEntityNameException(com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException) ArrayList(java.util.ArrayList) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 4 with Attribute

use of com.haulmont.cuba.core.sys.jpql.model.Attribute in project cuba by cuba-platform.

the class QueryParserAstBased method getEntityNameAndPathIfSecondaryReturnedInsteadOfMain.

protected EntityNameAndPath getEntityNameAndPathIfSecondaryReturnedInsteadOfMain() {
    List<PathNode> returnedPathNodes = getQueryAnalyzer().getReturnedPathNodes();
    if (CollectionUtils.isEmpty(returnedPathNodes) || returnedPathNodes.size() > 1) {
        return null;
    }
    QueryVariableContext rootQueryVariableContext = getQueryAnalyzer().getRootQueryVariableContext();
    PathNode pathNode = returnedPathNodes.get(0);
    if (pathNode.getChildren() == null) {
        JpqlEntityModel entity = rootQueryVariableContext.getEntityByVariableName(pathNode.getEntityVariableName());
        if (entity != null) {
            if (!Objects.equals(entity.getName(), getEntityName())) {
                return new EntityNameAndPath(entity.getName(), pathNode.getEntityVariableName());
            }
            // fix for scary Eclipselink which consider "select p from sec$GroupHierarchy h join h.parent p"
            // (even if h.parent is also sec$GroupHierarchy)
            // as report query and does not allow to set view
            IdentificationVariableNode mainEntityIdentification = getQueryAnalyzer().getMainEntityIdentification();
            if (mainEntityIdentification != null && !pathNode.getEntityVariableName().equals(mainEntityIdentification.getVariableName())) {
                return entity.getName() != null ? new EntityNameAndPath(entity.getName(), pathNode.getEntityVariableName()) : null;
            }
        }
        return null;
    }
    JpqlEntityModel entity;
    String entityPath;
    boolean collectionSelect = false;
    try {
        entity = rootQueryVariableContext.getEntityByVariableName(pathNode.getEntityVariableName());
        if (entity != null) {
            entityPath = pathNode.asPathString();
            for (int i = 0; i < pathNode.getChildCount(); i++) {
                String fieldName = pathNode.getChild(i).toString();
                Attribute entityAttribute = entity.getAttributeByName(fieldName);
                if (entityAttribute != null && entityAttribute.isEntityReferenceAttribute()) {
                    entity = model.getEntityByName(entityAttribute.getReferencedEntityName());
                    if (!collectionSelect) {
                        collectionSelect = entityAttribute.isCollection();
                    }
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
    } catch (UnknownEntityNameException e) {
        throw new RuntimeException(format("Unable to find entity by name %s", e.getEntityName()), e);
    }
    return entity != null && entity.getName() != null ? new EntityNameAndPath(entity.getName(), entityPath, collectionSelect) : null;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Aggregations

Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)4 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)4 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)2 ArrayList (java.util.ArrayList)2 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)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 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)1 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)1 List (java.util.List)1 RecognitionException (org.antlr.runtime.RecognitionException)1