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);
}
}
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
}
}
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
}
}
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
}
}
Aggregations