Search in sources :

Example 1 with CategoryProductXrefImpl

use of org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl in project BroadleafCommerce by BroadleafCommerce.

the class CandidateItemOfferTest method setUp.

@Override
protected void setUp() throws Exception {
    OfferDataItemProvider dataProvider = new OfferDataItemProvider();
    CandidateItemOfferImpl candidate = new CandidateItemOfferImpl();
    Category category1 = new CategoryImpl();
    category1.setName("test1");
    category1.setId(1L);
    Product product1 = new ProductImpl();
    Sku sku1 = new SkuImpl();
    sku1.setName("test1");
    sku1.setDiscountable(true);
    sku1.setRetailPrice(new Money(19.99D));
    product1.setDefaultSku(sku1);
    CategoryProductXref xref1 = new CategoryProductXrefImpl();
    xref1.setProduct(product1);
    xref1.setCategory(category1);
    category1.getAllProductXrefs().add(xref1);
    Category category2 = new CategoryImpl();
    category2.setName("test2");
    category2.setId(2L);
    Product product2 = new ProductImpl();
    Sku sku2 = new SkuImpl();
    sku2.setName("test2");
    sku2.setDiscountable(true);
    sku2.setRetailPrice(new Money(29.99D));
    product2.setDefaultSku(sku2);
    CategoryProductXref xref2 = new CategoryProductXrefImpl();
    xref2.setProduct(product2);
    xref2.setCategory(category2);
    category2.getAllProductXrefs().add(xref2);
    DiscreteOrderItemImpl orderItem1 = new DiscreteOrderItemImpl();
    orderItem1.setCategory(category1);
    orderItem1.setName("test1");
    orderItem1.setOrderItemType(OrderItemType.DISCRETE);
    orderItem1.setProduct(product1);
    orderItem1.setQuantity(2);
    orderItem1.setSku(sku1);
    Order order = new OrderImpl();
    orderItem1.setOrder(order);
    promotableOrder = new PromotableOrderImpl(order, new PromotableItemFactoryImpl(), false);
    offer = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))", "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))").get(0);
    candidateOffer = new PromotableCandidateItemOfferImpl(promotableOrder, offer);
    promotableOrderItem = new PromotableOrderItemImpl(orderItem1, null, new PromotableItemFactoryImpl(), false);
    OrderItemPriceDetail pdetail = new OrderItemPriceDetailImpl();
    pdetail.setOrderItem(orderItem1);
    pdetail.setQuantity(2);
    priceDetail = new PromotableOrderItemPriceDetailImpl(promotableOrderItem, 2);
    List<PromotableOrderItem> items = new ArrayList<PromotableOrderItem>();
    items.add(promotableOrderItem);
    promotableCandidate = new PromotableCandidateItemOfferImpl(promotableOrder, offer);
    OfferTargetCriteriaXref xref = offer.getTargetItemCriteriaXref().iterator().next();
    promotableCandidate.getCandidateTargetsMap().put(xref.getOfferItemCriteria(), items);
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) PromotableOrderImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) DiscreteOrderItemImpl(org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl) OrderItemPriceDetailImpl(org.broadleafcommerce.core.order.domain.OrderItemPriceDetailImpl) PromotableOrderItemPriceDetailImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetailImpl) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) PromotableCandidateItemOfferImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) Money(org.broadleafcommerce.common.money.Money) PromotableItemFactoryImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableItemFactoryImpl) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) Order(org.broadleafcommerce.core.order.domain.Order) PromotableOrderItemImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemImpl) CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) PromotableOrderImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl) OrderImpl(org.broadleafcommerce.core.order.domain.OrderImpl) PromotableCandidateItemOfferImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl) OfferDataItemProvider(org.broadleafcommerce.core.offer.service.OfferDataItemProvider) OrderItemPriceDetail(org.broadleafcommerce.core.order.domain.OrderItemPriceDetail) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail) Sku(org.broadleafcommerce.core.catalog.domain.Sku) PromotableOrderItemPriceDetailImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetailImpl)

Example 2 with CategoryProductXrefImpl

use of org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl in project BroadleafCommerce by BroadleafCommerce.

the class MVELTest method createProduct.

private Product createProduct() {
    Category category = new CategoryImpl();
    category.setName("t-shirt");
    category = catalogService.saveCategory(category);
    Product product = new ProductImpl();
    Sku sku = new SkuImpl();
    sku = catalogService.saveSku(sku);
    product.setDefaultSku(sku);
    product.setName("Lavender Soap");
    Calendar activeStartCal = Calendar.getInstance();
    activeStartCal.add(Calendar.DAY_OF_YEAR, -2);
    product.setActiveStartDate(activeStartCal.getTime());
    product.setCategory(category);
    product.getAllParentCategoryXrefs().clear();
    product = catalogService.saveProduct(product);
    CategoryProductXref categoryXref = new CategoryProductXrefImpl();
    categoryXref.setProduct(product);
    categoryXref.setCategory(category);
    product.getAllParentCategoryXrefs().add(categoryXref);
    product = catalogService.saveProduct(product);
    return product;
}
Also used : CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) Category(org.broadleafcommerce.core.catalog.domain.Category) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) Calendar(java.util.Calendar) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 3 with CategoryProductXrefImpl

use of org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl in project BroadleafCommerce by BroadleafCommerce.

the class MVELTest method testOfferAppliesToItemsInCategoryAndOrderValueGreaterThanFifty.

@Test
@Transactional
public void testOfferAppliesToItemsInCategoryAndOrderValueGreaterThanFifty() {
    // ----------------------------------------------------------------------------------------------------
    // Mock up some order data
    OrderImpl order = new OrderImpl();
    CategoryImpl category = new CategoryImpl();
    category.setName("t-shirt");
    Product product = createProduct();
    DiscreteOrderItemImpl orderItem = new DiscreteOrderItemImpl();
    ArrayList<CategoryProductXref> categories = new ArrayList<>();
    CategoryProductXref categoryXref = new CategoryProductXrefImpl();
    categoryXref.setProduct(product);
    categoryXref.setCategory(category);
    categories.add(categoryXref);
    product.setAllParentCategoryXrefs(categories);
    orderItem.setProduct(product);
    order.getOrderItems().add(orderItem);
    order.setSubTotal(new Money(110D));
    // Set up MVEL Context
    ParserContext context = new ParserContext();
    // Import OfferType into the MVEL context since it may be used
    context.addImport("OfferType", OfferType.class);
    // Compile the MVEL Expression
    Serializable domainExp1 = MVEL.compileExpression("result = false; for (cat : currentItem.product.allParentCategories) {if (cat.name == 't-shirt') {result = true;}}; return result and order.subTotal.amount >= 50", context);
    // Add variables to a HashMap that should be passed in to execute the expression
    HashMap<String, Object> domainVars = new HashMap<>();
    domainVars.put("order", order);
    domainVars.put("currentItem", orderItem);
    // Execute the expression
    Boolean expressionOutcome1 = (Boolean) MVEL.executeExpression(domainExp1, domainVars);
    assert expressionOutcome1 != null && expressionOutcome1;
    // Do the same thing using a different expression.
    Serializable domainExp2 = MVEL.compileExpression("($ in currentItem.product.allParentCategories if $.name == 't-shirt') != empty and order.subTotal.amount >= 50", context);
    Boolean expressionOutcome2 = (Boolean) MVEL.executeExpression(domainExp2, domainVars);
    assert expressionOutcome2 != null && expressionOutcome2;
}
Also used : Serializable(java.io.Serializable) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) DiscreteOrderItemImpl(org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) Money(org.broadleafcommerce.common.money.Money) OrderImpl(org.broadleafcommerce.core.order.domain.OrderImpl) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) ParserContext(org.mvel2.ParserContext) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with CategoryProductXrefImpl

use of org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl in project BroadleafCommerce by BroadleafCommerce.

the class ProductCustomPersistenceHandler method setupXref.

protected void setupXref(Product adminInstance) {
    if (isDefaultCategoryLegacyMode()) {
        CategoryProductXref categoryXref = new CategoryProductXrefImpl();
        categoryXref.setCategory(getExistingDefaultCategory(adminInstance));
        categoryXref.setProduct(adminInstance);
        if (!adminInstance.getAllParentCategoryXrefs().contains(categoryXref) && categoryXref.getCategory() != null) {
            adminInstance.getAllParentCategoryXrefs().add(categoryXref);
        }
    }
}
Also used : CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)

Example 5 with CategoryProductXrefImpl

use of org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl 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)

Aggregations

CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)9 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)8 Product (org.broadleafcommerce.core.catalog.domain.Product)7 Category (org.broadleafcommerce.core.catalog.domain.Category)6 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)6 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)6 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 ArrayList (java.util.ArrayList)5 Money (org.broadleafcommerce.common.money.Money)5 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)5 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)4 OrderImpl (org.broadleafcommerce.core.order.domain.OrderImpl)4 PromotableOrder (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder)3 PromotableOrderImpl (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl)3 Order (org.broadleafcommerce.core.order.domain.Order)3 OrderItemPriceDetail (org.broadleafcommerce.core.order.domain.OrderItemPriceDetail)3 OrderItemPriceDetailImpl (org.broadleafcommerce.core.order.domain.OrderItemPriceDetailImpl)3 Calendar (java.util.Calendar)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Predicate (javax.persistence.criteria.Predicate)2