Subversion Repositories SmartDukaan

Rev

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