Subversion Repositories SmartDukaan

Rev

Rev 21315 | Rev 21426 | 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.time.LocalDateTime;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.enumuration.RetailerType;
import com.spice.profitmandi.dao.enumuration.SaleValue;
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.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.model.ProfitMandiResponse;
import com.spice.profitmandi.web.model.Response;
import com.spice.profitmandi.web.model.ResponseStatus;
import com.spice.profitmandi.web.req.CreateRetailerRequestModel;

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
        RetailerRepository retailerRepository;
        
        @Autowired
        BrandRepository brandRepository;
        
        @Autowired
        AddressRepository addressRepository;
        
        @Autowired
        DocumentRepository documentRepository;
        
        @Autowired
        ShopRepository shopRepository;
        
        @Autowired
        RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
        
        @Autowired
        ShopAddressRepository shopAddressRepository;
        
        @Autowired
        RetailerBrandRepository retailerBrandRepository;
        
        @SuppressWarnings("unchecked")
        @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 CreateRetailerRequestModel createRetailerRequestModel){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                final Map<String, Object> createRetailerMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.CREATE_RETAILER_MAP);
                request.removeAttribute(ProfitMandiConstants.CREATE_RETAILER_MAP);
                Retailer retailer = new Retailer();
                retailer.setName(createRetailerMap.get(ProfitMandiConstants.NAME).toString());
                retailer.setNumber(createRetailerMap.get(ProfitMandiConstants.NUMBER).toString());
                retailer.setType((RetailerType)createRetailerMap.get(ProfitMandiConstants.TYPE));
                retailer.setMonthlySaleValue((SaleValue)createRetailerMap.get(ProfitMandiConstants.MONTLY_SALE_VALUE));
                retailer.setSmartphoneSaleValue((SaleValue)createRetailerMap.get(ProfitMandiConstants.SMARTPHONE_SALE_VALUE));
                retailer.setRecharge((boolean)createRetailerMap.get(ProfitMandiConstants.RECHARGE));
                retailer.setMobile((boolean)createRetailerMap.get(ProfitMandiConstants.MOBILE));
                retailer.setAccessories((boolean)createRetailerMap.get(ProfitMandiConstants.ACCESSORITES));
                retailer.setOther1((boolean)createRetailerMap.get(ProfitMandiConstants.OTHER1));
                retailer.setOther2((boolean)createRetailerMap.get(ProfitMandiConstants.OTHER2));
                Set<String> brandNames = (Set<String>) createRetailerMap.get(ProfitMandiConstants.BRAND_NAMES);
                
                try {
                        
                        Document retailerDocument = documentRepository.selectById((int)createRetailerMap.get(ProfitMandiConstants.DOCUMENT_ID));
                        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());
                        retailerRepository.persist(retailer);
                        Map<String, Object> createShopMap = (Map<String, Object>)createRetailerMap.get(ProfitMandiConstants.SHOP);
                        final Document shopDocument = documentRepository.selectById((int)createShopMap.get(ProfitMandiConstants.DOCUMENT_ID));
                        if(shopRepository.isExistByDocumentId(shopDocument.getId())){
                                throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "");
                        }
                        Shop shop = new Shop();
                        shop.setName(createShopMap.get(ProfitMandiConstants.NAME).toString());
                        shop.setDocumentId(shopDocument.getId());
                        shop.setRetailerId(retailer.getId());
                        try{
                                shopRepository.persist(shop);
                        }catch(ProfitMandiBusinessException profitMandiBusinessException){
                                
                        }
                        for(String brandName : brandNames){
                                Brand brand =new Brand();
                                brand.setName(brandName);
                                if(!brandRepository.isExistByName(brandName)){
                                        brandRepository.persist(brand);
                                }
                                final RetailerBrand retailerBrand = new RetailerBrand();
                                retailerBrand.setRetailerId(retailer.getId());
                                retailerBrand.setBrandId(brand.getId());
                                retailerBrandRepository.persist(retailerBrand);
                        }
                        final Address addressRetailer = (Address)createRetailerMap.get(ProfitMandiConstants.ADDRESS);
                        if(!addressRepository.isExist(addressRetailer)){
                                addressRepository.persist(addressRetailer);
                        }
                        final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
                        retailerRegisteredAddress.setRetailerId(retailer.getId());
                        retailerRegisteredAddress.setAddressId(addressRetailer.getId());
                        try{
                                retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
                        }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                                
                        }
                        
                        final Address addressShop = (Address)createShopMap.get(ProfitMandiConstants.ADDRESS);
                        if(!addressRepository.isExist(addressShop)){
                                addressRepository.persist(addressShop);
                                ShopAddress shopAddress = new ShopAddress();
                                shopAddress.setShopId(shop.getId());
                                shopAddress.setAddressId(addressShop.getId());
                                try{
                                        shopAddressRepository.persist(shopAddress);
                                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                                        
                                }
                        }
                        ShopAddress shopAddress = new ShopAddress();
                        shopAddress.setShopId(shop.getId());
                        shopAddress.setAddressId(addressShop.getId());
                        try{
                                shopAddressRepository.persist(shopAddress);
                        }catch(ProfitMandiBusinessException profitMandiBusinessException){
                                
                        }
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("RTLR_OK_1000"));
                        return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }catch (Exception e) {
                        LOGGER.error("Internal Server Error : ",e);
                        final Response response=new Response("","","", e.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
        public ResponseEntity<?> getAll(HttpServletRequest request){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectAll());
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (Exception e) {
                        LOGGER.error("Internal Server Error : ",e);
                        final Response response=new Response("","","", e.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        @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 {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectById(id));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }catch (Exception e) {
                        LOGGER.error("Internal Server Error : ",e);
                        final Response response=new Response("","","", e.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        
        @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 {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectByName(name));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }catch (Exception e) {
                        LOGGER.error("Internal Server Error : ",e);
                        final Response response=new Response("","","", e.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
                
        
        @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);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }catch (Exception e) {
                        LOGGER.error("Internal Server error : ",e);
                        final Response response=new Response("","","", e.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        
        @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);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }catch (Exception e) {
                        LOGGER.error("Internal Server error : ",e);
                        final Response response=new Response("","","", e.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        
        
}