Subversion Repositories SmartDukaan

Rev

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