use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class CommonSetupBaseTest method addProductBundle.
public ProductBundle addProductBundle() {
// Create the product
Product p = addTestProduct("bundleproduct1", "bundlecat1");
// Create the sku for the ProductBundle object
Sku bundleSku = catalogService.createSku();
bundleSku.setName(p.getName());
bundleSku.setRetailPrice(new Money(44.99));
bundleSku.setActiveStartDate(p.getActiveStartDate());
bundleSku.setActiveEndDate(p.getActiveEndDate());
bundleSku.setDiscountable(true);
// Create the ProductBundle and associate the sku
ProductBundle bundle = (ProductBundle) catalogService.createProduct(ProductType.BUNDLE);
bundle.setDefaultCategory(p.getDefaultCategory());
bundle.setDefaultSku(bundleSku);
bundle = (ProductBundle) catalogService.saveProduct(bundle);
// Reverse-associate the ProductBundle to the sku (Must be done this way because it's a
// bidirectional OneToOne relationship
// bundleSku.setDefaultProduct(bundle);
// catalogService.saveSku(bundleSku);
// Wrap the product/sku that is part of the bundle in a SkuBundleItem
SkuBundleItem skuBundleItem = new SkuBundleItemImpl();
skuBundleItem.setBundle(bundle);
skuBundleItem.setQuantity(1);
skuBundleItem.setSku(p.getDefaultSku());
// Add the SkuBundleItem to the ProductBundle
bundle.getSkuBundleItems().add(skuBundleItem);
bundle = (ProductBundle) catalogService.saveProduct(bundle);
return bundle;
}
use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class OrderTest method addBundleToOrder.
@Test(groups = { "addBundleToOrder" }, dependsOnGroups = { "addAnotherItemToOrder" })
@Rollback(false)
@Transactional
public void addBundleToOrder() throws AddToCartException {
numOrderItems++;
Sku sku = skuDao.readFirstSku();
Order order = orderService.findOrderById(orderId);
assert order != null;
assert sku.getId() != null;
ProductBundle bundleItem = addProductBundle();
OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO();
orderItemRequestDTO.setProductId(bundleItem.getId());
orderItemRequestDTO.setSkuId(bundleItem.getDefaultSku().getId());
orderItemRequestDTO.setQuantity(1);
order = orderService.addItem(order.getId(), orderItemRequestDTO, true);
BundleOrderItem item = (BundleOrderItem) orderService.findLastMatchingItem(order, bundleItem.getDefaultSku().getId(), null);
bundleOrderItemId = item.getId();
assert item != null;
assert item.getQuantity() == 1;
assert item.getDiscreteOrderItems().size() == 1;
}
use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class ProductDataProvider method getProduct.
private static Product getProduct(Long id) {
Calendar activeStartCal = Calendar.getInstance();
activeStartCal.add(Calendar.DAY_OF_YEAR, -2);
Product product = new ProductImpl();
Sku defaultSku = new SkuImpl();
defaultSku.setRetailPrice(new Money(BigDecimal.valueOf(15.0)));
defaultSku.setSalePrice(new Money(BigDecimal.valueOf(10.0)));
defaultSku.setActiveStartDate(activeStartCal.getTime());
product.setDefaultSku(defaultSku);
if (id == null) {
defaultSku.setName("productNameTest");
return product;
}
product.setId(id);
defaultSku.setName(id.toString());
defaultSku.setId(id);
return product;
}
use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class SearchFacetDaoImpl method readDistinctValuesForField.
@Override
public <T> List<T> readDistinctValuesForField(String fieldName, Class<T> fieldValueClass) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<T> criteria = builder.createQuery(fieldValueClass);
Root<ProductImpl> product = criteria.from(ProductImpl.class);
Path<Sku> sku = product.get("defaultSku");
Path<?> pathToUse;
if (fieldName.contains("defaultSku.")) {
pathToUse = sku;
fieldName = fieldName.substring("defaultSku.".length());
} else if (fieldName.contains("productAttributes.")) {
pathToUse = product.join("productAttributes");
fieldName = fieldName.substring("productAttributes.".length());
criteria.where(builder.equal(builder.lower(pathToUse.get("name").as(String.class)), fieldName.toLowerCase()));
fieldName = "value";
} else if (fieldName.contains("product.")) {
pathToUse = product;
fieldName = fieldName.substring("product.".length());
} else {
throw new IllegalArgumentException("Invalid facet fieldName specified: " + fieldName);
}
criteria.where(pathToUse.get(fieldName).as(fieldValueClass).isNotNull());
criteria.distinct(true).select(pathToUse.get(fieldName).as(fieldValueClass));
TypedQuery<T> query = em.createQuery(criteria);
query.setHint(QueryHints.HINT_CACHEABLE, true);
query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search");
return query.getResultList();
}
use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class ProductCustomPersistenceHandler method add.
@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Product adminInstance = (Product) Class.forName(entity.getType()[0]).newInstance();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
if (adminInstance instanceof ProductBundle) {
removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
}
adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
adminInstance = dynamicEntityDao.merge(adminInstance);
// any Sku fields, and thus a Sku would not be created. Product still needs a default Sku so instantiate one
if (adminInstance.getDefaultSku() == null) {
Sku newSku = catalogService.createSku();
dynamicEntityDao.persist(newSku);
adminInstance.setDefaultSku(newSku);
adminInstance = dynamicEntityDao.merge(adminInstance);
}
// also set the default product for the Sku
adminInstance.getDefaultSku().setDefaultProduct(adminInstance);
dynamicEntityDao.merge(adminInstance.getDefaultSku());
// if this is a Pre-Add, skip the rest of the method
if (entity.isPreAdd()) {
return helper.getRecord(adminProperties, adminInstance, null, null);
}
boolean handled = false;
if (extensionManager != null) {
ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForAdd(persistencePackage, adminInstance);
handled = ExtensionResultStatusType.NOT_HANDLED != result;
}
if (!handled) {
setupXref(adminInstance);
}
return helper.getRecord(adminProperties, adminInstance, null, null);
} catch (Exception e) {
throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
}
}
Aggregations