Subversion Repositories SmartDukaan

Rev

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