Search in sources :

Example 1 with EmptyFilterValues

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

the class BasicFieldPersistenceProvider method addSearchMapping.

@Override
public MetadataProviderResponse addSearchMapping(AddSearchMappingRequest addSearchMappingRequest, List<FilterMapping> filterMappings) {
    if (!canHandleSearchMapping(addSearchMappingRequest, filterMappings)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    Class clazz;
    try {
        clazz = Class.forName(addSearchMappingRequest.getMergedProperties().get(addSearchMappingRequest.getPropertyName()).getInheritedFromType());
    } catch (ClassNotFoundException e) {
        throw new PersistenceException(e);
    }
    Field field = addSearchMappingRequest.getFieldManager().getField(clazz, addSearchMappingRequest.getPropertyName());
    Class<?> targetType = null;
    if (field != null) {
        targetType = field.getType();
    }
    BasicFieldMetadata metadata = (BasicFieldMetadata) addSearchMappingRequest.getMergedProperties().get(addSearchMappingRequest.getPropertyName());
    FilterAndSortCriteria fasc = addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName());
    FilterMapping filterMapping = new FilterMapping().withInheritedFromClass(clazz).withFullPropertyName(addSearchMappingRequest.getPropertyName()).withFilterValues(fasc.getFilterValues()).withSortDirection(fasc.getSortDirection()).withOrder(fasc.getOrder()).withNullsLast(fasc.isNullsLast());
    filterMappings.add(filterMapping);
    if (fasc.hasSpecialFilterValue()) {
        filterMapping.setDirectFilterValues(new EmptyFilterValues());
        // Handle special values on a case by case basis
        List<String> specialValues = fasc.getSpecialFilterValues();
        if (specialValues.contains(FilterAndSortCriteria.IS_NULL_FILTER_VALUE)) {
            filterMapping.setRestriction(new Restriction().withPredicateProvider(new IsNullPredicateProvider()));
        }
        if (specialValues.contains(FilterAndSortCriteria.IS_NOT_NULL_FILTER_VALUE)) {
            filterMapping.setRestriction(new Restriction().withPredicateProvider(new IsNotNullPredicateProvider()));
        }
    } else {
        if (fasc.getRestrictionType() != null) {
            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(fasc.getRestrictionType().getType(), addSearchMappingRequest.getPropertyName()));
        } else {
            switch(metadata.getFieldType()) {
                case BOOLEAN:
                    if (targetType == null || targetType.equals(Boolean.class) || targetType.equals(boolean.class)) {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.BOOLEAN.getType(), addSearchMappingRequest.getPropertyName()));
                    } else {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.CHARACTER.getType(), addSearchMappingRequest.getPropertyName()));
                    }
                    break;
                case DATE:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.DATE.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                case DECIMAL:
                case MONEY:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.DECIMAL.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                case INTEGER:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                case BROADLEAF_ENUMERATION:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                default:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_LIKE.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                case FOREIGN_KEY:
                    if (!addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().isEmpty()) {
                        ForeignKey foreignKey = (ForeignKey) addSearchMappingRequest.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
                        if (metadata.getForeignKeyCollection()) {
                            if (ForeignKeyRestrictionType.COLLECTION_SIZE_EQ.toString().equals(foreignKey.getRestrictionType().toString())) {
                                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.COLLECTION_SIZE_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                                filterMapping.setFieldPath(new FieldPath());
                            } else {
                                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
                                filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
                            }
                        } else if (addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0) == null || "null".equals(addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0))) {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.IS_NULL_LONG.getType(), addSearchMappingRequest.getPropertyName()));
                        } else if (metadata.getSecondaryType() == SupportedFieldType.STRING) {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                            filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
                        } else {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                            filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
                        }
                    }
                    break;
                case ADDITIONAL_FOREIGN_KEY:
                    int additionalForeignKeyIndexPosition = Arrays.binarySearch(addSearchMappingRequest.getPersistencePerspective().getAdditionalForeignKeys(), new ForeignKey(addSearchMappingRequest.getPropertyName(), null, null), new Comparator<ForeignKey>() {

                        @Override
                        public int compare(ForeignKey o1, ForeignKey o2) {
                            return o1.getManyToField().compareTo(o2.getManyToField());
                        }
                    });
                    ForeignKey foreignKey = null;
                    if (additionalForeignKeyIndexPosition >= 0) {
                        foreignKey = addSearchMappingRequest.getPersistencePerspective().getAdditionalForeignKeys()[additionalForeignKeyIndexPosition];
                    }
                    // default to just using a ForeignKeyRestrictionType.ID_EQ
                    if (metadata.getForeignKeyCollection()) {
                        if (ForeignKeyRestrictionType.COLLECTION_SIZE_EQ.toString().equals(foreignKey.getRestrictionType().toString())) {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.COLLECTION_SIZE_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                            filterMapping.setFieldPath(new FieldPath());
                        } else {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
                            filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
                        }
                    } else if (CollectionUtils.isEmpty(addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues()) || addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0) == null || "null".equals(addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0))) {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.IS_NULL_LONG.getType(), addSearchMappingRequest.getPropertyName()));
                    } else if (metadata.getSecondaryType() == SupportedFieldType.STRING) {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                        filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
                    } else {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                        filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
                    }
                    break;
                case ID:
                    switch(metadata.getSecondaryType()) {
                        case INTEGER:
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                            break;
                        case STRING:
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                            break;
                    }
                    break;
            }
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : IsNotNullPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.IsNotNullPredicateProvider) IsNullPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.IsNullPredicateProvider) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) Field(java.lang.reflect.Field) EmptyFilterValues(org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)

Example 2 with EmptyFilterValues

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

the class CriteriaTranslatorImpl method addRestrictions.

protected void addRestrictions(String ceilingEntity, List<FilterMapping> filterMappings, CriteriaBuilder criteriaBuilder, Root original, List<Predicate> restrictions, List<Order> sorts, CriteriaQuery criteria) {
    Collections.sort(filterMappings, new FilterMapping.ComparatorByOrder());
    for (FilterMapping filterMapping : filterMappings) {
        Path explicitPath = null;
        if (filterMapping.getFieldPath() != null) {
            explicitPath = filterMapping.getRestriction().getFieldPathBuilder().getPath(original, filterMapping.getFieldPath(), criteriaBuilder);
        }
        if (filterMapping.getRestriction() != null) {
            List directValues = null;
            boolean shouldConvert = true;
            if (CollectionUtils.isNotEmpty(filterMapping.getFilterValues())) {
                directValues = filterMapping.getFilterValues();
            } else if (CollectionUtils.isNotEmpty(filterMapping.getDirectFilterValues()) || (filterMapping.getDirectFilterValues() != null && filterMapping.getDirectFilterValues() instanceof EmptyFilterValues)) {
                directValues = filterMapping.getDirectFilterValues();
                shouldConvert = false;
            }
            if (directValues != null) {
                Predicate predicate = filterMapping.getRestriction().buildRestriction(criteriaBuilder, original, ceilingEntity, filterMapping.getFullPropertyName(), explicitPath, directValues, shouldConvert, criteria, restrictions);
                if (predicate != null) {
                    restrictions.add(predicate);
                }
            }
        }
        if (filterMapping.getSortDirection() != null) {
            Path sortPath = explicitPath;
            if (sortPath == null && !StringUtils.isEmpty(filterMapping.getFullPropertyName())) {
                FieldPathBuilder fieldPathBuilder = filterMapping.getRestriction().getFieldPathBuilder();
                fieldPathBuilder.setCriteria(criteria);
                fieldPathBuilder.setRestrictions(restrictions);
                sortPath = filterMapping.getRestriction().getFieldPathBuilder().getPath(original, filterMapping.getFullPropertyName(), criteriaBuilder);
            }
            if (sortPath != null) {
                addSorting(criteriaBuilder, sorts, filterMapping, sortPath);
            }
        }
    }
    // add in the row-level security handlers to this as well
    rowSecurityService.addFetchRestrictions(adminSecurityService.getPersistentAdminUser(), ceilingEntity, restrictions, sorts, original, criteria, criteriaBuilder);
    for (CriteriaTranslatorEventHandler eventHandler : eventHandlers) {
        eventHandler.addRestrictions(ceilingEntity, filterMappings, criteriaBuilder, original, restrictions, sorts, criteria);
    }
}
Also used : Path(javax.persistence.criteria.Path) EmptyFilterValues(org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues) ArrayList(java.util.ArrayList) List(java.util.List) Predicate(javax.persistence.criteria.Predicate)

Example 3 with EmptyFilterValues

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

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

Example 5 with EmptyFilterValues

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

Aggregations

EmptyFilterValues (org.broadleafcommerce.openadmin.server.service.persistence.module.EmptyFilterValues)6 List (java.util.List)5 Path (javax.persistence.criteria.Path)5 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)5 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)5 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 From (javax.persistence.criteria.From)4 FieldPathBuilder (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)4 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)4 Predicate (javax.persistence.criteria.Predicate)3 FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)3 ArrayList (java.util.ArrayList)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 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 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)1