Search in sources :

Example 21 with FilterMapping

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

the class SkuCustomPersistenceHandler method fetch.

@SuppressWarnings("unchecked")
@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        // get the default properties from Sku and its subclasses
        Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(Sku.class.getName(), persistencePerspective);
        // Pull back the Skus based on the criteria from the client
        List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, originalProps);
        // allow subclasses to provide additional criteria before executing the query
        applyProductOptionValueCriteria(filterMappings, cto, persistencePackage, null);
        applySkuBundleItemValueCriteria(filterMappings, cto, persistencePackage);
        applyAdditionalFetchCriteria(filterMappings, cto, persistencePackage);
        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);
        // Now fill out the relevant properties for the product options for the Skus that were returned
        updateProductOptionFieldsForFetch(records, payload);
        return new DynamicResultSet(payload, totalRecords);
    } catch (Exception e) {
        throw new ServiceException("Unable to perform fetch for entity: " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ServiceException(org.broadleafcommerce.common.exception.ServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Sku(org.broadleafcommerce.core.catalog.domain.Sku) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 22 with FilterMapping

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

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