Search in sources :

Example 1 with JpqlSyntaxException

use of com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException 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 JpqlSyntaxException

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

the class QueryTransformerAstBasedTest method addWhere_onIncorrectQuery.

// todo eude : fix the following
// @Test
// public void addWhere_onIncorrectHavingInTheEnd() throws RecognitionException {
// DomainModel model = prepareDomainModel();
// 
// try {
// new QueryTransformerAstBased(model,
// "select h from sec$GroupHierarchy h join h.parent.constraints c group by c.level order by c.level having c.level > 0");
// fail("Incorrectly placed 'having' passed");
// } catch (JpqlSyntaxException e) {
// //expected
// }
// }
@Test
public void addWhere_onIncorrectQuery() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    try {
        QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h join h.parent.constraints");
        transformer.getResult();
        fail("Not named join variable passed");
    } catch (JpqlSyntaxException e) {
    // expected
    }
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) Test(org.junit.Test)

Example 3 with JpqlSyntaxException

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

the class QueryTransformerAstBasedTest method testAddWrongWhere.

@Test
public void testAddWrongWhere() throws RecognitionException {
    try {
        DomainModel model = prepareDomainModel();
        QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$Constraint c");
        transformer.addWhere("{E}.group.id == :group");
        fail();
    } catch (JpqlSyntaxException e) {
    // expected
    }
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) Test(org.junit.Test)

Example 4 with JpqlSyntaxException

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

the class QueryParserAstBasedTest method testError.

@Test
public void testError() throws Exception {
    DomainModel model = prepareDomainModel();
    try {
        QueryParserAstBased parser = new QueryParserAstBased(model, "select u from sec$Constraint");
        parser.getEntityAlias();
        fail();
    } catch (JpqlSyntaxException e) {
    // OK
    }
    try {
        QueryParserAstBased parser = new QueryParserAstBased(model, "select u from sec$GroupHierarchy where u.createdBy = 'createdBy'");
        parser.getEntityAlias();
        fail();
    } catch (JpqlSyntaxException e) {
    // OK
    }
    try {
        QueryParserAstBased parser = new QueryParserAstBased(model, "select u from sec$GroupHierarchy u where u.createdBy != 'createdBy'");
        parser.getEntityAlias();
        fail();
    } catch (JpqlSyntaxException e) {
    // OK
    }
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) Test(org.junit.Test)

Aggregations

JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)4 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)3 Test (org.junit.Test)3 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)2 ErrorRec (com.haulmont.cuba.core.sys.jpql.ErrorRec)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Constraint (com.haulmont.cuba.security.entity.Constraint)1 Arrays.asList (java.util.Arrays.asList)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1