use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class PersistableProductDefinitionMapper method merge.
@Override
public Product merge(PersistableProductDefinition source, Product destination, MerchantStore store, Language language) {
Validate.notNull(destination, "Product must not be null");
try {
destination.setSku(source.getIdentifier());
destination.setAvailable(source.isVisible());
destination.setDateAvailable(new Date());
destination.setRefSku(source.getIdentifier());
if (source.getId() != null && source.getId().longValue() == 0) {
destination.setId(null);
} else {
destination.setId(source.getId());
}
// MANUFACTURER
if (!StringUtils.isBlank(source.getManufacturer())) {
Manufacturer manufacturer = manufacturerService.getByCode(store, source.getManufacturer());
if (manufacturer == null) {
throw new ConversionException("Manufacturer [" + source.getManufacturer() + "] does not exist");
}
destination.setManufacturer(manufacturer);
}
// PRODUCT TYPE
if (!StringUtils.isBlank(source.getType())) {
ProductType type = productTypeService.getByCode(source.getType(), store, language);
if (type == null) {
throw new ConversionException("Product type [" + source.getType() + "] does not exist");
}
destination.setType(type);
}
if (!StringUtils.isBlank(source.getDateAvailable())) {
destination.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
}
destination.setMerchantStore(store);
List<Language> languages = new ArrayList<Language>();
Set<ProductDescription> descriptions = new HashSet<ProductDescription>();
if (!CollectionUtils.isEmpty(source.getDescriptions())) {
for (com.salesmanager.shop.model.catalog.product.ProductDescription description : source.getDescriptions()) {
ProductDescription productDescription = new ProductDescription();
Language lang = languageService.getByCode(description.getLanguage());
if (lang == null) {
throw new ConversionException("Language code " + description.getLanguage() + " is invalid, use ISO code (en, fr ...)");
}
if (!CollectionUtils.isEmpty(destination.getDescriptions())) {
for (ProductDescription desc : destination.getDescriptions()) {
if (desc.getLanguage().getCode().equals(description.getLanguage())) {
productDescription = desc;
break;
}
}
}
productDescription.setProduct(destination);
productDescription.setDescription(description.getDescription());
productDescription.setProductHighlight(description.getHighlights());
productDescription.setName(description.getName());
productDescription.setSeUrl(description.getFriendlyUrl());
productDescription.setMetatagKeywords(description.getKeyWords());
productDescription.setMetatagDescription(description.getMetaDescription());
productDescription.setTitle(description.getTitle());
languages.add(lang);
productDescription.setLanguage(lang);
descriptions.add(productDescription);
}
}
if (descriptions.size() > 0) {
destination.setDescriptions(descriptions);
}
// if(source.getRating() != null) {
// destination.setProductReviewAvg(new BigDecimal(source.getRating()));
// }
// destination.setProductReviewCount(source.getRatingCount());
/**
* Product definition
*/
ProductAvailability productAvailability = null;
ProductPrice defaultPrice = null;
if (!CollectionUtils.isEmpty(destination.getAvailabilities())) {
for (ProductAvailability avail : destination.getAvailabilities()) {
Set<ProductPrice> prices = avail.getPrices();
for (ProductPrice p : prices) {
if (p.isDefaultPrice()) {
if (productAvailability == null) {
productAvailability = avail;
defaultPrice = p;
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductStatus(source.isCanBePurchased());
p.setProductPriceAmount(source.getPrice());
break;
}
}
}
}
}
if (productAvailability == null) {
// create with default values
productAvailability = new ProductAvailability(destination, store);
destination.getAvailabilities().add(productAvailability);
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductQuantityOrderMin(1);
productAvailability.setProductQuantityOrderMax(1);
productAvailability.setRegion(Constants.ALL_REGIONS);
productAvailability.setAvailable(Boolean.valueOf(destination.isAvailable()));
productAvailability.setProductStatus(source.isCanBePurchased());
}
if (defaultPrice == null) {
defaultPrice = new ProductPrice();
defaultPrice.setDefaultPrice(true);
defaultPrice.setProductPriceAmount(source.getPrice());
defaultPrice.setCode(ProductPriceEntity.DEFAULT_PRICE_CODE);
defaultPrice.setProductAvailability(productAvailability);
productAvailability.getPrices().add(defaultPrice);
for (Language lang : languages) {
ProductPriceDescription ppd = new ProductPriceDescription();
ppd.setProductPrice(defaultPrice);
ppd.setLanguage(lang);
ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
defaultPrice.getDescriptions().add(ppd);
}
}
if (source.getProductSpecifications() != null) {
destination.setProductHeight(source.getProductSpecifications().getHeight());
destination.setProductLength(source.getProductSpecifications().getLength());
destination.setProductWeight(source.getProductSpecifications().getWeight());
destination.setProductWidth(source.getProductSpecifications().getWidth());
if (source.getProductSpecifications().getManufacturer() != null) {
Manufacturer manuf = null;
if (!StringUtils.isBlank(source.getProductSpecifications().getManufacturer())) {
manuf = manufacturerService.getByCode(store, source.getProductSpecifications().getManufacturer());
}
if (manuf == null) {
throw new ConversionException("Invalid manufacturer id");
}
if (manuf != null) {
if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid manufacturer id");
}
destination.setManufacturer(manuf);
}
}
}
destination.setSortOrder(source.getSortOrder());
destination.setProductVirtual(source.isVirtual());
destination.setProductShipeable(source.isShipeable());
// attributes
if (source.getProperties() != null) {
for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getProperties()) {
ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
attribute.setProduct(destination);
destination.getAttributes().add(attribute);
}
}
// categories
if (!CollectionUtils.isEmpty(source.getCategories())) {
for (com.salesmanager.shop.model.catalog.category.Category categ : source.getCategories()) {
Category c = null;
if (!StringUtils.isBlank(categ.getCode())) {
c = categoryService.getByCode(store, categ.getCode());
} else {
Validate.notNull(categ.getId(), "Category id nust not be null");
c = categoryService.getById(categ.getId(), store.getId());
}
if (c == null) {
throw new ConversionException("Category id " + categ.getId() + " does not exist");
}
if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid category id");
}
destination.getCategories().add(c);
}
}
return destination;
} catch (Exception e) {
throw new ConversionRuntimeException("Error converting product mapper", e);
}
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class CategoryRepositoryImpl method listByProduct.
@Override
public List<Category> listByProduct(MerchantStore store, Long product) {
StringBuilder qs = new StringBuilder();
qs.append("select category from Product product ");
qs.append("inner join product.categories category inner join product.merchantStore pm ");
qs.append("where product.id=:id and pm.id=:mid ");
qs.append("group by category.id");
String hql = qs.toString();
Query q = this.em.createQuery(hql);
q.setParameter("id", product);
q.setParameter("mid", store.getId());
@SuppressWarnings("unchecked") List<Category> c = q.getResultList();
return c;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class CategoryTest method testCategory.
/**
* This method creates multiple products using multiple catalog APIs
* @throws ServiceException
*/
@Test
public void testCategory() throws Exception {
Language en = languageService.getByCode("en");
Language fr = languageService.getByCode("fr");
MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
Category materingstuff = new Category();
materingstuff.setMerchantStore(store);
materingstuff.setCode("materingstuff");
CategoryDescription bookEnglishDescription = new CategoryDescription();
bookEnglishDescription.setName("Book");
bookEnglishDescription.setCategory(materingstuff);
bookEnglishDescription.setLanguage(en);
CategoryDescription bookFrenchDescription = new CategoryDescription();
bookFrenchDescription.setName("Livre");
bookFrenchDescription.setCategory(materingstuff);
bookFrenchDescription.setLanguage(fr);
Set<CategoryDescription> descriptions = new HashSet<CategoryDescription>();
descriptions.add(bookEnglishDescription);
descriptions.add(bookFrenchDescription);
materingstuff.setDescriptions(descriptions);
categoryService.create(materingstuff);
assertNotNull(materingstuff.getId());
Long bookId = materingstuff.getId();
Category fetchedBook = categoryService.getById(bookId, store.getId());
Assert.assertEquals(2, fetchedBook.getDescriptions().size());
// Clean up for other tests
categoryService.delete(materingstuff);
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ProductTest method testCreateRelationShip.
private void testCreateRelationShip(Product product) throws Exception {
MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
Language en = languageService.getByCode("en");
Manufacturer oreilley = manufacturerService.getByCode(store, "oreilley");
ProductType generalType = productTypeService.getProductType(ProductType.GENERAL_TYPE);
Category tech = categoryService.getByCode(store, "tech");
// create new related product
// PRODUCT 1
Product related = new Product();
related.setProductHeight(new BigDecimal(4));
related.setProductLength(new BigDecimal(3));
related.setProductWidth(new BigDecimal(1));
related.setSku("TB67891");
related.setManufacturer(oreilley);
related.setType(generalType);
related.setMerchantStore(store);
// Product description
ProductDescription description = new ProductDescription();
description.setName("Spring 4 in Action");
description.setLanguage(en);
description.setProduct(related);
product.getDescriptions().add(description);
// add category
product.getCategories().add(tech);
// Availability
ProductAvailability availability = new ProductAvailability();
availability.setProductDateAvailable(date);
availability.setProductQuantity(200);
availability.setRegion("*");
// associate with product
availability.setProduct(related);
// productAvailabilityService.create(availability);
related.getAvailabilities().add(availability);
ProductPrice dprice = new ProductPrice();
dprice.setDefaultPrice(true);
dprice.setProductPriceAmount(new BigDecimal(39.99));
dprice.setProductAvailability(availability);
ProductPriceDescription dpd = new ProductPriceDescription();
dpd.setName("Base price");
dpd.setProductPrice(dprice);
dpd.setLanguage(en);
dprice.getDescriptions().add(dpd);
availability.getPrices().add(dprice);
related.getAvailabilities().add(availability);
productService.save(related);
ProductRelationship relationship = new ProductRelationship();
relationship.setActive(true);
relationship.setCode("spring");
relationship.setProduct(product);
relationship.setRelatedProduct(related);
relationship.setStore(store);
// because relationships are nor joined fetched, make sure you query relationships first, then ad to an existing list
// so relationship and review are they only objects not joined fetch when querying a product
// need to do a subsequent query
List<ProductRelationship> relationships = productRelationshipService.listByProduct(product);
relationships.add(relationship);
product.setRelationships(new HashSet<ProductRelationship>(relationships));
productService.save(product);
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ProductCommonFacadeImpl method addProductToCategory.
@Override
public ReadableProduct addProductToCategory(Category category, Product product, Language language) {
Validate.notNull(category, "Category cannot be null");
Validate.notNull(product, "Product cannot be null");
// not alloweed if category already attached
List<Category> assigned = product.getCategories().stream().filter(cat -> cat.getId().longValue() == category.getId().longValue()).collect(Collectors.toList());
if (assigned.size() > 0) {
throw new OperationNotAllowedException("Category with id [" + category.getId() + "] already attached to product [" + product.getId() + "]");
}
product.getCategories().add(category);
ReadableProduct readableProduct = new ReadableProduct();
try {
productService.update(product);
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
populator.populate(product, readableProduct, product.getMerchantStore(), language);
} catch (Exception e) {
throw new RuntimeException("Exception when adding product [" + product.getId() + "] to category [" + category.getId() + "]", e);
}
return readableProduct;
}
Aggregations