use of com.haulmont.cuba.core.sys.jpql.model.Attribute 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.model.Attribute in project cuba by cuba-platform.
the class EntityPointer method next.
@Override
public Pointer next(DomainModel model, String field) {
Attribute attribute = entity.getAttributeByName(field);
if (attribute == null) {
return NoPointer.instance();
}
if (!attribute.isEntityReferenceAttribute()) {
return new SimpleAttributePointer(entity, attribute);
}
String targetEntityName = attribute.getReferencedEntityName();
try {
JpqlEntityModel targetEntity = model.getEntityByName(targetEntityName);
return attribute.isCollection() ? new CollectionPointer(targetEntity) : new EntityPointer(targetEntity);
} catch (UnknownEntityNameException e) {
return NoPointer.instance();
}
}
use of com.haulmont.cuba.core.sys.jpql.model.Attribute in project cuba by cuba-platform.
the class QueryTreeTransformer method getPathNodesForOrderBy.
protected List<PathNode> getPathNodesForOrderBy(PathEntityReference pathEntityReference) {
List<PathNode> pathNodes = new ArrayList<>();
String entityName = pathEntityReference.getPathStartingEntityName();
PathNode pathNode = pathEntityReference.getPathNode();
try {
JpqlEntityModel entity = model.getEntityByName(entityName);
String currentVariableName = pathNode.getEntityVariableName();
PathNode currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
for (int i = 0; i < pathNode.getChildCount(); i++) {
String fieldName = pathNode.getChild(i).toString();
Attribute entityAttribute = entity.getAttributeByName(fieldName);
if (entityAttribute.isEntityReferenceAttribute() && !entityAttribute.isEmbedded()) {
currentPathNode.addDefaultChild(fieldName);
pathNodes.add(currentPathNode);
currentVariableName = currentPathNode.asPathString('_');
currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
} else {
currentPathNode.addDefaultChild(fieldName);
}
if (entityAttribute.isEntityReferenceAttribute()) {
entityName = entityAttribute.getReferencedEntityName();
entity = model.getEntityByName(entityName);
}
}
pathNodes.add(currentPathNode);
} catch (UnknownEntityNameException e) {
throw new RuntimeException(String.format("Could not find entity by name %s", entityName), e);
}
return pathNodes;
}
use of com.haulmont.cuba.core.sys.jpql.model.Attribute in project cuba by cuba-platform.
the class QueryParserAstBased method getEntityNameAndPathIfSecondaryReturnedInsteadOfMain.
protected EntityNameAndPath getEntityNameAndPathIfSecondaryReturnedInsteadOfMain() {
List<PathNode> returnedPathNodes = getQueryAnalyzer().getReturnedPathNodes();
if (CollectionUtils.isEmpty(returnedPathNodes) || returnedPathNodes.size() > 1) {
return null;
}
QueryVariableContext rootQueryVariableContext = getQueryAnalyzer().getRootQueryVariableContext();
PathNode pathNode = returnedPathNodes.get(0);
if (pathNode.getChildren() == null) {
JpqlEntityModel entity = rootQueryVariableContext.getEntityByVariableName(pathNode.getEntityVariableName());
if (entity != null) {
if (!Objects.equals(entity.getName(), getEntityName())) {
return new EntityNameAndPath(entity.getName(), pathNode.getEntityVariableName());
}
// fix for scary Eclipselink which consider "select p from sec$GroupHierarchy h join h.parent p"
// (even if h.parent is also sec$GroupHierarchy)
// as report query and does not allow to set view
IdentificationVariableNode mainEntityIdentification = getQueryAnalyzer().getMainEntityIdentification();
if (mainEntityIdentification != null && !pathNode.getEntityVariableName().equals(mainEntityIdentification.getVariableName())) {
return entity.getName() != null ? new EntityNameAndPath(entity.getName(), pathNode.getEntityVariableName()) : null;
}
}
return null;
}
JpqlEntityModel entity;
String entityPath;
boolean collectionSelect = false;
try {
entity = rootQueryVariableContext.getEntityByVariableName(pathNode.getEntityVariableName());
if (entity != null) {
entityPath = pathNode.asPathString();
for (int i = 0; i < pathNode.getChildCount(); i++) {
String fieldName = pathNode.getChild(i).toString();
Attribute entityAttribute = entity.getAttributeByName(fieldName);
if (entityAttribute != null && entityAttribute.isEntityReferenceAttribute()) {
entity = model.getEntityByName(entityAttribute.getReferencedEntityName());
if (!collectionSelect) {
collectionSelect = entityAttribute.isCollection();
}
} else {
return null;
}
}
} else {
return null;
}
} catch (UnknownEntityNameException e) {
throw new RuntimeException(format("Unable to find entity by name %s", e.getEntityName()), e);
}
return entity != null && entity.getName() != null ? new EntityNameAndPath(entity.getName(), entityPath, collectionSelect) : null;
}
Aggregations