Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22981 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
23494 ashik.ali 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
24124 govind 6
import java.util.ArrayList;
7
import java.util.List;
22981 ashik.ali 8
import java.util.Map;
9
 
10
import javax.servlet.http.HttpServletRequest;
11
 
23955 govind 12
import org.apache.logging.log4j.Logger;
23945 amit.gupta 13
import org.apache.logging.log4j.LogManager;
22981 ashik.ali 14
import org.springframework.beans.factory.annotation.Autowired;
23494 ashik.ali 15
import org.springframework.core.io.InputStreamResource;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.HttpStatus;
23330 ashik.ali 18
import org.springframework.http.ResponseEntity;
22981 ashik.ali 19
import org.springframework.stereotype.Controller;
20
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.ui.Model;
24124 govind 22
import org.springframework.web.bind.annotation.GetMapping;
23
import org.springframework.web.bind.annotation.PostMapping;
23026 ashik.ali 24
import org.springframework.web.bind.annotation.RequestBody;
22981 ashik.ali 25
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestParam;
28
 
23494 ashik.ali 29
import com.spice.profitmandi.common.enumuration.ContentType;
23955 govind 30
import com.spice.profitmandi.common.enumuration.CounterSize;
22981 ashik.ali 31
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24123 tejbeer 32
import com.spice.profitmandi.common.model.AddLocationModel;
24124 govind 33
import com.spice.profitmandi.common.model.CustomRetailer;
22981 ashik.ali 34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24123 tejbeer 35
import com.spice.profitmandi.common.model.SendNotificationModel;
23026 ashik.ali 36
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
24123 tejbeer 37
import com.spice.profitmandi.common.util.Utils;
23330 ashik.ali 38
import com.spice.profitmandi.common.web.util.ResponseSender;
23494 ashik.ali 39
import com.spice.profitmandi.dao.entity.dtr.Document;
40
import com.spice.profitmandi.dao.entity.dtr.Retailer;
41
import com.spice.profitmandi.dao.entity.dtr.Shop;
24124 govind 42
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
43
import com.spice.profitmandi.dao.entity.fofo.InActiveFofoStore;
24123 tejbeer 44
import com.spice.profitmandi.dao.entity.user.User;
45
import com.spice.profitmandi.dao.entity.user.Location;
23494 ashik.ali 46
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
24124 govind 47
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
23494 ashik.ali 48
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
49
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
24123 tejbeer 50
import com.spice.profitmandi.dao.repository.user.LocationRepository;
51
import com.spice.profitmandi.dao.repository.user.UserRepository;
22981 ashik.ali 52
import com.spice.profitmandi.service.user.RetailerService;
24123 tejbeer 53
import com.spice.profitmandi.web.util.MVCResponseSender;
22981 ashik.ali 54
 
55
@Controller
24124 govind 56
@Transactional(rollbackFor = Throwable.class)
22981 ashik.ali 57
public class RetailerController {
58
 
23568 govind 59
	private static final Logger LOGGER = LogManager.getLogger(RetailerController.class);
24124 govind 60
 
22981 ashik.ali 61
	@Autowired
62
	private RetailerService retailerService;
24124 govind 63
 
23330 ashik.ali 64
	@Autowired
23494 ashik.ali 65
	private RetailerRepository retailerRepository;
24124 govind 66
 
23494 ashik.ali 67
	@Autowired
68
	private ShopRepository shopRepository;
24124 govind 69
 
23494 ashik.ali 70
	@Autowired
24124 govind 71
	private FofoStoreRepository fofoStoreRepository;
72
 
73
	@Autowired
23494 ashik.ali 74
	private DocumentRepository documentRepository;
24124 govind 75
 
23494 ashik.ali 76
	@Autowired
24123 tejbeer 77
	private UserRepository userRepository;
24124 govind 78
 
24123 tejbeer 79
	@Autowired
80
	private LocationRepository locationRepository;
24124 govind 81
 
24123 tejbeer 82
	@Autowired
23330 ashik.ali 83
	private ResponseSender<?> responseSender;
24124 govind 84
 
24123 tejbeer 85
	@Autowired
86
	private MVCResponseSender mvcResponseSender;
24124 govind 87
 
23784 ashik.ali 88
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.GET)
24124 govind 89
	public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request,
90
			@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
91
			Model model) throws ProfitMandiBusinessException {
92
		LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(),
93
				emailIdOrMobileNumber);
22981 ashik.ali 94
		Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
95
		model.addAllAttributes(map);
23955 govind 96
		model.addAttribute("counterSizes", CounterSize.values());
24140 amit.gupta 97
		if(map.containsKey("retailer")) {
98
				Retailer retailer = (Retailer)map.get("retailer");
99
				User user = userRepository.selectById(retailer.getId());
100
				if (user.getLocation() != null) {
101
					Location location = locationRepository.selectById(user.getLocation());
102
					model.addAttribute("locationdetail", location);
103
					LOGGER.info("location" + location);
104
				}
24123 tejbeer 105
		}
24124 govind 106
 
22981 ashik.ali 107
		return "retailer-details";
108
	}
24124 govind 109
 
23026 ashik.ali 110
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 111
	public String updateRetailerDetails(HttpServletRequest request,
112
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 113
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
114
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
115
		model.addAllAttributes(map);
23955 govind 116
		model.addAttribute("counterSizes", CounterSize.values());
23026 ashik.ali 117
		return "retailer-details";
118
	}
22981 ashik.ali 119
 
120
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 121
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 122
		return "retailer-info";
123
	}
24124 govind 124
 
23330 ashik.ali 125
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 126
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 127
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
128
	}
24124 govind 129
 
23494 ashik.ali 130
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 131
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
132
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
133
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 134
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 135
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 136
 
137
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 138
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
139
		}
24124 govind 140
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 141
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
142
		}
143
		return responseSender.ok(document);
144
	}
24124 govind 145
 
23494 ashik.ali 146
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 147
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
148
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
149
			throws ProfitMandiBusinessException {
150
 
23499 ashik.ali 151
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 152
 
153
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 154
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
155
		}
24124 govind 156
 
23494 ashik.ali 157
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 158
 
23494 ashik.ali 159
		FileInputStream file = null;
160
		try {
161
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
162
		} catch (FileNotFoundException e) {
163
			LOGGER.error("Retailer Document file not found : ", e);
164
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
165
		}
24124 govind 166
		// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
167
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
168
 
169
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 170
		String contentType = "";
24124 govind 171
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 172
			contentType = "image/jpeg";
24124 govind 173
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 174
			contentType = "image/png";
24124 govind 175
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 176
			contentType = "application/pdf";
177
		}
24124 govind 178
		headers.set("Content-Type", contentType);
179
		headers.set("Content-disposition", "inline; filename=" + document.getName());
180
		headers.setContentLength(document.getSize());
181
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
182
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
183
 
184
		// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 185
	}
24124 govind 186
 
23494 ashik.ali 187
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 188
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
189
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
190
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 191
		Shop shop = shopRepository.selectById(shopId);
24124 govind 192
 
193
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 194
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
195
		}
24124 govind 196
 
197
		if (shop.getDocumentId() == null) {
23494 ashik.ali 198
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
199
		}
24124 govind 200
 
23494 ashik.ali 201
		Document document = documentRepository.selectById(shop.getDocumentId());
202
		return responseSender.ok(document);
203
	}
24124 govind 204
 
23494 ashik.ali 205
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 206
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
207
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
208
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
209
			throws ProfitMandiBusinessException {
210
 
23494 ashik.ali 211
		Shop shop = shopRepository.selectById(shopId);
24124 govind 212
 
213
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 214
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
215
		}
24124 govind 216
 
217
		if (shop.getDocumentId() == null) {
23494 ashik.ali 218
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
219
		}
24124 govind 220
 
23494 ashik.ali 221
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 222
 
23494 ashik.ali 223
		FileInputStream file = null;
224
		try {
225
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
226
		} catch (FileNotFoundException e) {
227
			LOGGER.error("Retailer Document file not found : ", e);
228
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
229
		}
24124 govind 230
		// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
231
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
232
 
233
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 234
		String contentType = "";
24124 govind 235
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 236
			contentType = "image/jpeg";
24124 govind 237
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 238
			contentType = "image/png";
24124 govind 239
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 240
			contentType = "application/pdf";
241
		}
24124 govind 242
		headers.set("Content-Type", contentType);
243
		headers.set("Content-disposition", "inline; filename=" + document.getName());
244
		headers.setContentLength(document.getSize());
245
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
246
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
247
 
248
		// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 249
	}
24124 govind 250
 
24123 tejbeer 251
	@RequestMapping(value = "/getAddLocation", method = RequestMethod.GET)
24124 govind 252
	public String getAddLocationModal(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
24123 tejbeer 253
		model.addAttribute("stateNames", Utils.getAllStateNames());
24124 govind 254
 
24123 tejbeer 255
		return "add-location-modal";
256
	}
24124 govind 257
 
24123 tejbeer 258
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
24124 govind 259
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
260
			throws Exception {
261
 
262
		Location location = new Location();
263
		location.setName(addLocationModel.getName());
264
		location.setLine1(addLocationModel.getLine1());
265
		location.setLine2(addLocationModel.getLine2());
266
		location.setCity(addLocationModel.getCity());
267
		location.setState(addLocationModel.getState());
268
		location.setPin(addLocationModel.getPin());
269
 
270
		LOGGER.info("PostLocation" + location);
271
		locationRepository.persist(location);
272
 
273
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
24123 tejbeer 274
		return "response";
24124 govind 275
 
24123 tejbeer 276
	}
24124 govind 277
 
278
	@GetMapping(value = "/getAllStores")
279
	public String getAllStores(HttpServletRequest request, Model model) {
280
 
281
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll();
282
 
283
		LOGGER.info(this.getFofoIds(fofoStores));
284
 
285
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(this.getFofoIds(fofoStores));
286
 
287
		model.addAttribute("fofoStores", fofoStores);
288
 
289
		model.addAttribute("customRetailers", customRetailers);
290
 
291
		return "store";
292
 
293
	}
294
 
295
	@PostMapping(value = "/deactivateStore")
296
	public String deActivateStore(HttpServletRequest request,
297
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
298
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
299
 
300
		InActiveFofoStore inActiveFofoStore = new InActiveFofoStore();
301
		inActiveFofoStore.setId(fofoStore.getId());
302
		inActiveFofoStore.setCode(fofoStore.getCode());
303
		inActiveFofoStore.setCounterSize(fofoStore.getCounterSize());
304
		inActiveFofoStore.setLatitude(fofoStore.getLatitude());
305
		inActiveFofoStore.setLongitude(fofoStore.getLongitude());
306
 
307
		inActiveFofoStore.setMinimumInvestment(fofoStore.getMinimumInvestment());
308
 
309
		inActiveFofoStore.setUserAddress(fofoStore.getUserAddress());
310
 
311
		fofoStoreRepository.saveInActiveStore(inActiveFofoStore);
312
 
313
		LOGGER.info("inserted into InActiveFofoStore successfully");
314
 
315
		fofoStoreRepository.delete(fofoStore);
316
 
317
		LOGGER.info("Deleted from fofoStore successfully");
318
 
319
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
320
 
321
		return "response";
322
 
323
	}
324
 
325
	private List<Integer> getFofoIds(List<FofoStore> fofoStores) {
326
 
327
		List<Integer> fofoIds = new ArrayList<>();
328
 
329
		for (FofoStore fofoStore : fofoStores) {
330
 
331
			fofoIds.add(fofoStore.getId());
332
 
333
		}
334
 
335
		return fofoIds;
336
 
337
	}
338
 
339
}