Subversion Repositories SmartDukaan

Rev

Rev 21440 | Rev 21463 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21292 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
21448 ashik.ali 3
import java.util.Set;
4
 
21292 ashik.ali 5
import javax.servlet.http.HttpServletRequest;
6
 
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.ResponseEntity;
11
import org.springframework.stereotype.Controller;
21309 ashik.ali 12
import org.springframework.web.bind.annotation.RequestBody;
21292 ashik.ali 13
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RequestParam;
16
 
17
import com.spice.profitmandi.common.ResponseCodeHolder;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21390 ashik.ali 20
import com.spice.profitmandi.dao.entity.Address;
21
import com.spice.profitmandi.dao.entity.Brand;
22
import com.spice.profitmandi.dao.entity.Document;
21292 ashik.ali 23
import com.spice.profitmandi.dao.entity.Retailer;
21390 ashik.ali 24
import com.spice.profitmandi.dao.entity.RetailerBrand;
25
import com.spice.profitmandi.dao.entity.RetailerRegisteredAddress;
26
import com.spice.profitmandi.dao.entity.Shop;
27
import com.spice.profitmandi.dao.entity.ShopAddress;
28
import com.spice.profitmandi.dao.repository.AddressRepository;
29
import com.spice.profitmandi.dao.repository.BrandRepository;
30
import com.spice.profitmandi.dao.repository.DocumentRepository;
21426 ashik.ali 31
import com.spice.profitmandi.dao.repository.RetailerAddressRepository;
21390 ashik.ali 32
import com.spice.profitmandi.dao.repository.RetailerBrandRepository;
33
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
21292 ashik.ali 34
import com.spice.profitmandi.dao.repository.RetailerRepository;
21390 ashik.ali 35
import com.spice.profitmandi.dao.repository.ShopAddressRepository;
36
import com.spice.profitmandi.dao.repository.ShopRepository;
21431 ashik.ali 37
import com.spice.profitmandi.web.req.Category;
21426 ashik.ali 38
import com.spice.profitmandi.web.req.CreateRetailerAddressRequest;
39
import com.spice.profitmandi.web.req.CreateRetailerRequest;
40
import com.spice.profitmandi.web.req.RetailerAddBrandRequest;
21440 ashik.ali 41
import com.spice.profitmandi.web.util.ResponseSender;
21292 ashik.ali 42
 
21302 ashik.ali 43
import io.swagger.annotations.ApiImplicitParam;
44
import io.swagger.annotations.ApiImplicitParams;
45
import io.swagger.annotations.ApiOperation;
46
 
21292 ashik.ali 47
@Controller
48
public class RetailerController {
49
 
50
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
51
 
52
	@Autowired
21440 ashik.ali 53
	ResponseSender<?> responseSender;
54
 
55
	@Autowired
21292 ashik.ali 56
	RetailerRepository retailerRepository;
57
 
21390 ashik.ali 58
	@Autowired
59
	BrandRepository brandRepository;
60
 
61
	@Autowired
62
	AddressRepository addressRepository;
63
 
64
	@Autowired
65
	DocumentRepository documentRepository;
66
 
67
	@Autowired
68
	ShopRepository shopRepository;
69
 
70
	@Autowired
71
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
72
 
73
	@Autowired
21426 ashik.ali 74
	RetailerAddressRepository retailerAddressRepository;
75
 
76
	@Autowired
21390 ashik.ali 77
	ShopAddressRepository shopAddressRepository;
78
 
79
	@Autowired
80
	RetailerBrandRepository retailerBrandRepository;
81
 
21302 ashik.ali 82
	@ApiImplicitParams({
83
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
84
				required = true, dataType = "string", paramType = "header")
85
	})
21309 ashik.ali 86
 
21302 ashik.ali 87
	@ApiOperation(value = "Create Retailer")
88
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
21426 ashik.ali 89
	public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequest createRetailerRequest){
21292 ashik.ali 90
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 91
		try{
92
			this.createRetailer(createRetailerRequest);
93
			return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));
94
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
95
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
96
			return responseSender.badRequest(profitMandiBusinessException);
97
		}
98
	}
99
 
100
	private void createRetailer(CreateRetailerRequest createRetailerRequest) 
101
			throws ProfitMandiBusinessException{
21390 ashik.ali 102
		Retailer retailer = new Retailer();
21426 ashik.ali 103
		retailer.setName(createRetailerRequest.getName());
104
		retailer.setNumber(createRetailerRequest.getNumber());
105
		retailer.setType(createRetailerRequest.getType());
106
		retailer.setMonthlySaleValue(createRetailerRequest.getMonthlySaleValue());
107
		retailer.setSmartphoneSaleValue(createRetailerRequest.getSmartphoneSaleValue());
108
		retailer.setRecharge(createRetailerRequest.getLineOfBusiness().isRecharge());
109
		retailer.setMobile(createRetailerRequest.getLineOfBusiness().isMobile());
110
		retailer.setAccessories(createRetailerRequest.getLineOfBusiness().isAccessories());
21431 ashik.ali 111
		retailer.setOther1(createRetailerRequest.getLineOfBusiness().getOther1());
112
		retailer.setOther2(createRetailerRequest.getLineOfBusiness().getOther2());
21440 ashik.ali 113
		Document retailerDocument = documentRepository.selectById(createRetailerRequest.getDocumentId());
114
		if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
115
			LOGGER.error("documet is already mapped with another retailer");
21448 ashik.ali 116
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "DCMNT_1000");
21440 ashik.ali 117
		}
118
		retailer.setDocumentId(retailerDocument.getId());
119
		final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
120
		if(shopRepository.isExistByDocumentId(shopDocument.getId())){
21448 ashik.ali 121
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "DCMNT_1000");
21440 ashik.ali 122
		}
21448 ashik.ali 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");
126
		}
21440 ashik.ali 127
		retailerRepository.persist(retailer);
128
		Shop shop = new Shop();
129
		shop.setName(createRetailerRequest.getShop().getName());
130
		shop.setDocumentId(shopDocument.getId());
131
		shop.setRetailerId(retailer.getId());
132
		shopRepository.persist(shop);
21448 ashik.ali 133
		this.addBrandWithRetailer(retailer.getId(), createRetailerRequest.getCategories());
21440 ashik.ali 134
		final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
21426 ashik.ali 135
 
21440 ashik.ali 136
		if(!addressRepository.isExist(addressRetailer)){
137
			addressRepository.persist(addressRetailer);
21292 ashik.ali 138
		}
21440 ashik.ali 139
		final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
140
		retailerRegisteredAddress.setRetailerId(retailer.getId());
141
		retailerRegisteredAddress.setAddressId(addressRetailer.getId());
142
		retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
143
		final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
144
		if(!addressRepository.isExist(addressShop)){
145
			addressRepository.persist(addressShop);
146
		}
147
		ShopAddress shopAddress = new ShopAddress();
148
		shopAddress.setShopId(shop.getId());
149
		shopAddress.setAddressId(addressShop.getId());
150
		shopAddressRepository.persist(shopAddress);
21292 ashik.ali 151
	}
152
 
21448 ashik.ali 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
	}
21292 ashik.ali 165
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
166
	public ResponseEntity<?> getAll(HttpServletRequest request){
167
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 168
		return responseSender.ok(retailerRepository.selectAll());
21292 ashik.ali 169
	}
170
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 171
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 172
		LOGGER.info("requested url : "+request.getRequestURL().toString());
173
		try {
21440 ashik.ali 174
			return responseSender.ok(retailerRepository.selectById(id));
175
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
176
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
177
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 178
		}
179
	}
180
 
181
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
182
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
183
		LOGGER.info("requested url : "+request.getRequestURL().toString());
184
		try {
21440 ashik.ali 185
			return responseSender.ok(retailerRepository.selectByName(name));
186
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
187
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
188
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 189
		}
190
	}
191
 
192
 
193
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 194
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 195
		LOGGER.info("requested url : "+request.getRequestURL().toString());
196
		try {
197
			retailerRepository.deleteById(id);
21440 ashik.ali 198
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
199
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
200
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
201
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 202
		}
203
	}
204
 
21426 ashik.ali 205
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 206
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 207
		LOGGER.info("requested url : "+request.getRequestURL().toString());
208
		try {
209
			Document document = documentRepository.selectById(createShop.getDocumentId());
210
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
211
				LOGGER.error("documet is already mapped with another shop");
212
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
213
			}
214
			retailerRepository.selectById(retailerId);
215
			Shop shop = new Shop();
216
			shop.setRetailerId(retailerId);
217
			Address address = this.createAddress(createShop.getAddress());
218
			addressRepository.persist(address);
219
			shop.setAddressId(address.getId());
220
			shop.setDocumentId(document.getId());
221
			shopRepository.persist(shop);
222
			ShopAddress shopAddress = new ShopAddress();
223
			shopAddress.setAddressId(address.getId());
224
			shopAddress.setShopId(shop.getId());
225
			shopAddressRepository.persist(shopAddress);
21440 ashik.ali 226
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
227
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
228
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
229
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 230
		}
231
	}
232
 
233
 
21302 ashik.ali 234
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 235
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 236
		LOGGER.info("requested url : "+request.getRequestURL().toString());
237
		try {
238
			retailerRepository.removeShop(shopId, retailerId);
21440 ashik.ali 239
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
240
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
241
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
242
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 243
		}
244
	}
245
 
21426 ashik.ali 246
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
247
		Address address = new Address();
248
		address.setName(createAddress.getName());
249
		address.setLine1(createAddress.getLine1());
250
		address.setLine2(createAddress.getLine2());
251
		address.setLandmark(createAddress.getLandmark());
252
		address.setCity(createAddress.getCity());
253
		address.setState(createAddress.getState());
254
		address.setPinCode(createAddress.getPinCode());
255
		address.setCountry(createAddress.getCountry());
256
		address.setPhoneNumber(createAddress.getPhoneNumber());
257
		return address;
258
	}
21440 ashik.ali 259
 
21426 ashik.ali 260
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
261
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
262
		LOGGER.info("requested url : "+request.getRequestURL().toString());
263
		try {
264
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 265
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
266
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
267
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
268
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 269
		}
270
	}
21292 ashik.ali 271
 
21426 ashik.ali 272
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 273
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 274
		LOGGER.info("requested url : "+request.getRequestURL().toString());
275
		try {
276
			retailerRepository.removeAddress(addressId, retailerId);
21440 ashik.ali 277
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
278
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
279
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
280
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 281
		}
282
	}
283
 
284
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
285
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
286
		LOGGER.info("requested url : "+request.getRequestURL().toString());
287
		try {
21448 ashik.ali 288
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
289
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
21440 ashik.ali 290
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
291
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
292
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
293
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 294
		}
295
	}
296
 
297
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 298
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 299
		LOGGER.info("requested url : "+request.getRequestURL().toString());
300
		try {
301
			brandRepository.selectById(brandId);
302
			retailerRepository.selectById(retailerId);
21448 ashik.ali 303
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 304
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
305
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
306
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
307
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 308
		}
309
	}
310
 
311
 
21448 ashik.ali 312
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
313
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 314
		LOGGER.info("requested url : "+request.getRequestURL().toString());
315
		try {
21448 ashik.ali 316
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 317
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
318
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
319
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 320
		}
321
	}
322
 
21448 ashik.ali 323
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
324
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
325
		LOGGER.info("requested url : "+request.getRequestURL().toString());
326
		try {
327
			return responseSender.ok(retailerRegisteredAddressRepository.selectByRetailerId(retailerId));
328
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
329
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
330
			return responseSender.badRequest(profitMandiBusinessException);
331
		}
332
	}
333
 
21426 ashik.ali 334
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 335
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 336
		LOGGER.info("requested url : "+request.getRequestURL().toString());
337
		try {
21440 ashik.ali 338
			return responseSender.ok(shopRepository.selectByRetailerId(id));
339
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
340
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
341
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 342
		}
343
	}
344
 
345
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 346
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 347
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 348
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 349
	}
350
 
351
 
352
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 353
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 354
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 355
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 356
	}
357
 
21292 ashik.ali 358
}