Subversion Repositories SmartDukaan

Rev

Rev 29855 | Rev 29904 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29855 Rev 29900
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.awt.image.BufferedImage;
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayInputStream;
-
 
5
import java.io.File;
-
 
6
import java.io.FileNotFoundException;
4
import java.io.InputStream;
7
import java.io.InputStream;
-
 
8
import java.time.Instant;
5
import java.time.LocalDate;
9
import java.time.LocalDate;
6
import java.time.LocalDateTime;
10
import java.time.LocalDateTime;
7
import java.time.YearMonth;
11
import java.time.YearMonth;
8
import java.time.temporal.ChronoField;
-
 
9
import java.util.ArrayList;
12
import java.util.ArrayList;
10
import java.util.Arrays;
13
import java.util.Arrays;
11
import java.util.Collection;
14
import java.util.Collection;
12
import java.util.Comparator;
15
import java.util.Comparator;
-
 
16
import java.util.HashMap;
13
import java.util.List;
17
import java.util.List;
14
import java.util.Map;
18
import java.util.Map;
15
import java.util.Set;
19
import java.util.Set;
16
import java.util.stream.Collectors;
20
import java.util.stream.Collectors;
17
 
21
 
-
 
22
import javax.imageio.ImageIO;
18
import javax.servlet.http.HttpServletRequest;
23
import javax.servlet.http.HttpServletRequest;
19
import javax.transaction.Transactional;
24
import javax.transaction.Transactional;
20
 
25
 
-
 
26
import org.apache.commons.io.FileUtils;
21
import org.apache.commons.io.output.ByteArrayOutputStream;
27
import org.apache.commons.io.output.ByteArrayOutputStream;
22
import org.apache.logging.log4j.LogManager;
28
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.Logger;
29
import org.apache.logging.log4j.Logger;
-
 
30
import org.apache.velocity.app.VelocityEngine;
24
import org.springframework.beans.factory.annotation.Autowired;
31
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.cache.CacheManager;
32
import org.springframework.cache.CacheManager;
26
import org.springframework.core.io.InputStreamResource;
33
import org.springframework.core.io.InputStreamResource;
27
import org.springframework.http.HttpHeaders;
34
import org.springframework.http.HttpHeaders;
28
import org.springframework.http.HttpStatus;
35
import org.springframework.http.HttpStatus;
29
import org.springframework.http.ResponseEntity;
36
import org.springframework.http.ResponseEntity;
30
import org.springframework.stereotype.Controller;
37
import org.springframework.stereotype.Controller;
31
import org.springframework.ui.Model;
38
import org.springframework.ui.Model;
-
 
39
import org.springframework.ui.velocity.VelocityEngineUtils;
32
import org.springframework.web.bind.annotation.PathVariable;
40
import org.springframework.web.bind.annotation.PathVariable;
33
import org.springframework.web.bind.annotation.PostMapping;
41
import org.springframework.web.bind.annotation.PostMapping;
34
import org.springframework.web.bind.annotation.RequestBody;
42
import org.springframework.web.bind.annotation.RequestBody;
35
import org.springframework.web.bind.annotation.RequestMapping;
43
import org.springframework.web.bind.annotation.RequestMapping;
36
import org.springframework.web.bind.annotation.RequestMethod;
44
import org.springframework.web.bind.annotation.RequestMethod;
37
import org.springframework.web.bind.annotation.RequestParam;
45
import org.springframework.web.bind.annotation.RequestParam;
38
import org.springframework.web.bind.annotation.RequestPart;
46
import org.springframework.web.bind.annotation.RequestPart;
39
import org.springframework.web.multipart.MultipartFile;
47
import org.springframework.web.multipart.MultipartFile;
-
 
48
import org.xhtmlrenderer.simple.Graphics2DRenderer;
40
 
49
 
41
import com.google.gson.Gson;
50
import com.google.gson.Gson;
-
 
51
import com.jcraft.jsch.ChannelSftp;
-
 
52
import com.jcraft.jsch.JSch;
-
 
53
import com.jcraft.jsch.JSchException;
-
 
54
import com.jcraft.jsch.Session;
-
 
55
import com.jcraft.jsch.SftpATTRS;
-
 
56
import com.jcraft.jsch.SftpException;
-
 
57
import com.spice.profitmandi.common.enumuration.MessageType;
42
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
58
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
43
import com.spice.profitmandi.common.model.CustomRetailer;
59
import com.spice.profitmandi.common.model.CustomRetailer;
44
import com.spice.profitmandi.common.model.ProfitMandiConstants;
60
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
61
import com.spice.profitmandi.common.model.SendNotificationModel;
45
import com.spice.profitmandi.common.util.FileUtil;
62
import com.spice.profitmandi.common.util.FileUtil;
46
import com.spice.profitmandi.common.web.util.ResponseSender;
63
import com.spice.profitmandi.common.web.util.ResponseSender;
47
import com.spice.profitmandi.dao.entity.catalog.Offer;
64
import com.spice.profitmandi.dao.entity.catalog.Offer;
48
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
65
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
49
import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;
66
import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;
50
import com.spice.profitmandi.dao.model.CreateOfferRequest;
67
import com.spice.profitmandi.dao.model.CreateOfferRequest;
51
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
-
 
52
import com.spice.profitmandi.dao.model.OfferRowModel;
68
import com.spice.profitmandi.dao.model.OfferRowModel;
53
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
69
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
54
import com.spice.profitmandi.dao.repository.catalog.OfferMarginRepository;
70
import com.spice.profitmandi.dao.repository.catalog.OfferMarginRepository;
55
import com.spice.profitmandi.dao.repository.catalog.OfferPartnerRepository;
71
import com.spice.profitmandi.dao.repository.catalog.OfferPartnerRepository;
56
import com.spice.profitmandi.dao.repository.catalog.OfferRepository;
72
import com.spice.profitmandi.dao.repository.catalog.OfferRepository;
57
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
73
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
58
import com.spice.profitmandi.dao.repository.dtr.Mongo;
74
import com.spice.profitmandi.dao.repository.dtr.Mongo;
59
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
75
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
-
 
76
import com.spice.profitmandi.service.NotificationService;
60
import com.spice.profitmandi.service.authentication.RoleManager;
77
import com.spice.profitmandi.service.authentication.RoleManager;
61
import com.spice.profitmandi.service.offers.OfferService;
78
import com.spice.profitmandi.service.offers.OfferService;
62
import com.spice.profitmandi.service.user.RetailerService;
79
import com.spice.profitmandi.service.user.RetailerService;
63
import com.spice.profitmandi.web.model.LoginDetails;
80
import com.spice.profitmandi.web.model.LoginDetails;
64
import com.spice.profitmandi.web.util.CookiesProcessor;
81
import com.spice.profitmandi.web.util.CookiesProcessor;
Line 81... Line 98...
81
	private ResponseSender responseSender;
98
	private ResponseSender responseSender;
82
 
99
 
83
	@Autowired
100
	@Autowired
84
	private OfferPartnerRepository offerPartnerRepository;
101
	private OfferPartnerRepository offerPartnerRepository;
85
 
102
 
-
 
103
	private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
-
 
104
	private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
-
 
105
	
86
	@Autowired
106
	@Autowired
87
	private ItemRepository itemRepository;
107
	private ItemRepository itemRepository;
88
 
108
 
89
	@Autowired
109
	@Autowired
90
	private MVCResponseSender mvcResponseSender;
110
	private MVCResponseSender mvcResponseSender;
Line 97... Line 117...
97
 
117
 
98
	@Autowired
118
	@Autowired
99
	private Mongo mongoClient;
119
	private Mongo mongoClient;
100
 
120
 
101
	@Autowired
121
	@Autowired
-
 
122
	private NotificationService notificationService;
-
 
123
 
-
 
124
	@Autowired
102
	private CookiesProcessor cookiesProcessor;
125
	private CookiesProcessor cookiesProcessor;
103
 
126
 
104
	@Autowired
127
	@Autowired
105
	private OfferService offerService;
128
	private OfferService offerService;
106
	
129
 
107
	@Autowired
130
	@Autowired
108
	private CacheManager thirtyMinsTimeOutCacheManager;
131
	private CacheManager thirtyMinsTimeOutCacheManager;
109
	
132
 
110
	@Autowired
133
	@Autowired
111
	private CacheManager oneDayCacheManager;
134
	private CacheManager oneDayCacheManager;
112
 
135
 
113
	@Autowired
136
	@Autowired
114
	private PartnerTypeChangeService partnerTypeChangeService;
137
	private PartnerTypeChangeService partnerTypeChangeService;
Line 155... Line 178...
155
 
178
 
156
	@RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
179
	@RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
157
	public String activateOffer(HttpServletRequest request, @PathVariable int offerId, Model model)
180
	public String activateOffer(HttpServletRequest request, @PathVariable int offerId, Model model)
158
			throws ProfitMandiBusinessException, Exception {
181
			throws ProfitMandiBusinessException, Exception {
159
		Offer offer = offerRepository.selectById(offerId);
182
		Offer offer = offerRepository.selectById(offerId);
-
 
183
		boolean alreadyActive = false;
160
		offer.setActive(true);
184
		offer.setActive(true);
-
 
185
		if(alreadyActive) {
161
		oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());
186
			oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());
-
 
187
		}
-
 
188
		this.sendNotification(offer);
162
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
189
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
163
		return "response";
190
		return "response";
-
 
191
		//CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
-
 
192
		//model.addAttribute("offer", createOfferRequest);
-
 
193
		//return "offer_margin_detail_partner2";
-
 
194
	}
-
 
195
 
-
 
196
	@Autowired
-
 
197
	private com.spice.profitmandi.dao.repository.dtr.UserRepository dtrUserRepository;
-
 
198
 
-
 
199
	private void sendNotification(Offer offer) throws Exception {
-
 
200
		String fileName = "offer-" + offer.getId() + ".png";
-
 
201
		CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
-
 
202
		SendNotificationModel sendNotificationModel = new SendNotificationModel();
-
 
203
		sendNotificationModel.setCampaignName("SchemeOffer");
-
 
204
		sendNotificationModel.setTitle(offer.getName());
-
 
205
		sendNotificationModel.setMessage(createOfferRequest.getDashboardHtml());
-
 
206
		sendNotificationModel.setType("url");
-
 
207
		sendNotificationModel.setImageUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
-
 
208
		sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
-
 
209
		sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
-
 
210
		sendNotificationModel.setMessageType(MessageType.scheme);
-
 
211
		Map<Integer, Map<Integer, Long>> partnerWiseMap = null;
-
 
212
		Map<Integer, List<Offer>> offersMap = offerRepository.selectAllPublishedMapByPartner(YearMonth.now());
-
 
213
		List<Integer> fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer))
-
 
214
				.map(x -> x.getKey()).collect(Collectors.toList());
-
 
215
		sendNotificationModel.setFofoIds(fofoIds);
-
 
216
		Map<String, InputStream> fileStreamsMap = new HashMap<>();
-
 
217
		List<Integer> dtrIds = dtrUserRepository
-
 
218
				.selectAllByEmailIds(Arrays.asList("tarun.verma@smartdukaan.com", "amit.gupta@shop2020.in"))
-
 
219
				.stream().map(x->x.getId()).collect(Collectors.toList());
-
 
220
		sendNotificationModel.setUserIds(dtrIds);
-
 
221
		notificationService.sendNotification(sendNotificationModel);
-
 
222
		try {
-
 
223
			Map<String, Object> model = new HashMap<>();
-
 
224
			model.put("offer", createOfferRequest);
-
 
225
			String htmlContent=this.getContentFromTemplate("offer_margin_detail_partner.vm", model);
-
 
226
			LOGGER.info("this.getContentFromTemplate {}", htmlContent);
-
 
227
			fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
-
 
228
			this.uploadFile(fileStreamsMap);
-
 
229
		} catch (Exception e) {
-
 
230
			e.printStackTrace();
-
 
231
		}
-
 
232
 
-
 
233
	}
-
 
234
	
-
 
235
	private InputStream	asInputStream(BufferedImage bi) throws Exception {
-
 
236
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
 
237
		ImageIO.write(bi, "png", baos);
-
 
238
		return new ByteArrayInputStream(baos.toByteArray());
-
 
239
	
-
 
240
	}
-
 
241
 
-
 
242
	private ChannelSftp setupJsch() throws JSchException {
-
 
243
		JSch jsch = new JSch();
-
 
244
		Session jschSession = jsch.getSession("root", "192.168.179.131");
-
 
245
		//Session jschSession = jsch.getSession("root", "173.255.254.24");
-
 
246
		LOGGER.info("getClass().getResource(\"id_rsa\") {}", getClass().getClassLoader().getResource("id_rsa").getPath());
-
 
247
		jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
-
 
248
		//jschSession.setPassword("spic@2015static0");
-
 
249
		jschSession.setConfig("StrictHostKeyChecking", "no");
-
 
250
		jschSession.connect();
-
 
251
		return (ChannelSftp) jschSession.openChannel("sftp");
-
 
252
	}
-
 
253
 
-
 
254
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
-
 
255
			throws SftpException, FileNotFoundException {
-
 
256
 
-
 
257
		channelSftp.cd(destinationPath);
-
 
258
		String folderName = "image" + LocalDate.now();
-
 
259
 
-
 
260
		channelSftp.cd(destinationPath);
-
 
261
		SftpATTRS attrs = null;
-
 
262
 
-
 
263
		// check if the directory is already existing
-
 
264
		try {
-
 
265
			attrs = channelSftp.stat(folderName);
-
 
266
		} catch (Exception e) {
-
 
267
			System.out.println(destinationPath + "/" + folderName + " not found");
-
 
268
		}
-
 
269
 
-
 
270
		// else create a directory
-
 
271
		if (attrs == null) {
-
 
272
			channelSftp.mkdir(folderName);
-
 
273
			channelSftp.chmod(0755, ".");
-
 
274
		}
-
 
275
		channelSftp.cd(folderName);
-
 
276
 
-
 
277
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
-
 
278
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
-
 
279
		}
-
 
280
 
-
 
281
	}
-
 
282
	
-
 
283
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
-
 
284
		ChannelSftp channelSftp = setupJsch();
-
 
285
		channelSftp.connect();
-
 
286
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
-
 
287
		channelSftp.exit();
-
 
288
	}
-
 
289
	
-
 
290
	private InputStream getImageBuffer (String html) throws Exception {
-
 
291
		String fileName = "/tmp/" + Instant.now().toEpochMilli();
-
 
292
		FileUtils.writeStringToFile(new File(fileName), html, "UTF-8");
-
 
293
		return this.asInputStream(Graphics2DRenderer.renderToImageAutoSize("file:" + fileName, 500));
-
 
294
	}
-
 
295
 
-
 
296
	@Autowired
-
 
297
	VelocityEngine velocityEngine;
-
 
298
 
-
 
299
	private String getContentFromTemplate(String template, Map<String, Object> model) {
-
 
300
		StringBuffer content = new StringBuffer();
-
 
301
		try {
-
 
302
			content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model));
-
 
303
		} catch (Exception e) {
-
 
304
			e.printStackTrace();
-
 
305
		}
-
 
306
		return content.toString();
164
	}
307
	}
165
 
308
 
166
	@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
309
	@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
167
	public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
310
	public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
168
			throws ProfitMandiBusinessException {
311
			throws ProfitMandiBusinessException {
169
 
312
 
170
		List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values()
313
		List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values().stream()
171
				.stream().sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
314
				.sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
172
		model.addAttribute("offers", publishedOffers);
315
		model.addAttribute("offers", publishedOffers);
173
		model.addAttribute("yearMonth", yearMonth);
316
		model.addAttribute("yearMonth", yearMonth);
174
 
317
 
175
		return "offer_history";
318
		return "offer_history";
176
	}
319
	}
Line 219... Line 362...
219
				), listOfRows);
362
				), listOfRows);
220
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
363
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
221
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
364
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
222
		return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
365
		return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
223
	}
366
	}
224
	
367
 
225
	@RequestMapping(value = "/offerById", method = RequestMethod.GET)
368
	@RequestMapping(value = "/offerById", method = RequestMethod.GET)
226
	public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
369
	public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
227
		Offer offer = offerRepository.selectById(offerId);
370
		Offer offer = offerRepository.selectById(offerId);
228
 
371
 
229
		model.addAttribute("offer", offer);
372
		model.addAttribute("offer", offer);
230
 
373
 
231
		return "offer-edit";
374
		return "offer-edit";
232
 
375
 
233
	}
376
	}
234
	
377
 
235
	@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)
378
	@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)
236
	public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model) throws ProfitMandiBusinessException {
379
	public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model)
-
 
380
			throws ProfitMandiBusinessException {
237
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
381
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
238
		int fofoId = loginDetails.getFofoId();
382
		int fofoId = loginDetails.getFofoId();
239
		List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
383
		List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId,
-
 
384
				YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
240
		
385
 
241
		model.addAttribute("publishedOffers", createOffers);
386
		model.addAttribute("publishedOffers", createOffers);
242
		
-
 
243
		
-
 
244
		
387
 
245
		return "published-offers";
388
		return "published-offers";
246
	}
389
	}
247
	
390
 
248
	@PostMapping(value = "/offers/upload")
391
	@PostMapping(value = "/offers/upload")
249
	public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
392
	public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
250
			throws Exception {
393
			throws Exception {
251
		offerService.createOffers(targetFile.getInputStream());
394
		offerService.createOffers(targetFile.getInputStream());
252
		model.addAttribute("response", true);
395
		model.addAttribute("response", true);
253
		return "response";
396
		return "response";
254
	}
397
	}
255
	
398
 
256
	@Autowired
399
	@Autowired
257
	RoleManager roleManager;
400
	RoleManager roleManager;
258
 
401
 
259
	@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
402
	@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
260
	public String getOfferMargins(HttpServletRequest request,
403
	public String getOfferMargins(HttpServletRequest request,
261
			@RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
404
			@RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
262
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
405
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
263
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
406
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
264
		CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin?0:loginDetails.getFofoId(), offerId);
407
		CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin ? 0 : loginDetails.getFofoId(), offerId);
265
		
408
 
266
		model.addAttribute("offer", createOfferRequest);
409
		model.addAttribute("offer", createOfferRequest);
267
 
410
 
268
		return "offer_margin_detail_partner";
411
		return "offer_margin_detail_partner";
269
 
412
 
270
	}
413
	}