Subversion Repositories SmartDukaan

Rev

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