Subversion Repositories SmartDukaan

Rev

Rev 23955 | Rev 24124 | 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;
24123 tejbeer 28
import com.spice.profitmandi.common.model.AddLocationModel;
22981 ashik.ali 29
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24123 tejbeer 30
import com.spice.profitmandi.common.model.SendNotificationModel;
23026 ashik.ali 31
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
24123 tejbeer 32
import com.spice.profitmandi.common.util.Utils;
23330 ashik.ali 33
import com.spice.profitmandi.common.web.util.ResponseSender;
23494 ashik.ali 34
import com.spice.profitmandi.dao.entity.dtr.Document;
35
import com.spice.profitmandi.dao.entity.dtr.Retailer;
36
import com.spice.profitmandi.dao.entity.dtr.Shop;
24123 tejbeer 37
import com.spice.profitmandi.dao.entity.user.User;
38
import com.spice.profitmandi.dao.entity.user.Location;
23494 ashik.ali 39
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
40
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
41
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
24123 tejbeer 42
import com.spice.profitmandi.dao.repository.user.LocationRepository;
43
import com.spice.profitmandi.dao.repository.user.UserRepository;
22981 ashik.ali 44
import com.spice.profitmandi.service.user.RetailerService;
24123 tejbeer 45
import com.spice.profitmandi.web.util.MVCResponseSender;
22981 ashik.ali 46
 
47
@Controller
23955 govind 48
@Transactional(rollbackFor=Throwable.class)
22981 ashik.ali 49
public class RetailerController {
50
 
23568 govind 51
	private static final Logger LOGGER = LogManager.getLogger(RetailerController.class);
23955 govind 52
 
22981 ashik.ali 53
	@Autowired
54
	private RetailerService retailerService;
23955 govind 55
 
23330 ashik.ali 56
	@Autowired
23494 ashik.ali 57
	private RetailerRepository retailerRepository;
23955 govind 58
 
23494 ashik.ali 59
	@Autowired
60
	private ShopRepository shopRepository;
61
 
62
	@Autowired
63
	private DocumentRepository documentRepository;
23955 govind 64
 
23494 ashik.ali 65
	@Autowired
24123 tejbeer 66
	private UserRepository userRepository;
67
 
68
	@Autowired
69
	private LocationRepository locationRepository;
70
 
71
	@Autowired
23330 ashik.ali 72
	private ResponseSender<?> responseSender;
73
 
24123 tejbeer 74
	@Autowired
75
	private MVCResponseSender mvcResponseSender;
76
 
23784 ashik.ali 77
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.GET)
23955 govind 78
	public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber, Model model)  throws ProfitMandiBusinessException{
79
		LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(), emailIdOrMobileNumber);
22981 ashik.ali 80
		Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
81
		model.addAllAttributes(map);
23955 govind 82
		model.addAttribute("counterSizes", CounterSize.values());
24123 tejbeer 83
		User user = userRepository.selectByEmailId(emailIdOrMobileNumber);
84
		if(user.getLocation() != null){
85
		Location location = locationRepository.selectById(user.getLocation());
86
		model.addAttribute("locationdetail",location);
87
		 LOGGER.info("location"+location );
88
		}
89
		LOGGER.info("map detail"+ map) ;
90
 
22981 ashik.ali 91
		return "retailer-details";
92
	}
23955 govind 93
 
23026 ashik.ali 94
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
23955 govind 95
	public String updateRetailerDetails(HttpServletRequest request, @RequestBody UpdateRetailerRequest updateRetailerRequest, Model model)  throws ProfitMandiBusinessException{
23026 ashik.ali 96
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
97
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
98
		model.addAllAttributes(map);
23955 govind 99
		model.addAttribute("counterSizes", CounterSize.values());
23026 ashik.ali 100
		return "retailer-details";
101
	}
23955 govind 102
 
22981 ashik.ali 103
 
104
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
23955 govind 105
	public String retailerInfo(HttpServletRequest request)  throws Exception{
22981 ashik.ali 106
		return "retailer-info";
107
	}
23955 govind 108
 
23330 ashik.ali 109
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
23955 govind 110
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName){
23330 ashik.ali 111
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
112
	}
23955 govind 113
 
23494 ashik.ali 114
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
23955 govind 115
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId, @RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException{
23494 ashik.ali 116
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 117
		Retailer retailer = retailerRepository.selectById(retailerId);
23955 govind 118
 
119
		if(retailer.getDocumentId() == null) {
23494 ashik.ali 120
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
121
		}
23955 govind 122
		if(retailer.getDocumentId() != documentId) {
23494 ashik.ali 123
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
124
		}
125
		return responseSender.ok(document);
126
	}
23955 govind 127
 
23494 ashik.ali 128
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
23955 govind 129
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model) throws ProfitMandiBusinessException{
130
 
23499 ashik.ali 131
		Retailer retailer = retailerRepository.selectById(retailerId);
23955 govind 132
 
133
		if(retailer.getDocumentId() == null) {
23494 ashik.ali 134
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
135
		}
23955 govind 136
 
23494 ashik.ali 137
		Document document = documentRepository.selectById(retailer.getDocumentId());
23955 govind 138
 
23494 ashik.ali 139
		FileInputStream file = null;
140
		try {
141
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
142
		} catch (FileNotFoundException e) {
143
			LOGGER.error("Retailer Document file not found : ", e);
144
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
145
		}
23955 govind 146
		//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
147
		//ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
148
 
149
		final HttpHeaders headers=new HttpHeaders();
23494 ashik.ali 150
		String contentType = "";
23955 govind 151
		if(document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 152
			contentType = "image/jpeg";
23955 govind 153
		}else if(document.getContentType() == ContentType.PNG) {
23494 ashik.ali 154
			contentType = "image/png";
23955 govind 155
		}else if(document.getContentType() == ContentType.PDF) {
23494 ashik.ali 156
			contentType = "application/pdf";
157
		}
23955 govind 158
        headers.set("Content-Type", contentType);
159
		headers.set("Content-disposition", "inline; filename="+document.getName());
160
        headers.setContentLength(document.getSize());
161
        final InputStreamResource inputStreamResource=new InputStreamResource(file);
162
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
163
 
164
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 165
	}
23955 govind 166
 
23494 ashik.ali 167
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
23955 govind 168
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId, @RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException{
23494 ashik.ali 169
		Shop shop = shopRepository.selectById(shopId);
23955 govind 170
 
171
		if(shop.getRetailerId() != retailerId) {
23494 ashik.ali 172
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
173
		}
23955 govind 174
 
175
		if(shop.getDocumentId() == null) {
23494 ashik.ali 176
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
177
		}
23955 govind 178
 
23494 ashik.ali 179
		Document document = documentRepository.selectById(shop.getDocumentId());
180
		return responseSender.ok(document);
181
	}
23955 govind 182
 
23494 ashik.ali 183
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
23955 govind 184
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId, @RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model) throws ProfitMandiBusinessException{
185
 
23494 ashik.ali 186
		Shop shop = shopRepository.selectById(shopId);
23955 govind 187
 
188
		if(shop.getRetailerId() != retailerId) {
23494 ashik.ali 189
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
190
		}
23955 govind 191
 
192
		if(shop.getDocumentId() == null) {
23494 ashik.ali 193
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
194
		}
23955 govind 195
 
23494 ashik.ali 196
		Document document = documentRepository.selectById(shop.getDocumentId());
23955 govind 197
 
23494 ashik.ali 198
		FileInputStream file = null;
199
		try {
200
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
201
		} catch (FileNotFoundException e) {
202
			LOGGER.error("Retailer Document file not found : ", e);
203
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
204
		}
23955 govind 205
		//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
206
		//ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
207
 
208
		final HttpHeaders headers=new HttpHeaders();
23494 ashik.ali 209
		String contentType = "";
23955 govind 210
		if(document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 211
			contentType = "image/jpeg";
23955 govind 212
		}else if(document.getContentType() == ContentType.PNG) {
23494 ashik.ali 213
			contentType = "image/png";
23955 govind 214
		}else if(document.getContentType() == ContentType.PDF) {
23494 ashik.ali 215
			contentType = "application/pdf";
216
		}
23955 govind 217
        headers.set("Content-Type", contentType);
218
		headers.set("Content-disposition", "inline; filename="+document.getName());
219
        headers.setContentLength(document.getSize());
220
        final InputStreamResource inputStreamResource=new InputStreamResource(file);
221
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
222
 
223
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 224
	}
24123 tejbeer 225
 
226
	@RequestMapping(value = "/getAddLocation", method = RequestMethod.GET)
227
	public String getAddLocationModal(HttpServletRequest request , Model model)  throws ProfitMandiBusinessException{
228
		model.addAttribute("stateNames", Utils.getAllStateNames());
229
 
230
		return "add-location-modal";
231
	}
232
 
233
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
234
	public String addLocation(HttpServletRequest request , @RequestBody AddLocationModel addLocationModel,Model model)  throws Exception{
235
 
236
        Location location = new Location();
237
        location.setName(addLocationModel.getName());
238
        location.setLine1(addLocationModel.getLine1());
239
        location.setLine2(addLocationModel.getLine2());
240
        location.setCity(addLocationModel.getCity());
241
        location.setState(addLocationModel.getState());
242
        location.setPin(addLocationModel.getPin());
243
 
244
        LOGGER.info("PostLocation"+location);
245
       locationRepository.persist(location);
246
 
247
        model.addAttribute("response", mvcResponseSender.createResponseString(true));
248
		return "response";
249
 
250
	}
22981 ashik.ali 251
}