Search in sources :

Example 1 with LikePredicateProvider

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider 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 LikePredicateProvider

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider in project BroadleafCommerce by BroadleafCommerce.

the class SkuRestrictionFactoryImpl method getRestriction.

@Override
public Restriction getRestriction(final String type, String propertyId) {
    final Restriction delegateRestriction = delegate.getRestriction(type, propertyId);
    return new Restriction().withFilterValueConverter(delegateRestriction.getFilterValueConverter()).withPredicateProvider(new PredicateProvider() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
            FieldPath fieldPath = fieldPathBuilder.getFieldPath(root, fullPropertyName);
            if ((StringUtils.isNotEmpty(skuPropertyPrefix) && fullPropertyName.startsWith(skuPropertyPrefix)) || CollectionUtils.isEmpty(fieldPath.getAssociationPath())) {
                Path targetPropertyPath = fieldPathBuilder.getPath(root, fieldPath, builder);
                Path defaultSkuPropertyPath = fieldPathBuilder.getPath(root, DEFAULT_SKU_PATH_PREFIX + fullPropertyName, builder);
                Path productPath = fieldPathBuilder.getPath(root, "product", builder);
                Predicate propertyExpression;
                Predicate defaultSkuExpression;
                if (delegateRestriction.getPredicateProvider() instanceof LikePredicateProvider) {
                    propertyExpression = builder.like(builder.lower(targetPropertyPath), ((String) directValues.get(0)).toLowerCase());
                    defaultSkuExpression = builder.like(builder.lower(defaultSkuPropertyPath), ((String) directValues.get(0)).toLowerCase());
                } else if (delegateRestriction.getPredicateProvider() instanceof IsNullPredicateProvider) {
                    propertyExpression = builder.isNull(targetPropertyPath);
                    defaultSkuExpression = builder.isNull(defaultSkuPropertyPath);
                } else if (delegateRestriction.getPredicateProvider() instanceof BetweenDatePredicateProvider) {
                    if (directValues.size() == 2) {
                        if (directValues.get(0) == null) {
                            propertyExpression = builder.lessThan(targetPropertyPath, (Comparable) directValues.get(1));
                            defaultSkuExpression = builder.lessThan(defaultSkuPropertyPath, (Comparable) directValues.get(1));
                        } else if (directValues.get(1) == null) {
                            propertyExpression = builder.greaterThanOrEqualTo(targetPropertyPath, (Comparable) directValues.get(0));
                            defaultSkuExpression = builder.greaterThanOrEqualTo(defaultSkuPropertyPath, (Comparable) directValues.get(0));
                        } else {
                            propertyExpression = builder.between(targetPropertyPath, (Comparable) directValues.get(0), (Comparable) directValues.get(1));
                            defaultSkuExpression = builder.between(defaultSkuPropertyPath, (Comparable) directValues.get(0), (Comparable) directValues.get(1));
                        }
                    } else {
                        propertyExpression = builder.equal(targetPropertyPath, directValues.get(0));
                        defaultSkuExpression = builder.equal(defaultSkuPropertyPath, directValues.get(0));
                    }
                } else if (delegateRestriction.getPredicateProvider() instanceof BetweenPredicateProvider) {
                    if (directValues.size() > 1) {
                        propertyExpression = builder.between(targetPropertyPath, (Comparable) directValues.get(0), (Comparable) directValues.get(1));
                        defaultSkuExpression = builder.between(defaultSkuPropertyPath, (Comparable) directValues.get(0), (Comparable) directValues.get(1));
                    } else {
                        propertyExpression = builder.equal(targetPropertyPath, directValues.get(0));
                        defaultSkuExpression = builder.equal(defaultSkuPropertyPath, directValues.get(0));
                    }
                } else if (delegateRestriction.getPredicateProvider() instanceof CollectionSizeEqualPredicateProvider) {
                    propertyExpression = builder.equal(builder.size(targetPropertyPath), directValues.get(0));
                    defaultSkuExpression = builder.equal(builder.size(defaultSkuPropertyPath), directValues.get(0));
                } else if (delegateRestriction.getPredicateProvider() instanceof EqPredicateProvider) {
                    propertyExpression = builder.equal(targetPropertyPath, directValues.get(0));
                    defaultSkuExpression = builder.equal(defaultSkuPropertyPath, directValues.get(0));
                } else {
                    throw new IllegalArgumentException("Unknown PredicateProvider instance: " + delegateRestriction.getPredicateProvider().getClass().getName());
                }
                return buildCompositePredicate(builder, targetPropertyPath, productPath, propertyExpression, defaultSkuExpression);
            }
            return delegateRestriction.getPredicateProvider().buildPredicate(builder, fieldPathBuilder, root, ceilingEntity, fullPropertyName, explicitPath, directValues);
        }
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) IsNullPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.IsNullPredicateProvider) BetweenPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.BetweenPredicateProvider) From(javax.persistence.criteria.From) FieldPathBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder) Predicate(javax.persistence.criteria.Predicate) EqPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) BetweenDatePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.BetweenDatePredicateProvider) CollectionSizeEqualPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.CollectionSizeEqualPredicateProvider) LikePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider) BetweenPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.BetweenPredicateProvider) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) BetweenDatePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.BetweenDatePredicateProvider) EqPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider) IsNullPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.IsNullPredicateProvider) CollectionSizeEqualPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.CollectionSizeEqualPredicateProvider) List(java.util.List) LikePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider)

Aggregations

EqPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider)2 LikePredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider)2 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 From (javax.persistence.criteria.From)1 Path (javax.persistence.criteria.Path)1 Predicate (javax.persistence.criteria.Predicate)1 TQRestriction (org.broadleafcommerce.common.util.dao.TQRestriction)1 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)1 FieldPathBuilder (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)1 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)1 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)1 FilterValueConverter (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter)1 BetweenDatePredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.BetweenDatePredicateProvider)1 BetweenPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.BetweenPredicateProvider)1 CollectionSizeEqualPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.CollectionSizeEqualPredicateProvider)1 IsNullPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.IsNullPredicateProvider)1