Subversion Repositories SmartDukaan

Rev

Rev 24123 | Rev 24140 | 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());
24123 tejbeer 97
		User user = userRepository.selectByEmailId(emailIdOrMobileNumber);
24124 govind 98
		if (user.getLocation() != null) {
99
			Location location = locationRepository.selectById(user.getLocation());
100
			model.addAttribute("locationdetail", location);
101
			LOGGER.info("location" + location);
24123 tejbeer 102
		}
24124 govind 103
		LOGGER.info("map detail" + map);
104
 
22981 ashik.ali 105
		return "retailer-details";
106
	}
24124 govind 107
 
23026 ashik.ali 108
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 109
	public String updateRetailerDetails(HttpServletRequest request,
110
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 111
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
112
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
113
		model.addAllAttributes(map);
23955 govind 114
		model.addAttribute("counterSizes", CounterSize.values());
23026 ashik.ali 115
		return "retailer-details";
116
	}
22981 ashik.ali 117
 
118
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 119
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 120
		return "retailer-info";
121
	}
24124 govind 122
 
23330 ashik.ali 123
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 124
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 125
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
126
	}
24124 govind 127
 
23494 ashik.ali 128
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 129
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
130
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
131
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 132
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 133
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 134
 
135
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 136
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
137
		}
24124 govind 138
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 139
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
140
		}
141
		return responseSender.ok(document);
142
	}
24124 govind 143
 
23494 ashik.ali 144
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 145
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
146
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
147
			throws ProfitMandiBusinessException {
148
 
23499 ashik.ali 149
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 150
 
151
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 152
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
153
		}
24124 govind 154
 
23494 ashik.ali 155
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 156
 
23494 ashik.ali 157
		FileInputStream file = null;
158
		try {
159
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
160
		} catch (FileNotFoundException e) {
161
			LOGGER.error("Retailer Document file not found : ", e);
162
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
163
		}
24124 govind 164
		// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
165
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
166
 
167
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 168
		String contentType = "";
24124 govind 169
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 170
			contentType = "image/jpeg";
24124 govind 171
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 172
			contentType = "image/png";
24124 govind 173
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 174
			contentType = "application/pdf";
175
		}
24124 govind 176
		headers.set("Content-Type", contentType);
177
		headers.set("Content-disposition", "inline; filename=" + document.getName());
178
		headers.setContentLength(document.getSize());
179
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
180
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
181
 
182
		// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 183
	}
24124 govind 184
 
23494 ashik.ali 185
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 186
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
187
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
188
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 189
		Shop shop = shopRepository.selectById(shopId);
24124 govind 190
 
191
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 192
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
193
		}
24124 govind 194
 
195
		if (shop.getDocumentId() == null) {
23494 ashik.ali 196
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
197
		}
24124 govind 198
 
23494 ashik.ali 199
		Document document = documentRepository.selectById(shop.getDocumentId());
200
		return responseSender.ok(document);
201
	}
24124 govind 202
 
23494 ashik.ali 203
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 204
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
205
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
206
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
207
			throws ProfitMandiBusinessException {
208
 
23494 ashik.ali 209
		Shop shop = shopRepository.selectById(shopId);
24124 govind 210
 
211
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 212
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
213
		}
24124 govind 214
 
215
		if (shop.getDocumentId() == null) {
23494 ashik.ali 216
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
217
		}
24124 govind 218
 
23494 ashik.ali 219
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 220
 
23494 ashik.ali 221
		FileInputStream file = null;
222
		try {
223
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
224
		} catch (FileNotFoundException e) {
225
			LOGGER.error("Retailer Document file not found : ", e);
226
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
227
		}
24124 govind 228
		// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
229
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
230
 
231
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 232
		String contentType = "";
24124 govind 233
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 234
			contentType = "image/jpeg";
24124 govind 235
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 236
			contentType = "image/png";
24124 govind 237
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 238
			contentType = "application/pdf";
239
		}
24124 govind 240
		headers.set("Content-Type", contentType);
241
		headers.set("Content-disposition", "inline; filename=" + document.getName());
242
		headers.setContentLength(document.getSize());
243
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
244
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
245
 
246
		// return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 247
	}
24124 govind 248
 
24123 tejbeer 249
	@RequestMapping(value = "/getAddLocation", method = RequestMethod.GET)
24124 govind 250
	public String getAddLocationModal(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
24123 tejbeer 251
		model.addAttribute("stateNames", Utils.getAllStateNames());
24124 govind 252
 
24123 tejbeer 253
		return "add-location-modal";
254
	}
24124 govind 255
 
24123 tejbeer 256
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
24124 govind 257
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
258
			throws Exception {
259
 
260
		Location location = new Location();
261
		location.setName(addLocationModel.getName());
262
		location.setLine1(addLocationModel.getLine1());
263
		location.setLine2(addLocationModel.getLine2());
264
		location.setCity(addLocationModel.getCity());
265
		location.setState(addLocationModel.getState());
266
		location.setPin(addLocationModel.getPin());
267
 
268
		LOGGER.info("PostLocation" + location);
269
		locationRepository.persist(location);
270
 
271
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
24123 tejbeer 272
		return "response";
24124 govind 273
 
24123 tejbeer 274
	}
24124 govind 275
 
276
	@GetMapping(value = "/getAllStores")
277
	public String getAllStores(HttpServletRequest request, Model model) {
278
 
279
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll();
280
 
281
		LOGGER.info(this.getFofoIds(fofoStores));
282
 
283
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(this.getFofoIds(fofoStores));
284
 
285
		model.addAttribute("fofoStores", fofoStores);
286
 
287
		model.addAttribute("customRetailers", customRetailers);
288
 
289
		return "store";
290
 
291
	}
292
 
293
	@PostMapping(value = "/deactivateStore")
294
	public String deActivateStore(HttpServletRequest request,
295
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
296
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
297
 
298
		InActiveFofoStore inActiveFofoStore = new InActiveFofoStore();
299
		inActiveFofoStore.setId(fofoStore.getId());
300
		inActiveFofoStore.setCode(fofoStore.getCode());
301
		inActiveFofoStore.setCounterSize(fofoStore.getCounterSize());
302
		inActiveFofoStore.setLatitude(fofoStore.getLatitude());
303
		inActiveFofoStore.setLongitude(fofoStore.getLongitude());
304
 
305
		inActiveFofoStore.setMinimumInvestment(fofoStore.getMinimumInvestment());
306
 
307
		inActiveFofoStore.setUserAddress(fofoStore.getUserAddress());
308
 
309
		fofoStoreRepository.saveInActiveStore(inActiveFofoStore);
310
 
311
		LOGGER.info("inserted into InActiveFofoStore successfully");
312
 
313
		fofoStoreRepository.delete(fofoStore);
314
 
315
		LOGGER.info("Deleted from fofoStore successfully");
316
 
317
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
318
 
319
		return "response";
320
 
321
	}
322
 
323
	private List<Integer> getFofoIds(List<FofoStore> fofoStores) {
324
 
325
		List<Integer> fofoIds = new ArrayList<>();
326
 
327
		for (FofoStore fofoStore : fofoStores) {
328
 
329
			fofoIds.add(fofoStore.getId());
330
 
331
		}
332
 
333
		return fofoIds;
334
 
335
	}
336
 
337
}