use of com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException 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);
}
Aggregations