Search in sources :

Example 1 with ErrorRec

use of com.haulmont.cuba.core.sys.jpql.ErrorRec in project cuba by cuba-platform.

the class ConstraintEditor method testConstraint.

public void testConstraint() {
    Constraint constraint = getItem();
    String entityName = constraint.getEntityName();
    if (validateAll()) {
        if (!Strings.isNullOrEmpty(constraint.getWhereClause())) {
            String baseQueryString = "select e from " + entityName + " e";
            try {
                QueryTransformer transformer = QueryTransformerFactory.createTransformer(baseQueryString);
                if (StringUtils.isNotBlank(constraint.getJoinClause())) {
                    transformer.addJoinAndWhere(constraint.getJoinClause(), constraint.getWhereClause());
                } else {
                    transformer.addWhere(constraint.getWhereClause());
                }
                CollectionDatasource datasource = DsBuilder.create().setMetaClass(metadata.getSession().getClassNN(entityName)).setMaxResults(0).buildCollectionDatasource();
                datasource.setQuery(transformer.getResult());
                datasource.refresh();
            } catch (JpqlSyntaxException e) {
                StringBuilder stringBuilder = new StringBuilder();
                for (ErrorRec rec : e.getErrorRecs()) {
                    stringBuilder.append(rec.toString()).append("<br>");
                }
                showMessageDialog(getMessage("notification.error"), formatMessage("notification.syntaxErrors", stringBuilder), MessageType.WARNING_HTML);
                return;
            } catch (Exception e) {
                String msg;
                Throwable rootCause = ExceptionUtils.getRootCause(e);
                if (rootCause == null)
                    rootCause = e;
                if (rootCause instanceof RemoteException) {
                    List<RemoteException.Cause> causes = ((RemoteException) rootCause).getCauses();
                    RemoteException.Cause cause = causes.get(causes.size() - 1);
                    msg = cause.getThrowable() != null ? cause.getThrowable().toString() : cause.getClassName() + ": " + cause.getMessage();
                } else {
                    msg = rootCause.toString();
                }
                showMessageDialog(getMessage("notification.error"), formatMessage("notification.runtimeError", msg), MessageType.WARNING_HTML);
                return;
            }
        }
        if (!Strings.isNullOrEmpty(constraint.getGroovyScript())) {
            try {
                security.evaluateConstraintScript(metadata.create(entityName), constraint.getGroovyScript());
            } catch (CompilationFailedException e) {
                showMessageDialog(getMessage("notification.error"), formatMessage("notification.scriptCompilationError", e.toString()), MessageType.WARNING_HTML);
                return;
            } catch (Exception e) {
            // ignore
            }
        }
        showNotification(getMessage("notification.success"), NotificationType.HUMANIZED);
    }
}
Also used : Constraint(com.haulmont.cuba.security.entity.Constraint) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ErrorRec(com.haulmont.cuba.core.sys.jpql.ErrorRec) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) Arrays.asList(java.util.Arrays.asList)

Example 2 with ErrorRec

use of com.haulmont.cuba.core.sys.jpql.ErrorRec in project cuba by cuba-platform.

the class BaseJoinNode method identifyVariableEntity.

public void identifyVariableEntity(DomainModel model, Deque<QueryVariableContext> stack, List<ErrorRec> invalidNodes) {
    String variableName = getVariableName();
    if (variableName == null) {
        invalidNodes.add(new ErrorRec(this, "No variable name found"));
        return;
    }
    List children = getChildren();
    if (children == null || children.size() == 0) {
        invalidNodes.add(new ErrorRec(this, "No children found"));
        return;
    }
    CommonTree child0 = (CommonTree) children.get(0);
    if (child0 instanceof CommonErrorNode) {
        invalidNodes.add(new ErrorRec(this, "Child 0 is an error node"));
        return;
    }
    QueryVariableContext queryVC = stack.peekLast();
    if (child0 instanceof PathNode) {
        PathNode pathNode = (PathNode) child0;
        Pointer pointer = pathNode.resolvePointer(model, queryVC);
        if (pointer instanceof NoPointer) {
            invalidNodes.add(new ErrorRec(this, "Cannot resolve joined entity"));
        } else if (pointer instanceof SimpleAttributePointer) {
            invalidNodes.add(new ErrorRec(this, "Joined entity resolved to a non-entity attribute"));
        } else if (pointer instanceof EntityPointer) {
            queryVC.addEntityVariable(variableName, ((EntityPointer) pointer).getEntity());
        } else if (pointer instanceof CollectionPointer) {
            queryVC.addEntityVariable(variableName, ((CollectionPointer) pointer).getEntity());
        } else {
            invalidNodes.add(new ErrorRec(this, "Unexpected pointer variable type: " + pointer.getClass()));
        }
    } else {
        // this special case is for "join X on X.a = Y.b" query. Entity name would be just text in the child node
        try {
            queryVC.addEntityVariable(variableName, model.getEntityByName(child0.getText()));
        } catch (UnknownEntityNameException e) {
            invalidNodes.add(new ErrorRec(this, "Could not find entity for name " + child0.getText()));
        }
    }
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode) UnknownEntityNameException(com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException) ErrorRec(com.haulmont.cuba.core.sys.jpql.ErrorRec) List(java.util.List) QueryVariableContext(com.haulmont.cuba.core.sys.jpql.QueryVariableContext)

Example 3 with ErrorRec

use of com.haulmont.cuba.core.sys.jpql.ErrorRec in project cuba by cuba-platform.

the class JoinVariableNode method treeToQueryPre.

@Override
public CommonTree treeToQueryPre(QueryBuilder sb, List<ErrorRec> invalidNodes) {
    int childCount = getChildCount();
    if (childCount == 0) {
        invalidNodes.add(new ErrorRec(this, "No children found"));
        return null;
    }
    sb.appendSpace();
    sb.appendString(joinSpec);
    sb.appendSpace();
    sb.appendString(toQuery(getChild(0)));
    sb.appendSpace();
    sb.appendString(variableName);
    if (childCount > 1) {
        sb.appendSpace();
        sb.appendString("on");
        for (int idx = 1; idx < childCount; idx++) {
            sb.appendSpace();
            sb.appendString(toQuery(getChild(idx)));
        }
    }
    return null;
}
Also used : ErrorRec(com.haulmont.cuba.core.sys.jpql.ErrorRec)

Aggregations

ErrorRec (com.haulmont.cuba.core.sys.jpql.ErrorRec)3 JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)1 QueryVariableContext (com.haulmont.cuba.core.sys.jpql.QueryVariableContext)1 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Constraint (com.haulmont.cuba.security.entity.Constraint)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 CommonErrorNode (org.antlr.runtime.tree.CommonErrorNode)1 CommonTree (org.antlr.runtime.tree.CommonTree)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1