Subversion Repositories SmartDukaan

Rev

Rev 27608 | Rev 28266 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25380 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.io.ByteArrayInputStream;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.InputStream;
8
import java.io.InputStreamReader;
28264 tejbeer 9
import java.sql.Timestamp;
10
import java.time.LocalDate;
25380 amit.gupta 11
import java.time.LocalDateTime;
12
import java.time.ZoneOffset;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.stream.Collectors;
19
 
20
import javax.servlet.http.HttpServletRequest;
21
import javax.transaction.Transactional;
22
import javax.xml.bind.DatatypeConverter;
23
 
24
import org.apache.commons.csv.CSVFormat;
25
import org.apache.commons.csv.CSVParser;
26
import org.apache.commons.csv.CSVRecord;
27
import org.apache.commons.lang3.StringUtils;
25400 amit.gupta 28
import org.apache.logging.log4j.LogManager;
29
import org.apache.logging.log4j.Logger;
25380 amit.gupta 30
import org.springframework.beans.factory.annotation.Autowired;
31
import org.springframework.stereotype.Controller;
32
import org.springframework.ui.Model;
33
import org.springframework.web.bind.annotation.GetMapping;
34
import org.springframework.web.bind.annotation.PostMapping;
35
import org.springframework.web.bind.annotation.RequestBody;
36
import org.springframework.web.bind.annotation.RequestParam;
37
import org.springframework.web.bind.annotation.RequestPart;
38
import org.springframework.web.multipart.MultipartFile;
39
 
40
import com.google.gson.Gson;
41
import com.jcraft.jsch.ChannelSftp;
42
import com.jcraft.jsch.JSch;
43
import com.jcraft.jsch.JSchException;
44
import com.jcraft.jsch.Session;
45
import com.jcraft.jsch.SftpATTRS;
46
import com.jcraft.jsch.SftpException;
47
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
48
import com.spice.profitmandi.common.solr.SolrService;
49
import com.spice.profitmandi.dao.entity.catalog.Item;
28264 tejbeer 50
import com.spice.profitmandi.dao.entity.dtr.DocumentUrl;
25380 amit.gupta 51
import com.spice.profitmandi.dao.model.ContentPojo;
52
import com.spice.profitmandi.dao.model.MediaPojo;
53
import com.spice.profitmandi.dao.model.Specification;
54
import com.spice.profitmandi.dao.model.SpecificationGroup;
55
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
28264 tejbeer 56
import com.spice.profitmandi.dao.repository.dtr.DocumentUrlRepository;
25380 amit.gupta 57
import com.spice.profitmandi.dao.repository.dtr.Mongo;
58
import com.spice.profitmandi.web.model.EntityMediaPojo;
59
import com.spice.profitmandi.web.util.MVCResponseSender;
60
 
61
@Transactional(rollbackOn = Throwable.class)
62
@Controller
63
public class ContentController {
64
	@Autowired
65
	MVCResponseSender mvcResponseSender;
66
 
67
	@Autowired
68
	Mongo mongoClient;
69
 
70
	@Autowired
71
	SolrService solrService;
72
 
73
	@Autowired
74
	ItemRepository itemRepository;
75
 
28264 tejbeer 76
	@Autowired
77
	DocumentUrlRepository documentUrlRepository;
78
 
25380 amit.gupta 79
	private Gson gson = new Gson();
80
 
81
	public static final int Entity_Id = 0;
82
	public static final int Title = 1;
83
	public static final int KeySpec1 = 2;
84
	public static final int KeySpec2 = 3;
85
	public static final int KeySpec3 = 4;
86
	public static final int KeySpec4 = 5;
87
	public static final int Warranty = 6;
88
	public static final int Package_Contents = 7;
28264 tejbeer 89
	private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
90
	private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
25380 amit.gupta 91
 
92
	private static final String REMOTE_DIR = "/var/www/static.saholic.com/images/media/";
25986 amit.gupta 93
	private static final String STATIC_SERVER_URL = "https://static%d.smartdukaan.com/images/media/";
25380 amit.gupta 94
 
95
	private static final String THUMBNAIL = "thumbnail";
96
	private static final String ICON = "icon";
97
	private static final String DEFAULT = "default";
98
	private static final String NONE = "";
25409 amit.gupta 99
 
25400 amit.gupta 100
	private static final Logger LOGGER = LogManager.getLogger(ContentController.class);
25380 amit.gupta 101
 
102
	private String getFileName(int entityId, String description, String extension) {
103
		List<Item> items = itemRepository.selectAllByCatalogItemId(entityId);
25409 amit.gupta 104
		String imageString = getMediaPrefix(items.get(0).getItemDescriptionNoColor() + " " + description) + "-"
25380 amit.gupta 105
				+ LocalDateTime.now().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30));
25413 amit.gupta 106
		return imageString + "." + extension;
25380 amit.gupta 107
	}
108
 
109
	@PostMapping(value = "/content/upload")
110
	public String uploadContent(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
111
			throws Exception {
112
		List<ContentPojo> contentPojos = readFile(file);
113
		for (ContentPojo contentPojo : contentPojos) {
114
			mongoClient.persistEntity(contentPojo);
115
		}
116
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
117
		return "response";
118
	}
119
 
120
	@PostMapping(value = "/content/media/upload")
121
	public String uploadMediaContent(HttpServletRequest request, @RequestBody EntityMediaPojo entityMediaPojo,
122
			Model model) throws Exception {
123
		ContentPojo contentPojo = mongoClient.getEntityById(entityMediaPojo.getEntityId());
124
		Map<String, InputStream> fileStreamsMap = getStreamFileMap(contentPojo, entityMediaPojo);
25400 amit.gupta 125
		LOGGER.info("fileStreamsMap {}, " + fileStreamsMap.keySet());
25398 amit.gupta 126
		uploadFiles(fileStreamsMap, entityMediaPojo.getEntityId());
25380 amit.gupta 127
		mongoClient.persistEntity(contentPojo);
128
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
129
		return "response";
130
	}
131
 
28264 tejbeer 132
	@PostMapping(value = "/image/media/upload")
133
	public String uploadImageMediaContent(HttpServletRequest request, @RequestBody MediaPojo mediaPojo, Model model)
134
			throws Exception {
135
 
136
		LOGGER.info("mediaPojo" + mediaPojo);
137
		Map<String, InputStream> fileStreamsMap = new HashMap<>();
138
		String extension;
139
		String base64String = mediaPojo.getImageData();
140
		String[] strings = base64String.split(",");
141
		switch (strings[0]) {// check image's extension
142
		case "data:image/jpeg;base64":
143
			extension = "jpeg";
144
			break;
145
		case "data:image/png;base64":
146
			extension = "png";
147
			break;
148
		default:// should write cases for more images types
149
			extension = ".jpg";
150
			break;
151
		}
152
		LOGGER.info("After switch statement = {}", extension);
153
		// convert base64 string to binary data
154
		byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
155
		Timestamp tm = new Timestamp(System.currentTimeMillis());
156
		String fileName = mediaPojo.getTitle() + tm.getTime() + "." + extension;
157
		LOGGER.info("After switch statement Filename = {}", fileName);
158
		mediaPojo.setImageData(null);
159
		mediaPojo.setUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
160
		fileStreamsMap.put(fileName, new ByteArrayInputStream(data));
161
		LOGGER.info("fileStreamsMap" + fileStreamsMap);
162
 
163
		uploadFile(fileStreamsMap);
164
 
165
		DocumentUrl du = new DocumentUrl();
166
		du.setTitle(mediaPojo.getTitle());
167
		du.setUrl(mediaPojo.getUrl());
168
		du.setCreatedTimestamp(LocalDateTime.now());
169
		documentUrlRepository.persist(du);
170
 
171
		model.addAttribute("documents", du);
172
		return "image-url";
173
	}
174
 
175
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
176
		ChannelSftp channelSftp = setupJsch();
177
		channelSftp.connect();
178
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
179
		channelSftp.exit();
180
	}
181
 
182
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
183
			throws SftpException, FileNotFoundException {
184
 
185
		channelSftp.cd(destinationPath);
186
		String folderName = "image" + LocalDate.now();
187
 
188
		channelSftp.cd(destinationPath);
189
		SftpATTRS attrs = null;
190
 
191
		// check if the directory is already existing
192
		try {
193
			attrs = channelSftp.stat(folderName);
194
		} catch (Exception e) {
195
			System.out.println(destinationPath + "/" + "image" + LocalDate.now() + " not found");
196
		}
197
 
198
		// else create a directory
199
		if (attrs == null) {
200
			channelSftp.mkdir(folderName);
201
			channelSftp.chmod(0755, ".");
202
		}
203
		channelSftp.cd(folderName);
204
 
205
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
206
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
207
		}
208
 
209
	}
210
 
211
	@GetMapping(value = "/search/image/media")
212
	public String getImage(HttpServletRequest request, Model model, @RequestParam String title) throws Exception {
213
		List<DocumentUrl> documents = documentUrlRepository.selectBySearch(title);
214
 
215
		model.addAttribute("documents", documents);
216
		return "image-url";
217
 
218
	}
219
 
25380 amit.gupta 220
	@GetMapping(value = "/content/media")
221
	public String getMediaContent(HttpServletRequest request, Model model, @RequestParam int entityId)
222
			throws Exception {
223
		ContentPojo contentPojo = mongoClient.getEntityById(entityId);
25409 amit.gupta 224
		if (contentPojo == null) {
25380 amit.gupta 225
			throw new Exception("Please add content first");
226
		}
227
		EntityMediaPojo empojo = getEntityMediaPojo(contentPojo);
228
		model.addAttribute("response", mvcResponseSender.createResponseString(empojo));
229
		return "response";
230
	}
28264 tejbeer 231
 
27185 amit.gupta 232
	@GetMapping(value = "/catalog-item")
28264 tejbeer 233
	public String catalogItem(HttpServletRequest request, Model model) throws Exception {
27185 amit.gupta 234
		return "catalog-item";
235
	}
25380 amit.gupta 236
 
237
	private EntityMediaPojo getEntityMediaPojo(ContentPojo contentPojo) {
238
		EntityMediaPojo ep = new EntityMediaPojo();
239
		int defaultIndex = 0;
25409 amit.gupta 240
		if (contentPojo.getImages() == null) {
25380 amit.gupta 241
			ep.setMediaPojos(new ArrayList<>());
242
		} else {
243
			ep.setMediaPojos(contentPojo.getImages());
244
			for (MediaPojo mediaPojo : contentPojo.getImages()) {
25409 amit.gupta 245
				if (mediaPojo.getUrl() == null)
246
					continue;
247
 
25380 amit.gupta 248
				if (!mediaPojo.getUrl().equals(contentPojo.getDefaultImageUrl())) {
249
					defaultIndex++;
250
				}
251
			}
252
		}
253
		ep.setDefaultImageIndex(defaultIndex);
254
		return ep;
255
	}
256
 
257
	@GetMapping(value = "/entity")
28264 tejbeer 258
	public String searchEntity(HttpServletRequest request, @RequestParam(defaultValue = "null") String query,
259
			@RequestParam(defaultValue = "0") int categoryId, @RequestParam(defaultValue = "") List<String> brands,
260
			@RequestParam(defaultValue = "30") int limit, Model model) throws Exception {
27608 amit.gupta 261
		model.addAttribute("response", solrService.getContent(query, categoryId, brands, limit));
25380 amit.gupta 262
		return "response";
263
	}
264
 
265
	@GetMapping(value = "/content/index")
266
	public String index(HttpServletRequest request, Model model) throws Exception {
267
		return "content";
268
	}
269
 
270
	private List<ContentPojo> readFile(MultipartFile file) throws Exception {
271
		CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
272
		List<CSVRecord> records = parser.getRecords();
273
		if (records.size() < 2) {
274
			parser.close();
275
			throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
276
		}
277
		// Remove header
278
		records.remove(0);
279
		List<ContentPojo> returnList = new ArrayList<ContentPojo>();
280
		for (CSVRecord record : records) {
25430 amit.gupta 281
			ContentPojo cp = null;
282
			Long entityId = Long.parseLong(record.get(Entity_Id));
25380 amit.gupta 283
			try {
25430 amit.gupta 284
				cp = mongoClient.getEntityById(entityId);
285
			} catch (Exception e) {
286
			}
287
			try {
288
				if (cp == null) {
289
					cp = new ContentPojo(entityId);
290
				}
25380 amit.gupta 291
				cp.setWarranty(record.get(Warranty));
292
				cp.setKeySpecs(Arrays
293
						.asList(record.get(KeySpec1), record.get(KeySpec2), record.get(KeySpec3), record.get(KeySpec4))
25429 amit.gupta 294
						.stream().filter(x -> x != null && !x.equals("")).collect(Collectors.toList()));
25380 amit.gupta 295
				cp.setPackageContents(Arrays.asList(record.get(Package_Contents).split(",")));
296
				cp.setDetailedSpecs(getDetailedSpecs(record));
297
				returnList.add(cp);
298
			} catch (Exception e) {
299
				continue;
300
			}
301
		}
302
		parser.close();
303
		return returnList;
304
	}
305
 
306
	private List<SpecificationGroup> getDetailedSpecs(CSVRecord record) throws Exception {
307
		List<SpecificationGroup> specificationGroups = new ArrayList<>();
308
		int currentIndex = 8;
309
		while (StringUtils.isNotEmpty(record.get(currentIndex))) {
310
			int start = currentIndex;
311
			List<Specification> specifications = new ArrayList<>();
312
			int begin = 0;
313
			while (begin < 5) {
314
				int specKeyIndex = (begin * 2) + 1;
315
				int specValueIndex = (begin * 2) + 2;
316
 
317
				if (StringUtils.isNotEmpty(record.get(currentIndex + specKeyIndex))
318
						&& StringUtils.isNotEmpty(record.get(currentIndex + specValueIndex))) {
319
					Specification specification = new Specification(record.get(currentIndex + specKeyIndex),
320
							Arrays.asList(record.get(currentIndex + specValueIndex)));
321
					specifications.add(specification);
322
				}
323
				begin++;
324
			}
325
			SpecificationGroup specificationGroup = new SpecificationGroup(record.get(start), specifications);
326
			specificationGroups.add(specificationGroup);
25392 amit.gupta 327
			currentIndex += 11;
25380 amit.gupta 328
		}
329
 
330
		return specificationGroups;
331
	}
332
 
333
	private ChannelSftp setupJsch() throws JSchException {
334
		JSch jsch = new JSch();
28264 tejbeer 335
		Session jschSession = jsch.getSession("root", "50.116.10.120");
25410 amit.gupta 336
		jschSession.setPassword("spic@2015static0");
25396 amit.gupta 337
		jschSession.setConfig("StrictHostKeyChecking", "no");
25380 amit.gupta 338
		jschSession.connect();
339
		return (ChannelSftp) jschSession.openChannel("sftp");
340
	}
341
 
342
	private void uploadFiles(Map<String, InputStream> fileStreamsMap, int entityId) throws Exception {
343
		ChannelSftp channelSftp = setupJsch();
344
		channelSftp.connect();
345
		this.folderUpload(channelSftp, fileStreamsMap, REMOTE_DIR, entityId + "");
346
		channelSftp.exit();
347
	}
348
 
349
	private void recursiveFolderUpload(ChannelSftp channelSftp, String sourcePath, String destinationPath)
350
			throws SftpException, FileNotFoundException {
351
 
352
		File sourceFile = new File(sourcePath);
353
		if (sourceFile.isFile()) {
354
 
355
			// copy if it is a file
356
			channelSftp.cd(destinationPath);
357
			if (!sourceFile.getName().startsWith("."))
358
				channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);
359
 
360
		} else {
361
 
362
			System.out.println("inside else " + sourceFile.getName());
363
			File[] files = sourceFile.listFiles();
364
 
365
			if (files != null && !sourceFile.getName().startsWith(".")) {
366
 
367
				channelSftp.cd(destinationPath);
368
				SftpATTRS attrs = null;
369
 
370
				// check if the directory is already existing
371
				try {
372
					attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName());
373
				} catch (Exception e) {
374
					System.out.println(destinationPath + "/" + sourceFile.getName() + " not found");
375
				}
376
 
377
				// else create a directory
378
				if (attrs != null) {
379
					System.out.println("Directory exists IsDir=" + attrs.isDir());
380
				} else {
381
					System.out.println("Creating dir " + sourceFile.getName());
382
					channelSftp.mkdir(sourceFile.getName());
383
				}
384
 
385
				for (File f : files) {
386
					recursiveFolderUpload(channelSftp, f.getAbsolutePath(),
387
							destinationPath + "/" + sourceFile.getName());
388
				}
389
 
390
			}
391
		}
392
 
393
	}
394
 
395
	private void folderUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath,
396
			String folderName) throws SftpException, FileNotFoundException {
397
 
398
		channelSftp.cd(destinationPath);
399
		SftpATTRS attrs = null;
400
 
401
		// check if the directory is already existing
402
		try {
403
			attrs = channelSftp.stat(folderName);
404
		} catch (Exception e) {
405
			System.out.println(destinationPath + "/" + folderName + " not found");
406
		}
407
 
408
		// else create a directory
25413 amit.gupta 409
		if (attrs == null) {
410
			channelSftp.mkdir(folderName);
411
			channelSftp.chmod(0755, ".");
25380 amit.gupta 412
		}
413
		channelSftp.cd(folderName);
25413 amit.gupta 414
		channelSftp.rm("*");
25380 amit.gupta 415
 
416
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
417
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
418
		}
419
 
420
	}
421
 
422
	private Map<String, InputStream> getStreamFileMap(ContentPojo contentPojo, EntityMediaPojo entityMediaPojo) {
423
		Map<String, InputStream> map = new HashMap<>();
25401 amit.gupta 424
		LOGGER.info("entityMediaPojo.getMediaPojos() -[{}]", entityMediaPojo.getMediaPojos());
25404 amit.gupta 425
		for (int i = 0; i < entityMediaPojo.getMediaPojos().size(); i++) {
25380 amit.gupta 426
			MediaPojo mediaPojo = entityMediaPojo.getMediaPojos().get(i);
427
			String extension;
428
			String base64String = mediaPojo.getImageData();
429
			String[] strings = base64String.split(",");
430
			switch (strings[0]) {// check image's extension
431
			case "data:image/jpeg;base64":
432
				extension = "jpeg";
433
				break;
434
			case "data:image/png;base64":
435
				extension = "png";
436
				break;
437
			default:// should write cases for more images types
438
				extension = "jpg";
439
				break;
440
			}
25402 amit.gupta 441
			LOGGER.info("After switch statement = {}", extension);
25380 amit.gupta 442
			// convert base64 string to binary data
443
			byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
444
			String fileName = getFileName(entityMediaPojo.getEntityId(), mediaPojo.getTitle(), extension);
25402 amit.gupta 445
			LOGGER.info("After switch statement Filename = {}", fileName);
25380 amit.gupta 446
			mediaPojo.setImageData(null);
25430 amit.gupta 447
			String staticServerUrl = String.format(STATIC_SERVER_URL, entityMediaPojo.getEntityId() % 3);
25414 amit.gupta 448
			mediaPojo.setUrl(staticServerUrl + entityMediaPojo.getEntityId() + "/" + fileName);
25380 amit.gupta 449
			map.put(fileName, new ByteArrayInputStream(data));
450
			if (i == entityMediaPojo.getDefaultImageIndex()) {
451
				String defaultFileName = getFileName(entityMediaPojo.getEntityId(), DEFAULT, extension);
452
				map.put(defaultFileName, new ByteArrayInputStream(data));
25414 amit.gupta 453
				contentPojo.setDefaultImageUrl(staticServerUrl + entityMediaPojo.getEntityId() + "/" + defaultFileName);
25380 amit.gupta 454
			}
455
		}
456
		contentPojo.setImages(entityMediaPojo.getMediaPojos());
457
 
458
		return map;
459
	}
460
 
25409 amit.gupta 461
	private String getMediaPrefix(String productName) {
462
		String mediaPrefix = productName.toLowerCase().replace(' ', '-');
463
		mediaPrefix = mediaPrefix.replaceAll("/", "-");
464
		mediaPrefix = mediaPrefix.replaceAll("-+", "-");
465
		return mediaPrefix;
466
	}
467
 
28264 tejbeer 468
	@GetMapping(value = "/urlGeneration")
469
	public String urlGeneration(HttpServletRequest request, Model model) throws Exception {
470
		return "url-generation";
471
	}
25380 amit.gupta 472
}