Search in sources :

Example 6 with ReadableProductReview

use of com.salesmanager.shop.model.catalog.product.ReadableProductReview in project shopizer by shopizer-ecommerce.

the class CustomerProductReviewController method submitProductReview.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/review/submit.html", method = RequestMethod.POST)
public String submitProductReview(@ModelAttribute("review") PersistableProductReview review, BindingResult bindingResult, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Language language = getLanguage(request);
    Customer customer = customerFacade.getCustomerByUserName(request.getRemoteUser(), store);
    if (customer == null) {
        return "redirect:" + Constants.SHOP_URI;
    }
    Product product = productService.getById(review.getProductId());
    if (product == null) {
        return "redirect:" + Constants.SHOP_URI;
    }
    if (StringUtils.isBlank(review.getDescription())) {
        FieldError error = new FieldError("description", "description", messages.getMessage("NotEmpty.review.description", locale));
        bindingResult.addError(error);
    }
    if (review.getRating() == null) {
        FieldError error = new FieldError("rating", "rating", messages.getMessage("NotEmpty.review.rating", locale, "Product rating is required"));
        bindingResult.addError(error);
    }
    review.setDescription(SanitizeUtils.getSafeString(review.getDescription()));
    ReadableProduct readableProduct = new ReadableProduct();
    ReadableProductPopulator readableProductPopulator = new ReadableProductPopulator();
    readableProductPopulator.setPricingService(pricingService);
    readableProductPopulator.setimageUtils(imageUtils);
    readableProductPopulator.populate(product, readableProduct, store, language);
    model.addAttribute("product", readableProduct);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.review).append(".").append(store.getStoreTemplate());
    if (bindingResult.hasErrors()) {
        return template.toString();
    }
    // check if customer has already evaluated the product
    List<ProductReview> reviews = productReviewService.getByProduct(product);
    for (ProductReview r : reviews) {
        if (r.getCustomer().getId().longValue() == customer.getId().longValue()) {
            ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
            ReadableProductReview rev = new ReadableProductReview();
            reviewPopulator.populate(r, rev, store, language);
            model.addAttribute("customerReview", rev);
            return template.toString();
        }
    }
    PersistableProductReviewPopulator populator = new PersistableProductReviewPopulator();
    populator.setCustomerService(customerService);
    populator.setLanguageService(languageService);
    populator.setProductService(productService);
    review.setDate(DateUtil.formatDate(new Date()));
    review.setCustomerId(customer.getId());
    ProductReview productReview = populator.populate(review, store, language);
    productReviewService.create(productReview);
    model.addAttribute("review", review);
    model.addAttribute("success", "success");
    ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
    ReadableProductReview rev = new ReadableProductReview();
    reviewPopulator.populate(productReview, rev, store, language);
    model.addAttribute("customerReview", rev);
    return template.toString();
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) FieldError(org.springframework.validation.FieldError) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) Date(java.util.Date) ReadableProductReviewPopulator(com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator) PersistableProductReviewPopulator(com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ReadableProductReview (com.salesmanager.shop.model.catalog.product.ReadableProductReview)6 ProductReview (com.salesmanager.core.model.catalog.product.review.ProductReview)5 ReadableProductReviewPopulator (com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator)5 Product (com.salesmanager.core.model.catalog.product.Product)4 PersistableProductReview (com.salesmanager.shop.model.catalog.product.PersistableProductReview)4 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)3 Language (com.salesmanager.core.model.reference.language.Language)3 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)3 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)3 ArrayList (java.util.ArrayList)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Customer (com.salesmanager.core.model.customer.Customer)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)1 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)1 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)1 Breadcrumb (com.salesmanager.shop.model.shop.Breadcrumb)1 PageInformation (com.salesmanager.shop.model.shop.PageInformation)1 PersistableProductReviewPopulator (com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator)1 Attribute (com.salesmanager.shop.store.model.catalog.Attribute)1