Search in sources :

Example 1 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ShoppingCartItemPopulator method populate.

@Override
public ShoppingCartItem populate(PersistableOrderProduct source, /**
 * TODO: Fix, target not used possible future bug ! *
 */
ShoppingCartItem target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "Requires to set productService");
    Validate.notNull(productAttributeService, "Requires to set productAttributeService");
    Validate.notNull(shoppingCartService, "Requires to set shoppingCartService");
    Product product = productService.getById(source.getProduct().getId());
    if (source.getAttributes() != null) {
        for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attr : source.getAttributes()) {
            ProductAttribute attribute = productAttributeService.getById(attr.getId());
            if (attribute == null) {
                throw new ConversionException("ProductAttribute with id " + attr.getId() + " is null");
            }
            if (attribute.getProduct().getId().longValue() != source.getProduct().getId().longValue()) {
                throw new ConversionException("ProductAttribute with id " + attr.getId() + " is not assigned to Product id " + source.getProduct().getId());
            }
            product.getAttributes().add(attribute);
        }
    }
    try {
        return shoppingCartService.populateShoppingCartItem(product);
    } catch (ServiceException e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) Product(com.salesmanager.core.model.catalog.product.Product) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)

Example 2 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ShopProductRESTController method getProducts.

private ReadableProductList getProducts(final int start, final int max, final String store, final String language, final String category, final List<QueryFilter> filters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        /**
         * How to Spring MVC Rest web service - ajax / jquery
         * http://codetutr.com/2013/04/09/spring-mvc-easy-rest-based-json-services-with-responsebody/
         */
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        Map<String, Language> langs = languageService.getLanguagesMap();
        if (merchantStore != null) {
            if (!merchantStore.getCode().equals(store)) {
                // reset for the current request
                merchantStore = null;
            }
        }
        if (merchantStore == null) {
            merchantStore = merchantStoreService.getByCode(store);
        }
        if (merchantStore == null) {
            LOGGER.error("Merchant store is null for code " + store);
            // TODO localized message
            response.sendError(503, "Merchant store is null for code " + store);
            return null;
        }
        Language lang = langs.get(language);
        if (lang == null) {
            lang = langs.get(Constants.DEFAULT_LANGUAGE);
        }
        ProductCriteria productCriteria = new ProductCriteria();
        productCriteria.setMaxCount(max);
        productCriteria.setStartIndex(start);
        // get the category by code
        if (!StringUtils.isBlank(category)) {
            Category cat = categoryService.getBySeUrl(merchantStore, category);
            if (cat == null) {
                LOGGER.error("Category " + category + " is null");
                // TODO localized message
                response.sendError(503, "Category is null");
                return null;
            }
            String lineage = new StringBuilder().append(cat.getLineage()).append(cat.getId()).append("/").toString();
            List<Category> categories = categoryService.getListByLineage(store, lineage);
            List<Long> ids = new ArrayList<Long>();
            if (categories != null && categories.size() > 0) {
                for (Category c : categories) {
                    ids.add(c.getId());
                }
            }
            ids.add(cat.getId());
            productCriteria.setCategoryIds(ids);
        }
        if (filters != null) {
            for (QueryFilter filter : filters) {
                if (filter.getFilterType().name().equals(QueryFilterType.BRAND.name())) {
                    // the only filter implemented
                    productCriteria.setManufacturerId(filter.getFilterId());
                }
            }
        }
        com.salesmanager.core.model.catalog.product.ProductList products = productService.listByStore(merchantStore, lang, productCriteria);
        ReadableProductPopulator populator = new ReadableProductPopulator();
        populator.setPricingService(pricingService);
        populator.setimageUtils(imageUtils);
        ReadableProductList productList = new ReadableProductList();
        for (Product product : products.getProducts()) {
            // create new proxy product
            ReadableProduct readProduct = populator.populate(product, new ReadableProduct(), merchantStore, lang);
            productList.getProducts().add(readProduct);
        }
        productList.setTotalPages(Math.toIntExact(products.getTotalCount()));
        return productList;
    } catch (Exception e) {
        LOGGER.error("Error while getting products", e);
        response.sendError(503, "An error occured while retrieving products " + e.getMessage());
    }
    return null;
}
Also used : ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) Category(com.salesmanager.core.model.catalog.category.Category) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) ArrayList(java.util.ArrayList) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) QueryFilter(com.salesmanager.shop.store.model.filter.QueryFilter) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 3 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductItemsRESTController method getProductItemsByGroup.

/**
 * Query for a product group
 * public/products/{store code}/products/group/{id}?lang=fr|en
 * no lang it will take session lang or default store lang
 * @param store
 * @param language
 * @param groupCode
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping("/public/{store}/products/group/{code}")
@ResponseBody
public ReadableProductList getProductItemsByGroup(@PathVariable String store, @PathVariable final String code, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        if (merchantStore != null) {
            if (!merchantStore.getCode().equals(store)) {
                // reset for the current request
                merchantStore = null;
            }
        }
        if (merchantStore == null) {
            merchantStore = merchantStoreService.getByCode(store);
        }
        if (merchantStore == null) {
            LOGGER.error("Merchant store is null for code " + store);
            // TODO localized message
            response.sendError(503, "Merchant store is null for code " + store);
            return null;
        }
        Language lang = languageUtils.getRequestLanguage(request, response);
        // get product group
        List<ProductRelationship> group = productRelationshipService.getByGroup(merchantStore, code, lang);
        if (group != null) {
            Date today = new Date();
            List<Long> ids = new ArrayList<Long>();
            for (ProductRelationship relationship : group) {
                Product product = relationship.getRelatedProduct();
                if (product.isAvailable() && DateUtil.dateBeforeEqualsDate(product.getDateAvailable(), today)) {
                    ids.add(product.getId());
                }
            }
            ReadableProductList list = productItemsFacade.listItemsByIds(merchantStore, lang, ids, 0, 0);
            return list;
        }
    } catch (Exception e) {
        LOGGER.error("Error while getting products", e);
        response.sendError(503, "An error occured while retrieving products " + e.getMessage());
    }
    return null;
}
Also used : ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) Language(com.salesmanager.core.model.reference.language.Language) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductImageApi method imageDetails.

/**
 * Patch image (change position)
 *
 * @param id
 * @param files
 * @param position
 * @param merchantStore
 * @param language
 * @throws IOException
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/private/products/{id}/image/{imageId}", "/auth/products/{id}/image/{id}" }, method = RequestMethod.PATCH)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void imageDetails(@PathVariable Long id, @PathVariable Long imageId, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
    try {
        Product p = productService.getById(id);
        if (p == null) {
            throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
        }
        if (p.getMerchantStore().getId() != merchantStore.getId()) {
            throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
        }
        Optional<ProductImage> productImage = productImageService.getProductImage(imageId, id, merchantStore);
        if (productImage.isPresent()) {
            productImage.get().setSortOrder(position);
            productImageService.updateProductImage(p, productImage.get());
        } else {
            throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
        }
    } catch (Exception e) {
        LOGGER.error("Error while deleting ProductImage", e);
        throw new ServiceRuntimeException("ProductImage [" + imageId + "] cannot be edited");
    }
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ShoppingCartModelPopulator method createCartItem.

private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCart cart, ShoppingCartItem shoppingCartItem, MerchantStore store) throws Exception {
    Product product = productService.getById(shoppingCartItem.getProductId());
    if (product == null) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not exist");
    }
    if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not belong to merchant " + store.getId());
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = new com.salesmanager.core.model.shoppingcart.ShoppingCartItem(cart, product);
    item.setQuantity(shoppingCartItem.getQuantity());
    item.setItemPrice(shoppingCartItem.getProductPrice());
    item.setShoppingCart(cart);
    // attributes
    List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
    if (!CollectionUtils.isEmpty(cartAttributes)) {
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> newAttributes = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem>();
        for (ShoppingCartAttribute attribute : cartAttributes) {
            ProductAttribute productAttribute = productAttributeService.getById(attribute.getAttributeId());
            if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
                com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                if (attribute.getAttributeId() > 0) {
                    attributeItem.setId(attribute.getId());
                }
                item.addAttributes(attributeItem);
            // newAttributes.add( attributeItem );
            }
        }
    // item.setAttributes( newAttributes );
    }
    return item;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) HashSet(java.util.HashSet)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)96 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)36 ArrayList (java.util.ArrayList)35 Language (com.salesmanager.core.model.reference.language.Language)31 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)30 ServiceException (com.salesmanager.core.business.exception.ServiceException)26 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)24 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)23 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)22 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)22 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)21 Date (java.util.Date)21 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)20 Category (com.salesmanager.core.model.catalog.category.Category)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)19 LightPersistableProduct (com.salesmanager.shop.model.catalog.product.LightPersistableProduct)18 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)13 HashSet (java.util.HashSet)12