Search in sources :

Example 1 with FilterValueConverter

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method buildStandardRestrictions.

/**
 * Generate LIKE or EQUALS restrictions for any filter property specified on the root entity (not the collection field in the @Embeddable object)
 *
 * @see #getSpecialCaseQueryBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath, java.util.List, String)
 * @param embeddedCollectionPath the path for the collection field in the @Embeddable object - this is what caused the whole thing
 * @param filterMappings all the fetch restrictions for this request
 * @return the list of restrictions on the root entity
 */
protected List<TQRestriction> buildStandardRestrictions(FieldPath embeddedCollectionPath, List<FilterMapping> filterMappings) {
    String expression = embeddedCollectionPath.getTargetProperty().substring(0, embeddedCollectionPath.getTargetProperty().lastIndexOf("."));
    List<TQRestriction> restrictions = new ArrayList<TQRestriction>();
    for (FilterMapping mapping : filterMappings) {
        checkProperty: {
            String mappingProperty = mapping.getFieldPath() == null ? null : mapping.getFieldPath().getTargetProperty();
            if (StringUtils.isEmpty(mappingProperty)) {
                mappingProperty = mapping.getFullPropertyName();
            }
            if (!embeddedCollectionPath.getTargetProperty().equals(mappingProperty) && !StringUtils.isEmpty(mappingProperty)) {
                PredicateProvider predicateProvider = mapping.getRestriction().getPredicateProvider();
                if (predicateProvider != null) {
                    FilterValueConverter converter = mapping.getRestriction().getFilterValueConverter();
                    if (converter != null && CollectionUtils.isNotEmpty(mapping.getFilterValues())) {
                        Object val = converter.convert(mapping.getFilterValues().get(0));
                        if (predicateProvider instanceof LikePredicateProvider) {
                            restrictions.add(new TQRestriction("specialEntity." + mappingProperty, "LIKE", val + "%"));
                            break checkProperty;
                        } else if (predicateProvider instanceof EqPredicateProvider) {
                            restrictions.add(new TQRestriction("specialEntity." + mappingProperty, "=", val));
                            break checkProperty;
                        }
                    }
                }
                LOG.warn(String.format("Unable to filter the embedded collection (%s) on an additional property (%s)", StringUtil.sanitize(expression), StringUtil.sanitize(mappingProperty)));
            }
        }
    }
    return restrictions;
}
Also used : EqPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider) LikePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider) EqPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) ArrayList(java.util.ArrayList) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) LikePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider) FilterValueConverter(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter) TQRestriction(org.broadleafcommerce.common.util.dao.TQRestriction)

Example 2 with FilterValueConverter

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method buildSpecialRestrictions.

/**
 * Generate EQUALS restrictions for any filter property specified on the entity member of the collection field in the @Embeddable object
 *
 * @see #getSpecialCaseQueryBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath, java.util.List, String)
 * @param specialExpression the String representation of the path for the collection field in the @Embeddable object
 * @param filterMappings all the fetch restrictions for this request
 * @return the list of restrictions on the collection in the @Embeddable object
 */
protected List<TQRestriction> buildSpecialRestrictions(String specialExpression, List<FilterMapping> filterMappings) {
    List<TQRestriction> restrictions = new ArrayList<TQRestriction>();
    for (FilterMapping mapping : filterMappings) {
        if (mapping.getFieldPath() != null && mapping.getFieldPath().getTargetProperty() != null && mapping.getFieldPath().getTargetProperty().startsWith(specialExpression)) {
            FilterValueConverter converter = mapping.getRestriction().getFilterValueConverter();
            if (converter != null && CollectionUtils.isNotEmpty(mapping.getFilterValues())) {
                Object val = converter.convert(mapping.getFilterValues().get(0));
                String property = mapping.getFieldPath().getTargetProperty().substring(mapping.getFieldPath().getTargetProperty().lastIndexOf(".") + 1, mapping.getFieldPath().getTargetProperty().length());
                restrictions.add(new TQRestriction("embeddedCollection." + property, "=", val));
            }
        }
    }
    return restrictions;
}
Also used : ArrayList(java.util.ArrayList) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) FilterValueConverter(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter) TQRestriction(org.broadleafcommerce.common.util.dao.TQRestriction)

Aggregations

ArrayList (java.util.ArrayList)2 TQRestriction (org.broadleafcommerce.common.util.dao.TQRestriction)2 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)2 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)2 FilterValueConverter (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter)2 EqPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider)1 LikePredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider)1 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)1