Search in sources :

Example 6 with FieldPathBuilder

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

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

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

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

Example 10 with FieldPathBuilder

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

Aggregations

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 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)11 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)11 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)10 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)10 ArrayList (java.util.ArrayList)5 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 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)2 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)1 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)1