Search in sources :

Example 6 with FieldPath

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

the class OfferCustomPersistenceHandler method addIsActiveFiltering.

protected void addIsActiveFiltering(CriteriaTransferObject cto) {
    if (isActiveFilter && cto.getCriteriaMap().containsKey(IS_ACTIVE)) {
        FilterAndSortCriteria filter = cto.get(IS_ACTIVE);
        final Boolean isActive = Boolean.parseBoolean(filter.getFilterValues().get(0));
        FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("id")).withDirectFilterValues(new EmptyFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {

            @Override
            public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
                Date currentTime = SystemTime.asDate(true);
                if (isActive) {
                    return builder.and(builder.isNotNull(root.get("startDate")), builder.lessThan(root.get("startDate"), currentTime), builder.or(builder.isNull(root.get("endDate")), builder.greaterThan(root.get("endDate"), currentTime)));
                } else {
                    return builder.or(builder.isNull(root.get("startDate")), builder.greaterThan(root.get("startDate"), currentTime), builder.and(builder.isNotNull(root.get("endDate")), builder.lessThan(root.get("endDate"), currentTime)));
                }
            }
        }));
        cto.getAdditionalFilterMappings().add(filterMapping);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) From(javax.persistence.criteria.From) FieldPathBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder) Date(java.util.Date) EmptyFilterValues(org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) List(java.util.List) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)

Example 7 with FieldPath

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

the class SkuCustomPersistenceHandler method applyProductOptionValueCriteria.

public void applyProductOptionValueCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto, PersistencePackage persistencePackage, String skuPropertyPrefix) {
    // if the front
    final List<Long> productOptionValueFilterIDs = new ArrayList<>();
    for (String filterProperty : cto.getCriteriaMap().keySet()) {
        if (filterProperty.startsWith(PRODUCT_OPTION_FIELD_PREFIX)) {
            FilterAndSortCriteria criteria = cto.get(filterProperty);
            productOptionValueFilterIDs.add(Long.parseLong(criteria.getFilterValues().get(0)));
        }
    }
    // also determine if there is a consolidated POV query
    final List<String> productOptionValueFilterValues = new ArrayList<>();
    FilterAndSortCriteria consolidatedCriteria = cto.get(CONSOLIDATED_PRODUCT_OPTIONS_FIELD_NAME);
    if (!consolidatedCriteria.getFilterValues().isEmpty()) {
        // the criteria in this case would be a semi-colon delimeter value list
        productOptionValueFilterValues.addAll(Arrays.asList(StringUtils.split(consolidatedCriteria.getFilterValues().get(0), CONSOLIDATED_PRODUCT_OPTIONS_DELIMETER)));
    }
    if (productOptionValueFilterIDs.size() > 0) {
        FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(StringUtils.isEmpty(skuPropertyPrefix) ? "" : skuPropertyPrefix + ".productOptionValueXrefs.productOptionValue.id")).withDirectFilterValues(productOptionValueFilterIDs).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {

            @Override
            public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
                return explicitPath.as(Long.class).in(directValues);
            }
        }));
        filterMappings.add(filterMapping);
    }
    if (productOptionValueFilterValues.size() > 0) {
        FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(StringUtils.isEmpty(skuPropertyPrefix) ? "" : skuPropertyPrefix + ".productOptionValueXrefs.productOptionValue.attributeValue")).withDirectFilterValues(productOptionValueFilterValues).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {

            @Override
            public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
                return explicitPath.as(String.class).in(directValues);
            }
        }));
        filterMappings.add(filterMapping);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) From(javax.persistence.criteria.From) FieldPathBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) List(java.util.List) ArrayList(java.util.ArrayList) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)

Example 8 with FieldPath

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

the class ISOCountryPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("name")).withDirectFilterValues(new EmptyFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider<Character, Character>() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Character> explicitPath, List<Character> directValues) {
            return builder.isNotNull(explicitPath);
        }
    }));
    cto.getAdditionalFilterMappings().add(filterMapping);
    FilterMapping countryRestrictionMapping = new FilterMapping().withDirectFilterValues(new EmptyFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider<Character, Character>() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Character> explicitPath, List<Character> directValues) {
            CriteriaQuery<Serializable> criteria = fieldPathBuilder.getCriteria();
            Root<CountryImpl> blcCountry = criteria.from(CountryImpl.class);
            Predicate join = builder.equal(root.get("alpha2").as(String.class), blcCountry.get("abbreviation").as(String.class));
            return join;
        }
    }));
    cto.getAdditionalFilterMappings().add(countryRestrictionMapping);
    PersistenceModule myModule = helper.getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getFetchType());
    return myModule.fetch(persistencePackage, cto);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) Serializable(java.io.Serializable) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) From(javax.persistence.criteria.From) PersistenceModule(org.broadleafcommerce.openadmin.server.service.persistence.module.PersistenceModule) FieldPathBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder) Predicate(javax.persistence.criteria.Predicate) EmptyFilterValues(org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) CountryImpl(org.broadleafcommerce.profile.core.domain.CountryImpl) List(java.util.List)

Example 9 with FieldPath

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

FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)9 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)9 List (java.util.List)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 From (javax.persistence.criteria.From)8 Path (javax.persistence.criteria.Path)8 FieldPathBuilder (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)8 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)8 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)8 FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)5 EmptyFilterValues (org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues)5 Predicate (javax.persistence.criteria.Predicate)4 ArrayList (java.util.ArrayList)3 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)2 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)2 IsNullPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.IsNullPredicateProvider)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1