Subversion Repositories SmartDukaan

Rev

Rev 31323 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import org.springframework.transaction.annotation.Transactional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.solr.SolrService;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.dtr.WebOffer;
import com.spice.profitmandi.dao.entity.dtr.WebOfferProduct;
import com.spice.profitmandi.dao.repository.dtr.WebOfferProductRepository;
import com.spice.profitmandi.dao.repository.dtr.WebOfferRepository;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class WebOffersController {

        private static final Logger LOGGER = LogManager.getLogger(WebOffersController.class);

        @Autowired
        ResponseSender<?> responseSender;

        @Autowired
        WebOfferRepository webOfferRepository;

        @Autowired
        WebOfferProductRepository webOfferProductRepository;

        @Autowired
        SolrService solrService;

        @RequestMapping(value = "/web-offers", method = RequestMethod.GET)
        public String getOfferListing(HttpServletRequest request, Model model) {
                List<WebOffer> webListing = webOfferRepository.selectAllWebListing(Optional.of(true));
                model.addAttribute("webListings", webListing);
                return "web-offer";
        }

        @RequestMapping(value = "/web-offer/add", method = RequestMethod.POST)
        public String addWebListing(HttpServletRequest request, @RequestBody WebOffer webOffer, Model model) {
                webOffer.setCreatedDate(LocalDateTime.now());

                webOfferRepository.persist(webOffer);
                return getOfferListing(request, model);
        }

        @RequestMapping(value = "/getwebOffer/{webOfferId}", method = RequestMethod.GET)
        public String getWebOfferbyId(HttpServletRequest request, Model model, @PathVariable int webOfferId)
                        throws Exception {
                WebOffer webOffer = webOfferRepository.selectById(webOfferId);

                LOGGER.info("webOffer {}", webOffer);
                model.addAttribute("webOffer", webOffer);
                return "web-offer-edit";
        }

        @RequestMapping(value = "/web-offer/{webOfferId}", method = RequestMethod.GET)
        public String getWebProductListing(HttpServletRequest request, Model model, @PathVariable int webOfferId)
                        throws Exception {
                List<WebOfferProduct> webOfferProducts = webOfferProductRepository.selectAllByWebOfferId(webOfferId);
                if (webOfferProducts.size() > 0) {
                        Map<Integer, JSONObject> entityMap = solrService.getContentByCatalogIds(
                                        webOfferProducts.stream().map(x -> x.getEntityId()).collect(Collectors.toList()));
                        webOfferProducts.stream()
                                        .forEach(x -> x.setProductName(entityMap.get(x.getEntityId()).getString("title_s")));
                }

                WebOffer webOffer = webOfferRepository.selectById(webOfferId);
                model.addAttribute("productListings", webOfferProducts);
                model.addAttribute("webOffer", webOffer);
                return "web-offer-product";
        }

        @RequestMapping(value = "/web-offer-product/add", method = RequestMethod.POST)
        public String addWebOfferProduct(HttpServletRequest request, @RequestBody List<WebOfferProduct> webOfferProducts,
                        Model model) throws Exception {
                webOfferProducts.stream().forEach(webOfferProduct -> {
                        boolean exists = webOfferProductRepository.isExists(webOfferProduct.getWebOfferId(),
                                        webOfferProduct.getEntityId());
                        if (!exists) {
                                webOfferProductRepository.persist(webOfferProduct);
                        }

                });
                return getWebProductListing(request, model, webOfferProducts.get(0).getWebOfferId());
        }

        @RequestMapping(value = "/web-offer-product/remove", method = RequestMethod.DELETE)
        public String addWebOfferProduct(HttpServletRequest request, @RequestParam int webOfferProductId, Model model)
                        throws Exception {
                WebOfferProduct webOfferProduct = webOfferProductRepository.selectById(webOfferProductId);
                webOfferProductRepository.delete(webOfferProduct);
                return getWebProductListing(request, model, webOfferProduct.getWebOfferId());
        }

        @RequestMapping(value = "/web-offer-duration/update", method = RequestMethod.POST)
        public String updateWebOfferDuration(HttpServletRequest request, @RequestBody WebOffer wo, Model model)
                        throws Exception {
                WebOffer webOffer = webOfferRepository.selectById(wo.getId());
                LOGGER.info("webOffer {}", webOffer);

                if (webOffer != null) {

                        webOffer.setStartDate(wo.getStartDate());
                        webOffer.setEndDate(wo.getEndDate());
                        webOffer.setDetailedText(wo.getDetailedText());
                        webOffer.setLargeBannerUrl(wo.getLargeBannerUrl());
                        webOffer.setTitle(wo.getTitle());
                        webOfferRepository.persist(webOffer);
                }
                return getOfferListing(request, model);
        }

        @RequestMapping(value = "/web-offer/order/{webListingId}", method = RequestMethod.POST)
        public String orderWebProductListing(HttpServletRequest request, Model model, @PathVariable int webListingId,

                        @RequestBody List<Integer> webListingProductIds) throws Exception {
                List<WebOfferProduct> productListings = webOfferProductRepository.selectAllByWebOfferId(webListingId);

                productListings.forEach(productListing -> {
                        productListing.setRank(webListingProductIds.indexOf(productListing.getId()) + 1);
                });
                return getWebProductListing(request, model, webListingId);
        }

}