Search in sources :

Example 1 with HintProvider

use of com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider in project cuba by cuba-platform.

the class JpqlSuggestionFactory method requestHint.

public static List<Suggestion> requestHint(String query, int queryPosition, AutoCompleteSupport sender, int senderCursorPosition, @Nullable HintProvider provider) {
    DomainModelBuilder builder = AppBeans.get(DomainModelWithCaptionsBuilder.NAME);
    DomainModel domainModel = builder.produce();
    if (provider == null) {
        provider = new HintProvider(domainModel);
    }
    try {
        HintRequest request = new HintRequest();
        request.setQuery(query);
        request.setPosition(queryPosition);
        HintResponse response = provider.requestHint(request);
        String prefix = response.getLastWord();
        List<Option> options = response.getOptionObjects();
        List<Suggestion> result = new ArrayList<>();
        for (Option option : options) {
            Suggestion suggestion = JpqlSuggestionFactory.produce(sender, option.getValue(), option.getDescription(), senderCursorPosition, prefix == null ? 0 : prefix.length());
            result.add(suggestion);
        }
        return result;
    } catch (org.antlr.runtime.RecognitionException e) {
        throw new RuntimeException(e);
    }
}
Also used : HintRequest(com.haulmont.cuba.gui.components.autocomplete.impl.HintRequest) DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) ArrayList(java.util.ArrayList) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) DomainModelBuilder(com.haulmont.cuba.core.sys.jpql.DomainModelBuilder) Option(com.haulmont.cuba.gui.components.autocomplete.impl.Option)

Example 2 with HintProvider

use of com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider 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 3 with HintProvider

use of com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider 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 4 with HintProvider

use of com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider in project cuba by cuba-platform.

the class HintProviderTest method requestHint_entityNameHint_order.

@Test
public void requestHint_entityNameHint_order() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModelImpl playerEntity = builder.produceImmediately("Player");
    JpqlEntityModelImpl parentEntity = builder.produceImmediately("Parent");
    DomainModel model = new DomainModel();
    model.add(playerEntity);
    model.add(parentEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("SELECT p FROM P~");
    List<String> options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("Parent", options.get(0));
    assertEquals("Player", options.get(1));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlEntityModelImpl(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 5 with HintProvider

use of com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider 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

DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)21 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)21 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)20 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)20 Test (org.junit.Test)20 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)17 JpqlEntityModelImpl (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl)3 DomainModelBuilder (com.haulmont.cuba.core.sys.jpql.DomainModelBuilder)1 HintRequest (com.haulmont.cuba.gui.components.autocomplete.impl.HintRequest)1 Option (com.haulmont.cuba.gui.components.autocomplete.impl.Option)1 ArrayList (java.util.ArrayList)1