Search in sources :

Example 1 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory 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;
}
Also used : DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) DimensionUnitOfMeasure(com.salesmanager.shop.model.references.DimensionUnitOfMeasure) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) Mapper(com.salesmanager.shop.mapper.Mapper) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType) Set(java.util.Set) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Collectors(java.util.stream.Collectors) Category(com.salesmanager.core.model.catalog.category.Category) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) WeightUnitOfMeasure(com.salesmanager.shop.model.references.WeightUnitOfMeasure) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ArrayList(java.util.ArrayList) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType)

Example 2 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.

the class ShoppingCategoryController method getSubCategories.

private List<ReadableCategory> getSubCategories(MerchantStore store, Category category, Map<Long, Long> productCount, Language language, Locale locale) throws Exception {
    // sub categories
    List<Category> subCategories = categoryService.listByParent(category, language);
    ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
    List<ReadableCategory> subCategoryProxies = new ArrayList<ReadableCategory>();
    for (Category sub : subCategories) {
        ReadableCategory cProxy = populator.populate(sub, new ReadableCategory(), store, language);
        if (productCount != null) {
            Long total = productCount.get(cProxy.getId());
            if (total != null) {
                cProxy.setProductCount(total.intValue());
            }
        }
        subCategoryProxies.add(cProxy);
    }
    return subCategoryProxies;
}
Also used : ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ArrayList(java.util.ArrayList) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)

Example 3 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.

the class StoreFilter method setTopCategories.

@SuppressWarnings("unchecked")
private void setTopCategories(MerchantStore store, Language language, HttpServletRequest request) throws Exception {
    StringBuilder categoriesKey = new StringBuilder();
    categoriesKey.append(store.getId()).append("_").append(Constants.CATEGORIES_CACHE_KEY).append("-").append(language.getCode());
    StringBuilder categoriesKeyMissed = new StringBuilder();
    categoriesKeyMissed.append(categoriesKey.toString()).append(Constants.MISSED_CACHE_KEY);
    // language code - List of category
    Map<String, List<ReadableCategory>> objects = null;
    List<ReadableCategory> loadedCategories = null;
    if (store.isUseCache()) {
        objects = (Map<String, List<ReadableCategory>>) webApplicationCache.getFromCache(categoriesKey.toString());
        if (objects == null) {
            // load categories
            ReadableCategoryList categoryList = categoryFacade.getCategoryHierarchy(store, null, 0, language, null, 0, // null
            200);
            loadedCategories = categoryList.getCategories();
            // filter out invisible category
            loadedCategories.stream().filter(cat -> cat.isVisible() == true).collect(Collectors.toList());
            objects = new ConcurrentHashMap<String, List<ReadableCategory>>();
            objects.put(language.getCode(), loadedCategories);
            webApplicationCache.putInCache(categoriesKey.toString(), objects);
        } else {
            loadedCategories = objects.get(language.getCode());
        }
    } else {
        ReadableCategoryList categoryList = categoryFacade.getCategoryHierarchy(store, null, 0, language, null, 0, // null // filter
        200);
        loadedCategories = categoryList.getCategories();
    }
    if (loadedCategories != null) {
        request.setAttribute(Constants.REQUEST_TOP_CATEGORIES, loadedCategories);
    }
}
Also used : ContentDescription(com.salesmanager.core.model.content.ContentDescription) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) LoggerFactory(org.slf4j.LoggerFactory) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) StringUtils(org.apache.commons.lang3.StringUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) Content(com.salesmanager.core.model.content.Content) UserContext(com.salesmanager.core.model.common.UserContext) ContentType(com.salesmanager.core.model.content.ContentType) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) MerchantStoreService(com.salesmanager.core.business.services.merchant.MerchantStoreService) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) CustomerService(com.salesmanager.core.business.services.customer.CustomerService) Breadcrumb(com.salesmanager.shop.model.shop.Breadcrumb) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription) ModelAndView(org.springframework.web.servlet.ModelAndView) BreadcrumbItem(com.salesmanager.shop.model.shop.BreadcrumbItem) CacheUtils(com.salesmanager.core.business.utils.CacheUtils) MerchantConfiguration(com.salesmanager.core.model.system.MerchantConfiguration) LanguageUtils(com.salesmanager.shop.utils.LanguageUtils) GeoLocationUtils(com.salesmanager.shop.utils.GeoLocationUtils) CoreConfiguration(com.salesmanager.core.business.utils.CoreConfiguration) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Address(com.salesmanager.shop.model.customer.address.Address) Authentication(org.springframework.security.core.Authentication) WebApplicationCacheUtils(com.salesmanager.shop.utils.WebApplicationCacheUtils) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) java.util(java.util) CategoryFacade(com.salesmanager.shop.store.controller.category.facade.CategoryFacade) Constants(com.salesmanager.shop.constants.Constants) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Language(com.salesmanager.core.model.reference.language.Language) Inject(javax.inject.Inject) BreadcrumbItemType(com.salesmanager.shop.model.shop.BreadcrumbItemType) HttpServletRequest(javax.servlet.http.HttpServletRequest) LabelUtils(com.salesmanager.shop.utils.LabelUtils) PageInformation(com.salesmanager.shop.model.shop.PageInformation) Nullable(javax.annotation.Nullable) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Customer(com.salesmanager.core.model.customer.Customer) MerchantConfig(com.salesmanager.core.model.system.MerchantConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) MerchantConfigurationService(com.salesmanager.core.business.services.system.MerchantConfigurationService) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) ContentService(com.salesmanager.core.business.services.content.ContentService) Category(com.salesmanager.core.model.catalog.category.Category) MerchantConfigurationType(com.salesmanager.core.model.system.MerchantConfigurationType) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList)

Example 4 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.

the class CategoryManagementAPIIntegrationTest method putCategory.

@Test
public void putCategory() throws Exception {
    // create
    PersistableCategory newCategory = new PersistableCategory();
    newCategory.setCode("angular");
    newCategory.setSortOrder(1);
    newCategory.setVisible(true);
    newCategory.setDepth(4);
    CategoryDescription description = new CategoryDescription();
    description.setLanguage("en");
    description.setName("angular");
    description.setFriendlyUrl("angular");
    description.setTitle("angular");
    List<CategoryDescription> descriptions = new ArrayList<>();
    descriptions.add(description);
    newCategory.setDescriptions(descriptions);
    final ObjectWriter writer = new ObjectMapper().writer().withDefaultPrettyPrinter();
    final String json = writer.writeValueAsString(newCategory);
    final HttpEntity<String> entity = new HttpEntity<>(json, getHeader());
    // create category
    final ResponseEntity response = testRestTemplate.postForEntity("/api/v1/private/category", entity, PersistableCategory.class);
    final PersistableCategory cat = (PersistableCategory) response.getBody();
    assertThat(response.getStatusCode(), is(CREATED));
    assertNotNull(cat.getId());
    HttpEntity<String> httpEntity = new HttpEntity<>(getHeader());
    final ResponseEntity<ReadableCategory> readableQuery = testRestTemplate.exchange(String.format("/api/v1/category/" + cat.getId()), HttpMethod.GET, httpEntity, ReadableCategory.class);
    assertThat(readableQuery.getStatusCode(), is(OK));
    ReadableCategory readableCategory = readableQuery.getBody();
    newCategory = new PersistableCategory();
    newCategory.setCode("angular");
    newCategory.setVisible(true);
    newCategory.setDepth(4);
    newCategory.setSortOrder(2);
    description = new CategoryDescription();
    description.setLanguage("en");
    description.setName("angular");
    description.setFriendlyUrl("angular");
    description.setTitle("angular");
    descriptions = new ArrayList<>();
    descriptions.add(description);
    newCategory.setDescriptions(descriptions);
    HttpEntity<PersistableCategory> requestUpdate = new HttpEntity<>(newCategory, getHeader());
    ResponseEntity resp = testRestTemplate.exchange("/api/v1/private/category/" + cat.getId(), HttpMethod.PUT, requestUpdate, Void.class);
    assertThat(resp.getStatusCode(), is(OK));
// update
}
Also used : PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ResponseEntity(org.springframework.http.ResponseEntity) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) CategoryDescription(com.salesmanager.shop.model.catalog.category.CategoryDescription) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory 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;
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription)

Aggregations

ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)25 Category (com.salesmanager.core.model.catalog.category.Category)18 Language (com.salesmanager.core.model.reference.language.Language)13 ArrayList (java.util.ArrayList)13 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)12 ReadableCategoryPopulator (com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)11 Collectors (java.util.stream.Collectors)8 List (java.util.List)7 CollectionUtils (org.apache.commons.collections4.CollectionUtils)7 Product (com.salesmanager.core.model.catalog.product.Product)6 Mapper (com.salesmanager.shop.mapper.Mapper)6 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)6 DateUtil (com.salesmanager.shop.utils.DateUtil)6 Map (java.util.Map)6 Optional (java.util.Optional)6 Validate (org.apache.commons.lang3.Validate)6 ConversionException (com.salesmanager.core.business.exception.ConversionException)5 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)5 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)5