Search in sources :

Example 1 with ConditionParamBuilder

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

the class DynamicAttributesConditionFrame method commit.

@Override
public boolean commit() {
    if (!super.commit())
        return false;
    String error = checkCondition();
    if (error != null) {
        showNotification(messages.getMainMessage(error), Frame.NotificationType.TRAY);
        return false;
    }
    CategoryAttribute attribute = attributeLookup.getValue();
    String alias = condition.getEntityAlias();
    String cavAlias = "cav" + RandomStringUtils.randomNumeric(5);
    String paramName;
    String operation = operationLookup.<Op>getValue().forJpql();
    Op op = operationLookup.getValue();
    Class javaClass = DynamicAttributesUtils.getAttributeClass(attribute);
    String propertyPath = Strings.isNullOrEmpty(condition.getPropertyPath()) ? "" : "." + condition.getPropertyPath();
    ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
    paramName = paramBuilder.createParamName(condition);
    String cavEntityId = referenceToEntitySupport.getReferenceIdPropertyName(condition.getDatasource().getMetaClass());
    String where;
    if (op == Op.NOT_EMPTY) {
        where = "(exists (select " + cavAlias + " from sys$CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'))";
    } else {
        String valueFieldName = "stringValue";
        if (Entity.class.isAssignableFrom(javaClass))
            valueFieldName = "entityValue." + referenceToEntitySupport.getReferenceIdPropertyName(metadata.getClassNN(javaClass));
        else if (String.class.isAssignableFrom(javaClass))
            valueFieldName = "stringValue";
        else if (Integer.class.isAssignableFrom(javaClass))
            valueFieldName = "intValue";
        else if (Double.class.isAssignableFrom(javaClass))
            valueFieldName = "doubleValue";
        else if (Boolean.class.isAssignableFrom(javaClass))
            valueFieldName = "booleanValue";
        else if (Date.class.isAssignableFrom(javaClass))
            valueFieldName = "dateValue";
        if (!attribute.getIsCollection()) {
            condition.setJoin(", sys$CategoryAttributeValue " + cavAlias + " ");
            String paramStr = " ? ";
            where = cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " " + operation + (op.isUnary() ? " " : paramStr) + "and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'";
            where = where.replace("?", ":" + paramName);
        } else {
            where = "(exists (select " + cavAlias + " from sys$CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " = :" + paramName + " and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'))";
        }
    }
    condition.setWhere(where);
    condition.setUnary(op.isUnary());
    condition.setEntityParamView(null);
    condition.setEntityParamWhere(null);
    condition.setInExpr(Op.IN.equals(op) || Op.NOT_IN.equals(op));
    condition.setOperator(operationLookup.<Op>getValue());
    Class paramJavaClass = op.isUnary() ? Boolean.class : javaClass;
    condition.setJavaClass(javaClass);
    Param param = Param.Builder.getInstance().setName(paramName).setJavaClass(paramJavaClass).setDataSource(condition.getDatasource()).setProperty(DynamicAttributesUtils.getMetaPropertyPath(null, attribute).getMetaProperty()).setInExpr(condition.getInExpr()).setRequired(condition.getRequired()).setCategoryAttrId(attribute.getId()).build();
    Object defaultValue = condition.getParam().getDefaultValue();
    param.setDefaultValue(defaultValue);
    condition.setParam(param);
    condition.setCategoryId(categoryLookup.<Category>getValue().getId());
    condition.setCategoryAttributeId(attributeLookup.<CategoryAttribute>getValue().getId());
    condition.setIsCollection(BooleanUtils.isTrue(attributeLookup.<CategoryAttribute>getValue().getIsCollection()));
    condition.setLocCaption(attribute.getLocaleName());
    condition.setCaption(caption.getValue());
    return true;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Category(com.haulmont.cuba.core.entity.Category) Param(com.haulmont.cuba.gui.components.filter.Param) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 2 with ConditionParamBuilder

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

the class AbstractCondition method resolveParam.

protected void resolveParam(Element element) {
    Scripting scripting = AppBeans.get(Scripting.NAME);
    String aclass = element.attributeValue("class");
    if (!isBlank(aclass)) {
        javaClass = scripting.loadClass(aclass);
    }
    String operatorName = element.attributeValue("operatorType", null);
    if (operatorName != null) {
        operator = Op.valueOf(operatorName);
    }
    List<Element> paramElements = Dom4j.elements(element, "param");
    if (!paramElements.isEmpty()) {
        Element paramElem = paramElements.iterator().next();
        if (BooleanUtils.toBoolean(paramElem.attributeValue("hidden", "false"), "true", "false")) {
            paramElem = paramElements.iterator().next();
        }
        paramName = paramElem.attributeValue("name");
        if (!isBlank(paramElem.attributeValue("javaClass"))) {
            paramClass = scripting.loadClass(paramElem.attributeValue("javaClass"));
        }
        ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
        if (Strings.isNullOrEmpty(paramName)) {
            paramName = paramBuilder.createParamName(this);
        }
        param = paramBuilder.createParam(this);
        param.setDateInterval(BooleanUtils.toBoolean(paramElem.attributeValue("isDateInterval", "false"), "true", "false"));
        param.parseValue(paramElem.getText());
        param.setDefaultValue(param.getValue());
    }
    if ("EMPTY".equals(operatorName)) {
        // for backward compatibility with old filters that still use EMPTY operator
        operatorName = "NOT_EMPTY";
        if (BooleanUtils.isTrue((Boolean) param.getValue()))
            param.setValue(false);
        param.setDefaultValue(false);
        operator = Op.valueOf(operatorName);
    }
}
Also used : ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) Element(org.dom4j.Element) Scripting(com.haulmont.cuba.core.global.Scripting)

Example 3 with ConditionParamBuilder

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

the class DynamicAttributesCondition method setOperator.

@Override
public void setOperator(Op operator) {
    if (!Objects.equals(this.operator, operator)) {
        this.operator = operator;
        String paramName = param.getName();
        ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
        if (operator.isUnary()) {
            unary = true;
            inExpr = false;
            Param param = Param.Builder.getInstance().setName(paramName).setJavaClass(Boolean.class).setInExpr(false).setRequired(required).build();
            setParam(param);
        } else {
            unary = false;
            inExpr = operator.equals(Op.IN) || operator.equals(Op.NOT_IN);
            Param param = paramBuilder.createParam(this);
            setParam(param);
        }
    }
}
Also used : ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) Param(com.haulmont.cuba.gui.components.filter.Param)

Example 4 with ConditionParamBuilder

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

the class PropertyCondition method setOperator.

@Override
public void setOperator(Op operator) {
    if (!Objects.equals(this.operator, operator)) {
        this.operator = operator;
        String paramName = param.getName();
        ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
        if (operator.isUnary()) {
            unary = true;
            inExpr = false;
            Param param = Param.Builder.getInstance().setName(paramName).setJavaClass(Boolean.class).setInExpr(false).setRequired(required).build();
            setParam(param);
        } else {
            unary = false;
            inExpr = operator.equals(Op.IN) || operator.equals(Op.NOT_IN);
            Param param = paramBuilder.createParam(this);
            if (operator == Op.DATE_INTERVAL) {
                // each parameter value change must modify the condition text
                param.addValueChangeListener((prevValue, value) -> updateText());
            }
            setParam(param);
        }
    }
}
Also used : ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) Param(com.haulmont.cuba.gui.components.filter.Param)

Example 5 with ConditionParamBuilder

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

the class FilteringLookupAction method createCustomCondition.

protected CustomCondition createCustomCondition(Filter filterComponent) {
    CustomCondition condition = new CustomCondition(createConditionXmlElement(), AppConfig.getMessagesPack(), getFilterComponentName(filterComponent), filterComponent.getDatasource());
    condition.setUnary(true);
    condition.setHidden(true);
    condition.setWhere(whereClause.replaceAll("\\?", ":" + condition.getParamName()));
    condition.setJoin(joinClause);
    ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
    Param param = Param.Builder.getInstance().setName(paramBuilder.createParamName(condition)).setJavaClass(Boolean.class).setEntityWhere("").setEntityView("").setDataSource(filterComponent.getDatasource()).setInExpr(true).setRequired(true).build();
    param.setValue(true);
    condition.setParam(param);
    return condition;
}
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)

Aggregations

ConditionParamBuilder (com.haulmont.cuba.gui.components.filter.ConditionParamBuilder)5 Param (com.haulmont.cuba.gui.components.filter.Param)4 MetaClass (com.haulmont.chile.core.model.MetaClass)1 Category (com.haulmont.cuba.core.entity.Category)1 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 Scripting (com.haulmont.cuba.core.global.Scripting)1 Op (com.haulmont.cuba.core.global.filter.Op)1 CustomCondition (com.haulmont.cuba.gui.components.filter.condition.CustomCondition)1 Element (org.dom4j.Element)1