Search in sources :

Example 1 with Constraint

use of com.haulmont.cuba.security.entity.Constraint 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 Constraint

use of com.haulmont.cuba.security.entity.Constraint in project cuba by cuba-platform.

the class ConstraintEditor method setupVisibility.

protected void setupVisibility() {
    Constraint item = getItem();
    asList(groovyScript, groovyScriptLabel).forEach(component -> component.setVisible(item.getCheckType().memory()));
    asList(joinClause, joinClauseLabel, whereClause, whereClauseLabel).forEach(component -> component.setVisible(item.getCheckType().database() && item.getOperationType() != ConstraintOperationType.CREATE && item.getOperationType() != ConstraintOperationType.DELETE && item.getOperationType() != ConstraintOperationType.UPDATE));
    asList(code, codeLabel).forEach(component -> component.setVisible(item.getOperationType() == ConstraintOperationType.CUSTOM));
    if (item.getOperationType() != ConstraintOperationType.ALL && item.getOperationType() != ConstraintOperationType.CUSTOM && item.getOperationType() != ConstraintOperationType.READ) {
        item.setCheckType(ConstraintCheckType.MEMORY);
        type.setEnabled(false);
    } else {
        type.setEnabled(true);
    }
}
Also used : Constraint(com.haulmont.cuba.security.entity.Constraint)

Example 3 with Constraint

use of com.haulmont.cuba.security.entity.Constraint in project cuba by cuba-platform.

the class ConstraintEditor method openWizard.

public void openWizard() {
    String entityNameValue = entityName.getValue();
    if (StringUtils.isBlank(entityNameValue)) {
        showNotification(getMessage("notification.entityIsEmpty"), NotificationType.HUMANIZED);
        entityName.requestFocus();
        return;
    }
    FakeFilterSupport fakeFilterSupport = new FakeFilterSupport(this, metadata.getSession().getClass(entityNameValue));
    WindowInfo windowInfo = windowConfig.getWindowInfo("filterEditor");
    Map<String, Object> params = new HashMap<>();
    Constraint constraint = getItem();
    final Filter fakeFilter = fakeFilterSupport.createFakeFilter();
    final FilterEntity filterEntity = fakeFilterSupport.createFakeFilterEntity(constraint.getFilterXml());
    final ConditionsTree conditionsTree = fakeFilterSupport.createFakeConditionsTree(fakeFilter, filterEntity);
    params.put("filter", fakeFilter);
    params.put("filterEntity", filterEntity);
    params.put("conditions", conditionsTree);
    params.put("useShortConditionForm", true);
    params.put("hideDynamicAttributes", constraint.getCheckType() != ConstraintCheckType.DATABASE);
    params.put("hideCustomConditions", constraint.getCheckType() != ConstraintCheckType.DATABASE);
    FilterEditor filterEditor = (FilterEditor) windowManagerProvider.get().openWindow(windowInfo, WindowManager.OpenType.DIALOG, params);
    filterEditor.addCloseListener(actionId -> {
        if (!COMMIT_ACTION_ID.equals(actionId))
            return;
        FilterParser filterParser1 = AppBeans.get(FilterParser.class);
        // todo eude rename com.haulmont.cuba.gui.components.filter.FilterParser
        filterEntity.setXml(filterParser1.getXml(filterEditor.getConditions(), Param.ValueProperty.DEFAULT_VALUE));
        if (filterEntity.getXml() != null) {
            Element element = Dom4j.readDocument(filterEntity.getXml()).getRootElement();
            com.haulmont.cuba.core.global.filter.FilterParser filterParser = new com.haulmont.cuba.core.global.filter.FilterParser(element);
            Constraint item = getItem();
            if (item.getCheckType().database()) {
                String jpql = new SecurityJpqlGenerator().generateJpql(filterParser.getRoot());
                constraint.setWhereClause(jpql);
                Set<String> joins = filterParser.getRoot().getJoins();
                if (!joins.isEmpty()) {
                    String joinsStr = new StrBuilder().appendWithSeparators(joins, " ").toString();
                    constraint.setJoinClause(joinsStr);
                }
            }
            if (item.getCheckType().memory()) {
                String groovy = new GroovyGenerator().generateGroovy(filterParser.getRoot());
                constraint.setGroovyScript(groovy);
            }
            constraint.setFilterXml(filterEntity.getXml());
        }
    });
}
Also used : Constraint(com.haulmont.cuba.security.entity.Constraint) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) Element(org.dom4j.Element) GroovyGenerator(com.haulmont.cuba.core.global.filter.GroovyGenerator) FilterEditor(com.haulmont.cuba.gui.components.filter.edit.FilterEditor) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) WindowInfo(com.haulmont.cuba.gui.config.WindowInfo) FakeFilterSupport(com.haulmont.cuba.gui.components.filter.FakeFilterSupport) FilterParser(com.haulmont.cuba.gui.components.filter.FilterParser) SecurityJpqlGenerator(com.haulmont.cuba.core.global.filter.SecurityJpqlGenerator) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) StrBuilder(org.apache.commons.lang.text.StrBuilder)

Aggregations

Constraint (com.haulmont.cuba.security.entity.Constraint)3 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 GroovyGenerator (com.haulmont.cuba.core.global.filter.GroovyGenerator)1 SecurityJpqlGenerator (com.haulmont.cuba.core.global.filter.SecurityJpqlGenerator)1 ErrorRec (com.haulmont.cuba.core.sys.jpql.ErrorRec)1 JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)1 ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)1 FakeFilterSupport (com.haulmont.cuba.gui.components.filter.FakeFilterSupport)1 FilterParser (com.haulmont.cuba.gui.components.filter.FilterParser)1 FilterEditor (com.haulmont.cuba.gui.components.filter.edit.FilterEditor)1 WindowInfo (com.haulmont.cuba.gui.config.WindowInfo)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 FilterEntity (com.haulmont.cuba.security.entity.FilterEntity)1 Arrays.asList (java.util.Arrays.asList)1 StrBuilder (org.apache.commons.lang.text.StrBuilder)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 Element (org.dom4j.Element)1