Rev 25369 | Rev 26674 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.time.LocalDateTime;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.transaction.Transactional;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.core.io.InputStreamResource;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;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.enumuration.DateTimePattern;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.MapWrapper;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.SchemeItems;import com.spice.profitmandi.common.model.SchemeModel;import com.spice.profitmandi.common.util.ExcelUtils;import com.spice.profitmandi.common.util.StringUtils;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.catalog.Scheme;import com.spice.profitmandi.dao.entity.fofo.PartnerType;import com.spice.profitmandi.dao.entity.fofo.SchemeItem;import com.spice.profitmandi.dao.model.CreateSchemeRequest;import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;import com.spice.profitmandi.dao.repository.dtr.RoleRepository;import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;import com.spice.profitmandi.service.authentication.RoleManager;import com.spice.profitmandi.service.inventory.InventoryService;import com.spice.profitmandi.service.scheme.SchemeService;import com.spice.profitmandi.web.model.LoginDetails;import com.spice.profitmandi.web.util.CookiesProcessor;import com.spice.profitmandi.web.util.MVCResponseSender;@Controller@Transactional(rollbackOn = Throwable.class)public class SchemeController {private static final Logger LOGGER = LogManager.getLogger(SchemeController.class);@Autowiredprivate SchemeService schemeService;@Autowiredprivate SchemeRepository schemeRepository;@Autowiredprivate SchemeItemRepository schemeItemRepository;@Autowiredprivate MVCResponseSender mvcResponseSender;@Autowiredprivate CookiesProcessor cookiesProcessor;@Autowired@Qualifier("fofoInventoryService")private InventoryService inventoryService;@Autowiredprivate TagListingRepository tagListingRepository;@Autowiredprivate RoleManager roleManager;@AutowiredPurchaseRepository purchaseRepository;@Autowiredprivate RoleRepository roleRepository;@Autowiredprivate ResponseSender<?> responseSender;@RequestMapping(value = "/createScheme", method = RequestMethod.GET)public String createScheme(HttpServletRequest request, Model model) {// Map<Integer, String> itemIdItemDescriptionMap =// inventoryService.getAllItemIdItemDescriptionMap();// model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);brands.addAll(inventoryService.getAllTagListingBrands(14206));model.addAttribute("brands", brands);model.addAttribute("retailerTypes", PartnerType.values());return "create-scheme";}@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.POST)public String getTagListingItemsByBrand(HttpServletRequest request, @RequestBody List<String> brands, Model model) {Map<Integer, String> itemIdItemDescriptionMap = new HashMap<>();LOGGER.info("brands" + brands);List<MapWrapper<Integer, String>> itemIdItemDescriptionMaplist = inventoryService.getAllTagListingItemIdItemDescriptionMap(new HashSet<>(brands));for (MapWrapper<Integer, String> mapWrapper : itemIdItemDescriptionMaplist) {itemIdItemDescriptionMap.put(mapWrapper.getKey(), mapWrapper.getValue());}model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);// model.addAttribute("brands", inventoryService.getAllBrands());return "tag-listing-items-description";}@RequestMapping(value = "/schemes/update-schemes-page", method = RequestMethod.GET)public String updateShcemes(HttpServletRequest request) throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);if (!roleManager.isAdmin(loginDetails.getRoleIds())) {throw new ProfitMandiBusinessException("User", loginDetails.getEmailId(), "Unauthorised access");}return "update-schemes-page";}@RequestMapping(value = "/schemes/update", method = RequestMethod.POST)public String updateShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)throws Exception {for (int schemeId : schemeItems.getSchemeIds()) {if (schemeRepository.selectById(schemeId) != null)for (int itemId : schemeItems.getItemIds()) {if (tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)),new HashSet<>(Arrays.asList(4, 7))).size() > 0) {SchemeItem si = new SchemeItem();si.setItemId(itemId);si.setSchemeId(schemeId);try {schemeItemRepository.persist(si);} catch (Exception e) {LOGGER.info("Scheme aleady exist");}model.addAttribute("response", mvcResponseSender.createResponseString(true));} else {model.addAttribute("response", mvcResponseSender.createResponseString(false));throw new ProfitMandiBusinessException("ItemId", itemId, "Invalid Item Id");}}}return "response";}@RequestMapping(value = "/addItemToScheme", method = RequestMethod.POST)public String updateScheme(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)throws Exception {for (int schemeId : schemeItems.getSchemeIds()) {List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(schemeId);if (schemeRepository.selectById(schemeId) != null)for (int itemId : schemeItems.getItemIds()) {if (tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)),new HashSet<>(Arrays.asList(4, 7))).size() > 0 && (!(itemIds.contains(itemId)))) {SchemeItem si = new SchemeItem();si.setItemId(itemId);si.setSchemeId(schemeId);try {schemeItemRepository.persist(si);} catch (Exception e) {LOGGER.info("Scheme already exist");}model.addAttribute("response", mvcResponseSender.createResponseString(true));} else {model.addAttribute("response", mvcResponseSender.createResponseString(false));}}}return "response";}@RequestMapping(value = "/schemes/delete", method = RequestMethod.DELETE)public String deleteShcemes(HttpServletRequest request,@RequestParam(name = "schemeId", required = false, defaultValue = "0") int schemeId,@RequestParam(name = "itemId", required = false, defaultValue = "0") int itemId, Model model)throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);if (!(schemeId == 0 && itemId == 0) || (!(schemeId == 0 || itemId == 0))) {schemeItemRepository.deletebyItemIdsandSchemeIds(itemId, schemeId);model.addAttribute("response", mvcResponseSender.createResponseString(true));model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));}return "response";}@RequestMapping(value = "/extendAllSchemes", method = RequestMethod.POST)public String extendAllScheme(HttpServletRequest request, @RequestBody LocalDateTime extendDatetime, Model model)throws Exception {List<Scheme> schemes = schemeRepository.selectActiveAll();if (schemes.size() > 0) {for (Scheme scheme : schemes) {if (scheme.getExpireTimestamp() == null) {scheme.setEndDateTime(extendDatetime);schemeRepository.persist(scheme);}}model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";}model.addAttribute("response", mvcResponseSender.createResponseString(false));return "response";}@RequestMapping(value = "/extendSchemeById", method = RequestMethod.POST)public String extendSchemeById(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,@RequestBody LocalDateTime extendDatetime, Model model) throws Exception {LOGGER.info("ExtendDatetime" + extendDatetime);LOGGER.info("schemeId" + schemeId);Scheme scheme = schemeRepository.selectById(schemeId);if ((!(scheme.getActiveTimestamp() == null)) && scheme.getExpireTimestamp() == null) {scheme.setEndDateTime(extendDatetime);schemeRepository.persist(scheme);model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";}model.addAttribute("response", mvcResponseSender.createResponseString(false));return "response";}@RequestMapping(value = "/createScheme", method = RequestMethod.POST)public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit,@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem,@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);LOGGER.info("Scheme saved successfully");long size = schemeRepository.selectAllCount();List<Scheme> schemes = schemeRepository.selectAll(offset, limit);model.addAttribute("schemes", schemes);model.addAttribute("start", offset + 1);model.addAttribute("size", size);model.addAttribute("searchItem", searchItem);model.addAttribute("searchTerm", searchTerm);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));if (schemes.size() < limit) {model.addAttribute("end", offset + schemes.size());} else {model.addAttribute("end", offset + limit);}return "schemes";}@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit,@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem,@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);List<Scheme> schemes = null;long size = 0;if (roleManager.isAdmin(loginDetails.getRoleIds())) {schemes = schemeRepository.selectAll(offset, limit);size = schemeRepository.selectAllCount();} else {schemes = schemeRepository.selectActiveAll(offset, limit);size = schemeRepository.selectAllActiveCount();}model.addAttribute("schemes", schemes);model.addAttribute("start", offset + 1);model.addAttribute("size", size);model.addAttribute("searchItem", searchItem);model.addAttribute("searchTerm", searchTerm);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));if (schemes.size() < limit) {model.addAttribute("end", offset + schemes.size());} else {model.addAttribute("end", offset + limit);}// model.addAttribute("roleTypes", loginDetails.getRoleTypes());return "schemes";}@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)public String getPaginatedSchemes(HttpServletRequest request,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);List<Scheme> schemes = null;if (roleManager.isAdmin(loginDetails.getRoleIds())) {schemes = schemeRepository.selectAll(offset, limit);} else {schemes = schemeRepository.selectActiveAll(offset, limit);}model.addAttribute("schemes", schemes);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));return "schemes-paginated";}@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)public String downloadPage(HttpServletRequest request, Model model) {return "schemes-download";}@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.START_DATE_TIME) String startDateTimeString,@RequestParam(name = ProfitMandiConstants.END_DATE_TIME) String endDateTimeString, Model model)throws ProfitMandiBusinessException {LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString,DateTimePattern.DD_MM_yyyy_T_HH_MM_SS);LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_yyyy_T_HH_MM_SS);List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);final HttpHeaders headers = new HttpHeaders();headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");headers.setContentLength(byteArrayOutputStream.toByteArray().length);final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));}@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)public String getSchemeById(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);Scheme scheme = schemeService.getSchemeById(schemeId);model.addAttribute("scheme", scheme);model.addAttribute("isAdmin", roleManager.isAdmin(loginDetails.getRoleIds()));return "scheme-details";}@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)public String activeSchemeById(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)throws ProfitMandiBusinessException {schemeService.activeSchemeById(schemeId);List<Scheme> schemes = schemeRepository.selectAll(offset, limit);model.addAttribute("schemes", schemes);return "schemes-paginated";}@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)public String expireSchemeById(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,@RequestParam(name = ProfitMandiConstants.EXPIRE_TIMESTAMP) LocalDateTime expiryTimestamp,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)throws ProfitMandiBusinessException {schemeService.expireSchemeById(schemeId, expiryTimestamp);List<Scheme> schemes = schemeRepository.selectAll(offset, limit);model.addAttribute("schemes", schemes);return "schemes-paginated";}@RequestMapping(value = "/getSchemesJson", method = RequestMethod.GET)public ResponseEntity<?> getSchemesJson(HttpServletRequest request,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);return responseSender.ok(schemeService.getSchemes(loginDetails.getRoleIds(), offset, limit));}@RequestMapping(value = "/searchScheme")public String getSchemeBySchemeId(HttpServletRequest request,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit,@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm,@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);List<Scheme> schemes = null;long size = 0;if (!(searchTerm.equals("")) && searchItem.equals("")) {schemes = schemeRepository.selectBySearchTerm(searchTerm, offset, limit);if (!(schemes.size() == 0)) {size = schemeRepository.selectAllCount();LOGGER.info("schemes" + schemes);model.addAttribute("schemes", schemes);model.addAttribute("start", offset + 1);model.addAttribute("size", size);model.addAttribute("searchTerm", searchTerm);model.addAttribute("searchItem", searchItem);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));if (schemes.size() < limit) {model.addAttribute("end", offset + schemes.size());} else {model.addAttribute("end", offset + limit);}} else {throw new ProfitMandiBusinessException("SchemeId", searchTerm, "SchemeId Not Found");}} else if (!(searchItem.equals("")) && searchTerm.equals("")) {List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(Integer.parseInt(searchItem));if (!(schemeIds.size() == 0)) {LOGGER.info("schemeIds in searchItemScheme" + schemeIds);schemes = schemeRepository.selectBySchemeIds(schemeIds, offset, limit);size = schemeIds.size();LOGGER.info("Size" + size);model.addAttribute("schemes", schemes);model.addAttribute("start", offset + 1);model.addAttribute("size", size);model.addAttribute("searchItem", searchItem);model.addAttribute("searchTerm", searchTerm);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));if (schemes.size() < limit) {model.addAttribute("end", offset + schemes.size());} else {model.addAttribute("end", offset + limit);}} else {throw new ProfitMandiBusinessException("SchemeIds", searchItem, "SchemeId Not Found");}} else {if (roleManager.isAdmin(loginDetails.getRoleIds())) {schemes = schemeRepository.selectAll(offset, limit);size = schemeRepository.selectAllCount();} else {schemes = schemeRepository.selectActiveAll(offset, limit);size = schemeRepository.selectAllActiveCount();}model.addAttribute("schemes", schemes);model.addAttribute("start", offset + 1);model.addAttribute("size", size);model.addAttribute("searchItem", searchItem);model.addAttribute("searchTerm", searchTerm);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));if (schemes.size() < limit) {model.addAttribute("end", offset + schemes.size());} else {model.addAttribute("end", offset + limit);}}return "schemes";}@RequestMapping(value = "/searchItemSchemePanigated")public String getSchemeByItemPanigated(HttpServletRequest request,@RequestParam(name = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit,@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem, Model model)throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);LOGGER.info("In search Item....");List<Scheme> schemes = null;if (!searchItem.equals("")) {List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(Integer.parseInt(searchItem));if (schemeIds != null) {LOGGER.info(schemeIds);schemes = schemeRepository.selectBySchemeIds(schemeIds, offset, limit);}}model.addAttribute("schemes", schemes);model.addAttribute("searchItem", searchItem);model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));return "schemes-paginated";}}