use of com.haulmont.cuba.core.sys.jpql.pointer.NoPointer 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);
}
use of com.haulmont.cuba.core.sys.jpql.pointer.NoPointer in project cuba by cuba-platform.
the class IdentificationVariableNode method deductFields.
public void deductFields(QueryVariableContext queryVC, CommonTree node, DomainModel model) {
List children = node.getChildren();
CommonTree T_SELECTED_ITEMS_NODE = (CommonTree) children.get(0);
for (Object o : T_SELECTED_ITEMS_NODE.getChildren()) {
o = ((SelectedItemNode) o).getChild(0);
if (!(o instanceof PathNode)) {
throw new RuntimeException("Not a path node");
}
PathNode pathNode = (PathNode) o;
Pointer pointer = pathNode.resolvePointer(model, queryVC);
if (pointer instanceof NoPointer) {
queryVC.setEntity(NoJpqlEntityModel.getInstance());
return;
}
if (pointer instanceof SimpleAttributePointer) {
SimpleAttributePointer saPointer = (SimpleAttributePointer) pointer;
queryVC.getEntity().addAttributeCopy(saPointer.getAttribute());
} else if (pointer instanceof EntityPointer) {
if (T_SELECTED_ITEMS_NODE.getChildren().size() != 1) {
// todo implement
throw new UnsupportedOperationException("Unimplemented variant with returned array");
} else {
queryVC.setEntity(((EntityPointer) pointer).getEntity());
}
}
}
}
Aggregations