Search in sources :

Example 1 with PropertyCondition

use of io.jmix.core.querycondition.PropertyCondition in project jmix by jmix-framework.

the class DynAttrPropertyConditionGenerator method generateWhere.

@Override
public String generateWhere(ConditionGenerationContext context) {
    PropertyCondition condition = (PropertyCondition) context.getCondition();
    if (condition == null) {
        return "";
    }
    String[] properties = condition.getProperty().split("\\.");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < properties.length; i++) {
        String property = properties[i];
        if (DynAttrUtils.isDynamicAttributeProperty(property)) {
            String propertyPath = sb.toString();
            String dynAttrPropertyPath = i == 0 ? property : propertyPath.substring(1) + "." + property;
            String entityName = context.getEntityName();
            MetaClass entityMetaClass = metadata.findClass(entityName);
            if (entityMetaClass != null) {
                MetaPropertyPath mpp = metadataTools.resolveMetaPropertyPathOrNull(entityMetaClass, dynAttrPropertyPath);
                if (mpp != null && mpp.getMetaProperty() instanceof DynAttrMetaProperty) {
                    return generateWhere(propertyPath, context, (DynAttrMetaProperty) mpp.getMetaProperty());
                }
            }
        }
        sb.append(".");
        sb.append(property);
    }
    return "";
}
Also used : PropertyCondition(io.jmix.core.querycondition.PropertyCondition) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath)

Example 2 with PropertyCondition

use of io.jmix.core.querycondition.PropertyCondition in project jmix by jmix-framework.

the class DynAttrPropertyConditionGenerator method generateWhere.

protected String generateWhere(String entityPropertyPath, ConditionGenerationContext context, DynAttrMetaProperty metaProperty) {
    PropertyCondition condition = (PropertyCondition) context.getCondition();
    if (condition == null) {
        return "";
    }
    String cavEntityId = referenceToEntitySupport.getReferenceIdPropertyName(metaProperty.getDomain());
    String cavAlias = "cav" + RandomStringUtils.randomAlphabetic(5);
    String parameterName = condition.getParameterName();
    String valueFieldName = getValueFieldName(metaProperty);
    String entityAlias = context.getEntityAlias();
    String attributeId = dynAttrMetadata.getAttributeByCode(metaProperty.getDomain(), DynAttrUtils.getAttributeCodeFromProperty(metaProperty.getName())).map(AttributeDefinition::getId).orElse("");
    String operation = PropertyConditionUtils.getJpqlOperation(condition);
    if (!PropertyConditionUtils.isUnaryOperation(condition) && !PropertyConditionUtils.isInIntervalOperation(condition)) {
        operation = operation + " :" + parameterName;
    }
    String formattedOperation = cavAlias + "." + valueFieldName + " " + operation;
    if (PropertyConditionUtils.isInIntervalOperation(condition)) {
        String lastSymbol = operation.contains("@between") ? "," : " ";
        formattedOperation = new StringBuilder(operation).replace(operation.indexOf("{"), operation.indexOf(lastSymbol), cavAlias + "." + valueFieldName).toString();
    }
    return "(exists (select " + cavAlias + " from dynat_CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + entityAlias + entityPropertyPath + ".id and " + formattedOperation + " and " + cavAlias + ".categoryAttribute.id='" + attributeId + "'))";
}
Also used : PropertyCondition(io.jmix.core.querycondition.PropertyCondition)

Example 3 with PropertyCondition

use of io.jmix.core.querycondition.PropertyCondition in project jmix by jmix-framework.

the class KeyValuePropertyConditionGenerator method generateWhere.

@Override
public String generateWhere(ConditionGenerationContext context) {
    PropertyCondition propertyCondition = (PropertyCondition) context.getCondition();
    if (propertyCondition == null) {
        return "";
    }
    List<String> valueProperties = context.getValueProperties();
    List<String> selectedExpressions = context.getSelectedExpressions();
    if (valueProperties == null || selectedExpressions == null) {
        return "";
    }
    String entityAlias = propertyCondition.getProperty();
    String property = null;
    if (entityAlias.contains(".")) {
        int indexOfDot = entityAlias.indexOf(".");
        property = entityAlias.substring(indexOfDot + 1);
        entityAlias = entityAlias.substring(0, indexOfDot);
    }
    int index = valueProperties.indexOf(entityAlias);
    if (index >= 0 && index < selectedExpressions.size()) {
        entityAlias = selectedExpressions.get(index);
    }
    if (property != null) {
        return generateWhere(propertyCondition, entityAlias, property);
    } else {
        return generateKeyValueWhere(propertyCondition, entityAlias);
    }
}
Also used : PropertyCondition(io.jmix.core.querycondition.PropertyCondition)

Example 4 with PropertyCondition

use of io.jmix.core.querycondition.PropertyCondition in project jmix by jmix-framework.

the class ParameterJpqlGenerator method processParameters.

/**
 * Returns parameters for JPQL query modified according to the given tree of conditions.
 *
 * @param parameters      result parameters
 * @param queryParameters query parameters
 * @param actualized      an actualized condition
 * @return modified parameters
 */
public Map<String, Object> processParameters(Map<String, Object> parameters, Map<String, Object> queryParameters, Condition actualized, @Nullable String entityName) {
    List<PropertyCondition> propertyConditions = collectNestedPropertyConditions(actualized);
    for (PropertyCondition propertyCondition : propertyConditions) {
        String parameterName = propertyCondition.getParameterName();
        if (PropertyConditionUtils.isUnaryOperation(propertyCondition) || PropertyConditionUtils.isInIntervalOperation(propertyCondition)) {
            // remove query parameter for unary operations (e.g. IS_NULL) and "in interval" operations
            parameters.remove(parameterName);
        } else {
            // PropertyCondition may take a value from queryParameters collection or from the
            // PropertyCondition.parameterValue attribute. queryParameters has higher priority.
            Object parameterValue;
            if (!queryParameters.containsKey(parameterName) || queryParameters.get(parameterName) == null) {
                parameterValue = generateParameterValue(propertyCondition, propertyCondition.getParameterValue(), entityName);
            } else {
                // modify the query parameter value (e.g. wrap value for "contains" jpql operation)
                Object queryParameterValue = queryParameters.get(parameterName);
                parameterValue = generateParameterValue(propertyCondition, queryParameterValue, entityName);
            }
            parameters.put(parameterName, parameterValue);
        }
    }
    List<JpqlCondition> jpqlConditions = collectNestedJpqlConditions(actualized);
    for (JpqlCondition jpqlCondition : jpqlConditions) {
        for (Map.Entry<String, Object> parameter : jpqlCondition.getParameterValuesMap().entrySet()) {
            // JpqlCondition may take a value from queryParameters collection or from the
            // JpqlCondition.parameterValuesMap attribute. queryParameters value has higher priority.
            Object parameterValue;
            String parameterName = parameter.getKey();
            if (!queryParameters.containsKey(parameterName) || queryParameters.get(parameterName) == null) {
                // Modify the query parameter value (e.g. wrap value from JpqlFilter for "contains"
                // jpql operation)
                parameterValue = generateParameterValue(jpqlCondition, parameter.getValue(), entityName);
            } else {
                // In other cases, it is assumed that the value has already been modified
                // (e.g. wrapped value from DataLoadCoordinator)
                parameterValue = queryParameters.get(parameter.getKey());
            }
            parameters.put(parameter.getKey(), parameterValue);
        }
    }
    return parameters;
}
Also used : PropertyCondition(io.jmix.core.querycondition.PropertyCondition) JpqlCondition(io.jmix.core.querycondition.JpqlCondition) Map(java.util.Map)

Example 5 with PropertyCondition

use of io.jmix.core.querycondition.PropertyCondition in project jmix by jmix-framework.

the class PropertyConditionGenerator method generateJoin.

@Override
public String generateJoin(ConditionGenerationContext context) {
    PropertyCondition propertyCondition = (PropertyCondition) context.getCondition();
    if (propertyCondition == null || context.getEntityName() == null) {
        return "";
    }
    String propertyName = propertyCondition.getProperty();
    StringBuilder joinBuilder = new StringBuilder();
    StringBuilder joinPropertyBuilder = new StringBuilder(context.entityAlias);
    MetaClass metaClass = metadata.getClass(context.getEntityName());
    while (propertyName.contains(".")) {
        String basePropertyName = StringUtils.substringBefore(propertyName, ".");
        String childProperty = StringUtils.substringAfter(propertyName, ".");
        MetaProperty metaProperty = metaClass.getProperty(basePropertyName);
        if (metaProperty.getRange().getCardinality().isMany()) {
            String joinAlias = basePropertyName.substring(0, 3) + RandomStringUtils.randomAlphabetic(3);
            context.setJoinAlias(joinAlias);
            context.setJoinProperty(childProperty);
            context.setJoinMetaClass(metaProperty.getRange().asClass());
            joinBuilder.append(" join " + joinPropertyBuilder + "." + basePropertyName + " " + joinAlias);
            joinPropertyBuilder = new StringBuilder(joinAlias);
        } else {
            joinPropertyBuilder.append(".").append(basePropertyName);
        }
        if (metaProperty.getRange().isClass()) {
            metaClass = metaProperty.getRange().asClass();
        } else {
            break;
        }
        propertyName = childProperty;
    }
    return joinBuilder.toString();
}
Also used : PropertyCondition(io.jmix.core.querycondition.PropertyCondition) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

PropertyCondition (io.jmix.core.querycondition.PropertyCondition)7 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 JpqlCondition (io.jmix.core.querycondition.JpqlCondition)1 Map (java.util.Map)1