Rev 23884 | Rev 23914 | 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.HashSet;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;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.transaction.annotation.Transactional;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.CreateSchemeRequest;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.SchemeItem;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(rollbackFor = 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);model.addAttribute("brands", inventoryService.getAllTagListingBrands());return "create-scheme";}@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.GET)public String getTagListingItemsByBrand(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model) {Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllTagListingItemIdItemDescriptionMap(brand);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 = "/schemes/delete", method = RequestMethod.POST)public String deleteShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)throws Exception {if (schemeItemRepository.selectBySchemeIdsAndItemIds(new HashSet<>(schemeItems.getSchemeIds()),new HashSet<>(schemeItems.getItemIds())).size() > 0) {for (int schemeId : schemeItems.getSchemeIds()) {for (int itemId : schemeItems.getItemIds()) {schemeItemRepository.deletebyItemIdsandSchemeIds(itemId, schemeId);model.addAttribute("response", mvcResponseSender.createResponseString(true));}}} else {throw new ProfitMandiBusinessException("ItemId", schemeItems.getItemIds(), "invalid Item Id");}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);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);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("roleIds", 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 LocalDateTime startDateTime,@RequestParam LocalDateTime endDateTime, Model model)throws ProfitMandiBusinessException {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("isFofoAdmin", 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 = "offset", defaultValue = "0") int offset,@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)throws ProfitMandiBusinessException {schemeService.expireSchemeById(schemeId);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);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);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);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 {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);return "schemes-paginated";}}