Rev 37212 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import com.fasterxml.jackson.databind.ObjectMapper;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.catalog.Catalog;import com.spice.profitmandi.dao.entity.catalog.ModelHotDeal;import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;import com.spice.profitmandi.service.catalog.BrandsService;import com.spice.profitmandi.service.catalog.ModelHotDealService;import com.spice.profitmandi.web.model.LoginDetails;import com.spice.profitmandi.web.util.CookiesProcessor;import com.spice.profitmandi.web.util.MVCResponseSender;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;import java.time.LocalDate;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.stream.Collectors;@Controller@Transactional(rollbackFor = Throwable.class)public class HotDealController {private static final Logger LOGGER = LoggerFactory.getLogger(HotDealController.class);private static final int MOBILE_BRAND_CATEGORY_ID = 3;@Autowiredprivate ModelHotDealService modelHotDealService;@Autowiredprivate BrandsService brandsService;@Autowiredprivate CatalogRepository catalogRepository;@Autowiredprivate CookiesProcessor cookiesProcessor;@Autowiredprivate MVCResponseSender mvcResponseSender;@Autowiredprivate ObjectMapper objectMapper;@RequestMapping(value = "/manageHotDeals", method = RequestMethod.GET)public String manageHotDeals(HttpServletRequest request, Model model) throws Exception {model.addAttribute("brands", brandsService.getBrandsToDisplay(MOBILE_BRAND_CATEGORY_ID));model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);return "hot-deals";}@RequestMapping(value = "/hotDeals", method = RequestMethod.GET)public String getHotDeals(@RequestParam int brandId, Model model) throws Exception {model.addAttribute("deals", modelHotDealService.getDealsForBrand(brandId));model.addAttribute("activeCount", modelHotDealService.countActive(brandId));model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);return "hot-deals-table";}@RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId,ProfitMandiConstants.MOBILE_CATEGORY_ID);Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream().filter(deal -> !"EXPIRED".equals(deal.getStatus())).map(ModelHotDeal::getCatalogItemId).collect(Collectors.toSet());List<Map<String, Object>> models = catalogs.stream().filter(catalog -> !activeIds.contains(catalog.getId())).map(catalog -> {Map<String, Object> entry = new HashMap<>();entry.put("catalogId", catalog.getId());entry.put("description", catalog.getDescription());return entry;}).collect(Collectors.toList());model.addAttribute("response1", objectMapper.writeValueAsString(models));return "response";}@RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,@RequestParam int catalogItemId, @RequestParam String startDate,@RequestParam String endDate, Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);try {modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),LocalDate.parse(endDate), loginDetails.getEmailId());model.addAttribute("response1", mvcResponseSender.createResponseString(true));} catch (ProfitMandiBusinessException e) {LOGGER.warn("Hot deal add rejected: {}", e.getMessage());model.addAttribute("response1", e.getMessage());}return "response";}@RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)public String updateHotDeal(HttpServletRequest request, @RequestParam int id,@RequestParam String startDate, @RequestParam String endDate,Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);try {modelHotDealService.updateDates(id, LocalDate.parse(startDate), LocalDate.parse(endDate),loginDetails.getEmailId());model.addAttribute("response1", mvcResponseSender.createResponseString(true));} catch (ProfitMandiBusinessException e) {LOGGER.warn("Hot deal update rejected: {}", e.getMessage());model.addAttribute("response1", e.getMessage());}return "response";}@RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)public String removeHotDeal(@RequestParam int id, Model model) throws Exception {try {modelHotDealService.removeDeal(id);model.addAttribute("response1", mvcResponseSender.createResponseString(true));} catch (ProfitMandiBusinessException e) {LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());model.addAttribute("response1", e.getMessage());}return "response";}}