Subversion Repositories SmartDukaan

Rev

Rev 21440 | Rev 21463 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21440 Rev 21448
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.util.Set;
-
 
4
 
3
import javax.servlet.http.HttpServletRequest;
5
import javax.servlet.http.HttpServletRequest;
4
 
6
 
5
import org.slf4j.Logger;
7
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
8
import org.slf4j.LoggerFactory;
7
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.http.ResponseEntity;
10
import org.springframework.http.ResponseEntity;
9
import org.springframework.stereotype.Controller;
11
import org.springframework.stereotype.Controller;
10
import org.springframework.transaction.annotation.Transactional;
-
 
11
import org.springframework.web.bind.annotation.RequestBody;
12
import org.springframework.web.bind.annotation.RequestBody;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.RequestMethod;
14
import org.springframework.web.bind.annotation.RequestMethod;
14
import org.springframework.web.bind.annotation.RequestParam;
15
import org.springframework.web.bind.annotation.RequestParam;
15
 
16
 
Line 94... Line 95...
94
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
95
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
95
			return responseSender.badRequest(profitMandiBusinessException);
96
			return responseSender.badRequest(profitMandiBusinessException);
96
		}
97
		}
97
	}
98
	}
98
	
99
	
99
	@Transactional
-
 
100
	private void createRetailer(CreateRetailerRequest createRetailerRequest) 
100
	private void createRetailer(CreateRetailerRequest createRetailerRequest) 
101
			throws ProfitMandiBusinessException{
101
			throws ProfitMandiBusinessException{
102
		Retailer retailer = new Retailer();
102
		Retailer retailer = new Retailer();
103
		retailer.setName(createRetailerRequest.getName());
103
		retailer.setName(createRetailerRequest.getName());
104
		retailer.setNumber(createRetailerRequest.getNumber());
104
		retailer.setNumber(createRetailerRequest.getNumber());
Line 111... Line 111...
111
		retailer.setOther1(createRetailerRequest.getLineOfBusiness().getOther1());
111
		retailer.setOther1(createRetailerRequest.getLineOfBusiness().getOther1());
112
		retailer.setOther2(createRetailerRequest.getLineOfBusiness().getOther2());
112
		retailer.setOther2(createRetailerRequest.getLineOfBusiness().getOther2());
113
		Document retailerDocument = documentRepository.selectById(createRetailerRequest.getDocumentId());
113
		Document retailerDocument = documentRepository.selectById(createRetailerRequest.getDocumentId());
114
		if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
114
		if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
115
			LOGGER.error("documet is already mapped with another retailer");
115
			LOGGER.error("documet is already mapped with another retailer");
116
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "");
116
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "DCMNT_1000");
117
		}
117
		}
118
		retailer.setDocumentId(retailerDocument.getId());
118
		retailer.setDocumentId(retailerDocument.getId());
119
		final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
119
		final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
120
		if(shopRepository.isExistByDocumentId(shopDocument.getId())){
120
		if(shopRepository.isExistByDocumentId(shopDocument.getId())){
121
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "");
121
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "DCMNT_1000");
-
 
122
		}
-
 
123
		System.out.println(retailer.getNumber() + ", " + retailer.getType());
-
 
124
		if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){
-
 
125
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");
122
		}
126
		}
123
		retailerRepository.persist(retailer);
127
		retailerRepository.persist(retailer);
124
		Shop shop = new Shop();
128
		Shop shop = new Shop();
125
		shop.setName(createRetailerRequest.getShop().getName());
129
		shop.setName(createRetailerRequest.getShop().getName());
126
		shop.setDocumentId(shopDocument.getId());
130
		shop.setDocumentId(shopDocument.getId());
127
		shop.setRetailerId(retailer.getId());
131
		shop.setRetailerId(retailer.getId());
128
		shopRepository.persist(shop);
132
		shopRepository.persist(shop);
129
		for(Category category : createRetailerRequest.getCategories()){
133
		this.addBrandWithRetailer(retailer.getId(), createRetailerRequest.getCategories());
130
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
-
 
131
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
-
 
132
				final RetailerBrand retailerBrand = new RetailerBrand();
-
 
133
				retailerBrand.setRetailerId(retailer.getId());
-
 
134
				retailerBrand.setBrandId(brand.getId());
-
 
135
				retailerBrandRepository.persist(retailerBrand);
-
 
136
			}
-
 
137
		}
-
 
138
		final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
134
		final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
139
			
135
			
140
		if(!addressRepository.isExist(addressRetailer)){
136
		if(!addressRepository.isExist(addressRetailer)){
141
			addressRepository.persist(addressRetailer);
137
			addressRepository.persist(addressRetailer);
142
		}
138
		}
Line 152... Line 148...
152
		shopAddress.setShopId(shop.getId());
148
		shopAddress.setShopId(shop.getId());
153
		shopAddress.setAddressId(addressShop.getId());
149
		shopAddress.setAddressId(addressShop.getId());
154
		shopAddressRepository.persist(shopAddress);
150
		shopAddressRepository.persist(shopAddress);
155
	}
151
	}
156
	
152
	
-
 
153
	private void addBrandWithRetailer(int retailerId, Set<Category> categories)
-
 
154
		throws ProfitMandiBusinessException{
-
 
155
		for(Category category : categories){
-
 
156
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
-
 
157
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
-
 
158
				final RetailerBrand retailerBrand = new RetailerBrand();
-
 
159
				retailerBrand.setRetailerId(retailerId);
-
 
160
				retailerBrand.setBrandId(brand.getId());
-
 
161
				retailerBrandRepository.persist(retailerBrand);
-
 
162
			}
-
 
163
		}
-
 
164
	}
157
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
165
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
158
	public ResponseEntity<?> getAll(HttpServletRequest request){
166
	public ResponseEntity<?> getAll(HttpServletRequest request){
159
		LOGGER.info("requested url : "+request.getRequestURL().toString());
167
		LOGGER.info("requested url : "+request.getRequestURL().toString());
160
		return responseSender.ok(retailerRepository.selectAll());
168
		return responseSender.ok(retailerRepository.selectAll());
161
	}
169
	}
Line 192... Line 200...
192
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
200
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
193
			return responseSender.badRequest(profitMandiBusinessException);
201
			return responseSender.badRequest(profitMandiBusinessException);
194
		}
202
		}
195
	}
203
	}
196
	
204
	
197
	@Transactional
-
 
198
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
205
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
199
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
206
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
200
		LOGGER.info("requested url : "+request.getRequestURL().toString());
207
		LOGGER.info("requested url : "+request.getRequestURL().toString());
201
		try {
208
		try {
202
			Document document = documentRepository.selectById(createShop.getDocumentId());
209
			Document document = documentRepository.selectById(createShop.getDocumentId());
Line 276... Line 283...
276
	
283
	
277
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
284
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
278
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
285
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
279
		LOGGER.info("requested url : "+request.getRequestURL().toString());
286
		LOGGER.info("requested url : "+request.getRequestURL().toString());
280
		try {
287
		try {
281
			Retailer retailer = retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
288
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
282
			for(String brandName : retailerAddBrandRequest.getBrandNames()){
289
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
283
				Brand brand = brandRepository.selectByName(brandName);
-
 
284
				RetailerBrand retailerBrand = new RetailerBrand();
-
 
285
				retailerBrand.setBrandId(brand.getId());
-
 
286
				retailerBrand.setRetailerId(retailer.getId());
-
 
287
			}
-
 
288
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
290
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
289
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
291
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
290
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
292
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
291
			return responseSender.badRequest(profitMandiBusinessException);
293
			return responseSender.badRequest(profitMandiBusinessException);
292
		}
294
		}
Line 296... Line 298...
296
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
298
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
297
		LOGGER.info("requested url : "+request.getRequestURL().toString());
299
		LOGGER.info("requested url : "+request.getRequestURL().toString());
298
		try {
300
		try {
299
			brandRepository.selectById(brandId);
301
			brandRepository.selectById(brandId);
300
			retailerRepository.selectById(retailerId);
302
			retailerRepository.selectById(retailerId);
301
			retailerBrandRepository.deleteByRetailerId(retailerId, brandId);
303
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
302
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
304
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
303
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
305
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
304
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
306
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
305
			return responseSender.badRequest(profitMandiBusinessException);
307
			return responseSender.badRequest(profitMandiBusinessException);
306
		}
308
		}
307
	}
309
	}
308
	
310
	
309
	
311
	
310
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT_DOCUMENT_ID, method=RequestMethod.GET)
312
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
311
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "document") int documentId){
313
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
-
 
314
		LOGGER.info("requested url : "+request.getRequestURL().toString());
-
 
315
		try {
-
 
316
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
-
 
317
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
318
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
-
 
319
			return responseSender.badRequest(profitMandiBusinessException);
-
 
320
		}
-
 
321
	}
-
 
322
	
-
 
323
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
-
 
324
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
312
		LOGGER.info("requested url : "+request.getRequestURL().toString());
325
		LOGGER.info("requested url : "+request.getRequestURL().toString());
313
		try {
326
		try {
314
			retailerRepository.selectByDocumentId(documentId);
-
 
315
			return responseSender.ok(documentRepository.selectById(documentId));
327
			return responseSender.ok(retailerRegisteredAddressRepository.selectByRetailerId(retailerId));
316
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
328
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
317
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
329
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
318
			return responseSender.badRequest(profitMandiBusinessException);
330
			return responseSender.badRequest(profitMandiBusinessException);
319
		}
331
		}
320
	}
332
	}