use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class SearchServiceImpl method index.
@Async
@SuppressWarnings("rawtypes")
public void index(MerchantStore store, Product product) throws ServiceException {
if (configuration.getProperty(INDEX_PRODUCTS) == null || configuration.getProperty(INDEX_PRODUCTS).equals(Constants.FALSE)) {
return;
}
FinalPrice price = pricingService.calculateProductPrice(product);
Set<ProductDescription> descriptions = product.getDescriptions();
for (ProductDescription description : descriptions) {
StringBuilder collectionName = new StringBuilder();
collectionName.append(PRODUCT_INDEX_NAME).append(UNDERSCORE).append(description.getLanguage().getCode()).append(UNDERSCORE).append(store.getCode().toLowerCase());
IndexProduct index = new IndexProduct();
index.setId(String.valueOf(product.getId()));
index.setStore(store.getCode().toLowerCase());
index.setLang(description.getLanguage().getCode());
index.setAvailable(product.isAvailable());
index.setDescription(description.getDescription());
index.setName(description.getName());
if (product.getManufacturer() != null) {
index.setManufacturer(String.valueOf(product.getManufacturer().getId()));
}
if (price != null) {
index.setPrice(price.getFinalPrice().doubleValue());
}
index.setHighlight(description.getProductHighlight());
if (!StringUtils.isBlank(description.getMetatagKeywords())) {
String[] tags = description.getMetatagKeywords().split(",");
@SuppressWarnings("unchecked") List<String> tagsList = new ArrayList(Arrays.asList(tags));
index.setTags(tagsList);
}
Set<Category> categories = product.getCategories();
if (!CollectionUtils.isEmpty(categories)) {
List<String> categoryList = new ArrayList<String>();
for (Category category : categories) {
categoryList.add(category.getCode());
}
index.setCategories(categoryList);
}
String jsonString = index.toJSONString();
try {
searchService.index(jsonString, collectionName.toString());
} catch (Exception e) {
throw new ServiceException("Cannot index product id [" + product.getId() + "], " + e.getMessage(), e);
}
}
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class PersistableProductPopulator method populate.
@Override
public Product populate(PersistableProduct source, Product target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(target, "Product must not be null");
try {
target.setSku(source.getSku());
target.setAvailable(source.isAvailable());
target.setPreOrder(source.isPreOrder());
target.setRefSku(source.getRefSku());
if (source.getId() != null && source.getId().longValue() == 0) {
target.setId(null);
} else {
target.setId(source.getId());
}
// 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");
}
target.setType(type);
}
if (source.getOwner() != null && source.getOwner().getId() != null) {
com.salesmanager.core.model.customer.Customer owner = customerService.getById(source.getOwner().getId());
target.setOwner(owner);
}
if (!StringUtils.isBlank(source.getDateAvailable())) {
target.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
}
target.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(target.getDescriptions())) {
for (ProductDescription desc : target.getDescriptions()) {
if (desc.getLanguage().getCode().equals(description.getLanguage())) {
productDescription = desc;
break;
}
}
}
productDescription.setProduct(target);
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) {
target.setDescriptions(descriptions);
}
if (source.getProductSpecifications() != null) {
target.setProductHeight(source.getProductSpecifications().getHeight());
target.setProductLength(source.getProductSpecifications().getLength());
target.setProductWeight(source.getProductSpecifications().getWeight());
target.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");
}
target.setManufacturer(manuf);
}
}
}
target.setSortOrder(source.getSortOrder());
target.setProductVirtual(source.isProductVirtual());
target.setProductShipeable(source.isProductShipeable());
if (source.getRating() != null) {
target.setProductReviewAvg(new BigDecimal(source.getRating()));
}
target.setProductReviewCount(source.getRatingCount());
if (CollectionUtils.isNotEmpty(source.getProductPrices())) {
// get product availability
// create new ProductAvailability
ProductAvailability productAvailability = new ProductAvailability(target, store);
// todo now support for specific regions
productAvailability.setRegion(Constants.ALL_REGIONS);
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductQuantityOrderMin(1);
productAvailability.setProductQuantityOrderMax(1);
productAvailability.setAvailable(Boolean.valueOf(target.isAvailable()));
for (com.salesmanager.shop.model.catalog.product.PersistableProductPrice priceEntity : source.getProductPrices()) {
ProductPrice price = new ProductPrice();
price.setProductAvailability(productAvailability);
price.setDefaultPrice(priceEntity.isDefaultPrice());
price.setProductPriceAmount(priceEntity.getOriginalPrice());
price.setCode(priceEntity.getCode());
price.setProductPriceSpecialAmount(priceEntity.getDiscountedPrice());
if (priceEntity.getDiscountStartDate() != null) {
Date startDate = DateUtil.getDate(priceEntity.getDiscountStartDate());
price.setProductPriceSpecialStartDate(startDate);
}
if (priceEntity.getDiscountEndDate() != null) {
Date endDate = DateUtil.getDate(priceEntity.getDiscountEndDate());
price.setProductPriceSpecialEndDate(endDate);
}
productAvailability.getPrices().add(price);
target.getAvailabilities().add(productAvailability);
for (Language lang : languages) {
ProductPriceDescription ppd = new ProductPriceDescription();
ppd.setProductPrice(price);
ppd.setLanguage(lang);
ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
// price appender
Optional<com.salesmanager.shop.model.catalog.product.ProductPriceDescription> description = priceEntity.getDescriptions().stream().filter(d -> d.getLanguage() != null && d.getLanguage().equals(lang.getCode())).findFirst();
if (description.isPresent()) {
ppd.setPriceAppender(description.get().getPriceAppender());
}
price.getDescriptions().add(ppd);
}
}
} else {
// create
ProductAvailability productAvailability = null;
ProductPrice defaultPrice = null;
if (!CollectionUtils.isEmpty(target.getAvailabilities())) {
for (ProductAvailability avail : target.getAvailabilities()) {
Set<ProductPrice> prices = avail.getPrices();
for (ProductPrice p : prices) {
if (p.isDefaultPrice()) {
if (productAvailability == null) {
productAvailability = avail;
defaultPrice = p;
break;
}
p.setDefaultPrice(false);
}
}
}
}
if (productAvailability == null) {
productAvailability = new ProductAvailability(target, store);
target.getAvailabilities().add(productAvailability);
}
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductQuantityOrderMin(1);
productAvailability.setProductQuantityOrderMax(1);
productAvailability.setRegion(Constants.ALL_REGIONS);
productAvailability.setAvailable(Boolean.valueOf(target.isAvailable()));
if (defaultPrice != null) {
defaultPrice.setProductPriceAmount(source.getPrice());
} else {
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);
}
}
}
// image
if (source.getImages() != null) {
for (PersistableImage img : source.getImages()) {
ProductImage productImage = new ProductImage();
productImage.setImageType(img.getImageType());
productImage.setDefaultImage(img.isDefaultImage());
if (img.getImageType() == 1) {
productImage.setProductImageUrl(img.getImageUrl());
productImage.setImage(new ByteArrayInputStream(new byte[0]));
} else {
ByteArrayInputStream in = new ByteArrayInputStream(img.getBytes());
productImage.setImage(in);
}
productImage.setProduct(target);
productImage.setProductImage(img.getName());
target.getImages().add(productImage);
}
}
// attributes
if (source.getAttributes() != null) {
for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getAttributes()) {
ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
attribute.setProduct(target);
target.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");
}
target.getCategories().add(c);
}
}
return target;
} catch (Exception e) {
throw new ConversionException(e);
}
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ReadableCategoryPopulator method populate.
@Override
public ReadableCategory populate(final Category source, final ReadableCategory target, final MerchantStore store, final Language language) throws ConversionException {
Validate.notNull(source, "Category must not be null");
target.setLineage(source.getLineage());
if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
CategoryDescription description = source.getDescription();
if (source.getDescriptions().size() > 1) {
for (final CategoryDescription desc : source.getDescriptions()) {
if (desc.getLanguage().getCode().equals(language.getCode())) {
description = desc;
break;
}
}
}
if (description != null) {
final com.salesmanager.shop.model.catalog.category.CategoryDescription desc = new com.salesmanager.shop.model.catalog.category.CategoryDescription();
desc.setFriendlyUrl(description.getSeUrl());
desc.setName(description.getName());
desc.setId(source.getId());
desc.setDescription(description.getDescription());
desc.setKeyWords(description.getMetatagKeywords());
desc.setHighlights(description.getCategoryHighlight());
desc.setTitle(description.getMetatagTitle());
desc.setMetaDescription(description.getMetatagDescription());
target.setDescription(desc);
}
}
if (source.getParent() != null) {
final com.salesmanager.shop.model.catalog.category.Category parent = new com.salesmanager.shop.model.catalog.category.Category();
parent.setCode(source.getParent().getCode());
parent.setId(source.getParent().getId());
target.setParent(parent);
}
target.setCode(source.getCode());
target.setId(source.getId());
if (source.getDepth() != null) {
target.setDepth(source.getDepth());
}
target.setSortOrder(source.getSortOrder());
target.setVisible(source.isVisible());
target.setFeatured(source.isFeatured());
return target;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ReadableProductDefinitionMapper method merge.
@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product cannot be null");
Validate.notNull(destination, "Product destination cannot be null");
ReadableProductDefinition returnDestination = destination;
if (language == null) {
returnDestination = new ReadableProductDefinitionFull();
}
List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
returnDestination.setIdentifier(source.getSku());
returnDestination.setId(source.getId());
returnDestination.setVisible(source.isAvailable());
returnDestination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
ProductDescription description = null;
if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
for (ProductDescription desc : source.getDescriptions()) {
if (language != null && desc.getLanguage() != null && desc.getLanguage().getId().intValue() == language.getId().intValue()) {
description = desc;
break;
} else {
fulldescriptions.add(populateDescription(desc));
}
}
}
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
returnDestination.setDescription(tragetDescription);
}
if (source.getManufacturer() != null) {
ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
returnDestination.setManufacturer(manufacturer);
}
if (!CollectionUtils.isEmpty(source.getCategories())) {
List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
for (Category category : source.getCategories()) {
ReadableCategory readableCategory = readableCategoryMapper.convert(category, store, language);
categoryList.add(readableCategory);
}
returnDestination.setCategories(categoryList);
}
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
if (!StringUtils.isBlank(store.getSeizeunitcode())) {
specifications.setDimensionUnitOfMeasure(DimensionUnitOfMeasure.valueOf(store.getSeizeunitcode().toLowerCase()));
}
if (!StringUtils.isBlank(store.getWeightunitcode())) {
specifications.setWeightUnitOfMeasure(WeightUnitOfMeasure.valueOf(store.getWeightunitcode().toLowerCase()));
}
returnDestination.setProductSpecifications(specifications);
if (source.getType() != null) {
ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
returnDestination.setType(readableType);
}
returnDestination.setSortOrder(source.getSortOrder());
// images
Set<ProductImage> images = source.getImages();
if (CollectionUtils.isNotEmpty(images)) {
List<ReadableImage> imageList = images.stream().map(i -> this.convertImage(source, i, store)).collect(Collectors.toList());
returnDestination.setImages(imageList);
}
// quantity
ProductAvailability availability = null;
for (ProductAvailability a : source.getAvailabilities()) {
availability = a;
returnDestination.setCanBePurchased(availability.getProductStatus());
returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
}
FinalPrice price = null;
try {
price = pricingService.calculateProductPrice(source);
} catch (ServiceException e) {
throw new ConversionRuntimeException("Unable to get product price", e);
}
if (price != null) {
returnDestination.setPrice(price.getStringPrice());
}
if (returnDestination instanceof ReadableProductDefinitionFull) {
((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
}
return returnDestination;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class PersistableCatalogCategoryEntryMapper method merge.
@Override
public CatalogCategoryEntry merge(PersistableCatalogCategoryEntry source, CatalogCategoryEntry destination, MerchantStore store, Language language) {
Validate.notNull(source, "CatalogEntry must not be null");
Validate.notNull(store, "MerchantStore must not be null");
Validate.notNull(source.getProductCode(), "ProductCode must not be null");
Validate.notNull(source.getCategoryCode(), "CategoryCode must not be null");
Validate.notNull(source.getCatalog(), "Catalog must not be null");
if (destination == null) {
destination = new CatalogCategoryEntry();
}
destination.setId(source.getId());
destination.setVisible(source.isVisible());
try {
String catalog = source.getCatalog();
Catalog catalogModel = catalogFacade.getCatalog(catalog, store);
if (catalogModel == null) {
throw new ConversionRuntimeException("Error while converting CatalogEntry product [" + source.getCatalog() + "] not found");
}
destination.setCatalog(catalogModel);
/* Product productModel = productFacade.getProduct(source.getProductCode(), store);
if(productModel == null) {
throw new ConversionRuntimeException("Error while converting CatalogEntry product [" + source.getProductCode() + "] not found");
}*/
// destination.setProduct(productModel);
Category categoryModel = categoryFacade.getByCode(source.getCategoryCode(), store);
if (categoryModel == null) {
throw new ConversionRuntimeException("Error while converting CatalogEntry category [" + source.getCategoryCode() + "] not found");
}
destination.setCategory(categoryModel);
} catch (Exception e) {
throw new ConversionRuntimeException("Error while converting CatalogEntry", e);
}
return destination;
}
Aggregations