Search in sources :

Example 1 with PredicateProvider

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

the class AdornedTargetListPersistenceModule method getAdornedTargetFilterMappings.

public List<FilterMapping> getAdornedTargetFilterMappings(PersistencePerspective persistencePerspective, CriteriaTransferObject cto, Map<String, FieldMetadata> mergedProperties, AdornedTargetList adornedTargetList) throws ClassNotFoundException {
    List<FilterMapping> filterMappings = getFilterMappings(persistencePerspective, cto, adornedTargetList.getAdornedTargetEntityClassname(), mergedProperties);
    FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty())).withFilterValues(cto.get(adornedTargetList.getCollectionFieldName()).getFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider<Serializable, String>() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Serializable> explicitPath, List<String> directValues) {
            if (String.class.isAssignableFrom(explicitPath.getJavaType())) {
                return builder.equal(explicitPath, directValues.get(0));
            } else {
                return builder.equal(explicitPath, Long.parseLong(directValues.get(0)));
            }
        }
    }));
    filterMappings.add(filterMapping);
    FilterMapping filterMapping2 = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty())).withFilterValues(cto.get(adornedTargetList.getCollectionFieldName() + "Target").getFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider<Serializable, String>() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Serializable> explicitPath, List<String> directValues) {
            if (String.class.isAssignableFrom(explicitPath.getJavaType())) {
                return builder.equal(explicitPath, directValues.get(0));
            } else {
                return builder.equal(explicitPath, Long.parseLong(directValues.get(0)));
            }
        }
    }));
    filterMappings.add(filterMapping2);
    return filterMappings;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Example 2 with PredicateProvider

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

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

the class ProductCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    boolean legacy = parentCategoryLegacyModeService.isLegacyMode();
    // which seeks matching values in  allParentCategoryXRefs instead
    if (!legacy) {
        FilterAndSortCriteria fsc = cto.getCriteriaMap().get("defaultCategory");
        if (fsc != null) {
            List<String> filterValues = fsc.getFilterValues();
            cto.getCriteriaMap().remove("defaultCategory");
            List<Long> transformedValues = BLCCollectionUtils.collectList(filterValues, new TypedTransformer<Long>() {

                @Override
                public Long transform(Object input) {
                    return Long.parseLong(((String) input));
                }
            });
            CriteriaBuilder builder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
            CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
            Root<CategoryProductXrefImpl> root = criteria.from(CategoryProductXrefImpl.class);
            criteria.select(root.get("product").get("id").as(Long.class));
            List<Predicate> restrictions = new ArrayList<Predicate>();
            restrictions.add(builder.equal(root.get("defaultReference"), Boolean.TRUE));
            if (CollectionUtils.isNotEmpty(transformedValues)) {
                restrictions.add(root.get("category").get("id").in(transformedValues));
            }
            criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
            TypedQuery<Long> query = dynamicEntityDao.getStandardEntityManager().createQuery(criteria);
            List<Long> productIds = query.getResultList();
            productIds = sandBoxHelper.mergeCloneIds(ProductImpl.class, productIds.toArray(new Long[productIds.size()]));
            if (productIds.size() == 0) {
                return new DynamicResultSet(null, new Entity[0], 0);
            }
            if (productIds.size() <= queryLimit) {
                FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("id")).withDirectFilterValues(productIds).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.in(directValues);
                    }
                }));
                cto.getAdditionalFilterMappings().add(filterMapping);
            } else {
                String joined = StringUtils.join(transformedValues, ',');
                LOG.warn(String.format("Skipping default category filtering for product fetch query since there are " + "more than " + queryLimit + " products found to belong to the selected default categories(%s). This is a " + "filter query limitation.", joined));
            }
        }
    }
    if (eagerFetchAssociations) {
        cto.getNonCountAdditionalFilterMappings().add(new FilterMapping().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) {
                root.fetch("defaultSku", JoinType.LEFT);
                root.fetch("defaultCategory", JoinType.LEFT);
                return null;
            }
        })));
    }
    return helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto);
}
Also used : FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) Predicate(javax.persistence.criteria.Predicate) EmptyFilterValues(org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) List(java.util.List) ArrayList(java.util.ArrayList) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) 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) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 4 with PredicateProvider

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

the class ProductOptionValuesCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Long optionId = getOptionKey(persistencePackage);
    if (optionId != null) {
        FilterMapping filterMapping = new FilterMapping().withDirectFilterValues(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, optionId)).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {

            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);
            }
        }));
        cto.getAdditionalFilterMappings().add(filterMapping);
    }
    return helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) 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) 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)

Example 5 with PredicateProvider

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

the class ArchiveStatusPersistenceEventHandler method preFetch.

@Override
public PersistenceManagerEventHandlerResponse preFetch(PersistenceManager persistenceManager, PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
    try {
        Class<?>[] entityClasses = persistenceManager.getDynamicEntityDao().getAllPolymorphicEntitiesFromCeiling(Class.forName(persistencePackage.getCeilingEntityFullyQualifiedClassname()));
        AtomicBoolean isArchivable = new AtomicBoolean(false);
        for (Class<?> entity : entityClasses) {
            AtomicBoolean test = new AtomicBoolean(true);
            extensionManager.getProxy().isArchivable(entity, test);
            if (!test.get()) {
                isArchivable.set(false);
                break;
            }
            if (Status.class.isAssignableFrom(entity)) {
                isArchivable.set(true);
            }
        }
        if (isArchivable.get() && !persistencePackage.getPersistencePerspective().getShowArchivedFields()) {
            String targetPropertyName = "archiveStatus.archived";
            if (persistencePackage.getPersistencePerspectiveItems().containsKey(PersistencePerspectiveItemType.ADORNEDTARGETLIST)) {
                AdornedTargetList atl = (AdornedTargetList) persistencePackage.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
                targetPropertyName = atl.getTargetObjectPath() + "." + targetPropertyName;
            }
            FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(targetPropertyName)).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.or(builder.equal(explicitPath, 'N'), builder.isNull(explicitPath));
                }
            }));
            cto.getAdditionalFilterMappings().add(filterMapping);
        }
        return new PersistenceManagerEventHandlerResponse().withStatus(PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED);
    } catch (ClassNotFoundException e) {
        LOG.error("Could not find the class " + persistencePackage.getCeilingEntityFullyQualifiedClassname() + " to " + "compute polymorphic entity types for. Assuming that the entity is not archivable");
        return new PersistenceManagerEventHandlerResponse().withStatus(PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.NOT_HANDLED);
    }
}
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) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) List(java.util.List) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList)

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