Search in sources :

Example 6 with PredicateProvider

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

the class IndexFieldCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    FilterAndSortCriteria fieldFsc = cto.getCriteriaMap().get("field");
    if (fieldFsc != null) {
        List<String> filterValues = fieldFsc.getFilterValues();
        cto.getCriteriaMap().remove("field");
        cto.getAdditionalFilterMappings().add(new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("field.friendlyName")).withFilterValues(filterValues).withSortDirection(fieldFsc.getSortDirection()).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 builder.like(explicitPath.as(String.class), "%" + directValues.get(0) + "%");
            }
        })));
    }
    DynamicResultSet resultSet = helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto);
    return resultSet;
}
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) Predicate(javax.persistence.criteria.Predicate) 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) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 7 with PredicateProvider

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

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

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

the class SkuCustomPersistenceHandler method createToOneIndividualOptionField.

/**
 * Using a ToOne lookup performs much better for large product option value lists, speeds up initial page load,
 * and is generally more accurate in relation to option value updates and how they impact available selections and cache.
 *
 * @param option
 * @param order
 * @return
 */
protected FieldMetadata createToOneIndividualOptionField(ProductOption option, int order) {
    PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
    FilterMapping filterMapping = new FilterMapping().withDirectFilterValues(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, option.getId())).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 root.get("productOption").get("id").in(directValues);
        }
    }));
    List<FilterMapping> mappings = new ArrayList<>();
    mappings.add(filterMapping);
    TypedQuery<Serializable> countQuery = criteriaTranslator.translateCountQuery(persistenceManager.getDynamicEntityDao(), ProductOptionValueImpl.class.getName(), mappings);
    Long count = (Long) countQuery.getSingleResult();
    BasicFieldMetadata metadata = null;
    if (count > 0) {
        metadata = new BasicFieldMetadata();
        metadata.setFieldType(SupportedFieldType.ADDITIONAL_FOREIGN_KEY);
        metadata.setSecondaryType(SupportedFieldType.INTEGER);
        metadata.setForeignKeyProperty("id");
        metadata.setForeignKeyClass(ProductOptionValueImpl.class.getName());
        metadata.setForeignKeyDisplayValueProperty("attributeValue");
        metadata.setLookupDisplayProperty("attributeValue");
        metadata.setForeignKeyCollection(false);
        metadata.setCustomCriteria(new String[] { "option=" + String.valueOf(option.getId()) });
        metadata.setName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
        metadata.setFriendlyName(option.getLabel());
        metadata.setGroup("productOption_group");
        metadata.setGroupOrder(-1);
        metadata.setOrder(order);
        metadata.setExplicitFieldType(SupportedFieldType.ADDITIONAL_FOREIGN_KEY);
        metadata.setProminent(false);
        metadata.setVisibility(VisibilityEnum.FORM_EXPLICITLY_SHOWN);
        metadata.setReadOnly(false);
        // these may not be actually required, but the CPH has this as a requirement for parsing the data, so we'll stick with it here
        metadata.setRequiredOverride(true);
        metadata.setLookupType(LookupType.STANDARD);
        metadata.setMutable(true);
        metadata.setInheritedFromType(SkuImpl.class.getName());
        metadata.setAvailableToTypes(getPolymorphicClasses(SkuImpl.class, em, skuMetadataCacheService.useCache()));
        metadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
        metadata.setTargetClass(SkuImpl.class.getName());
        metadata.setFieldName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
    }
    return metadata;
}
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) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) 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) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) List(java.util.List) ArrayList(java.util.ArrayList) ProductOptionValueImpl(org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)

Example 10 with PredicateProvider

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

the class SkuCustomPersistenceHandler method applySkuBundleItemValueCriteria.

/**
 * Add filter restriction such that a ProductBundle cannot add its own default sku as a Sku Bundle Item
 */
private void applySkuBundleItemValueCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto, PersistencePackage persistencePackage) {
    SectionCrumb[] sectionCrumbs = persistencePackage.getSectionCrumbs();
    if (isSkuBundleItemLookup(persistencePackage, sectionCrumbs)) {
        final Long defaultSkuId = getOwningProductBundlesDefaultSkuId(sectionCrumbs[0]);
        filterMappings.add(new FilterMapping().withDirectFilterValues(Collections.singletonList(defaultSkuId)).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 builder.notEqual(root, directValues.get(0));
            }
        })));
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) 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) Predicate(javax.persistence.criteria.Predicate) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) 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)

Aggregations

PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)12 List (java.util.List)11 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)11 From (javax.persistence.criteria.From)11 Path (javax.persistence.criteria.Path)11 FieldPathBuilder (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)11 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)11 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)11 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)10 ArrayList (java.util.ArrayList)6 Predicate (javax.persistence.criteria.Predicate)5 FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)4 EmptyFilterValues (org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues)4 Serializable (java.io.Serializable)2 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)2 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)2 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)2 EqPredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider)2 LikePredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider)2 Date (java.util.Date)1