Subversion Repositories SmartDukaan

Rev

Rev 28266 | Rev 29881 | 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());
28320 tejbeer 156
		String urlTitle = mediaPojo.getTitle().toLowerCase();
157
		String fileName = urlTitle.replace(' ', '-') + tm.getTime() + "." + extension;
28264 tejbeer 158
		LOGGER.info("After switch statement Filename = {}", fileName);
159
		mediaPojo.setImageData(null);
160
		mediaPojo.setUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
161
		fileStreamsMap.put(fileName, new ByteArrayInputStream(data));
162
		LOGGER.info("fileStreamsMap" + fileStreamsMap);
163
 
164
		uploadFile(fileStreamsMap);
165
 
166
		DocumentUrl du = new DocumentUrl();
167
		du.setTitle(mediaPojo.getTitle());
168
		du.setUrl(mediaPojo.getUrl());
169
		du.setCreatedTimestamp(LocalDateTime.now());
170
		documentUrlRepository.persist(du);
171
 
172
		model.addAttribute("documents", du);
173
		return "image-url";
174
	}
175
 
176
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
177
		ChannelSftp channelSftp = setupJsch();
178
		channelSftp.connect();
179
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
180
		channelSftp.exit();
181
	}
182
 
183
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
184
			throws SftpException, FileNotFoundException {
185
 
186
		channelSftp.cd(destinationPath);
187
		String folderName = "image" + LocalDate.now();
188
 
189
		channelSftp.cd(destinationPath);
190
		SftpATTRS attrs = null;
191
 
192
		// check if the directory is already existing
193
		try {
194
			attrs = channelSftp.stat(folderName);
195
		} catch (Exception e) {
196
			System.out.println(destinationPath + "/" + "image" + LocalDate.now() + " not found");
197
		}
198
 
199
		// else create a directory
200
		if (attrs == null) {
201
			channelSftp.mkdir(folderName);
202
			channelSftp.chmod(0755, ".");
203
		}
204
		channelSftp.cd(folderName);
205
 
206
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
207
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
208
		}
209
 
210
	}
211
 
212
	@GetMapping(value = "/search/image/media")
213
	public String getImage(HttpServletRequest request, Model model, @RequestParam String title) throws Exception {
214
		List<DocumentUrl> documents = documentUrlRepository.selectBySearch(title);
215
 
216
		model.addAttribute("documents", documents);
217
		return "image-url";
218
 
219
	}
220
 
25380 amit.gupta 221
	@GetMapping(value = "/content/media")
222
	public String getMediaContent(HttpServletRequest request, Model model, @RequestParam int entityId)
223
			throws Exception {
224
		ContentPojo contentPojo = mongoClient.getEntityById(entityId);
25409 amit.gupta 225
		if (contentPojo == null) {
25380 amit.gupta 226
			throw new Exception("Please add content first");
227
		}
228
		EntityMediaPojo empojo = getEntityMediaPojo(contentPojo);
229
		model.addAttribute("response", mvcResponseSender.createResponseString(empojo));
230
		return "response";
231
	}
28264 tejbeer 232
 
27185 amit.gupta 233
	@GetMapping(value = "/catalog-item")
28264 tejbeer 234
	public String catalogItem(HttpServletRequest request, Model model) throws Exception {
27185 amit.gupta 235
		return "catalog-item";
236
	}
25380 amit.gupta 237
 
238
	private EntityMediaPojo getEntityMediaPojo(ContentPojo contentPojo) {
239
		EntityMediaPojo ep = new EntityMediaPojo();
240
		int defaultIndex = 0;
25409 amit.gupta 241
		if (contentPojo.getImages() == null) {
25380 amit.gupta 242
			ep.setMediaPojos(new ArrayList<>());
243
		} else {
244
			ep.setMediaPojos(contentPojo.getImages());
245
			for (MediaPojo mediaPojo : contentPojo.getImages()) {
25409 amit.gupta 246
				if (mediaPojo.getUrl() == null)
247
					continue;
248
 
25380 amit.gupta 249
				if (!mediaPojo.getUrl().equals(contentPojo.getDefaultImageUrl())) {
250
					defaultIndex++;
251
				}
252
			}
253
		}
254
		ep.setDefaultImageIndex(defaultIndex);
255
		return ep;
256
	}
257
 
258
	@GetMapping(value = "/entity")
28264 tejbeer 259
	public String searchEntity(HttpServletRequest request, @RequestParam(defaultValue = "null") String query,
260
			@RequestParam(defaultValue = "0") int categoryId, @RequestParam(defaultValue = "") List<String> brands,
261
			@RequestParam(defaultValue = "30") int limit, Model model) throws Exception {
27608 amit.gupta 262
		model.addAttribute("response", solrService.getContent(query, categoryId, brands, limit));
25380 amit.gupta 263
		return "response";
264
	}
265
 
266
	@GetMapping(value = "/content/index")
267
	public String index(HttpServletRequest request, Model model) throws Exception {
268
		return "content";
269
	}
270
 
271
	private List<ContentPojo> readFile(MultipartFile file) throws Exception {
272
		CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
273
		List<CSVRecord> records = parser.getRecords();
274
		if (records.size() < 2) {
275
			parser.close();
276
			throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
277
		}
278
		// Remove header
279
		records.remove(0);
280
		List<ContentPojo> returnList = new ArrayList<ContentPojo>();
281
		for (CSVRecord record : records) {
25430 amit.gupta 282
			ContentPojo cp = null;
283
			Long entityId = Long.parseLong(record.get(Entity_Id));
25380 amit.gupta 284
			try {
25430 amit.gupta 285
				cp = mongoClient.getEntityById(entityId);
286
			} catch (Exception e) {
287
			}
288
			try {
289
				if (cp == null) {
290
					cp = new ContentPojo(entityId);
291
				}
25380 amit.gupta 292
				cp.setWarranty(record.get(Warranty));
293
				cp.setKeySpecs(Arrays
294
						.asList(record.get(KeySpec1), record.get(KeySpec2), record.get(KeySpec3), record.get(KeySpec4))
25429 amit.gupta 295
						.stream().filter(x -> x != null && !x.equals("")).collect(Collectors.toList()));
25380 amit.gupta 296
				cp.setPackageContents(Arrays.asList(record.get(Package_Contents).split(",")));
297
				cp.setDetailedSpecs(getDetailedSpecs(record));
298
				returnList.add(cp);
299
			} catch (Exception e) {
300
				continue;
301
			}
302
		}
303
		parser.close();
304
		return returnList;
305
	}
306
 
307
	private List<SpecificationGroup> getDetailedSpecs(CSVRecord record) throws Exception {
308
		List<SpecificationGroup> specificationGroups = new ArrayList<>();
309
		int currentIndex = 8;
310
		while (StringUtils.isNotEmpty(record.get(currentIndex))) {
311
			int start = currentIndex;
312
			List<Specification> specifications = new ArrayList<>();
313
			int begin = 0;
314
			while (begin < 5) {
315
				int specKeyIndex = (begin * 2) + 1;
316
				int specValueIndex = (begin * 2) + 2;
317
 
318
				if (StringUtils.isNotEmpty(record.get(currentIndex + specKeyIndex))
319
						&& StringUtils.isNotEmpty(record.get(currentIndex + specValueIndex))) {
320
					Specification specification = new Specification(record.get(currentIndex + specKeyIndex),
321
							Arrays.asList(record.get(currentIndex + specValueIndex)));
322
					specifications.add(specification);
323
				}
324
				begin++;
325
			}
326
			SpecificationGroup specificationGroup = new SpecificationGroup(record.get(start), specifications);
327
			specificationGroups.add(specificationGroup);
25392 amit.gupta 328
			currentIndex += 11;
25380 amit.gupta 329
		}
330
 
331
		return specificationGroups;
332
	}
333
 
334
	private ChannelSftp setupJsch() throws JSchException {
335
		JSch jsch = new JSch();
28266 tejbeer 336
		Session jschSession = jsch.getSession("root", "192.168.179.131");
25410 amit.gupta 337
		jschSession.setPassword("spic@2015static0");
25396 amit.gupta 338
		jschSession.setConfig("StrictHostKeyChecking", "no");
25380 amit.gupta 339
		jschSession.connect();
340
		return (ChannelSftp) jschSession.openChannel("sftp");
341
	}
342
 
343
	private void uploadFiles(Map<String, InputStream> fileStreamsMap, int entityId) throws Exception {
344
		ChannelSftp channelSftp = setupJsch();
345
		channelSftp.connect();
346
		this.folderUpload(channelSftp, fileStreamsMap, REMOTE_DIR, entityId + "");
347
		channelSftp.exit();
348
	}
349
 
350
	private void recursiveFolderUpload(ChannelSftp channelSftp, String sourcePath, String destinationPath)
351
			throws SftpException, FileNotFoundException {
352
 
353
		File sourceFile = new File(sourcePath);
354
		if (sourceFile.isFile()) {
355
 
356
			// copy if it is a file
357
			channelSftp.cd(destinationPath);
358
			if (!sourceFile.getName().startsWith("."))
359
				channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);
360
 
361
		} else {
362
 
363
			System.out.println("inside else " + sourceFile.getName());
364
			File[] files = sourceFile.listFiles();
365
 
366
			if (files != null && !sourceFile.getName().startsWith(".")) {
367
 
368
				channelSftp.cd(destinationPath);
369
				SftpATTRS attrs = null;
370
 
371
				// check if the directory is already existing
372
				try {
373
					attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName());
374
				} catch (Exception e) {
375
					System.out.println(destinationPath + "/" + sourceFile.getName() + " not found");
376
				}
377
 
378
				// else create a directory
379
				if (attrs != null) {
380
					System.out.println("Directory exists IsDir=" + attrs.isDir());
381
				} else {
382
					System.out.println("Creating dir " + sourceFile.getName());
383
					channelSftp.mkdir(sourceFile.getName());
384
				}
385
 
386
				for (File f : files) {
387
					recursiveFolderUpload(channelSftp, f.getAbsolutePath(),
388
							destinationPath + "/" + sourceFile.getName());
389
				}
390
 
391
			}
392
		}
393
 
394
	}
395
 
396
	private void folderUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath,
397
			String folderName) throws SftpException, FileNotFoundException {
398
 
399
		channelSftp.cd(destinationPath);
400
		SftpATTRS attrs = null;
401
 
402
		// check if the directory is already existing
403
		try {
404
			attrs = channelSftp.stat(folderName);
405
		} catch (Exception e) {
406
			System.out.println(destinationPath + "/" + folderName + " not found");
407
		}
408
 
409
		// else create a directory
25413 amit.gupta 410
		if (attrs == null) {
411
			channelSftp.mkdir(folderName);
412
			channelSftp.chmod(0755, ".");
25380 amit.gupta 413
		}
414
		channelSftp.cd(folderName);
25413 amit.gupta 415
		channelSftp.rm("*");
25380 amit.gupta 416
 
417
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
418
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
419
		}
420
 
421
	}
422
 
423
	private Map<String, InputStream> getStreamFileMap(ContentPojo contentPojo, EntityMediaPojo entityMediaPojo) {
424
		Map<String, InputStream> map = new HashMap<>();
25401 amit.gupta 425
		LOGGER.info("entityMediaPojo.getMediaPojos() -[{}]", entityMediaPojo.getMediaPojos());
25404 amit.gupta 426
		for (int i = 0; i < entityMediaPojo.getMediaPojos().size(); i++) {
25380 amit.gupta 427
			MediaPojo mediaPojo = entityMediaPojo.getMediaPojos().get(i);
428
			String extension;
429
			String base64String = mediaPojo.getImageData();
430
			String[] strings = base64String.split(",");
431
			switch (strings[0]) {// check image's extension
432
			case "data:image/jpeg;base64":
433
				extension = "jpeg";
434
				break;
435
			case "data:image/png;base64":
436
				extension = "png";
437
				break;
438
			default:// should write cases for more images types
439
				extension = "jpg";
440
				break;
441
			}
25402 amit.gupta 442
			LOGGER.info("After switch statement = {}", extension);
25380 amit.gupta 443
			// convert base64 string to binary data
444
			byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
445
			String fileName = getFileName(entityMediaPojo.getEntityId(), mediaPojo.getTitle(), extension);
25402 amit.gupta 446
			LOGGER.info("After switch statement Filename = {}", fileName);
25380 amit.gupta 447
			mediaPojo.setImageData(null);
25430 amit.gupta 448
			String staticServerUrl = String.format(STATIC_SERVER_URL, entityMediaPojo.getEntityId() % 3);
25414 amit.gupta 449
			mediaPojo.setUrl(staticServerUrl + entityMediaPojo.getEntityId() + "/" + fileName);
25380 amit.gupta 450
			map.put(fileName, new ByteArrayInputStream(data));
451
			if (i == entityMediaPojo.getDefaultImageIndex()) {
452
				String defaultFileName = getFileName(entityMediaPojo.getEntityId(), DEFAULT, extension);
453
				map.put(defaultFileName, new ByteArrayInputStream(data));
25414 amit.gupta 454
				contentPojo.setDefaultImageUrl(staticServerUrl + entityMediaPojo.getEntityId() + "/" + defaultFileName);
25380 amit.gupta 455
			}
456
		}
457
		contentPojo.setImages(entityMediaPojo.getMediaPojos());
458
 
459
		return map;
460
	}
461
 
25409 amit.gupta 462
	private String getMediaPrefix(String productName) {
463
		String mediaPrefix = productName.toLowerCase().replace(' ', '-');
464
		mediaPrefix = mediaPrefix.replaceAll("/", "-");
465
		mediaPrefix = mediaPrefix.replaceAll("-+", "-");
466
		return mediaPrefix;
467
	}
468
 
28264 tejbeer 469
	@GetMapping(value = "/urlGeneration")
470
	public String urlGeneration(HttpServletRequest request, Model model) throws Exception {
471
		return "url-generation";
472
	}
25380 amit.gupta 473
}