Search in sources :

Example 11 with FilterMapping

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

the class SkuBundleItemCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
        // sort it
        if (foreignKey != null && foreignKey.getSortField() != null) {
            FilterAndSortCriteria sortCriteria = cto.get(foreignKey.getSortField());
            sortCriteria.setSortAscending(foreignKey.getSortAscending());
        }
        // get the default properties from Inventory and its subclasses
        Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(SkuBundleItem.class.getName(), persistencePerspective);
        // Pull back the Inventory based on the criteria from the client
        List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, originalProps);
        // attach the product option criteria
        skuPersistenceHandler.applyProductOptionValueCriteria(filterMappings, cto, persistencePackage, "sku");
        List<Serializable> records = helper.getPersistentRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings, cto.getFirstResult(), cto.getMaxResults());
        // Convert Skus into the client-side Entity representation
        Entity[] payload = helper.getRecords(originalProps, records);
        int totalRecords = helper.getTotalRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings);
        for (int i = 0; i < payload.length; i++) {
            Entity entity = payload[i];
            SkuBundleItem bundleItem = (SkuBundleItem) records.get(i);
            Sku bundleSku = bundleItem.getSku();
            entity.findProperty("sku").setDisplayValue(bundleSku.getName());
            List<ProductOptionValue> productOptionValues = new ArrayList<>();
            for (SkuProductOptionValueXref skuProductOptionValueXref : bundleSku.getProductOptionValueXrefs()) {
                productOptionValues.add(skuProductOptionValueXref.getProductOptionValue());
            }
            Property optionsProperty = skuPersistenceHandler.getConsolidatedOptionProperty(productOptionValues);
            entity.addProperty(optionsProperty);
        }
        return new DynamicResultSet(null, payload, totalRecords);
    } catch (Exception e) {
        throw new ServiceException("There was a problem fetching inventory. See server logs for more details", e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) SkuProductOptionValueXref(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 12 with FilterMapping

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

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

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

the class BasicPersistenceModule method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
    Entity[] payload;
    int totalRecords;
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    try {
        if (foreignKey != null && foreignKey.getSortField() != null) {
            FilterAndSortCriteria sortCriteria = cto.get(foreignKey.getSortField());
            sortCriteria.setSortAscending(foreignKey.getSortAscending());
        }
        Map<String, FieldMetadata> mergedProperties = getMergedProperties(persistencePackage, cto);
        List<FilterMapping> filterMappings = getFilterMappings(persistencePerspective, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), mergedProperties);
        List<FilterMapping> standardFilterMappings = new ArrayList<FilterMapping>(filterMappings);
        if (CollectionUtils.isNotEmpty(cto.getAdditionalFilterMappings())) {
            standardFilterMappings.addAll(cto.getAdditionalFilterMappings());
        }
        if (CollectionUtils.isNotEmpty(cto.getNonCountAdditionalFilterMappings())) {
            standardFilterMappings.addAll(cto.getNonCountAdditionalFilterMappings());
        }
        FetchRequest fetchRequest = new FetchRequest(persistencePackage, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), standardFilterMappings);
        List<Serializable> records = getPersistentRecords(fetchRequest);
        List<FilterMapping> countFilterMappings = new ArrayList<FilterMapping>(filterMappings);
        if (CollectionUtils.isNotEmpty(cto.getAdditionalFilterMappings())) {
            countFilterMappings.addAll(cto.getAdditionalFilterMappings());
        }
        FetchRequest countFetchRequest = new FetchRequest(persistencePackage, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), countFilterMappings);
        totalRecords = getTotalRecords(countFetchRequest);
        FetchExtractionRequest fetchExtractionRequest = new FetchExtractionRequest(persistencePackage, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), mergedProperties, records);
        payload = getRecords(fetchExtractionRequest);
    } catch (Exception e) {
        throw new ServiceException("Unable to fetch results for " + ceilingEntityFullyQualifiedClassname, e);
    }
    return new DynamicResultSet(null, payload, totalRecords);
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) 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) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 15 with FilterMapping

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

the class BasicPersistenceModule method buildSpecialRestrictions.

/**
 * Generate EQUALS restrictions for any filter property specified on the entity member of the collection field in the @Embeddable object
 *
 * @see #getSpecialCaseQueryBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath, java.util.List, String)
 * @param specialExpression the String representation of the path for the collection field in the @Embeddable object
 * @param filterMappings all the fetch restrictions for this request
 * @return the list of restrictions on the collection in the @Embeddable object
 */
protected List<TQRestriction> buildSpecialRestrictions(String specialExpression, List<FilterMapping> filterMappings) {
    List<TQRestriction> restrictions = new ArrayList<TQRestriction>();
    for (FilterMapping mapping : filterMappings) {
        if (mapping.getFieldPath() != null && mapping.getFieldPath().getTargetProperty() != null && mapping.getFieldPath().getTargetProperty().startsWith(specialExpression)) {
            FilterValueConverter converter = mapping.getRestriction().getFilterValueConverter();
            if (converter != null && CollectionUtils.isNotEmpty(mapping.getFilterValues())) {
                Object val = converter.convert(mapping.getFilterValues().get(0));
                String property = mapping.getFieldPath().getTargetProperty().substring(mapping.getFieldPath().getTargetProperty().lastIndexOf(".") + 1, mapping.getFieldPath().getTargetProperty().length());
                restrictions.add(new TQRestriction("embeddedCollection." + property, "=", val));
            }
        }
    }
    return restrictions;
}
Also used : ArrayList(java.util.ArrayList) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) FilterValueConverter(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter) TQRestriction(org.broadleafcommerce.common.util.dao.TQRestriction)

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