Search in sources :

Example 16 with FilterMapping

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

the class BasicPersistenceModule method getSpecialCaseQueryBuilder.

/**
 * Use an alternate approach to generating a fetch query for a collection located inside of an @Embeddable object. Related
 * to https://hibernate.atlassian.net/browse/HHH-8802. The alternate approach leverages HQL rather than JPA criteria,
 * which seems to alleviate the problem.
 *
 * @param embeddedCollectionPath the path to the collection field itself
 * @param filterMappings all the fetch restrictions for this request
 * @param collectionClass the type of the collection members
 * @return the builder capable of generating an appropriate HQL query
 */
protected TypedQueryBuilder getSpecialCaseQueryBuilder(FieldPath embeddedCollectionPath, List<FilterMapping> filterMappings, String collectionClass) {
    String specialPath = embeddedCollectionPath.getTargetProperty();
    String[] pieces = specialPath.split("\\.");
    if (pieces.length != 3) {
        throw new CriteriaConversionException(String.format("Expected to find a target property of format [embedded field].[collection field].[property] for the embedded collection path (%s)", specialPath), embeddedCollectionPath);
    }
    String expression = specialPath.substring(0, specialPath.lastIndexOf("."));
    TypedQueryBuilder builder;
    try {
        builder = new TypedQueryBuilder(Class.forName(collectionClass), "specialEntity").addJoin(new TQJoin("specialEntity." + expression, "embeddedCollection"));
    } catch (Exception e) {
        throw ExceptionHelper.refineException(e);
    }
    for (TQRestriction restriction : buildSpecialRestrictions(expression, filterMappings)) {
        builder = builder.addRestriction(restriction);
    }
    for (TQRestriction restriction : buildStandardRestrictions(embeddedCollectionPath, filterMappings)) {
        builder = builder.addRestriction(restriction);
    }
    for (FilterMapping mapping : filterMappings) {
        if (mapping.getSortDirection() != null) {
            String mappingProperty = mapping.getFieldPath() == null ? null : mapping.getFieldPath().getTargetProperty();
            if (StringUtils.isEmpty(mappingProperty)) {
                mappingProperty = mapping.getFullPropertyName();
            }
            builder = builder.addOrder(new TQOrder("specialEntity." + mappingProperty, SortDirection.ASCENDING == mapping.getSortDirection()));
        }
    }
    return builder;
}
Also used : CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) TQOrder(org.broadleafcommerce.common.util.dao.TQOrder) TypedQueryBuilder(org.broadleafcommerce.common.util.dao.TypedQueryBuilder) TQJoin(org.broadleafcommerce.common.util.dao.TQJoin) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) TQRestriction(org.broadleafcommerce.common.util.dao.TQRestriction)

Example 17 with FilterMapping

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

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

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

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

FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)22 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)11 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)11 ArrayList (java.util.ArrayList)10 List (java.util.List)10 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)10 From (javax.persistence.criteria.From)10 Path (javax.persistence.criteria.Path)10 FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)10 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)10 FieldPathBuilder (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)10 Serializable (java.io.Serializable)8 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)8 ServiceException (org.broadleafcommerce.common.exception.ServiceException)7 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)7 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)7 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)6 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)6 Entity (org.broadleafcommerce.openadmin.dto.Entity)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5