Search in sources :

Example 1 with CustomCondition

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

the class RelatedEntitiesBean method getManyToOneCondition.

@Nullable
protected AbstractCondition getManyToOneCondition(List<Object> parentIds, CollectionDatasource datasource, String filterComponentName, String relatedPrimaryKey, MetaDataDescriptor descriptor) {
    MetaClass metaClass = descriptor.getMetaClass();
    String parentPrimaryKey = metadataTools.getPrimaryKeyName(metaClass);
    CustomCondition customCondition = getParentEntitiesCondition(parentIds, parentPrimaryKey, datasource, filterComponentName, metaClass);
    String entityAlias = RandomStringUtils.randomAlphabetic(6);
    String subQuery = String.format("select %s.%s.%s from %s %s where %s.%s in :%s", entityAlias, descriptor.getMetaProperty().getName(), relatedPrimaryKey, metaClass.getName(), entityAlias, entityAlias, parentPrimaryKey, customCondition.getParam().getName());
    String whereString = String.format("{E}.%s in (%s)", relatedPrimaryKey, subQuery);
    customCondition.setWhere(whereString);
    return customCondition;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) CustomCondition(com.haulmont.cuba.gui.components.filter.condition.CustomCondition) Nullable(javax.annotation.Nullable)

Example 2 with CustomCondition

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

the class ParamWrapper method getValue.

@Override
public Object getValue() {
    Object value = param.getValue();
    if (value instanceof String && !StringUtils.isEmpty((String) value) && !((String) value).startsWith(ParametersHelper.CASE_INSENSITIVE_MARKER)) {
        // try to wrap value for case-insensitive "like" search
        if (condition instanceof PropertyCondition || condition instanceof DynamicAttributesCondition) {
            String escapedValue = value.toString();
            if (condition.getEntityMetaClass() != null) {
                String thisStore = AppBeans.get(MetadataTools.class).getStoreName(condition.getEntityMetaClass());
                GlobalConfig config = AppBeans.get(Configuration.class).getConfig(GlobalConfig.class);
                if (config.getDisableEscapingLikeForDataStores() == null || !config.getDisableEscapingLikeForDataStores().contains(thisStore)) {
                    escapedValue = QueryUtils.escapeForLike(escapedValue);
                }
            } else {
                escapedValue = QueryUtils.escapeForLike(escapedValue);
            }
            Op op = condition.getOperator();
            if (Op.CONTAINS.equals(op) || op.equals(Op.DOES_NOT_CONTAIN)) {
                value = wrapValueForLike(escapedValue);
            } else if (Op.STARTS_WITH.equals(op)) {
                value = wrapValueForLike(escapedValue, false, true);
            } else if (Op.ENDS_WITH.equals(op)) {
                value = wrapValueForLike(escapedValue, true, false);
            }
        } else if (condition instanceof CustomCondition) {
            String where = ((CustomCondition) condition).getWhere();
            Op op = condition.getOperator();
            Matcher matcher = LIKE_PATTERN.matcher(where);
            if (matcher.find()) {
                String stringValue = value.toString();
                boolean escape = StringUtils.isNotEmpty(matcher.group(3));
                if (escape) {
                    String escapeChar = matcher.group(4);
                    if (StringUtils.isNotEmpty(escapeChar)) {
                        stringValue = QueryUtils.escapeForLike(stringValue, escapeChar);
                    }
                }
                if (Op.STARTS_WITH.equals(op)) {
                    value = wrapValueForLike(stringValue, false, true);
                } else if (Op.ENDS_WITH.equals(op)) {
                    value = wrapValueForLike(stringValue, true, false);
                } else {
                    value = wrapValueForLike(stringValue);
                }
            }
        }
    } else if (value instanceof Entity) {
        value = ((Entity) value).getId();
    } else if (value instanceof Collection) {
        List<Object> list = new ArrayList<>(((Collection) value).size());
        for (Object obj : ((Collection) value)) {
            list.add(obj instanceof Entity ? ((Entity) obj).getId() : obj);
        }
        value = list;
    } else if (value instanceof EnumClass) {
        value = ((EnumClass) value).getId();
    }
    return value;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) Entity(com.haulmont.cuba.core.entity.Entity) CustomCondition(com.haulmont.cuba.gui.components.filter.condition.CustomCondition) Matcher(java.util.regex.Matcher) DynamicAttributesCondition(com.haulmont.cuba.gui.components.filter.condition.DynamicAttributesCondition) ArrayList(java.util.ArrayList) PropertyCondition(com.haulmont.cuba.gui.components.filter.condition.PropertyCondition) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) Collection(java.util.Collection)

Example 3 with CustomCondition

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

the class CustomConditionCreator method createCondition.

@Override
public AbstractCondition createCondition() {
    CustomCondition customCondition = new CustomCondition(this, null, null, entityAlias, false);
    // default editor - text
    customCondition.setJavaClass(String.class);
    Param param = AppBeans.get(ConditionParamBuilder.class).createParam(customCondition);
    customCondition.setParam(param);
    return customCondition;
}
Also used : ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) CustomCondition(com.haulmont.cuba.gui.components.filter.condition.CustomCondition) Param(com.haulmont.cuba.gui.components.filter.Param)

Example 4 with CustomCondition

use of com.haulmont.cuba.gui.components.filter.condition.CustomCondition 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 5 with CustomCondition

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

the class RelatedEntitiesBean method getParentEntitiesCondition.

protected CustomCondition getParentEntitiesCondition(List<Object> parentIds, String parentPrimaryKey, MetaClass metaClass, String filterComponentName, MetaClass parentMetaClass) {
    String conditionName = String.format("related_%s", RandomStringUtils.randomAlphabetic(6));
    CustomCondition condition = new CustomCondition(getConditionXmlElement(conditionName, parentMetaClass), AppConfig.getMessagesPack(), filterComponentName, metaClass);
    Class<?> parentPrimaryKeyClass = parentMetaClass.getPropertyNN(parentPrimaryKey).getJavaType();
    condition.setJavaClass(parentPrimaryKeyClass);
    condition.setHidden(true);
    condition.setInExpr(true);
    int randInt = new Random().nextInt((99999 - 11111) + 1) + 11111;
    String paramName = String.format("component$%s.%s%s", filterComponentName, conditionName, randInt);
    condition.setParam(getParentEntitiesParam(parentIds, parentPrimaryKey, metaClass, parentPrimaryKeyClass, paramName, parentMetaClass));
    return condition;
}
Also used : CustomCondition(com.haulmont.cuba.gui.components.filter.condition.CustomCondition)

Aggregations

CustomCondition (com.haulmont.cuba.gui.components.filter.condition.CustomCondition)12 MetaClass (com.haulmont.chile.core.model.MetaClass)6 Nullable (javax.annotation.Nullable)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 ConditionParamBuilder (com.haulmont.cuba.gui.components.filter.ConditionParamBuilder)2 Param (com.haulmont.cuba.gui.components.filter.Param)2 Node (com.haulmont.bali.datastruct.Node)1 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Op (com.haulmont.cuba.core.global.filter.Op)1 ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)1 FilterParser (com.haulmont.cuba.gui.components.filter.FilterParser)1 DynamicAttributesCondition (com.haulmont.cuba.gui.components.filter.condition.DynamicAttributesCondition)1 PropertyCondition (com.haulmont.cuba.gui.components.filter.condition.PropertyCondition)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Matcher (java.util.regex.Matcher)1