Rev 22037 | Rev 22355 | 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.util.Set;import javax.servlet.http.HttpServletRequest;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;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.common.model.UserInfo;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.dtr.Brand;import com.spice.profitmandi.dao.entity.dtr.Document;import com.spice.profitmandi.dao.entity.dtr.Retailer;import com.spice.profitmandi.dao.entity.dtr.RetailerBrand;import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;import com.spice.profitmandi.dao.entity.dtr.Shop;import com.spice.profitmandi.dao.entity.dtr.ShopAddress;import com.spice.profitmandi.dao.entity.dtr.User;import com.spice.profitmandi.dao.entity.dtr.UserAccounts;import com.spice.profitmandi.dao.entity.dtr.UserRole;import com.spice.profitmandi.dao.entity.user.Address;import com.spice.profitmandi.dao.enumuration.dtr.AccountType;import com.spice.profitmandi.dao.enumuration.dtr.RoleType;import com.spice.profitmandi.dao.repository.dtr.BrandRepository;import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerAddressRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerBrandRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;import com.spice.profitmandi.dao.repository.dtr.ShopRepository;import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;import com.spice.profitmandi.dao.repository.dtr.UserRepository;import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;import com.spice.profitmandi.dao.repository.user.AddressRepository;import com.spice.profitmandi.thrift.clients.UserClient;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 in.shop2020.model.v1.user.Sex;import in.shop2020.model.v1.user.UserContextException;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;@Controller@Transactional(rollbackFor=Throwable.class)public class RetailerController {private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);@AutowiredUserRepository userRepository;@AutowiredUserRoleRepository userRoleRepository;@AutowiredResponseSender<?> responseSender;@AutowiredRetailerRepository retailerRepository;@AutowiredBrandRepository brandRepository;@AutowiredAddressRepository addressRepository;@AutowiredDocumentRepository documentRepository;@AutowiredShopRepository shopRepository;@AutowiredUserAccountRepository userAccountRepository;@AutowiredRetailerRegisteredAddressRepository retailerRegisteredAddressRepository;@AutowiredRetailerAddressRepository retailerAddressRepository;@AutowiredShopAddressRepository shopAddressRepository;@AutowiredRetailerBrandRepository 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{UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");User user = userRepository.selectById(userInfo.getUserId());this.createRetailer(user, createRetailerRequest);return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));}catch (ProfitMandiBusinessException profitMandiBusinessException) {LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);return responseSender.badRequest(profitMandiBusinessException);}}private void createRetailer(User user, 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(), "DCMNT_1000");}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(), "DCMNT_1000");}documentRepository.markDocumentAsPersisted(retailerDocument.getId());if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");}documentRepository.markDocumentAsPersisted(shopDocument.getId());in.shop2020.model.v1.user.User saholicUser = this.createSaholicUser(user.getEmailId());retailer.setId((int)saholicUser.getUserId());retailerRepository.persist(retailer);Shop shop = new Shop();shop.setName(createRetailerRequest.getShop().getName());shop.setDocumentId(shopDocument.getId());shop.setRetailerId(retailer.getId());shopRepository.persist(shop);this.addBrandWithRetailer(retailer.getId(), createRetailerRequest.getCategories());final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());addressRetailer.setRetaierId(retailer.getId());addressRepository.persist(addressRetailer);final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();retailerRegisteredAddress.setRetailerId(retailer.getId());retailerRegisteredAddress.setAddressId(addressRetailer.getId());retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);if(!createRetailerRequest.isShopAddressSameAsRetailerAddress() && createRetailerRequest.getAddress() == null){throw new ProfitMandiBusinessException(ProfitMandiConstants.ADDRESS, "", "");}if(createRetailerRequest.isShopAddressSameAsRetailerAddress()){ShopAddress shopAddress = new ShopAddress();shopAddress.setShopId(shop.getId());shopAddress.setAddressId(addressRetailer.getId());shopAddressRepository.persist(shopAddress);}else{final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());addressRepository.persist(addressShop);ShopAddress shopAddress = new ShopAddress();shopAddress.setShopId(shop.getId());shopAddress.setAddressId(addressShop.getId());shopAddressRepository.persist(shopAddress);}UserAccounts ua = new UserAccounts();ua.setAccount_key(String.valueOf(saholicUser.getUserId()));ua.setUser_id(user.getId());ua.setAccount_type(AccountType.saholic);userAccountRepository.persist(ua);UserAccounts ua2 = new UserAccounts();ua2.setAccount_key(String.valueOf(saholicUser.getActiveCartId()));ua2.setUser_id(user.getId());ua2.setAccount_type(AccountType.cartId);userAccountRepository.persist(ua2);UserRole ur = new UserRole();ur.setRoleType(RoleType.RETAILER);ur.setUserId(user.getId());userRoleRepository.persist(ur);}private in.shop2020.model.v1.user.User createSaholicUser(String emailId) throws ProfitMandiBusinessException{in.shop2020.model.v1.user.User user = new in.shop2020.model.v1.user.User();user.setEmail(emailId);user.setPassword("");user.setCommunicationEmail(emailId);user.setSex(Sex.WONT_SAY);try {UserClient userContextServiceClient = new UserClient();in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();user = userClient.createUser(user);return user;}catch (UserContextException ux){LOGGER.error("Unable to register user: " + ux.getMessage());throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");} catch (TTransportException e) {LOGGER.error("Unable to register user." + e);throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");}catch (TException e) {LOGGER.error("Unable to register user." + e);throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");}}private void addBrandWithRetailer(int retailerId, Set<Category> categories)throws ProfitMandiBusinessException{for(Category category : categories){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(retailerId);retailerBrand.setBrandId(brand.getId());retailerBrandRepository.persist(retailerBrand);}}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){LOGGER.info("requested url : "+request.getRequestURL().toString());return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));}@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);}}@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 {retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());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.deleteByRetailerAndBrandId(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, method=RequestMethod.GET)public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){LOGGER.info("requested url : "+request.getRequestURL().toString());try {return responseSender.ok(retailerRepository.selectDocumentById(retailerId));}catch (ProfitMandiBusinessException profitMandiBusinessException) {LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);return responseSender.badRequest(profitMandiBusinessException);}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){LOGGER.info("requested url : "+request.getRequestURL().toString());try {return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));}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));}}