Subversion Repositories SmartDukaan

Rev

Rev 23955 | Rev 24124 | 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.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.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.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.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.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);
        
        @Autowired
        private RetailerService retailerService;
        
        @Autowired
        private RetailerRepository retailerRepository;
        
        @Autowired
        private ShopRepository shopRepository;
        
        @Autowired
        private DocumentRepository documentRepository;
        
        @Autowired
        private UserRepository userRepository;
        
        @Autowired
        private LocationRepository locationRepository;
        
        @Autowired
        private ResponseSender<?> responseSender;
        
        @Autowired
        private 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());
                User user = userRepository.selectByEmailId(emailIdOrMobileNumber);
                if(user.getLocation() != null){
                Location location = locationRepository.selectById(user.getLocation());
                model.addAttribute("locationdetail",location);
                 LOGGER.info("location"+location );
                }
                LOGGER.info("map detail"+ 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);
                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";
      
        }
}