Subversion Repositories SmartDukaan

Rev

Rev 21435 | Rev 21448 | 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 javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
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.ResponseCodeHolder;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.Address;
import com.spice.profitmandi.dao.entity.Brand;
import com.spice.profitmandi.dao.entity.Document;
import com.spice.profitmandi.dao.entity.Retailer;
import com.spice.profitmandi.dao.entity.RetailerBrand;
import com.spice.profitmandi.dao.entity.RetailerRegisteredAddress;
import com.spice.profitmandi.dao.entity.Shop;
import com.spice.profitmandi.dao.entity.ShopAddress;
import com.spice.profitmandi.dao.repository.AddressRepository;
import com.spice.profitmandi.dao.repository.BrandRepository;
import com.spice.profitmandi.dao.repository.DocumentRepository;
import com.spice.profitmandi.dao.repository.RetailerAddressRepository;
import com.spice.profitmandi.dao.repository.RetailerBrandRepository;
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
import com.spice.profitmandi.dao.repository.RetailerRepository;
import com.spice.profitmandi.dao.repository.ShopAddressRepository;
import com.spice.profitmandi.dao.repository.ShopRepository;
import com.spice.profitmandi.web.req.Category;
import com.spice.profitmandi.web.req.CreateRetailerAddressRequest;
import com.spice.profitmandi.web.req.CreateRetailerRequest;
import com.spice.profitmandi.web.req.RetailerAddBrandRequest;
import com.spice.profitmandi.web.util.ResponseSender;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
public class RetailerController {
        
        private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
        
        @Autowired
        ResponseSender<?> responseSender;
        
        @Autowired
        RetailerRepository retailerRepository;
        
        @Autowired
        BrandRepository brandRepository;
        
        @Autowired
        AddressRepository addressRepository;
        
        @Autowired
        DocumentRepository documentRepository;
        
        @Autowired
        ShopRepository shopRepository;
        
        @Autowired
        RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
        
        @Autowired
        RetailerAddressRepository retailerAddressRepository;
        
        @Autowired
        ShopAddressRepository shopAddressRepository;
        
        @Autowired
        RetailerBrandRepository retailerBrandRepository;
        
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        
        @ApiOperation(value = "Create Retailer")
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
        public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequest createRetailerRequest){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try{
                        this.createRetailer(createRetailerRequest);
                        return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @Transactional
        private void createRetailer(CreateRetailerRequest createRetailerRequest) 
                        throws ProfitMandiBusinessException{
                Retailer retailer = new Retailer();
                retailer.setName(createRetailerRequest.getName());
                retailer.setNumber(createRetailerRequest.getNumber());
                retailer.setType(createRetailerRequest.getType());
                retailer.setMonthlySaleValue(createRetailerRequest.getMonthlySaleValue());
                retailer.setSmartphoneSaleValue(createRetailerRequest.getSmartphoneSaleValue());
                retailer.setRecharge(createRetailerRequest.getLineOfBusiness().isRecharge());
                retailer.setMobile(createRetailerRequest.getLineOfBusiness().isMobile());
                retailer.setAccessories(createRetailerRequest.getLineOfBusiness().isAccessories());
                retailer.setOther1(createRetailerRequest.getLineOfBusiness().getOther1());
                retailer.setOther2(createRetailerRequest.getLineOfBusiness().getOther2());
                Document retailerDocument = documentRepository.selectById(createRetailerRequest.getDocumentId());
                if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
                        LOGGER.error("documet is already mapped with another retailer");
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "");
                }
                retailer.setDocumentId(retailerDocument.getId());
                final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
                if(shopRepository.isExistByDocumentId(shopDocument.getId())){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "");
                }
                retailerRepository.persist(retailer);
                Shop shop = new Shop();
                shop.setName(createRetailerRequest.getShop().getName());
                shop.setDocumentId(shopDocument.getId());
                shop.setRetailerId(retailer.getId());
                shopRepository.persist(shop);
                for(Category category : createRetailerRequest.getCategories()){
                        for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
                                Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
                                final RetailerBrand retailerBrand = new RetailerBrand();
                                retailerBrand.setRetailerId(retailer.getId());
                                retailerBrand.setBrandId(brand.getId());
                                retailerBrandRepository.persist(retailerBrand);
                        }
                }
                final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
                        
                if(!addressRepository.isExist(addressRetailer)){
                        addressRepository.persist(addressRetailer);
                }
                final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
                retailerRegisteredAddress.setRetailerId(retailer.getId());
                retailerRegisteredAddress.setAddressId(addressRetailer.getId());
                retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
                final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
                if(!addressRepository.isExist(addressShop)){
                        addressRepository.persist(addressShop);
                }
                ShopAddress shopAddress = new ShopAddress();
                shopAddress.setShopId(shop.getId());
                shopAddress.setAddressId(addressShop.getId());
                shopAddressRepository.persist(shopAddress);
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
        public ResponseEntity<?> getAll(HttpServletRequest request){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                return responseSender.ok(retailerRepository.selectAll());
        }
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
        public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        return responseSender.ok(retailerRepository.selectById(id));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
        public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        return responseSender.ok(retailerRepository.selectByName(name));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
                
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
        public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        retailerRepository.deleteById(id);
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @Transactional
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
        public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        Document document = documentRepository.selectById(createShop.getDocumentId());
                        if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
                                LOGGER.error("documet is already mapped with another shop");
                                throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
                        }
                        retailerRepository.selectById(retailerId);
                        Shop shop = new Shop();
                        shop.setRetailerId(retailerId);
                        Address address = this.createAddress(createShop.getAddress());
                        addressRepository.persist(address);
                        shop.setAddressId(address.getId());
                        shop.setDocumentId(document.getId());
                        shopRepository.persist(shop);
                        ShopAddress shopAddress = new ShopAddress();
                        shopAddress.setAddressId(address.getId());
                        shopAddress.setShopId(shop.getId());
                        shopAddressRepository.persist(shopAddress);
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
        public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        retailerRepository.removeShop(shopId, retailerId);
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
                Address address = new Address();
                address.setName(createAddress.getName());
                address.setLine1(createAddress.getLine1());
                address.setLine2(createAddress.getLine2());
                address.setLandmark(createAddress.getLandmark());
                address.setCity(createAddress.getCity());
                address.setState(createAddress.getState());
                address.setPinCode(createAddress.getPinCode());
                address.setCountry(createAddress.getCountry());
                address.setPhoneNumber(createAddress.getPhoneNumber());
                return address;
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
        public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
        public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        retailerRepository.removeAddress(addressId, retailerId);
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
        public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        Retailer retailer = retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
                        for(String brandName : retailerAddBrandRequest.getBrandNames()){
                                Brand brand = brandRepository.selectByName(brandName);
                                RetailerBrand retailerBrand = new RetailerBrand();
                                retailerBrand.setBrandId(brand.getId());
                                retailerBrand.setRetailerId(retailer.getId());
                        }
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
        public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        brandRepository.selectById(brandId);
                        retailerRepository.selectById(retailerId);
                        retailerBrandRepository.deleteByRetailerId(retailerId, brandId);
                        return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT_DOCUMENT_ID, method=RequestMethod.GET)
        public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "document") int documentId){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        retailerRepository.selectByDocumentId(documentId);
                        return responseSender.ok(documentRepository.selectById(documentId));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
        public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        return responseSender.ok(shopRepository.selectByRetailerId(id));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
        public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
        }
        
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
        public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
        }
                
}