Search in sources :

Example 46 with JpqlEntityModel

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

the class QueryParserAstBased method getQueryPaths.

@Override
public List<QueryPath> getQueryPaths() {
    List<QueryPath> queryPaths = new ArrayList<>();
    QueryVariableContext context = getQueryAnalyzer().getRootQueryVariableContext();
    TreeVisitor visitor = new TreeVisitor();
    PathNodeFinder finder = new PathNodeFinder();
    visitor.visit(getQueryAnalyzer().getTree(), finder);
    for (PathNode node : finder.getSelectedPathNodes()) {
        JpqlEntityModel model = context.getEntityByVariableNameHierarchically(node.getEntityVariableName());
        QueryPath queryPath = new QueryPath(model.getName(), node.getEntityVariableName(), node.asPathString(), true);
        queryPaths.add(queryPath);
    }
    for (PathNode node : finder.getOtherPathNodes()) {
        JpqlEntityModel model = context.getEntityByVariableNameHierarchically(node.getEntityVariableName());
        QueryPath queryPath = new QueryPath(model.getName(), node.getEntityVariableName(), node.asPathString(), false);
        queryPaths.add(queryPath);
    }
    return queryPaths;
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) ArrayList(java.util.ArrayList) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 47 with JpqlEntityModel

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

Example 48 with JpqlEntityModel

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

the class HintProviderTest method requestHint_with_templateParam.

@Test
public void requestHint_with_templateParam() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel driver = builder.produceImmediately("Driver", "name", "signal");
    builder.startNewEntity("Car");
    builder.addStringAttribute("model");
    builder.addCollectionReferenceAttribute("drivers", "Driver");
    JpqlEntityModel car = builder.produce();
    DomainModel model = new DomainModel(car, driver);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("select a.~ from Car a where a.model = ${param}");
    List<String> options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("drivers", options.get(0));
    assertEquals("model", options.get(1));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 49 with JpqlEntityModel

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

the class HintProviderTest method requestHint_fieldNameHint_join_withCollections.

@Test
public void requestHint_fieldNameHint_join_withCollections() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel personEntity = builder.produceImmediately("Person", "name");
    builder.startNewEntity("Team");
    builder.addStringAttribute("name");
    builder.addStringAttribute("owner");
    builder.addReferenceAttribute("manager", "Person");
    JpqlEntityModel teamEntity = builder.produce();
    builder.startNewEntity("Player");
    builder.addStringAttribute("name");
    builder.addStringAttribute("nickname");
    builder.addReferenceAttribute("team", "Team");
    builder.addReferenceAttribute("agent", "Person");
    JpqlEntityModel playerEntity = builder.produce();
    builder.startNewEntity("League");
    builder.addStringAttribute("name");
    builder.addCollectionReferenceAttribute("teams", "Team");
    JpqlEntityModel leagueEntity = builder.produce();
    DomainModel model = new DomainModel();
    model.add(teamEntity);
    model.add(playerEntity);
    model.add(leagueEntity);
    model.add(personEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("select m.~ from League l " + "left join l.teams as t " + "left join t.manager as m");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("name", options.get(0));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select l from League l " + "join l.teams as t " + "join t.~");
    options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("manager", options.get(0));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 50 with JpqlEntityModel

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

the class HintProviderTest method requestHint_with_variableRebinding.

@Test
public void requestHint_with_variableRebinding() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel driver = builder.produceImmediately("Driver", "name", "signal");
    builder.startNewEntity("Car");
    builder.addStringAttribute("model");
    builder.addCollectionReferenceAttribute("drivers", "Driver");
    JpqlEntityModel car = builder.produce();
    DomainModel model = new DomainModel(car, driver);
    HintProvider hintProvider = createTestHintProvider(model);
    try {
        hintProvider.requestHint("select a.~ from Car a, in(a.drivers) a where a.model = ?1");
        Assert.fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) Test(org.junit.Test)

Aggregations

JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)57 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)46 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)45 Test (org.junit.Test)40 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)17 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)16 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)9 ArrayList (java.util.ArrayList)5 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)4 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)4 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)2 NoJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel)2 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)2 List (java.util.List)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)1 VirtualJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel)1