Search in sources :

Example 1 with JpqlEntityModel

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

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

the class HintProvider method hintEntityName.

private HintResponse hintEntityName(String lastWord) {
    List<JpqlEntityModel> matchingEntities = model.findEntitiesStartingWith(lastWord);
    List<Option> options = new ArrayList<>();
    for (JpqlEntityModel entity : matchingEntities) {
        options.add(new Option(entity.getName(), entity.getUserFriendlyName()));
    }
    return new HintResponse(options, lastWord);
}
Also used : ArrayList(java.util.ArrayList) NoJpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 3 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.

// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void requestHint_fieldNameHint_wherein_subquerywhere() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// JpqlEntityModel playerEntity = builder.produceImmediately("Player", "nickname", "name");
// DomainModel model = new DomainModel();
// model.add(playerEntity);
// model.add(teamEntity);
// 
// HintProvider hintProvider = createTestHintProvider(model);
// HintResponse response = hintProvider.requestHint("select p.name from Player p where p.team in (select a from Team a where a.~)");
// List<String> options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("owner", options.get(1));
// 
// hintProvider = createTestHintProvider(model);
// response = hintProvider.requestHint("select p.name from Player p where p.team in (select a from Team a where a.name = p.n~)");
// options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("nickname", options.get(1));
// }
@Test
public void requestHint_fieldNameHint_join() 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();
    DomainModel model = new DomainModel();
    model.add(teamEntity);
    model.add(playerEntity);
    model.add(personEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("select p.name, t.o~ from Player p join p.team as t");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("owner", options.get(0));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select p from Player p join p.team as t where t.~");
    options = response.getOptions();
    assertEquals(3, options.size());
    assertEquals("manager", options.get(0));
    assertEquals("name", options.get(1));
    assertEquals("owner", options.get(2));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select p from Player p join p.~ ");
    options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("agent", options.get(0));
    assertEquals("team", options.get(1));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select p from Player p join p.team as t where t.~");
    options = response.getOptions();
    assertEquals(3, options.size());
    assertEquals("manager", options.get(0));
    assertEquals("name", options.get(1));
    assertEquals("owner", options.get(2));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select m.~ from Player p " + "join p.team as t " + "join t.manager as m");
    options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("name", options.get(0));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select t from Player p " + "join p.team as t " + "join t.~");
    options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("manager", options.get(0));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select t from Player p join Team t on t.~ = p.agent.id");
    options = response.getOptions();
    assertEquals(3, 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 4 with JpqlEntityModel

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

the class HintProviderTest method requestHint_fieldNameHint_simple_referencedEntity.

@Test
public void requestHint_fieldNameHint_simple_referencedEntity() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
    builder.startNewEntity("Player");
    builder.addStringAttribute("name");
    builder.addStringAttribute("nickname");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel();
    model.add(teamEntity);
    model.add(playerEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("SELECT p.te~ FROM Player p");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("team", options.get(0));
    response = hintProvider.requestHint("SELECT p.team.~ FROM Player p");
    options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("name", options.get(0));
    response = hintProvider.requestHint("SELECT p.team.ni~ FROM Player p");
    options = response.getOptions();
    assertEquals(0, options.size());
}
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 5 with JpqlEntityModel

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

the class HintProviderTest method requestHint_fieldNameHint_simple_wherePart.

@Test
public void requestHint_fieldNameHint_simple_wherePart() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
    DomainModel model = new DomainModel();
    model.add(teamEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("select t from Team t where t.~");
    List<String> options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("name", options.get(0));
    assertEquals("owner", options.get(1));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select t.~ from Team t where t.name = 'KS'");
    options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("name", options.get(0));
    assertEquals("owner", options.get(1));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select t.name from Team t where t.~");
    options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("name", options.get(0));
    assertEquals("owner", options.get(1));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select t.~ from Team t where t.name in ('KS', 'Zenit')");
    options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("name", options.get(0));
    assertEquals("owner", 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)

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