| Line 2... |
Line 2... |
| 2 |
|
2 |
|
| 3 |
import java.util.Set;
|
3 |
import java.util.Set;
|
| 4 |
|
4 |
|
| 5 |
import javax.servlet.http.HttpServletRequest;
|
5 |
import javax.servlet.http.HttpServletRequest;
|
| 6 |
|
6 |
|
| - |
|
7 |
import org.apache.thrift.TException;
|
| - |
|
8 |
import org.apache.thrift.transport.TTransportException;
|
| 7 |
import org.slf4j.Logger;
|
9 |
import org.slf4j.Logger;
|
| 8 |
import org.slf4j.LoggerFactory;
|
10 |
import org.slf4j.LoggerFactory;
|
| 9 |
import org.springframework.beans.factory.annotation.Autowired;
|
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 10 |
import org.springframework.http.ResponseEntity;
|
12 |
import org.springframework.http.ResponseEntity;
|
| 11 |
import org.springframework.stereotype.Controller;
|
13 |
import org.springframework.stereotype.Controller;
|
| Line 15... |
Line 17... |
| 15 |
import org.springframework.web.bind.annotation.RequestParam;
|
17 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 16 |
|
18 |
|
| 17 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
19 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
| 18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
20 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 19 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
21 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| - |
|
22 |
import com.spice.profitmandi.common.model.UserInfo;
|
| 20 |
import com.spice.profitmandi.dao.entity.Address;
|
23 |
import com.spice.profitmandi.dao.entity.Address;
|
| 21 |
import com.spice.profitmandi.dao.entity.Brand;
|
24 |
import com.spice.profitmandi.dao.entity.Brand;
|
| 22 |
import com.spice.profitmandi.dao.entity.Document;
|
25 |
import com.spice.profitmandi.dao.entity.Document;
|
| 23 |
import com.spice.profitmandi.dao.entity.Retailer;
|
26 |
import com.spice.profitmandi.dao.entity.Retailer;
|
| 24 |
import com.spice.profitmandi.dao.entity.RetailerBrand;
|
27 |
import com.spice.profitmandi.dao.entity.RetailerBrand;
|
| 25 |
import com.spice.profitmandi.dao.entity.RetailerRegisteredAddress;
|
28 |
import com.spice.profitmandi.dao.entity.RetailerRegisteredAddress;
|
| 26 |
import com.spice.profitmandi.dao.entity.Shop;
|
29 |
import com.spice.profitmandi.dao.entity.Shop;
|
| 27 |
import com.spice.profitmandi.dao.entity.ShopAddress;
|
30 |
import com.spice.profitmandi.dao.entity.ShopAddress;
|
| - |
|
31 |
import com.spice.profitmandi.dao.entity.User;
|
| 28 |
import com.spice.profitmandi.dao.repository.AddressRepository;
|
32 |
import com.spice.profitmandi.dao.repository.AddressRepository;
|
| 29 |
import com.spice.profitmandi.dao.repository.BrandRepository;
|
33 |
import com.spice.profitmandi.dao.repository.BrandRepository;
|
| 30 |
import com.spice.profitmandi.dao.repository.DocumentRepository;
|
34 |
import com.spice.profitmandi.dao.repository.DocumentRepository;
|
| 31 |
import com.spice.profitmandi.dao.repository.RetailerAddressRepository;
|
35 |
import com.spice.profitmandi.dao.repository.RetailerAddressRepository;
|
| 32 |
import com.spice.profitmandi.dao.repository.RetailerBrandRepository;
|
36 |
import com.spice.profitmandi.dao.repository.RetailerBrandRepository;
|
| 33 |
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
|
37 |
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
|
| 34 |
import com.spice.profitmandi.dao.repository.RetailerRepository;
|
38 |
import com.spice.profitmandi.dao.repository.RetailerRepository;
|
| 35 |
import com.spice.profitmandi.dao.repository.ShopAddressRepository;
|
39 |
import com.spice.profitmandi.dao.repository.ShopAddressRepository;
|
| 36 |
import com.spice.profitmandi.dao.repository.ShopRepository;
|
40 |
import com.spice.profitmandi.dao.repository.ShopRepository;
|
| - |
|
41 |
import com.spice.profitmandi.dao.repository.UserRepository;
|
| - |
|
42 |
import com.spice.profitmandi.thrift.clients.UserClient;
|
| 37 |
import com.spice.profitmandi.web.req.Category;
|
43 |
import com.spice.profitmandi.web.req.Category;
|
| 38 |
import com.spice.profitmandi.web.req.CreateRetailerAddressRequest;
|
44 |
import com.spice.profitmandi.web.req.CreateRetailerAddressRequest;
|
| 39 |
import com.spice.profitmandi.web.req.CreateRetailerRequest;
|
45 |
import com.spice.profitmandi.web.req.CreateRetailerRequest;
|
| 40 |
import com.spice.profitmandi.web.req.RetailerAddBrandRequest;
|
46 |
import com.spice.profitmandi.web.req.RetailerAddBrandRequest;
|
| 41 |
import com.spice.profitmandi.web.util.ResponseSender;
|
47 |
import com.spice.profitmandi.web.util.ResponseSender;
|
| 42 |
|
48 |
|
| - |
|
49 |
import in.shop2020.model.v1.user.Sex;
|
| - |
|
50 |
import in.shop2020.model.v1.user.UserContextException;
|
| 43 |
import io.swagger.annotations.ApiImplicitParam;
|
51 |
import io.swagger.annotations.ApiImplicitParam;
|
| 44 |
import io.swagger.annotations.ApiImplicitParams;
|
52 |
import io.swagger.annotations.ApiImplicitParams;
|
| 45 |
import io.swagger.annotations.ApiOperation;
|
53 |
import io.swagger.annotations.ApiOperation;
|
| 46 |
|
54 |
|
| 47 |
@Controller
|
55 |
@Controller
|
| 48 |
public class RetailerController {
|
56 |
public class RetailerController {
|
| 49 |
|
57 |
|
| 50 |
private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
|
58 |
private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
|
| 51 |
|
59 |
|
| 52 |
@Autowired
|
60 |
@Autowired
|
| - |
|
61 |
UserRepository userRepository;
|
| - |
|
62 |
|
| - |
|
63 |
@Autowired
|
| 53 |
ResponseSender<?> responseSender;
|
64 |
ResponseSender<?> responseSender;
|
| 54 |
|
65 |
|
| 55 |
@Autowired
|
66 |
@Autowired
|
| 56 |
RetailerRepository retailerRepository;
|
67 |
RetailerRepository retailerRepository;
|
| 57 |
|
68 |
|
| Line 87... |
Line 98... |
| 87 |
@ApiOperation(value = "Create Retailer")
|
98 |
@ApiOperation(value = "Create Retailer")
|
| 88 |
@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
|
99 |
@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
|
| 89 |
public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequest createRetailerRequest){
|
100 |
public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequest createRetailerRequest){
|
| 90 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
101 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
| 91 |
try{
|
102 |
try{
|
| - |
|
103 |
UserInfo userInfo = (UserInfo)request.getAttribute("userinfo");
|
| - |
|
104 |
User user = userRepository.selectById(userInfo.getUserId());
|
| 92 |
this.createRetailer(createRetailerRequest);
|
105 |
this.createRetailer(user.getEmailId(), createRetailerRequest);
|
| 93 |
return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));
|
106 |
return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));
|
| 94 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
107 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 95 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
108 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 96 |
return responseSender.badRequest(profitMandiBusinessException);
|
109 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 97 |
}
|
110 |
}
|
| 98 |
}
|
111 |
}
|
| 99 |
|
112 |
|
| 100 |
private void createRetailer(CreateRetailerRequest createRetailerRequest)
|
113 |
private void createRetailer(String emailId, CreateRetailerRequest createRetailerRequest)
|
| 101 |
throws ProfitMandiBusinessException{
|
114 |
throws ProfitMandiBusinessException{
|
| 102 |
Retailer retailer = new Retailer();
|
115 |
Retailer retailer = new Retailer();
|
| 103 |
retailer.setName(createRetailerRequest.getName());
|
116 |
retailer.setName(createRetailerRequest.getName());
|
| 104 |
retailer.setNumber(createRetailerRequest.getNumber());
|
117 |
retailer.setNumber(createRetailerRequest.getNumber());
|
| 105 |
retailer.setType(createRetailerRequest.getType());
|
118 |
retailer.setType(createRetailerRequest.getType());
|
| Line 118... |
Line 131... |
| 118 |
retailer.setDocumentId(retailerDocument.getId());
|
131 |
retailer.setDocumentId(retailerDocument.getId());
|
| 119 |
final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
|
132 |
final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
|
| 120 |
if(shopRepository.isExistByDocumentId(shopDocument.getId())){
|
133 |
if(shopRepository.isExistByDocumentId(shopDocument.getId())){
|
| 121 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "DCMNT_1000");
|
134 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "DCMNT_1000");
|
| 122 |
}
|
135 |
}
|
| 123 |
System.out.println(retailer.getNumber() + ", " + retailer.getType());
|
136 |
documentRepository.markDocumentAsPersisted(retailerDocument.getId());
|
| 124 |
if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){
|
137 |
if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){
|
| 125 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");
|
138 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");
|
| 126 |
}
|
139 |
}
|
| - |
|
140 |
documentRepository.markDocumentAsPersisted(shopDocument.getId());
|
| - |
|
141 |
retailer.setId(this.createSololicUser(emailId));
|
| 127 |
retailerRepository.persist(retailer);
|
142 |
retailerRepository.persist(retailer);
|
| 128 |
Shop shop = new Shop();
|
143 |
Shop shop = new Shop();
|
| 129 |
shop.setName(createRetailerRequest.getShop().getName());
|
144 |
shop.setName(createRetailerRequest.getShop().getName());
|
| 130 |
shop.setDocumentId(shopDocument.getId());
|
145 |
shop.setDocumentId(shopDocument.getId());
|
| 131 |
shop.setRetailerId(retailer.getId());
|
146 |
shop.setRetailerId(retailer.getId());
|
| Line 138... |
Line 153... |
| 138 |
}
|
153 |
}
|
| 139 |
final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
|
154 |
final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
|
| 140 |
retailerRegisteredAddress.setRetailerId(retailer.getId());
|
155 |
retailerRegisteredAddress.setRetailerId(retailer.getId());
|
| 141 |
retailerRegisteredAddress.setAddressId(addressRetailer.getId());
|
156 |
retailerRegisteredAddress.setAddressId(addressRetailer.getId());
|
| 142 |
retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
|
157 |
retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
|
| - |
|
158 |
if(!createRetailerRequest.isShopAddressSameAsRetailerAddress() && createRetailerRequest.getAddress() == null){
|
| - |
|
159 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ADDRESS, "", "");
|
| - |
|
160 |
}
|
| - |
|
161 |
if(createRetailerRequest.isShopAddressSameAsRetailerAddress()){
|
| - |
|
162 |
ShopAddress shopAddress = new ShopAddress();
|
| - |
|
163 |
shopAddress.setShopId(shop.getId());
|
| - |
|
164 |
shopAddress.setAddressId(addressRetailer.getId());
|
| - |
|
165 |
shopAddressRepository.persist(shopAddress);
|
| - |
|
166 |
}else{
|
| 143 |
final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
|
167 |
final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
|
| 144 |
if(!addressRepository.isExist(addressShop)){
|
168 |
if(!addressRepository.isExist(addressShop)){
|
| 145 |
addressRepository.persist(addressShop);
|
169 |
addressRepository.persist(addressShop);
|
| 146 |
}
|
170 |
}
|
| 147 |
ShopAddress shopAddress = new ShopAddress();
|
171 |
ShopAddress shopAddress = new ShopAddress();
|
| 148 |
shopAddress.setShopId(shop.getId());
|
172 |
shopAddress.setShopId(shop.getId());
|
| 149 |
shopAddress.setAddressId(addressShop.getId());
|
173 |
shopAddress.setAddressId(addressShop.getId());
|
| 150 |
shopAddressRepository.persist(shopAddress);
|
174 |
shopAddressRepository.persist(shopAddress);
|
| - |
|
175 |
}
|
| 151 |
}
|
176 |
}
|
| 152 |
|
177 |
|
| - |
|
178 |
private int createSololicUser(String emailId) throws ProfitMandiBusinessException{
|
| - |
|
179 |
in.shop2020.model.v1.user.User user = new in.shop2020.model.v1.user.User();
|
| - |
|
180 |
user.setEmail(emailId);
|
| - |
|
181 |
user.setPassword("");
|
| - |
|
182 |
user.setCommunicationEmail(emailId);
|
| - |
|
183 |
user.setSex(Sex.WONT_SAY);
|
| - |
|
184 |
|
| - |
|
185 |
try {
|
| - |
|
186 |
UserClient userContextServiceClient = new UserClient();
|
| - |
|
187 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
| - |
|
188 |
user = userClient.createUser(user);
|
| - |
|
189 |
return (int)user.getUserId();
|
| - |
|
190 |
}catch (UserContextException ux){
|
| - |
|
191 |
LOGGER.error("Unable to register user: " + ux.getMessage());
|
| - |
|
192 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
|
| - |
|
193 |
} catch (TTransportException e) {
|
| - |
|
194 |
LOGGER.error("Unable to register user." + e);
|
| - |
|
195 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
|
| - |
|
196 |
}catch (TException e) {
|
| - |
|
197 |
LOGGER.error("Unable to register user." + e);
|
| - |
|
198 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
|
| - |
|
199 |
}
|
| - |
|
200 |
}
|
| 153 |
private void addBrandWithRetailer(int retailerId, Set<Category> categories)
|
201 |
private void addBrandWithRetailer(int retailerId, Set<Category> categories)
|
| 154 |
throws ProfitMandiBusinessException{
|
202 |
throws ProfitMandiBusinessException{
|
| 155 |
for(Category category : categories){
|
203 |
for(Category category : categories){
|
| 156 |
for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
|
204 |
for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
|
| 157 |
Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
|
205 |
Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
|