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;
}
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));
}
}
}
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;
}
Aggregations