Search in sources :

Example 1 with FilterParser

use of com.haulmont.cuba.gui.components.filter.FilterParser in project cuba by cuba-platform.

the class AttributeEditor method openConstraintWizard.

public void openConstraintWizard() {
    CategoryAttribute attribute = getItem();
    Class entityClass = attribute.getJavaClassForEntity();
    if (entityClass == null) {
        showNotification(getMessage("selectEntityType"));
        return;
    }
    MetaClass metaClass = metadata.getClassNN(entityClass);
    FakeFilterSupport filterSupport = new FakeFilterSupport(this, metaClass);
    Filter fakeFilter = filterSupport.createFakeFilter();
    FilterEntity filterEntity = filterSupport.createFakeFilterEntity(attribute.getFilterXml());
    ConditionsTree conditionsTree = filterSupport.createFakeConditionsTree(fakeFilter, filterEntity);
    Map<String, Object> params = new HashMap<>();
    params.put("filter", fakeFilter);
    params.put("filterEntity", filterEntity);
    params.put("conditionsTree", conditionsTree);
    params.put("useShortConditionForm", true);
    FilterEditor filterEditor = (FilterEditor) openWindow("filterEditor", OpenType.DIALOG, params);
    filterEditor.addCloseListener(actionId -> {
        if (!COMMIT_ACTION_ID.equals(actionId)) {
            return;
        }
        filterEntity.setXml(filterParser.getXml(filterEditor.getConditions(), Param.ValueProperty.DEFAULT_VALUE));
        if (filterEntity.getXml() != null) {
            Element element = dom4JTools.readDocument(filterEntity.getXml()).getRootElement();
            com.haulmont.cuba.core.global.filter.FilterParser filterParser = new com.haulmont.cuba.core.global.filter.FilterParser(element);
            String jpql = new SecurityJpqlGenerator().generateJpql(filterParser.getRoot());
            attribute.setWhereClause(jpql);
            Set<String> joins = filterParser.getRoot().getJoins();
            if (!joins.isEmpty()) {
                String joinsStr = new TextStringBuilder().appendWithSeparators(joins, " ").toString();
                attribute.setJoinClause(joinsStr);
            }
            attribute.setFilterXml(filterEntity.getXml());
        }
    });
}
Also used : FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) Element(org.dom4j.Element) FilterEditor(com.haulmont.cuba.gui.components.filter.edit.FilterEditor) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) TextStringBuilder(org.apache.commons.text.TextStringBuilder) FakeFilterSupport(com.haulmont.cuba.gui.components.filter.FakeFilterSupport) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) FilterParser(com.haulmont.cuba.gui.components.filter.FilterParser) SecurityJpqlGenerator(com.haulmont.cuba.core.global.filter.SecurityJpqlGenerator) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree)

Example 2 with FilterParser

use of com.haulmont.cuba.gui.components.filter.FilterParser 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.focus();
        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("conditionsTree", conditionsTree);
    params.put("useShortConditionForm", true);
    params.put("hideDynamicAttributes", constraint.getCheckType() != ConstraintCheckType.DATABASE);
    params.put("hideCustomConditions", constraint.getCheckType() != ConstraintCheckType.DATABASE);
    FilterEditor filterEditor = (FilterEditor) getWindowManager().openWindow(windowInfo, 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 = dom4JTools.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 TextStringBuilder().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) TextStringBuilder(org.apache.commons.text.TextStringBuilder) 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)

Aggregations

com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 SecurityJpqlGenerator (com.haulmont.cuba.core.global.filter.SecurityJpqlGenerator)2 ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)2 FakeFilterSupport (com.haulmont.cuba.gui.components.filter.FakeFilterSupport)2 FilterParser (com.haulmont.cuba.gui.components.filter.FilterParser)2 FilterEditor (com.haulmont.cuba.gui.components.filter.edit.FilterEditor)2 FilterEntity (com.haulmont.cuba.security.entity.FilterEntity)2 TextStringBuilder (org.apache.commons.text.TextStringBuilder)2 Element (org.dom4j.Element)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 GroovyGenerator (com.haulmont.cuba.core.global.filter.GroovyGenerator)1 WindowInfo (com.haulmont.cuba.gui.config.WindowInfo)1 Constraint (com.haulmont.cuba.security.entity.Constraint)1