Subversion Repositories SmartDukaan

Rev

Rev 23784 | Rev 23955 | 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.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.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.ContentType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.Location;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
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.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.service.user.RetailerService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;

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

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

        @Autowired
        private RetailerService retailerService;

        @Autowired
        private RetailerRepository retailerRepository;

        @Autowired
        private ShopRepository shopRepository;
        
        @Autowired
        private FofoStoreRepository fofoStoreRepository;

        @Autowired
        private DocumentRepository documentRepository;

        @Autowired
        private ResponseSender<?> responseSender;
        
        @Autowired
        private CookiesProcessor cookiesProcessor;

        @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);
                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);
                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 = "/partner/location", method = RequestMethod.PUT)
        public ResponseEntity<?> updateLocation(HttpServletRequest request, @RequestBody Location location) throws ProfitMandiBusinessException{
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoDetails.getFofoId());
                fs.setLatitude(location.getLatitude());
                fs.setLongitude(location.getLongitude());
                fofoStoreRepository.persist(fs);
                return responseSender.ok(true);
        }
}