Search in sources :

Example 1 with ConditionsTree

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

the class FilterEditor method init.

@Override
public void init(Map<String, Object> params) {
    super.init(params);
    if (Boolean.TRUE.equals(useShortConditionForm)) {
        setCaption(messages.getMainMessage("filter.editor.captionShortForm"));
    }
    getDialogOptions().setWidth(theme.get("cuba.gui.filterEditor.dialog.width")).setHeight(theme.get("cuba.gui.filterEditor.dialog.height")).setResizable(true);
    filterEntity = (FilterEntity) params.get("filterEntity");
    if (filterEntity == null) {
        throw new RuntimeException("Filter entity was not passed to filter editor");
    }
    filter = (Filter) params.get("filter");
    ConditionsTree paramConditions = (ConditionsTree) params.get("conditions");
    conditions = paramConditions.createCopy();
    refreshConditionsDs();
    conditionsTree.expandTree();
    if (!messages.getMainMessage("filter.adHocFilter").equals(filterEntity.getName())) {
        filterName.setValue(filterEntity.getName());
    }
    availableForAllCb.setValue(filterEntity.getUser() == null);
    defaultCb.setValue(filterEntity.getIsDefault());
    applyDefaultCb.setValue(filterEntity.getApplyDefault());
    globalDefaultCb.setValue(filterEntity.getGlobalDefault());
    if (filterEntity.getUser() != null) {
        globalDefaultCb.setEnabled(false);
    }
    if (!userSessionSource.getUserSession().isSpecificPermitted(GLOBAL_FILTER_PERMISSION)) {
        availableForAllCb.setVisible(false);
        availableForAllLabel.setVisible(false);
        globalDefaultCb.setVisible(false);
        globalDefaultLabel.setVisible(false);
    }
    availableForAllCb.addValueChangeListener(e -> {
        Boolean isFilterAvailableForAll = (Boolean) e.getValue();
        globalDefaultCb.setEnabled(isFilterAvailableForAll);
        if (!isFilterAvailableForAll) {
            globalDefaultCb.setValue(null);
        }
    });
    Configuration configuration = AppBeans.get(Configuration.NAME);
    boolean manualApplyRequired = filter.getManualApplyRequired() != null ? filter.getManualApplyRequired() : configuration.getConfig(ClientConfig.class).getGenericFilterManualApplyRequired();
    if (!manualApplyRequired) {
        applyDefaultCb.setVisible(manualApplyRequired);
        applyDefaultLabel.setVisible(manualApplyRequired);
    }
    if (filterEntity.getFolder() != null) {
        defaultCb.setVisible(false);
        defaultLabel.setVisible(false);
    }
    conditionsDs.addItemChangeListener(e -> {
        if (!treeItemChangeListenerEnabled) {
            return;
        }
        // commit previously selected condition
        if (activeConditionFrame != null) {
            List<Validatable> validatables = new ArrayList<>();
            Collection<Component> frameComponents = ComponentsHelper.getComponents(activeConditionFrame);
            for (Component frameComponent : frameComponents) {
                if (frameComponent instanceof Validatable) {
                    validatables.add((Validatable) frameComponent);
                }
            }
            if (validate(validatables)) {
                activeConditionFrame.commit();
            } else {
                treeItemChangeListenerEnabled = false;
                conditionsTree.setSelected(e.getPrevItem());
                treeItemChangeListenerEnabled = true;
                return;
            }
        }
        if (e.getItem() == null) {
            activeConditionFrame = null;
        } else {
            if (e.getItem() instanceof PropertyCondition) {
                activeConditionFrame = propertyConditionFrame;
            } else if (e.getItem() instanceof DynamicAttributesCondition) {
                activeConditionFrame = dynamicAttributesConditionFrame;
            } else if (e.getItem() instanceof CustomCondition) {
                activeConditionFrame = customConditionFrame;
            } else if (e.getItem() instanceof GroupCondition) {
                activeConditionFrame = groupConditionFrame;
            } else if (e.getItem() instanceof FtsCondition) {
                activeConditionFrame = ftsConditionFrame;
            } else {
                log.warn("Conditions frame for condition with type " + e.getItem().getClass().getSimpleName() + " not found");
            }
        }
        propertyConditionFrame.setVisible(false);
        customConditionFrame.setVisible(false);
        dynamicAttributesConditionFrame.setVisible(false);
        groupConditionFrame.setVisible(false);
        ftsConditionFrame.setVisible(false);
        if (activeConditionFrame != null) {
            activeConditionFrame.setVisible(true);
            activeConditionFrame.setCondition(e.getItem());
            if (Boolean.TRUE.equals(useShortConditionForm)) {
                for (String componentName : componentsToHideInShortForm) {
                    Component component = activeConditionFrame.getComponent(componentName);
                    if (component != null) {
                        if (BooleanUtils.isTrue(showConditionHiddenOption) && componentsForHiddenOption.contains(componentName)) {
                            continue;
                        }
                        component.setVisible(false);
                    }
                }
            }
        }
    });
    addConditionHelper = new AddConditionHelper(filter, BooleanUtils.isTrue(hideDynamicAttributes), BooleanUtils.isTrue(hideCustomConditions), condition -> {
        AbstractCondition item = conditionsDs.getItem();
        if (item != null && item instanceof GroupCondition) {
            Node<AbstractCondition> newNode = new Node<>(condition);
            Node<AbstractCondition> selectedNode = conditions.getNode(item);
            selectedNode.addChild(newNode);
            refreshConditionsDs();
            conditionsTree.expand(item.getId());
        } else {
            conditions.getRootNodes().add(new Node<>(condition));
            refreshConditionsDs();
        }
        conditionsTree.setSelected(condition);
    });
    FilterHelper filterHelper = AppBeans.get(FilterHelper.class);
    filterHelper.initConditionsDragAndDrop(conditionsTree, conditions);
    if (Boolean.TRUE.equals(useShortConditionForm)) {
        filterPropertiesGrid.setVisible(false);
    }
}
Also used : AddConditionHelper(com.haulmont.cuba.gui.components.filter.AddConditionHelper) java.util(java.util) WindowManager(com.haulmont.cuba.gui.WindowManager) Logger(org.slf4j.Logger) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) GroupConditionDescriptor(com.haulmont.cuba.gui.components.filter.descriptor.GroupConditionDescriptor) GroupType(com.haulmont.cuba.gui.components.filter.GroupType) BooleanUtils(org.apache.commons.lang.BooleanUtils) Inject(javax.inject.Inject) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) FilterHelper(com.haulmont.cuba.gui.components.filter.FilterHelper) com.haulmont.cuba.gui.components.filter.condition(com.haulmont.cuba.gui.components.filter.condition) WindowParam(com.haulmont.cuba.gui.WindowParam) Node(com.haulmont.bali.datastruct.Node) ThemeConstants(com.haulmont.cuba.gui.theme.ThemeConstants) ClientConfig(com.haulmont.cuba.client.ClientConfig) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) Entity(com.haulmont.cuba.core.entity.Entity) Node(com.haulmont.bali.datastruct.Node) AddConditionHelper(com.haulmont.cuba.gui.components.filter.AddConditionHelper) FilterHelper(com.haulmont.cuba.gui.components.filter.FilterHelper) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree)

Example 2 with ConditionsTree

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

the class FilteringLookupAction method createFilterXml.

protected String createFilterXml(Filter filterComponent) {
    ConditionsTree tree = new ConditionsTree();
    CustomCondition condition = createCustomCondition(filterComponent);
    tree.setRootNodes(Collections.singletonList(new Node<>(condition)));
    return AppBeans.get(FilterParser.class).getXml(tree, Param.ValueProperty.VALUE);
}
Also used : CustomCondition(com.haulmont.cuba.gui.components.filter.condition.CustomCondition) Node(com.haulmont.bali.datastruct.Node) FilterParser(com.haulmont.cuba.gui.components.filter.FilterParser) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree)

Example 3 with ConditionsTree

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

the class AttributeEditor method openConstraintWizard.

public void openConstraintWizard() {
    Class entityClass = attribute.getJavaClassForEntity();
    if (entityClass == null) {
        showNotification(getMessage("selectEntityType"));
        return;
    }
    MetaClass metaClass = metadata.getClassNN(entityClass);
    FakeFilterSupport fakeFilterSupport = new FakeFilterSupport(this, metaClass);
    final Filter fakeFilter = fakeFilterSupport.createFakeFilter();
    final FilterEntity filterEntity = fakeFilterSupport.createFakeFilterEntity(attribute.getFilterXml());
    final ConditionsTree conditionsTree = fakeFilterSupport.createFakeConditionsTree(fakeFilter, filterEntity);
    Map<String, Object> params = new HashMap<>();
    params.put("filter", fakeFilter);
    params.put("filterEntity", filterEntity);
    params.put("conditions", conditionsTree);
    params.put("useShortConditionForm", true);
    FilterEditor filterEditor = (FilterEditor) openWindow("filterEditor", WindowManager.OpenType.DIALOG, params);
    filterEditor.addCloseListener(actionId -> {
        if (!COMMIT_ACTION_ID.equals(actionId))
            return;
        FilterParser filterParser1 = AppBeans.get(FilterParser.class);
        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);
            String jpql = new SecurityJpqlGenerator().generateJpql(filterParser.getRoot());
            attribute.setWhereClause(jpql);
            Set<String> joins = filterParser.getRoot().getJoins();
            if (!joins.isEmpty()) {
                String joinsStr = new StrBuilder().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) 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) StrBuilder(org.apache.commons.lang.text.StrBuilder)

Example 4 with ConditionsTree

use of com.haulmont.cuba.gui.components.filter.ConditionsTree 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)

Example 5 with ConditionsTree

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

the class RelatedEntitiesBean method getRelatedEntitiesFilterXml.

protected String getRelatedEntitiesFilterXml(MetaClass relatedMetaCLass, Collection<? extends Entity> selectedEntities, Filter component, MetaDataDescriptor descriptor) {
    ConditionsTree tree = new ConditionsTree();
    String filterComponentPath = ComponentsHelper.getFilterComponentPath(component);
    String[] strings = ValuePathHelper.parse(filterComponentPath);
    String filterComponentName = ValuePathHelper.format(Arrays.copyOfRange(strings, 1, strings.length));
    String relatedPrimaryKey = metadataTools.getPrimaryKeyName(relatedMetaCLass);
    AbstractCondition condition = getOptimizedCondition(getParentIds(selectedEntities), component.getDatasource(), filterComponentName, relatedPrimaryKey, descriptor);
    if (condition == null) {
        condition = getNonOptimizedCondition(relatedMetaCLass, getRelatedIds(selectedEntities, descriptor), component, filterComponentName, relatedPrimaryKey);
    }
    tree.setRootNodes(Collections.singletonList(new Node<>(condition)));
    return filterParser.getXml(tree, Param.ValueProperty.VALUE);
}
Also used : Node(com.haulmont.bali.datastruct.Node) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree)

Aggregations

ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)6 Node (com.haulmont.bali.datastruct.Node)4 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)3 FilterParser (com.haulmont.cuba.gui.components.filter.FilterParser)3 FilterEntity (com.haulmont.cuba.security.entity.FilterEntity)3 SecurityJpqlGenerator (com.haulmont.cuba.core.global.filter.SecurityJpqlGenerator)2 FakeFilterSupport (com.haulmont.cuba.gui.components.filter.FakeFilterSupport)2 AbstractCondition (com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)2 FilterEditor (com.haulmont.cuba.gui.components.filter.edit.FilterEditor)2 StrBuilder (org.apache.commons.lang.text.StrBuilder)2 Element (org.dom4j.Element)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 Entity (com.haulmont.cuba.core.entity.Entity)1 GroovyGenerator (com.haulmont.cuba.core.global.filter.GroovyGenerator)1 ComponentsHelper (com.haulmont.cuba.gui.ComponentsHelper)1 WindowManager (com.haulmont.cuba.gui.WindowManager)1 WindowParam (com.haulmont.cuba.gui.WindowParam)1 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)1 AddConditionHelper (com.haulmont.cuba.gui.components.filter.AddConditionHelper)1