Subversion Repositories SmartDukaan

Rev

Rev 21315 | Rev 21426 | 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 java.time.LocalDateTime;
21309 ashik.ali 4
import java.util.Map;
21390 ashik.ali 5
import java.util.Set;
21292 ashik.ali 6
 
7
import javax.servlet.http.HttpServletRequest;
8
 
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpStatus;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.stereotype.Controller;
21309 ashik.ali 15
import org.springframework.web.bind.annotation.RequestBody;
21292 ashik.ali 16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestMethod;
18
import org.springframework.web.bind.annotation.RequestParam;
19
 
20
import com.spice.profitmandi.common.ResponseCodeHolder;
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21390 ashik.ali 23
import com.spice.profitmandi.dao.entity.Address;
24
import com.spice.profitmandi.dao.entity.Brand;
25
import com.spice.profitmandi.dao.entity.Document;
21292 ashik.ali 26
import com.spice.profitmandi.dao.entity.Retailer;
21390 ashik.ali 27
import com.spice.profitmandi.dao.entity.RetailerBrand;
28
import com.spice.profitmandi.dao.entity.RetailerRegisteredAddress;
29
import com.spice.profitmandi.dao.entity.Shop;
30
import com.spice.profitmandi.dao.entity.ShopAddress;
31
import com.spice.profitmandi.dao.enumuration.RetailerType;
32
import com.spice.profitmandi.dao.enumuration.SaleValue;
33
import com.spice.profitmandi.dao.repository.AddressRepository;
34
import com.spice.profitmandi.dao.repository.BrandRepository;
35
import com.spice.profitmandi.dao.repository.DocumentRepository;
36
import com.spice.profitmandi.dao.repository.RetailerBrandRepository;
37
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
21292 ashik.ali 38
import com.spice.profitmandi.dao.repository.RetailerRepository;
21390 ashik.ali 39
import com.spice.profitmandi.dao.repository.ShopAddressRepository;
40
import com.spice.profitmandi.dao.repository.ShopRepository;
21292 ashik.ali 41
import com.spice.profitmandi.web.model.ProfitMandiResponse;
42
import com.spice.profitmandi.web.model.Response;
43
import com.spice.profitmandi.web.model.ResponseStatus;
21390 ashik.ali 44
import com.spice.profitmandi.web.req.CreateRetailerRequestModel;
21292 ashik.ali 45
 
21302 ashik.ali 46
import io.swagger.annotations.ApiImplicitParam;
47
import io.swagger.annotations.ApiImplicitParams;
48
import io.swagger.annotations.ApiOperation;
49
 
21292 ashik.ali 50
@Controller
51
public class RetailerController {
52
 
53
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
54
 
55
	@Autowired
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
74
	ShopAddressRepository shopAddressRepository;
75
 
76
	@Autowired
77
	RetailerBrandRepository retailerBrandRepository;
78
 
79
	@SuppressWarnings("unchecked")
21302 ashik.ali 80
	@ApiImplicitParams({
81
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
82
				required = true, dataType = "string", paramType = "header")
83
	})
21309 ashik.ali 84
 
21302 ashik.ali 85
	@ApiOperation(value = "Create Retailer")
86
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
21315 ashik.ali 87
	public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequestModel createRetailerRequestModel){
21292 ashik.ali 88
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21390 ashik.ali 89
		final Map<String, Object> createRetailerMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.CREATE_RETAILER_MAP);
90
		request.removeAttribute(ProfitMandiConstants.CREATE_RETAILER_MAP);
91
		Retailer retailer = new Retailer();
92
		retailer.setName(createRetailerMap.get(ProfitMandiConstants.NAME).toString());
93
		retailer.setNumber(createRetailerMap.get(ProfitMandiConstants.NUMBER).toString());
94
		retailer.setType((RetailerType)createRetailerMap.get(ProfitMandiConstants.TYPE));
95
		retailer.setMonthlySaleValue((SaleValue)createRetailerMap.get(ProfitMandiConstants.MONTLY_SALE_VALUE));
96
		retailer.setSmartphoneSaleValue((SaleValue)createRetailerMap.get(ProfitMandiConstants.SMARTPHONE_SALE_VALUE));
97
		retailer.setRecharge((boolean)createRetailerMap.get(ProfitMandiConstants.RECHARGE));
98
		retailer.setMobile((boolean)createRetailerMap.get(ProfitMandiConstants.MOBILE));
99
		retailer.setAccessories((boolean)createRetailerMap.get(ProfitMandiConstants.ACCESSORITES));
100
		retailer.setOther1((boolean)createRetailerMap.get(ProfitMandiConstants.OTHER1));
101
		retailer.setOther2((boolean)createRetailerMap.get(ProfitMandiConstants.OTHER2));
102
		Set<String> brandNames = (Set<String>) createRetailerMap.get(ProfitMandiConstants.BRAND_NAMES);
103
 
21292 ashik.ali 104
		try {
21390 ashik.ali 105
 
106
			Document retailerDocument = documentRepository.selectById((int)createRetailerMap.get(ProfitMandiConstants.DOCUMENT_ID));
107
			if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
108
				LOGGER.error("documet is already mapped with another retailer");
109
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "");
110
			}
111
			retailer.setDocumentId(retailerDocument.getId());
21292 ashik.ali 112
			retailerRepository.persist(retailer);
21390 ashik.ali 113
			Map<String, Object> createShopMap = (Map<String, Object>)createRetailerMap.get(ProfitMandiConstants.SHOP);
114
			final Document shopDocument = documentRepository.selectById((int)createShopMap.get(ProfitMandiConstants.DOCUMENT_ID));
115
			if(shopRepository.isExistByDocumentId(shopDocument.getId())){
116
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "");
117
			}
118
			Shop shop = new Shop();
119
			shop.setName(createShopMap.get(ProfitMandiConstants.NAME).toString());
120
			shop.setDocumentId(shopDocument.getId());
121
			shop.setRetailerId(retailer.getId());
122
			try{
123
				shopRepository.persist(shop);
124
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
125
 
126
			}
127
			for(String brandName : brandNames){
128
				Brand brand =new Brand();
129
				brand.setName(brandName);
130
				if(!brandRepository.isExistByName(brandName)){
131
					brandRepository.persist(brand);
132
				}
133
				final RetailerBrand retailerBrand = new RetailerBrand();
134
				retailerBrand.setRetailerId(retailer.getId());
135
				retailerBrand.setBrandId(brand.getId());
136
				retailerBrandRepository.persist(retailerBrand);
137
			}
138
			final Address addressRetailer = (Address)createRetailerMap.get(ProfitMandiConstants.ADDRESS);
139
			if(!addressRepository.isExist(addressRetailer)){
140
				addressRepository.persist(addressRetailer);
141
			}
142
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
143
			retailerRegisteredAddress.setRetailerId(retailer.getId());
144
			retailerRegisteredAddress.setAddressId(addressRetailer.getId());
145
			try{
146
				retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
147
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
148
 
149
			}
150
 
151
			final Address addressShop = (Address)createShopMap.get(ProfitMandiConstants.ADDRESS);
152
			if(!addressRepository.isExist(addressShop)){
153
				addressRepository.persist(addressShop);
154
				ShopAddress shopAddress = new ShopAddress();
155
				shopAddress.setShopId(shop.getId());
156
				shopAddress.setAddressId(addressShop.getId());
157
				try{
158
					shopAddressRepository.persist(shopAddress);
159
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
160
 
161
				}
162
			}
163
			ShopAddress shopAddress = new ShopAddress();
164
			shopAddress.setShopId(shop.getId());
165
			shopAddress.setAddressId(addressShop.getId());
166
			try{
167
				shopAddressRepository.persist(shopAddress);
168
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
169
 
170
			}
21309 ashik.ali 171
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("RTLR_OK_1000"));
21292 ashik.ali 172
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
173
		}catch (ProfitMandiBusinessException pmbe) {
174
			LOGGER.error("ProfitMandi error: ", pmbe);
175
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
176
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
177
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
178
		}catch (Exception e) {
179
			LOGGER.error("Internal Server Error : ",e);
180
			final Response response=new Response("","","", e.getMessage());
181
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
182
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
183
		}
184
	}
185
 
186
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
187
	public ResponseEntity<?> getAll(HttpServletRequest request){
188
		LOGGER.info("requested url : "+request.getRequestURL().toString());
189
		try {
190
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectAll());
191
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
192
		}catch (Exception e) {
193
			LOGGER.error("Internal Server Error : ",e);
194
			final Response response=new Response("","","", e.getMessage());
195
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
196
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
197
		}
198
	}
199
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21390 ashik.ali 200
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 201
		LOGGER.info("requested url : "+request.getRequestURL().toString());
202
		try {
203
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectById(id));
204
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
205
		}catch (ProfitMandiBusinessException pmbe) {
206
			LOGGER.error("ProfitMandi error: ", pmbe);
207
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
208
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
209
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
210
		}catch (Exception e) {
211
			LOGGER.error("Internal Server Error : ",e);
212
			final Response response=new Response("","","", e.getMessage());
213
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
214
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
215
		}
216
	}
217
 
218
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
219
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
220
		LOGGER.info("requested url : "+request.getRequestURL().toString());
221
		try {
222
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectByName(name));
223
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
224
		}catch (ProfitMandiBusinessException pmbe) {
225
			LOGGER.error("ProfitMandi error: ", pmbe);
226
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
227
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
228
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
229
		}catch (Exception e) {
230
			LOGGER.error("Internal Server Error : ",e);
231
			final Response response=new Response("","","", e.getMessage());
232
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
233
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
234
		}
235
	}
236
 
237
 
238
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21390 ashik.ali 239
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 240
		LOGGER.info("requested url : "+request.getRequestURL().toString());
241
		try {
242
			retailerRepository.deleteById(id);
243
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
244
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
245
		}catch (ProfitMandiBusinessException pmbe) {
246
			LOGGER.error("ProfitMandi error: ", pmbe);
247
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
248
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
249
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
250
		}catch (Exception e) {
251
			LOGGER.error("Internal Server error : ",e);
252
			final Response response=new Response("","","", e.getMessage());
253
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
254
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
255
		}
256
	}
257
 
21302 ashik.ali 258
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21390 ashik.ali 259
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 260
		LOGGER.info("requested url : "+request.getRequestURL().toString());
261
		try {
262
			retailerRepository.removeShop(shopId, retailerId);
263
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
264
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
265
		}catch (ProfitMandiBusinessException pmbe) {
266
			LOGGER.error("ProfitMandi error: ", pmbe);
267
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
268
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
269
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
270
		}catch (Exception e) {
271
			LOGGER.error("Internal Server error : ",e);
272
			final Response response=new Response("","","", e.getMessage());
273
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
274
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
275
		}
276
	}
277
 
278
 
279
}