Subversion Repositories SmartDukaan

Rev

Rev 23330 | Rev 23499 | 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;
22981 ashik.ali 6
import java.util.Map;
7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
23494 ashik.ali 13
import org.springframework.core.io.InputStreamResource;
14
import org.springframework.http.HttpHeaders;
15
import org.springframework.http.HttpStatus;
23330 ashik.ali 16
import org.springframework.http.ResponseEntity;
22981 ashik.ali 17
import org.springframework.stereotype.Controller;
18
import org.springframework.transaction.annotation.Transactional;
19
import org.springframework.ui.Model;
23026 ashik.ali 20
import org.springframework.web.bind.annotation.RequestBody;
22981 ashik.ali 21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestParam;
24
 
23494 ashik.ali 25
import com.spice.profitmandi.common.enumuration.ContentType;
22981 ashik.ali 26
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
27
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23026 ashik.ali 28
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
23330 ashik.ali 29
import com.spice.profitmandi.common.web.util.ResponseSender;
23494 ashik.ali 30
import com.spice.profitmandi.dao.entity.dtr.Document;
31
import com.spice.profitmandi.dao.entity.dtr.Retailer;
32
import com.spice.profitmandi.dao.entity.dtr.Shop;
33
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
34
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
35
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
22981 ashik.ali 36
import com.spice.profitmandi.service.user.RetailerService;
23494 ashik.ali 37
import com.spice.profitmandi.web.model.LoginDetails;
38
import com.spice.profitmandi.web.util.CookiesProcessor;
22981 ashik.ali 39
 
40
@Controller
41
@Transactional(rollbackFor=Throwable.class)
42
public class RetailerController {
43
 
44
	private static final Logger LOGGER = LoggerFactory.getLogger(RetailerController.class);
45
 
46
	@Autowired
47
	private RetailerService retailerService;
48
 
23330 ashik.ali 49
	@Autowired
23494 ashik.ali 50
	private RetailerRepository retailerRepository;
51
 
52
	@Autowired
53
	private ShopRepository shopRepository;
54
 
55
	@Autowired
56
	private DocumentRepository documentRepository;
57
 
58
	@Autowired
59
	private CookiesProcessor cookiesProcessor;
60
 
61
	@Autowired
23330 ashik.ali 62
	private ResponseSender<?> responseSender;
63
 
22981 ashik.ali 64
	@RequestMapping(value = "/retailerDetail", method = RequestMethod.GET)
65
	public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber, Model model)  throws ProfitMandiBusinessException{
66
		LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(), emailIdOrMobileNumber);
67
		Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
68
		model.addAllAttributes(map);
69
		return "retailer-details";
70
	}
71
 
23026 ashik.ali 72
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
73
	public String updateRetailerDetails(HttpServletRequest request, @RequestBody UpdateRetailerRequest updateRetailerRequest, Model model)  throws ProfitMandiBusinessException{
74
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
75
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
76
		model.addAllAttributes(map);
77
		return "retailer-details";
78
	}
79
 
22981 ashik.ali 80
 
81
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
82
	public String retailerInfo(HttpServletRequest request)  throws Exception{
83
		return "retailer-info";
84
	}
23330 ashik.ali 85
 
86
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
87
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName){
88
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
89
	}
23494 ashik.ali 90
 
91
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
92
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId) throws ProfitMandiBusinessException{
93
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
94
		Document document = documentRepository.selectById(documentId);
95
		Retailer retailer = retailerRepository.selectById(loginDetails.getFofoId());
96
 
97
		if(retailer.getDocumentId() == null) {
98
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
99
		}
100
		if(retailer.getDocumentId() != documentId) {
101
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
102
		}
103
		return responseSender.ok(document);
104
	}
105
 
106
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
107
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request, Model model) throws ProfitMandiBusinessException{
108
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
109
 
110
		Retailer retailer = retailerRepository.selectById(loginDetails.getFofoId());
111
 
112
		if(retailer.getDocumentId() == null) {
113
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
114
		}
115
 
116
		Document document = documentRepository.selectById(retailer.getDocumentId());
117
 
118
		FileInputStream file = null;
119
		try {
120
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
121
		} catch (FileNotFoundException e) {
122
			LOGGER.error("Retailer Document file not found : ", e);
123
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
124
		}
125
		//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
126
		//ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
127
 
128
		final HttpHeaders headers=new HttpHeaders();
129
		String contentType = "";
130
		if(document.getContentType() == ContentType.JPEG) {
131
			contentType = "image/jpeg";
132
		}else if(document.getContentType() == ContentType.PNG) {
133
			contentType = "image/png";
134
		}else if(document.getContentType() == ContentType.PDF) {
135
			contentType = "application/pdf";
136
		}
137
        headers.set("Content-Type", contentType);
138
		headers.set("Content-disposition", "inline; filename="+document.getName());
139
        headers.setContentLength(document.getSize());
140
        final InputStreamResource inputStreamResource=new InputStreamResource(file);
141
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
142
 
143
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
144
	}
145
 
146
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
147
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId) throws ProfitMandiBusinessException{
148
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
149
		Shop shop = shopRepository.selectById(shopId);
150
 
151
		if(shop.getRetailerId() != loginDetails.getFofoId()) {
152
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
153
		}
154
 
155
		if(shop.getDocumentId() == null) {
156
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
157
		}
158
 
159
		Document document = documentRepository.selectById(shop.getDocumentId());
160
		return responseSender.ok(document);
161
	}
162
 
163
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
164
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId, Model model) throws ProfitMandiBusinessException{
165
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
166
 
167
		Shop shop = shopRepository.selectById(shopId);
168
 
169
		if(shop.getRetailerId() != loginDetails.getFofoId()) {
170
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
171
		}
172
 
173
		if(shop.getDocumentId() == null) {
174
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
175
		}
176
 
177
		Document document = documentRepository.selectById(shop.getDocumentId());
178
 
179
		FileInputStream file = null;
180
		try {
181
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
182
		} catch (FileNotFoundException e) {
183
			LOGGER.error("Retailer Document file not found : ", e);
184
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
185
		}
186
		//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
187
		//ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
188
 
189
		final HttpHeaders headers=new HttpHeaders();
190
		String contentType = "";
191
		if(document.getContentType() == ContentType.JPEG) {
192
			contentType = "image/jpeg";
193
		}else if(document.getContentType() == ContentType.PNG) {
194
			contentType = "image/png";
195
		}else if(document.getContentType() == ContentType.PDF) {
196
			contentType = "application/pdf";
197
		}
198
        headers.set("Content-Type", contentType);
199
		headers.set("Content-disposition", "inline; filename="+document.getName());
200
        headers.setContentLength(document.getSize());
201
        final InputStreamResource inputStreamResource=new InputStreamResource(file);
202
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
203
 
204
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
205
	}
22981 ashik.ali 206
}