use of com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer in project shopizer by shopizer-ecommerce.
the class ReadableProductMapper method merge.
@Override
public ReadableProduct merge(Product source, ReadableProduct destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product cannot be null");
Validate.notNull(destination, "Product destination cannot be null");
destination.setSku(source.getSku());
destination.setRefSku(source.getRefSku());
destination.setId(source.getId());
destination.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;
}
}
destination.setId(source.getId());
destination.setAvailable(source.isAvailable());
destination.setProductShipeable(source.isProductShipeable());
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
destination.setProductSpecifications(specifications);
destination.setPreOrder(source.isPreOrder());
destination.setRefSku(source.getRefSku());
destination.setSortOrder(source.getSortOrder());
if (source.getType() != null) {
ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
destination.setType(readableType);
}
if (source.getDateAvailable() != null) {
destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
}
if (source.getAuditSection() != null) {
destination.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
}
destination.setProductVirtual(source.getProductVirtual());
if (source.getProductReviewCount() != null) {
destination.setRatingCount(source.getProductReviewCount().intValue());
}
if (source.getManufacturer() != null) {
ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
destination.setManufacturer(manufacturer);
}
// 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());
destination.setImages(imageList);
}
// read only product values
if (!CollectionUtils.isEmpty(source.getAttributes())) {
Set<ProductAttribute> attributes = source.getAttributes();
// split read only and options
// Map<Long,ReadableProductAttribute> readOnlyAttributes = null;
Map<Long, ReadableProductProperty> properties = null;
Map<Long, ReadableProductOption> selectableOptions = null;
if (!CollectionUtils.isEmpty(attributes)) {
for (ProductAttribute attribute : attributes) {
ReadableProductOption opt = null;
ReadableProductAttribute attr = null;
ReadableProductProperty property = null;
ReadableProductPropertyValue propertyValue = null;
ReadableProductOptionValueEntity optValue = new ReadableProductOptionValueEntity();
ReadableProductAttributeValue attrValue = new ReadableProductAttributeValue();
ProductOptionValue optionValue = attribute.getProductOptionValue();
// we need to set readonly attributes only
if (attribute.getAttributeDisplayOnly()) {
// read only attribute = property
property = createProperty(attribute, language);
// that is the property
ReadableProductOption readableOption = new ReadableProductOption();
ReadableProductPropertyValue readableOptionValue = new ReadableProductPropertyValue();
readableOption.setCode(attribute.getProductOption().getCode());
readableOption.setId(attribute.getProductOption().getId());
Set<ProductOptionDescription> podescriptions = attribute.getProductOption().getDescriptions();
if (podescriptions != null && podescriptions.size() > 0) {
for (ProductOptionDescription optionDescription : podescriptions) {
if (optionDescription.getLanguage().getCode().equals(language.getCode())) {
readableOption.setName(optionDescription.getName());
}
}
}
property.setProperty(readableOption);
Set<ProductOptionValueDescription> povdescriptions = attribute.getProductOptionValue().getDescriptions();
readableOptionValue.setId(attribute.getProductOptionValue().getId());
if (povdescriptions != null && povdescriptions.size() > 0) {
for (ProductOptionValueDescription optionValueDescription : povdescriptions) {
if (optionValueDescription.getLanguage().getCode().equals(language.getCode())) {
readableOptionValue.setName(optionValueDescription.getName());
}
}
}
property.setPropertyValue(readableOptionValue);
destination.getProperties().add(property);
}
if (selectableOptions != null) {
List<ReadableProductOption> options = new ArrayList<ReadableProductOption>(selectableOptions.values());
destination.setOptions(options);
}
}
}
}
}
// availability
ProductAvailability availability = null;
for (ProductAvailability a : source.getAvailabilities()) {
// TODO validate region
// if(availability.getRegion().equals(Constants.ALL_REGIONS)) {//TODO REL 2.1
// accept a region
availability = a;
destination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
destination.setQuantityOrderMaximum(availability.getProductQuantityOrderMax() == null ? 1 : availability.getProductQuantityOrderMax());
destination.setQuantityOrderMinimum(availability.getProductQuantityOrderMin() == null ? 1 : availability.getProductQuantityOrderMin());
if (availability.getProductQuantity().intValue() > 0 && destination.isAvailable()) {
destination.setCanBePurchased(true);
}
// }
}
destination.setSku(source.getSku());
try {
FinalPrice price = pricingService.calculateProductPrice(source);
if (price != null) {
destination.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
destination.setPrice(price.getFinalPrice());
destination.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
if (price.isDiscounted()) {
destination.setDiscounted(true);
}
// price appender
if (availability != null) {
Set<ProductPrice> prices = availability.getPrices();
if (!CollectionUtils.isEmpty(prices)) {
ReadableProductPrice readableProductPrice = new ReadableProductPrice();
readableProductPrice.setDiscounted(destination.isDiscounted());
readableProductPrice.setFinalPrice(destination.getFinalPrice());
readableProductPrice.setOriginalPrice(destination.getOriginalPrice());
Optional<ProductPrice> pr = prices.stream().filter(p -> p.getCode().equals(ProductPrice.DEFAULT_PRICE_CODE)).findFirst();
destination.setProductPrice(readableProductPrice);
if (pr.isPresent()) {
readableProductPrice.setId(pr.get().getId());
Optional<ProductPriceDescription> d = pr.get().getDescriptions().stream().filter(desc -> desc.getLanguage().getCode().equals(language.getCode())).findFirst();
if (d.isPresent()) {
com.salesmanager.shop.model.catalog.product.ProductPriceDescription priceDescription = new com.salesmanager.shop.model.catalog.product.ProductPriceDescription();
priceDescription.setLanguage(language.getCode());
priceDescription.setId(d.get().getId());
priceDescription.setPriceAppender(d.get().getPriceAppender());
readableProductPrice.setDescription(priceDescription);
}
}
}
}
}
} catch (Exception e) {
throw new ConversionRuntimeException("An error while converting product price", e);
}
if (source.getProductReviewAvg() != null) {
double avg = source.getProductReviewAvg().doubleValue();
double rating = Math.round(avg * 2) / 2.0f;
destination.setRating(rating);
}
if (source.getProductReviewCount() != null) {
destination.setRatingCount(source.getProductReviewCount().intValue());
}
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
destination.setDescription(tragetDescription);
}
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);
}
destination.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()));
}
destination.setProductSpecifications(specifications);
destination.setSortOrder(source.getSortOrder());
return destination;
}
use of com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer in project shopizer by shopizer-ecommerce.
the class CategoryManagementAPIIntegrationTest method manufacturerForItemsInCategory.
@Test
public void manufacturerForItemsInCategory() throws Exception {
ObjectWriter writer = new ObjectMapper().writer().withDefaultPrettyPrinter();
// create first manufacturer
PersistableManufacturer m1 = super.manufacturer("BRAND1");
String json = writer.writeValueAsString(m1);
HttpEntity<String> entity = new HttpEntity<>(json, getHeader());
@SuppressWarnings("rawtypes") ResponseEntity response = testRestTemplate.postForEntity("/api/v1/private/manufacturer", entity, PersistableManufacturer.class);
assertThat(response.getStatusCode(), is(CREATED));
// create second manufacturer
PersistableManufacturer m2 = super.manufacturer("BRAND2");
json = writer.writeValueAsString(m2);
entity = new HttpEntity<>(json, getHeader());
response = testRestTemplate.postForEntity("/api/v1/private/manufacturer", entity, PersistableManufacturer.class);
assertThat(response.getStatusCode(), is(CREATED));
// create category
PersistableCategory category = super.category("TEST");
// to be used in product
Category cat = new Category();
cat.setCode("TEST");
json = writer.writeValueAsString(category);
entity = new HttpEntity<>(json, getHeader());
@SuppressWarnings("rawtypes") ResponseEntity categoryResponse = testRestTemplate.postForEntity("/api/v1/private/category", entity, PersistableCategory.class);
assertThat(categoryResponse.getStatusCode(), is(CREATED));
final PersistableCategory persistable = (PersistableCategory) categoryResponse.getBody();
Long id = persistable.getId();
// create first item
PersistableProduct product1 = super.product("PRODUCT1");
product1.getCategories().add(cat);
ProductSpecification specifications = new ProductSpecification();
specifications.setManufacturer("BRAND1");
product1.setProductSpecifications(specifications);
json = writer.writeValueAsString(product1);
entity = new HttpEntity<>(json, getHeader());
response = testRestTemplate.postForEntity("/api/v1/private/product?store=" + Constants.DEFAULT_STORE, entity, PersistableProduct.class);
assertThat(response.getStatusCode(), is(CREATED));
// create second item
PersistableProduct product2 = super.product("PRODUCT2");
product2.getCategories().add(cat);
specifications = new ProductSpecification();
specifications.setManufacturer("BRAND2");
product2.setProductSpecifications(specifications);
json = writer.writeValueAsString(product2);
entity = new HttpEntity<>(json, getHeader());
response = testRestTemplate.postForEntity("/api/v1/private/product?store=" + Constants.DEFAULT_STORE, entity, PersistableProduct.class);
assertThat(response.getStatusCode(), is(CREATED));
entity = new HttpEntity<>(getHeader());
// get manufacturers in category
@SuppressWarnings("rawtypes") ResponseEntity<List> manufacturers = testRestTemplate.exchange(String.format("/api/v1/category/" + id + "/manufacturers"), HttpMethod.GET, entity, List.class);
assertThat(manufacturers.getStatusCode(), is(OK));
@SuppressWarnings("unchecked") List<ReadableManufacturer> manufacturerList = manufacturers.getBody();
// assertFalse(manufacturerList.isEmpty());
}
use of com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method displayCategory.
@SuppressWarnings("unchecked")
private String displayCategory(final String friendlyUrl, final String ref, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
// set ref as request attribute
String encoded = SanitizeUtils.getSafeRequestParamString(ref);
if (!encoded.equals(ref)) {
// possible xss
throw new Exception("Wrong input parameter [" + ref + "]");
}
request.setAttribute("ref", encoded);
// get category
Category category = categoryService.getBySeUrl(store, friendlyUrl);
Language language = (Language) request.getAttribute("LANGUAGE");
if (category == null) {
LOGGER.error("No category found for friendlyUrl " + friendlyUrl);
// redirect on page not found
return PageBuilderUtils.build404(store);
}
if (!category.isVisible()) {
return PageBuilderUtils.buildHomePage(store);
}
ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
ReadableCategory categoryProxy = populator.populate(category, new ReadableCategory(), store, language);
Breadcrumb breadCrumb = breadcrumbsUtils.buildCategoryBreadcrumb(categoryProxy, store, language, request.getContextPath());
request.getSession().setAttribute(Constants.BREADCRUMB, breadCrumb);
request.setAttribute(Constants.BREADCRUMB, breadCrumb);
// meta information
PageInformation pageInformation = new PageInformation();
pageInformation.setPageDescription(categoryProxy.getDescription().getMetaDescription());
pageInformation.setPageKeywords(categoryProxy.getDescription().getKeyWords());
pageInformation.setPageTitle(categoryProxy.getDescription().getTitle());
pageInformation.setPageUrl(categoryProxy.getDescription().getFriendlyUrl());
// ** retrieves category id drill down**//
String lineage = new StringBuilder().append(category.getLineage()).append(Constants.CATEGORY_LINEAGE_DELIMITER).toString();
request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
List<Category> categs = categoryService.getListByLineage(store, lineage);
categs.add(category);
StringBuilder subCategoriesCacheKey = new StringBuilder();
subCategoriesCacheKey.append(store.getId()).append("_").append(category.getId()).append("_").append(Constants.SUBCATEGORIES_CACHE_KEY).append("-").append(language.getCode());
StringBuilder subCategoriesMissed = new StringBuilder();
subCategoriesMissed.append(subCategoriesCacheKey.toString()).append(Constants.MISSED_CACHE_KEY);
List<BigDecimal> prices = new ArrayList<BigDecimal>();
List<ReadableCategory> subCategories = null;
Map<Long, Long> countProductsByCategories = null;
if (store.isUseCache()) {
// get from the cache
subCategories = (List<ReadableCategory>) cache.getFromCache(subCategoriesCacheKey.toString());
if (subCategories == null) {
// get from missed cache
// Boolean missedContent = (Boolean)cache.getFromCache(subCategoriesMissed.toString());
// if(missedContent==null) {
countProductsByCategories = getProductsByCategory(store, categs);
subCategories = getSubCategories(store, category, countProductsByCategories, language, locale);
if (subCategories != null) {
cache.putInCache(subCategories, subCategoriesCacheKey.toString());
} else {
// cache.putInCache(new Boolean(true), subCategoriesCacheKey.toString());
}
// }
}
} else {
countProductsByCategories = getProductsByCategory(store, categs);
subCategories = getSubCategories(store, category, countProductsByCategories, language, locale);
}
// Parent category
ReadableCategory parentProxy = null;
if (category.getParent() != null) {
Category parent = categoryService.getById(category.getParent().getId(), store.getId());
parentProxy = populator.populate(parent, new ReadableCategory(), store, language);
}
// ** List of manufacturers **//
List<ReadableManufacturer> manufacturerList = getManufacturersByProductAndCategory(store, category, categs, language);
model.addAttribute("manufacturers", manufacturerList);
model.addAttribute("parent", parentProxy);
model.addAttribute("category", categoryProxy);
model.addAttribute("subCategories", subCategories);
if (parentProxy != null) {
request.setAttribute(Constants.LINK_CODE, parentProxy.getDescription().getFriendlyUrl());
}
/**
* template *
*/
StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Category.category).append(".").append(store.getStoreTemplate());
return template.toString();
}
Aggregations