Search in sources :

Example 1 with Suggestion

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

the class ConstraintEditor method requestHint.

protected List<Suggestion> requestHint(SourceCodeEditor sender, String text, int cursorPosition) {
    if (entityName.getValue() == null) {
        return Collections.emptyList();
    }
    String joinStr = joinClause.getValue();
    String whereStr = whereClause.getValue();
    // the magic entity name!  The length is three character to match "{E}" length in query
    String entityNameAlias = "a39";
    int position = 0;
    StringBuilder queryBuilder = new StringBuilder();
    queryBuilder.append("select ");
    queryBuilder.append(entityNameAlias);
    queryBuilder.append(" from ");
    queryBuilder.append(entities.get(entityName.getValue()));
    queryBuilder.append(" ");
    queryBuilder.append(entityNameAlias);
    queryBuilder.append(" ");
    if (StringUtils.isNotEmpty(joinStr)) {
        if (sender == joinClause) {
            position = queryBuilder.length() + cursorPosition - 1;
        }
        if (!StringUtils.containsIgnoreCase(joinStr, "join") && !StringUtils.contains(joinStr, ",")) {
            queryBuilder.append("join ").append(joinStr);
            position += "join ".length();
        } else {
            queryBuilder.append(joinStr);
        }
    }
    if (StringUtils.isNotEmpty(whereStr)) {
        if (sender == whereClause) {
            position = queryBuilder.length() + " WHERE ".length() + cursorPosition - 1;
        }
        queryBuilder.append(" WHERE ").append(whereStr);
    }
    String query = queryBuilder.toString();
    query = query.replace("{E}", entityNameAlias);
    List<Suggestion> suggestions = JpqlSuggestionFactory.requestHint(query, position, sender.getAutoCompleteSupport(), cursorPosition);
    addSpecificSuggestions(sender, text, cursorPosition, suggestions);
    return suggestions;
}
Also used : Suggestion(com.haulmont.cuba.gui.components.autocomplete.Suggestion) TextStringBuilder(org.apache.commons.text.TextStringBuilder) Constraint(com.haulmont.cuba.security.entity.Constraint)

Example 2 with Suggestion

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

the class ConstraintEditor method addSpecificSuggestions.

protected void addSpecificSuggestions(SourceCodeEditor sender, String text, int cursorPosition, List<Suggestion> suggestions) {
    if (cursorPosition <= 0)
        return;
    int colonIdx = text.substring(0, cursorPosition).lastIndexOf(":");
    if (colonIdx < 0)
        return;
    List<String> strings = new ArrayList<>();
    strings.add(SESSION_PREFIX + "userGroupId");
    strings.add(SESSION_PREFIX + "userId");
    strings.add(SESSION_PREFIX + "userLogin");
    if (PersistenceHelper.isLoaded(getItem(), "group") && getItem().getGroup() != null) {
        List<String> attributeNames = userManagementService.getSessionAttributeNames(getItem().getGroup().getId());
        for (String name : attributeNames) {
            strings.add(SESSION_PREFIX + name);
        }
    }
    Collections.sort(strings);
    String entered = text.substring(colonIdx + 1, cursorPosition);
    for (String string : strings) {
        if (string.startsWith(entered)) {
            suggestions.add(new Suggestion(sender.getAutoCompleteSupport(), string, string.substring(entered.length()), "", cursorPosition, cursorPosition));
        }
    }
}
Also used : Suggestion(com.haulmont.cuba.gui.components.autocomplete.Suggestion) Constraint(com.haulmont.cuba.security.entity.Constraint)

Example 3 with Suggestion

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

the class SourceCodeEditorScreenTest method sourceCodeEditorSuggester.

@Install(to = "sourceCodeEditor", subject = "suggester")
private List<Suggestion> sourceCodeEditorSuggester(AutoCompleteSupport source, String text, int cursorPosition) {
    List<String> options = Arrays.asList("___");
    List<Suggestion> suggestions = new ArrayList<>();
    for (String item : options) {
        suggestions.add(new Suggestion(source, item, item, null, startPosition, endPosition));
    }
    return suggestions;
}
Also used : Suggestion(com.haulmont.cuba.gui.components.autocomplete.Suggestion) ArrayList(java.util.ArrayList) Install(com.haulmont.cuba.gui.screen.Install)

Aggregations

Suggestion (com.haulmont.cuba.gui.components.autocomplete.Suggestion)3 Constraint (com.haulmont.cuba.security.entity.Constraint)2 Install (com.haulmont.cuba.gui.screen.Install)1 ArrayList (java.util.ArrayList)1 TextStringBuilder (org.apache.commons.text.TextStringBuilder)1