Rev 24124 | Rev 24159 | 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.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager;import org.springframework.beans.factory.annotation.Autowired;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.GetMapping;import org.springframework.web.bind.annotation.PostMapping;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.ContentType;import com.spice.profitmandi.common.enumuration.CounterSize;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.AddLocationModel;import com.spice.profitmandi.common.model.CustomRetailer;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.SendNotificationModel;import com.spice.profitmandi.common.model.UpdateRetailerRequest;import com.spice.profitmandi.common.util.Utils;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.dtr.Document;import com.spice.profitmandi.dao.entity.dtr.Retailer;import com.spice.profitmandi.dao.entity.dtr.Shop;import com.spice.profitmandi.dao.entity.fofo.FofoStore;import com.spice.profitmandi.dao.entity.fofo.InActiveFofoStore;import com.spice.profitmandi.dao.entity.user.User;import com.spice.profitmandi.dao.entity.user.Location;import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;import com.spice.profitmandi.dao.repository.dtr.ShopRepository;import com.spice.profitmandi.dao.repository.user.LocationRepository;import com.spice.profitmandi.dao.repository.user.UserRepository;import com.spice.profitmandi.service.user.RetailerService;import com.spice.profitmandi.web.util.MVCResponseSender;@Controller@Transactional(rollbackFor = Throwable.class)public class RetailerController {private static final Logger LOGGER = LogManager.getLogger(RetailerController.class);@Autowiredprivate RetailerService retailerService;@Autowiredprivate RetailerRepository retailerRepository;@Autowiredprivate ShopRepository shopRepository;@Autowiredprivate FofoStoreRepository fofoStoreRepository;@Autowiredprivate DocumentRepository documentRepository;@Autowiredprivate UserRepository userRepository;@Autowiredprivate LocationRepository locationRepository;@Autowiredprivate ResponseSender<?> responseSender;@Autowiredprivate MVCResponseSender mvcResponseSender;@RequestMapping(value = "/retailerDetails", method = RequestMethod.GET)public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,Model model) throws ProfitMandiBusinessException {LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(),emailIdOrMobileNumber);Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);model.addAllAttributes(map);model.addAttribute("counterSizes", CounterSize.values());if(map.containsKey("retailer")) {Retailer retailer = (Retailer)map.get("retailer");User user = userRepository.selectById(retailer.getId());if (user.getLocation() != null) {Location location = locationRepository.selectById(user.getLocation());model.addAttribute("locationdetail", location);LOGGER.info("location" + location);}}return "retailer-details";}@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)public String updateRetailerDetails(HttpServletRequest request,@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);model.addAllAttributes(map);model.addAttribute("counterSizes", CounterSize.values());return "retailer-details";}@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)public String retailerInfo(HttpServletRequest request) throws Exception {return "retailer-info";}@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {return responseSender.ok(retailerService.getAllDistrictMaster(stateName));}@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {Document document = documentRepository.selectById(documentId);Retailer retailer = retailerRepository.selectById(retailerId);if (retailer.getDocumentId() == null) {throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");}if (retailer.getDocumentId() != documentId) {throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");}return responseSender.ok(document);}@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)throws ProfitMandiBusinessException {Retailer retailer = retailerRepository.selectById(retailerId);if (retailer.getDocumentId() == null) {throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");}Document document = documentRepository.selectById(retailer.getDocumentId());FileInputStream file = null;try {file = new FileInputStream(document.getPath() + File.separator + document.getName());} catch (FileNotFoundException e) {LOGGER.error("Retailer Document file not found : ", e);throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");}// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);final HttpHeaders headers = new HttpHeaders();String contentType = "";if (document.getContentType() == ContentType.JPEG) {contentType = "image/jpeg";} else if (document.getContentType() == ContentType.PNG) {contentType = "image/png";} else if (document.getContentType() == ContentType.PDF) {contentType = "application/pdf";}headers.set("Content-Type", contentType);headers.set("Content-disposition", "inline; filename=" + document.getName());headers.setContentLength(document.getSize());final InputStreamResource inputStreamResource = new InputStreamResource(file);return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));}@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {Shop shop = shopRepository.selectById(shopId);if (shop.getRetailerId() != retailerId) {throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");}if (shop.getDocumentId() == null) {throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");}Document document = documentRepository.selectById(shop.getDocumentId());return responseSender.ok(document);}@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)throws ProfitMandiBusinessException {Shop shop = shopRepository.selectById(shopId);if (shop.getRetailerId() != retailerId) {throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");}if (shop.getDocumentId() == null) {throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");}Document document = documentRepository.selectById(shop.getDocumentId());FileInputStream file = null;try {file = new FileInputStream(document.getPath() + File.separator + document.getName());} catch (FileNotFoundException e) {LOGGER.error("Retailer Document file not found : ", e);throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");}// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);final HttpHeaders headers = new HttpHeaders();String contentType = "";if (document.getContentType() == ContentType.JPEG) {contentType = "image/jpeg";} else if (document.getContentType() == ContentType.PNG) {contentType = "image/png";} else if (document.getContentType() == ContentType.PDF) {contentType = "application/pdf";}headers.set("Content-Type", contentType);headers.set("Content-disposition", "inline; filename=" + document.getName());headers.setContentLength(document.getSize());final InputStreamResource inputStreamResource = new InputStreamResource(file);return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));}@RequestMapping(value = "/getAddLocation", method = RequestMethod.GET)public String getAddLocationModal(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {model.addAttribute("stateNames", Utils.getAllStateNames());return "add-location-modal";}@RequestMapping(value = "/addLocation", method = RequestMethod.POST)public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)throws Exception {Location location = new Location();location.setName(addLocationModel.getName());location.setLine1(addLocationModel.getLine1());location.setLine2(addLocationModel.getLine2());location.setCity(addLocationModel.getCity());location.setState(addLocationModel.getState());location.setPin(addLocationModel.getPin());LOGGER.info("PostLocation" + location);locationRepository.persist(location);model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";}@GetMapping(value = "/getAllStores")public String getAllStores(HttpServletRequest request, Model model) {List<FofoStore> fofoStores = fofoStoreRepository.selectAll();LOGGER.info(this.getFofoIds(fofoStores));Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(this.getFofoIds(fofoStores));model.addAttribute("fofoStores", fofoStores);model.addAttribute("customRetailers", customRetailers);return "store";}@PostMapping(value = "/deactivateStore")public String deActivateStore(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);InActiveFofoStore inActiveFofoStore = new InActiveFofoStore();inActiveFofoStore.setId(fofoStore.getId());inActiveFofoStore.setCode(fofoStore.getCode());inActiveFofoStore.setCounterSize(fofoStore.getCounterSize());inActiveFofoStore.setLatitude(fofoStore.getLatitude());inActiveFofoStore.setLongitude(fofoStore.getLongitude());inActiveFofoStore.setMinimumInvestment(fofoStore.getMinimumInvestment());inActiveFofoStore.setUserAddress(fofoStore.getUserAddress());fofoStoreRepository.saveInActiveStore(inActiveFofoStore);LOGGER.info("inserted into InActiveFofoStore successfully");fofoStoreRepository.delete(fofoStore);LOGGER.info("Deleted from fofoStore successfully");model.addAttribute("response", mvcResponseSender.createResponseString(true));return "response";}private List<Integer> getFofoIds(List<FofoStore> fofoStores) {List<Integer> fofoIds = new ArrayList<>();for (FofoStore fofoStore : fofoStores) {fofoIds.add(fofoStore.getId());}return fofoIds;}}