Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2171 rajveer 1
package in.shop2020.ui.util;
2
 
3
import in.shop2020.metamodel.core.Bullet;
4
import in.shop2020.metamodel.core.Entity;
5
import in.shop2020.metamodel.core.EntityState;
6
import in.shop2020.metamodel.core.EntityStatus;
7
import in.shop2020.metamodel.core.Feature;
8
import in.shop2020.metamodel.core.PrimitiveDataObject;
9
import in.shop2020.metamodel.definitions.CMPBucketDefinition;
10
import in.shop2020.metamodel.definitions.Catalog;
11
import in.shop2020.metamodel.definitions.DefinitionsContainer;
12
 
13
import in.shop2020.metamodel.definitions.SlideDefinition;
14
import in.shop2020.metamodel.util.CreationUtils;
15
import in.shop2020.metamodel.util.ExpandedBullet;
16
import in.shop2020.metamodel.util.ExpandedEntity;
17
import in.shop2020.metamodel.util.ExpandedFeature;
18
import in.shop2020.metamodel.util.ExpandedSlide;
19
import in.shop2020.model.v1.catalog.InventoryService.Client;
20
import in.shop2020.model.v1.catalog.Item;
21
import in.shop2020.model.v1.catalog.status;
22
import in.shop2020.thrift.clients.CatalogServiceClient;
23
import in.shop2020.util.Utils;
24
 
25
import java.awt.Graphics2D;
26
import java.awt.geom.AffineTransform;
27
import java.awt.image.BufferedImage;
28
import java.io.BufferedReader;
29
import java.io.BufferedWriter;
30
import java.io.DataInputStream;
31
import java.io.DataOutputStream;
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.io.FileNotFoundException;
35
import java.io.FileOutputStream;
36
import java.io.FileReader;
37
import java.io.FileWriter;
38
import java.io.IOException;
39
import java.io.InputStream;
40
import java.io.InputStreamReader;
41
import java.io.OutputStream;
42
import java.io.OutputStreamWriter;
43
import java.io.StringWriter;
44
import java.text.DecimalFormat;
45
import java.util.ArrayList;
46
import java.util.Collection;
47
import java.util.Date;
48
import java.util.HashMap;
49
import java.util.LinkedHashMap;
50
import java.util.List;
51
import java.util.Map;
52
 
53
import javax.imageio.ImageIO;
54
 
2202 rajveer 55
import org.apache.commons.io.FileUtils;
56
import org.apache.commons.lang.StringEscapeUtils;
2171 rajveer 57
import org.apache.commons.lang.StringUtils;
58
import org.apache.velocity.Template;
59
import org.apache.velocity.VelocityContext;
60
import org.apache.velocity.app.Velocity;
61
import org.apache.velocity.exception.MethodInvocationException;
62
import org.apache.velocity.exception.ParseErrorException;
63
import org.apache.velocity.exception.ResourceNotFoundException;
64
 
65
/**
66
 * utility class to generated all html stuff from java objects
67
 * Driver class to merge Java data objects with VTL script. 
68
 * 
69
 * @author rajveer
70
 *
71
 */
72
public class NewVUI {
73
	CatalogServiceClient catalogServiceClient = null;
74
	Client client = null;
75
	String lastContentVersion;
76
	String contentVersion;  
77
 
78
	StringBuffer problems = new StringBuffer();
79
 
80
	public NewVUI(Long contentVersion) throws Exception {
81
		catalogServiceClient = new CatalogServiceClient();
82
		client = catalogServiceClient.getClient();
83
		this.lastContentVersion = contentVersion.toString();
84
		this.contentVersion =  (new Date()).getTime() +"" ;
85
	}
86
 
87
 
88
	/**
89
	 * Usage: VUI [entities | entity] [Entity ID] {Template file name}
90
	 * 
91
	 * @param args
92
	 */
93
	public static void main(String[] args) throws Exception {
94
 
95
		String[] commands = new String[] {"entity", "entities", "entitiesall"};
96
		String[] datatypes = new String[] {"Entity ID"};
97
 
98
		String usage = "Usage: VUI ["+ StringUtils.join(commands, "|") +"] ["+ StringUtils.join(datatypes, "|") + "] ";
99
 
100
		if(args.length < 1) {
101
			System.out.println(usage);
102
			System.exit(-1);
103
		}
104
		String inputCommand = args[0];
105
		String inputID = null;
106
		if(args.length == 2){
107
			inputID = args[1];
108
		}
109
 
110
	    Long lastGenerationTime = CreationUtils.getLastContentGenerationTime();
111
	    if(lastGenerationTime==null){
112
	    	lastGenerationTime = new Long(0);
113
	    }
114
 
115
	    //If non incremental content needs to be generated
116
	    if(inputCommand.equals("entitiesall") || inputCommand.equals("entity")) {
117
	    	lastGenerationTime = new Long(0);
118
	    }
119
 
120
		NewVUI vui = new NewVUI(lastGenerationTime);
121
 
122
		long catalogId = 0;
123
		if(inputCommand.equals("entity")) {
124
			try {
125
				catalogId = Long.parseLong(inputID);
126
//				vui.generateHtmlForOneEntity(CreationUtils.getEntity(catalogId), "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC);
127
//				vui.generateHtmlForOneEntity(CreationUtils.getEntity(catalogId), "shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020);
128
//				vui.generateHtmlForOneEntity(CreationUtils.getEntity(catalogId), "localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST);
129
			}
130
			catch (NumberFormatException nfe) {
131
				System.out.println("Invalid ID - " + inputID);
132
				System.exit(-1);
133
			}
134
		}
135
 
136
		if(inputCommand.equals("entities") || inputCommand.equals("entitiesall")) {
137
			vui.generateHtmlForAllEntities("saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC);
138
			vui.generateHtmlForAllEntities("shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020);
139
			vui.generateHtmlForAllEntities("localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST);
140
		}
141
 
142
 
143
		Velocity.init("src/main/java/in/shop2020/ui/util/velocity.properties");
144
 
145
		CreationUtils.storeLastContentGenerationTime((new Date()).getTime());
146
 
147
	}
148
 
149
	/**
150
	 * Generates content for the specified entity embedding links to the
151
	 * specified domain name.
152
	 * 
153
	 * The method updates the member variable problems in any of the following
154
	 * four cases:
155
	 * <ol>
156
	 * <li>The entity is not ready.
157
	 * <li>The category has not been updated yet. (Set to -1).
158
	 * <li>There are no items in the catalog corresponding to this entity.
159
	 * <li>There are no active or content-complete items in the catalog
160
	 * corresponding to this entity.
161
	 * <li>Neither the items have been updated nor the content has been updated.
162
	 * </ol>
163
	 * 
164
	 * @param entity
165
	 *            - Entity for which the content has to be generated.
166
	 * @param domain
167
	 *            - The domain name to be used to serve static content.
168
	 * @param exportPath
169
	 *            - Local file system path where content has to be generated.
170
	 * @return -True if content is generated successfully, False otherwise.
171
	 * @throws Exception
172
	 */
173
	public boolean generateHtmlForOneEntity(Entity entity, String domain, String exportPath, List<Item> items) throws Exception{
174
		String mediaPath = Utils.EXPORT_MEDIA_PATH + entity.getID();
175
 
176
        deleteOldResources(mediaPath, exportPath + entity.getID());
177
 
178
		ExpandedEntity expEntity = new ExpandedEntity(entity);
179
 
180
		long catalogId = expEntity.getID();
181
		String templateFile;
182
 
183
 
184
		//Create new directory
185
		File exportDir = new File(exportPath + catalogId);
186
		if(!exportDir.exists()) {
187
			exportDir.mkdir();
188
		}
189
 
190
		/*
191
		 * To delete 
192
		 */
193
 
194
 
195
		// This URL is used to ensure that images such as icon and thumbnail for
196
		// a particular entity are always downloaded from the same location.
197
		String staticurl = "http://static" + entity.getID()%3 + "." + domain;
198
 
199
		templateFile= Utils.VTL_SRC_PATH + "product_summary.vm";
200
		getProductSummaryHtml(expEntity, items, templateFile, exportPath);
2202 rajveer 201
		FileUtils.writeStringToFile(new File("ProductDetail.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("ProductDetail.html"))));
2171 rajveer 202
 
2202 rajveer 203
 
2171 rajveer 204
		templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_widget.vm";
205
		getEntityWidgetSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 206
		FileUtils.writeStringToFile(new File("WidgetSnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("WidgetSnippet.html"))));
2171 rajveer 207
 
2202 rajveer 208
 
2171 rajveer 209
		templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_homepage.vm";
210
		getEntityHomeSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 211
		FileUtils.writeStringToFile(new File("HomeSnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("HomeSnippet.html"))));
2171 rajveer 212
 
2202 rajveer 213
 
2171 rajveer 214
		templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_searchpage.vm";
215
		getEntitySearchSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 216
		FileUtils.writeStringToFile(new File("SearchSnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("SearchSnippet.html"))));
2171 rajveer 217
 
2202 rajveer 218
 
2171 rajveer 219
		templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_categorypage.vm";
220
		getEntityCategorySnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 221
		FileUtils.writeStringToFile(new File("CategorySnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("CategorySnippet.html"))));
2171 rajveer 222
 
2202 rajveer 223
 
2171 rajveer 224
		templateFile= Utils.VTL_SRC_PATH + "slideguide_img_video.vm";
225
		getEntitySlideGuideHtml(expEntity, templateFile, domain, exportPath);
2202 rajveer 226
		FileUtils.writeStringToFile(new File("SlideGuide.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("SlideGuide.html"))));
227
 
2171 rajveer 228
		templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_phones_i_own.vm";
229
		getEntityPhonesIOwnSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 230
		FileUtils.writeStringToFile(new File("PhonesIOwnSnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("PhonesIOwnSnippet.html"))));
2171 rajveer 231
 
232
		if(expEntity.getCategory().getParentCategory().getID() == Utils.MOBILE_PHONES_CATAGOEY){
233
 
234
		    templateFile= Utils.VTL_SRC_PATH + "slideguide_for_comparison.vm";
235
	        getEntitySnippetForComparison(expEntity, templateFile, domain, exportPath);
2202 rajveer 236
	        FileUtils.writeStringToFile(new File("ComparisonSnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("ComparisonSnippet.html"))));
2171 rajveer 237
 
238
	        templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_comparisonpage.vm";
239
	        getEntityCompareSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 240
	        FileUtils.writeStringToFile(new File("CompareProductSnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("CompareProductSnippet.html"))));
241
 
2171 rajveer 242
            templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_comparisonpage_summary.vm";
243
            getEntityCompareSummarySnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
2202 rajveer 244
            FileUtils.writeStringToFile(new File("CompareProductSummarySnippet.html"), StringEscapeUtils.escapeHtml(FileUtils.readFileToString(new File("CompareProductSummarySnippet.html"))));
245
 
2171 rajveer 246
            getSlidenamesSnippet(expEntity, exportPath);
247
		}
248
 
249
		getEntityTitleSnippetHtml(expEntity, exportPath);
250
 
251
		getEntityMetaDescriptionSnippetHtml(expEntity, exportPath);
252
 
253
		getEntityMetaKeywordSnippetHtml(expEntity, exportPath);
254
 
255
		generateImages(expEntity.getID(), getImagePrefix(expEntity));
256
 
257
		return true;
258
	}
259
 
260
	private boolean generateHtmlForAllEntities(String domain, String exportPath) throws Exception{
261
		Map<Long, Entity> entities =  CreationUtils.getEntities();
262
		for(Entity entity: entities.values()){
263
	//		generateHtmlForOneEntity(entity, domain, exportPath);
264
		}
265
		System.out.println(problems);
266
		return true;
267
	}
268
 
269
 
270
 
271
	public boolean deleteDir(File dir) {
272
	    if (dir.isDirectory()) {
273
	        String[] children = dir.list();
274
	        for (int i=0; i<children.length; i++) {
275
	            boolean success = deleteDir(new File(dir, children[i]));
276
	            if (!success) {
277
	                return false;
278
	            }
279
	        }
280
	    }
281
 
282
	    // The directory is now empty so delete it
283
	    return dir.delete();
284
	}
285
 
286
	private void deleteOldResources(String mediaPath, String contentPath) {
287
		File f = new File(contentPath);
288
		if(f.exists()){
289
			deleteDir(f);
290
		}
291
 
292
		f = new File(mediaPath);
293
		if(f.exists()){
294
			deleteDir(f);
295
		}
296
	}
297
 
298
	private void generateImages(long catalogId, String imagePrefix) throws IOException {
299
		String globalImageDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator;
300
		String globalDefaultImagePath = globalImageDirPath + "default.jpg";
301
 
302
		String imageDirPath = globalImageDirPath + catalogId + File.separator;
303
		String defaultImagePath = imageDirPath + "default.jpg";
304
 
305
		/*
306
		 * Create the directory for this entity if it didn't exist.
307
		 */
308
		File f = new File(globalImageDirPath + catalogId);
309
		if (!f.exists()) {
310
			f.mkdir();
311
		}
312
 
313
		/*
314
		 * If the default image is not present for this entity, copy the global
315
		 * default image.
316
		 * TODO: This part will be moved to the Jython Script
317
		 */
318
		File f3 = new File(defaultImagePath);
319
		if (!f3.exists()) {
320
		        copyFile(globalDefaultImagePath, defaultImagePath);
321
		    }
322
 
323
		String exportPath = Utils.EXPORT_MEDIA_PATH + catalogId;
324
		/*
325
		 * Copying the generated content from db/media to export/media. This
326
		 * will also insert a timestamp tag in the file names.
327
		 */
328
		File sourceFile = new File(globalImageDirPath + catalogId);
329
		File destinationFile = new File(exportPath);
2198 rajveer 330
		copyDirectory(sourceFile, destinationFile, imagePrefix);
2171 rajveer 331
 
332
		/*
333
		 * Copy the thumbnail and the icon files. This is required so that we can display the
334
		 * thumbnail on the cart page and icon on the facebook.
335
		 */
2198 rajveer 336
		copyFile(imageDirPath + "thumbnail.jpg", exportPath + File.separator + "thumbnail.jpg");
337
		copyFile(imageDirPath + "icon.jpg", exportPath + File.separator + "icon.jpg");
2171 rajveer 338
		}
339
 
340
	/**
341
	 * Copies the contents of the input file into the output file. Creates the
342
	 * output file if it doesn't exist already.
343
	 * 
344
	 * @param inputFile
345
	 *            File to be copied.
346
	 * @param outputFile
347
	 *            File to be created.
348
	 * @throws IOException
349
	 */
350
	private void copyFile(String inputFile, String outputFile) throws IOException {
351
		File sourceFile = new File(inputFile);
352
		File destinationFile = new File(outputFile);
353
/*
354
		if (!destinationFile.exists())
355
			destinationFile.createNewFile();
356
 
357
		InputStream in = new FileInputStream(sourceFile);
358
		OutputStream out = new FileOutputStream(destinationFile);
359
		// Copy the bits from instream to outstream
360
		byte[] buf = new byte[1024];
361
		int len;
362
		while ((len = in.read(buf)) > 0) {
363
			out.write(buf, 0, len);
364
		}
365
		in.close();
366
		out.close();
367
		*/
368
	}
369
 
370
	// If targetLocation does not exist, it will be created.
371
	public void copyDirectory(File sourceLocation , File targetLocation, String imagePrefix) throws IOException {
372
 
373
	    if (sourceLocation.isDirectory()) {
374
	        if (!targetLocation.exists()) {
375
	            targetLocation.mkdir();
376
	        }
377
 
378
	        String[] children = sourceLocation.list();
379
	        for (int i=0; i<children.length; i++) {
380
	            copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]), imagePrefix);
381
	        }
382
	    } else {
383
	        InputStream in = new FileInputStream(sourceLocation);
384
 
385
        	String fileName = targetLocation.getName().split("\\.")[0];
386
        	String fileExt = targetLocation.getName().split("\\.")[1];
387
        	String newFileName = targetLocation.getParent() + File.separator + imagePrefix + "-" + fileName + "-" + this.contentVersion + "." + fileExt; 
388
 
389
 
390
	        //String fileName = targetLocation
391
	        OutputStream out = new FileOutputStream(newFileName);
392
 
393
	        // Copy the bits from instream to outstream
394
	        byte[] buf = new byte[1024];
395
	        int len;
396
	        while ((len = in.read(buf)) > 0) {
397
	            out.write(buf, 0, len);
398
	        }
399
	        in.close();
400
	        out.close();
401
	    }
402
	}
403
 
404
 
405
	private  void getEntityMetaDescriptionSnippetHtml(ExpandedEntity expEntity, String exportPath) {
406
		long catalogId = expEntity.getID();
407
		try {
408
			expEntity = CreationUtils.getExpandedEntity(catalogId);
409
			String description = "Best Price " + expEntity.getBrand() + " " + expEntity.getModelName() 
410
				+ " " + expEntity.getModelNumber() + " ";
411
 
412
			if(expEntity.getCategory().getParentCategory().getID() == 10011) {
413
				description += expEntity.getCategory().getLabel() + " in India.";
414
			}
415
			else {
416
				description += ".";
417
			}
418
			description += " Experience n' buy online. FREE Next Day delivery."
419
				+ " Original product - Full manufacturer warranty. Comprehensive reviews.";
420
 
421
			description = description.replaceAll("--", "-");
422
			String exportFileName = exportPath + catalogId + File.separator + "MetaDescriptionSnippet.html";
423
			File exportFile = new File(exportFileName);
424
			if(!exportFile.exists()) {
425
				exportFile.createNewFile();
426
			}
427
 
428
			BufferedWriter writer = new BufferedWriter(
429
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
430
 
431
			writer.write(description);
432
 
433
			writer.flush();
434
			writer.close();
435
		} catch (Exception e) {
436
			e.printStackTrace();
437
		}
438
	}
439
 
440
	private  void getEntityMetaKeywordSnippetHtml(ExpandedEntity expEntity, String exportPath) {
441
		long catalogId = expEntity.getID();
442
		try {
443
			expEntity = CreationUtils.getExpandedEntity(catalogId);
2197 rajveer 444
			String keywords;
445
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
446
				keywords = expEntity.getBrand() + " " + expEntity.getModelName() + ", ";
447
				if (expEntity.getCategory().getParentCategory().getID() == 10001) {
448
					keywords += expEntity.getBrand() + " mobile phones, ";
449
				}
450
				if (expEntity.getCategory().getParentCategory().getID() == 10011) {
451
					keywords += "phone accessories, ";
452
				}
453
				keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " Price, ";
454
				keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " India, ";
455
				if (expEntity.getCategory().getParentCategory().getID() == 10001) {
456
					keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " Review, ";
457
				}	
458
	 		}
459
			else {
460
				keywords = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + ", ";
461
				if(expEntity.getCategory().getParentCategory().getID() == 10001) {
462
					keywords += expEntity.getBrand() + " mobile phones, ";
463
				}
464
				if(expEntity.getCategory().getParentCategory().getID() == 10011) {
465
					keywords += "phone accessories, ";
466
				}
467
				keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " Price, ";
468
				keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " India, ";
469
				if(expEntity.getCategory().getParentCategory().getID() == 10001) {
470
					keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " Review, ";
471
				}
2171 rajveer 472
			}
473
 
2197 rajveer 474
 
2171 rajveer 475
			String exportFileName = exportPath + catalogId + File.separator + "MetaKeywordsSnippet.html";
476
			File exportFile = new File(exportFileName);
477
			if(!exportFile.exists()) {
478
				exportFile.createNewFile();
479
			}
480
 
481
			BufferedWriter writer = new BufferedWriter(
482
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
483
 
484
			writer.write(keywords);
485
 
486
			writer.flush();
487
			writer.close();
488
		} catch (Exception e) {
489
			e.printStackTrace();
490
		}
491
	}
492
 
493
 
494
	private void getSlidenamesSnippet(ExpandedEntity expEntity, String exportPath) throws ResourceNotFoundException, ParseErrorException, Exception{
495
	    long catalogId = expEntity.getID();
496
 
497
	    StringBuilder slideNames = new StringBuilder();
498
 
499
	    slideNames.append(expEntity.getBrand() + " " + expEntity.getModelNumber() + " " + expEntity.getModelName() + "\n");
500
 
501
	    Map<Long, Double> slideScores = CreationUtils.getSlideComparisonScores(catalogId);
502
 
503
	    for(ExpandedSlide expSlide: expEntity.getExpandedSlides()){
504
	        if(expSlide.getSlideDefinitionID() == 130054){
505
	            continue;
506
	        }
507
	        slideNames.append(expSlide.getSlideDefinition().getLabel() + "!!!");
508
 
509
	        String bucketName = "None";
510
	        if(Catalog.getInstance().getDefinitionsContainer().getComparisonBucketName(expEntity.getCategoryID(), expSlide.getSlideDefinitionID()) != null){
511
	            bucketName = Catalog.getInstance().getDefinitionsContainer().getComparisonBucketName(expEntity.getCategoryID(), expSlide.getSlideDefinitionID());
512
	        }
513
	        slideNames.append(bucketName + "!!!");
514
 
515
	        double score = 0;
516
	        if(slideScores.get(expSlide.getSlideDefinitionID())!=null){
517
	            score = slideScores.get(expSlide.getSlideDefinitionID());
518
	        }
519
 
520
	        DecimalFormat oneDForm = new DecimalFormat("#.#");
521
	        score = Double.valueOf(oneDForm.format(score));
522
 
523
	        slideNames.append(score + "\n");
524
	    }
525
 
526
        String exportFileName = exportPath + catalogId + File.separator + "SlideNamesSnippet.html";
527
        File exportFile = new File(exportFileName);
528
        if(!exportFile.exists()) {
529
            exportFile.createNewFile();
530
        }
531
 
532
        BufferedWriter writer = new BufferedWriter(
533
            new OutputStreamWriter(new FileOutputStream(exportFile)));
534
 
535
        writer.write(slideNames.toString());
536
        writer.flush();
537
        writer.close();
538
 
539
	}
540
 
541
 
542
	private  void getEntitySnippetForComparison(ExpandedEntity expEntity, String templateFile, String domain, String exportPath) throws Exception {
543
		long catalogId = expEntity.getID();
544
		VelocityContext context = new VelocityContext();
545
 
546
		context.put("imagePrefix", getImagePrefix(expEntity));
547
		context.put("expentity", expEntity);
548
		context.put("domain", domain);
549
		context.put("contentVersion", this.contentVersion);
550
 
551
		String exportFileName = null;
552
 
553
		Template template = Velocity.getTemplate(templateFile);
554
		exportFileName = exportPath + catalogId + File.separator + "ComparisonSnippet.html";
555
 
556
		File exportFile = new File(exportFileName);
557
		if(!exportFile.exists()) {
558
			exportFile.createNewFile();
559
		}
560
 
561
		BufferedWriter writer = new BufferedWriter(
562
		    new OutputStreamWriter(new FileOutputStream(exportFile)));
563
 
564
		//Template template = Velocity.getTemplate(templateFile);
565
		template.merge(context, writer);
566
 
567
		writer.flush();
568
		writer.close();
569
		Utils.info("Export Complete!");
570
 
571
	}
572
 
573
	private  void getEntitySlideGuideHtml(ExpandedEntity expEntity, String templateFile, String domain, String exportPath) throws Exception {
574
        long catalogId = expEntity.getID();
575
        VelocityContext context = new VelocityContext();
576
 
577
        context.put("imagePrefix", getImagePrefix(expEntity));
578
        context.put("expentity", expEntity);
579
        context.put("domain", domain);
580
        context.put("contentVersion", this.contentVersion);
581
        context.put("defs", Catalog.getInstance().getDefinitionsContainer());
582
 
583
        String exportFileName = null;
584
 
585
        Template template = Velocity.getTemplate(templateFile);
586
        exportFileName = exportPath + catalogId + File.separator + "SlideGuide.html";
587
 
588
        File exportFile = new File(exportFileName);
589
        if(!exportFile.exists()) {
590
            exportFile.createNewFile();
591
        }
592
 
593
        BufferedWriter writer = new BufferedWriter(
594
            new OutputStreamWriter(new FileOutputStream(exportFile)));
595
 
596
        //Template template = Velocity.getTemplate(templateFile);
597
        template.merge(context, writer);
598
 
599
        writer.flush();
600
        writer.close();
601
        Utils.info("Export Complete!");
602
 
603
    }
604
 
605
	public void getProductSummaryHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String exportPath) {
606
		long catalogId = expEntity.getID();
607
		try {
608
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
609
			for(Item item: items){
610
				Map<String, String> itemDetail = new HashMap<String, String>();
611
				double sellingPrice = item.getSellingPrice();
612
				double mrp = item.getMrp();
613
				boolean showmrp = true;
614
				if (mrp <= sellingPrice) {
615
					showmrp = false;
616
				}
617
 
618
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
619
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");
620
				itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
621
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
622
				itemDetail.put("MRP", ((int)item.getMrp())+"");
623
				itemDetail.put("SAVING", ((int)saving)+"");
624
				itemDetail.put("COLOR", item.getColor());
625
				itemDetail.put("SHOWMRP", showmrp +"");
626
				itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
627
				itemDetails.add(itemDetail);
628
			}
629
 
630
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
631
			String categoryName = expEntity.getCategory().getLabel();
632
			String tagline = "";
633
			String warranty = "";
634
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
635
			for(Feature feature: features){
636
				if(feature.getFeatureDefinitionID() == 120084){
637
					PrimitiveDataObject dataObject= (PrimitiveDataObject)feature.getBullets().get(0).getDataObject();
638
					tagline = dataObject.getValue(); 
639
				}
640
				if(feature.getFeatureDefinitionID() == 120125){
641
					ExpandedFeature expFeature = new ExpandedFeature(feature);
642
					ExpandedBullet expBullet =expFeature.getExpandedBullets().get(0);
643
					if(expBullet != null){
644
						warranty = expBullet.getValue() + " "+ expBullet.getUnit().getShortForm();
645
					}
646
				}
647
			}
648
 
649
 
650
 
651
			long categoryId = expEntity.getCategory().getID();
652
			Map<String,String> params = new HashMap<String, String>();
653
			params.put("TITLE", title);
654
			params.put("CATEGORY_ID", ((int)categoryId)+"");
655
			params.put("CATEGORY_NAME", categoryName);
656
			params.put("CATEGORY_URL", categoryName.replaceAll(" ", "-").toLowerCase() + "/" + categoryId);
657
 
658
			params.put("CATALOG_ID", ((int)catalogId)+"");
659
			params.put("TAGLINE", tagline);
660
			params.put("WARRANTY", warranty);
661
 
662
			VelocityContext context = new VelocityContext();
663
 
664
			context.put("itemDetails", itemDetails);
665
 
666
			context.put("params", params);
667
 
668
			String exportFileName = exportPath + catalogId + File.separator + "ProductDetail.html";
669
 
670
			File exportFile = new File(exportFileName);
671
			if(!exportFile.exists()) {
672
				exportFile.createNewFile();
673
			}
674
 
675
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));
676
 
677
			Template template = Velocity.getTemplate(templateFile);
678
			template.merge(context, writer);
679
 
680
			writer.flush();
681
			writer.close();
682
			Utils.info("Export Complete!");
683
 
684
		} catch (Exception e) {
685
			e.printStackTrace();
686
		}
687
	}
688
 
689
	private String getEntityURL(ExpandedEntity expEntity){
690
		String url = "/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
691
		String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')
692
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
693
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')
694
        + "-" + expEntity.getID();
695
		productUrl = productUrl.replaceAll("/", "-");
696
		url = url + productUrl;
697
		url = url.replaceAll("--", "-");
698
		return url;
699
	}
700
 
701
	private String getImagePrefix(ExpandedEntity expEntity){
702
		String imagePrefix = expEntity.getBrand().toLowerCase().replace(' ', '-')
703
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
704
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-');
705
		imagePrefix = imagePrefix.replaceAll("/", "-");
706
		imagePrefix = imagePrefix.replaceAll("--", "-");
707
		return imagePrefix;
708
	}
709
 
710
	private void getEntityWidgetSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
711
		long catalogId = expEntity.getID();
712
		try {
713
			expEntity = CreationUtils.getExpandedEntity(catalogId);
714
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
2197 rajveer 715
			String prodName = title;
716
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
717
				prodName = expEntity.getBrand() + " " + expEntity.getModelName();
718
			}
2171 rajveer 719
			String imagePrefix = getImagePrefix(expEntity);
720
			String url = getEntityURL(expEntity);
721
 
722
			String tinySnippet = "";
723
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
724
			for(Feature feature: features){
725
				if(feature.getFeatureDefinitionID() == 120089){
726
					PrimitiveDataObject dataObject= (PrimitiveDataObject)feature.getBullets().get(0).getDataObject();
727
					tinySnippet = dataObject.getValue(); 
728
				}
729
			}
730
 
731
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
732
 
733
			Item minPriceItem = null; 
734
			for(Item item: items){
735
				Map<String, String> itemDetail = new HashMap<String, String>();
736
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
737
					minPriceItem = item;
738
				}
739
				double sellingPrice = item.getSellingPrice();
740
				double mrp = item.getMrp();
741
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
742
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
743
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
744
				itemDetail.put("MRP", ((int)item.getMrp())+"");
745
				itemDetail.put("SAVING", ((int)saving)+"");
746
				itemDetail.put("COLOR", item.getColor());
747
				itemDetails.add(itemDetail);
748
			}
749
 
750
			boolean showmrp = true;
751
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
752
				showmrp = false;
753
			}
754
			Map<String,String> params = new HashMap<String, String>();
755
			params.put("TITLE", title);
2197 rajveer 756
			params.put("PROD_NAME", prodName);
2171 rajveer 757
			params.put("URL", url);
758
			params.put("IMAGE_PREFIX", imagePrefix);
759
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
760
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
761
			params.put("SHOWMRP", showmrp +"");
762
			params.put("CATALOG_ID", ((int)catalogId)+"");
763
			params.put("ITEM_ID", minPriceItem.getId()+"");
764
			params.put("TINY_SNIPPET", tinySnippet);
765
			params.put("staticurl", staticurl);
766
			params.put("contentVersion", contentVersion);
767
			VelocityContext context = new VelocityContext();
768
			context.put("itemDetails", itemDetails);
769
			context.put("params", params);
770
 
771
			String exportFileName = exportPath + catalogId + File.separator + "WidgetSnippet.html";
772
			File exportFile = new File(exportFileName);
773
			if(!exportFile.exists()) {
774
				exportFile.createNewFile();
775
			}
776
 
777
			BufferedWriter writer = new BufferedWriter(
778
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
779
 
780
			Template template = Velocity.getTemplate(templateFile);
781
			template.merge(context, writer);
782
 
783
			writer.flush();
784
			writer.close();
785
			Utils.info("Export Complete!");
786
 
787
 
788
		} catch (Exception e) {
789
			e.printStackTrace();
790
		}
791
	}
792
 
793
 
794
	private  void getEntityHomeSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
795
		long catalogId = expEntity.getID();
796
 
797
		Map<String,String> params = new HashMap<String, String>();
798
 
799
		try {
800
			expEntity = CreationUtils.getExpandedEntity(catalogId);
801
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
2197 rajveer 802
			String prodName = title;
803
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
804
				prodName = expEntity.getBrand() + " " + expEntity.getModelName();
805
			}
2171 rajveer 806
			String url = getEntityURL(expEntity);
807
			String imagePrefix = getImagePrefix(expEntity);
808
 
809
			catalogServiceClient = new CatalogServiceClient();
810
			client = catalogServiceClient.getClient();
811
 
812
			String tinySnippet = "";
813
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
814
			for(Feature feature: features){
815
				if(feature.getFeatureDefinitionID() == 120081){
816
					List<Bullet> bullets = feature.getBullets();
817
					int count = 1;
818
					for(Bullet bullet: bullets){
819
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
820
						tinySnippet = dataObject.getValue();
821
						params.put("SNIPPET_"+count++, tinySnippet);
822
						System.out.println("Tiny Snippets is " + dataObject.getValue());
823
					}
824
 				}
825
			}
826
 
827
 
828
 
829
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
830
 
831
			Item minPriceItem = null;
832
 
833
			for(Item item: items){
834
				Map<String, String> itemDetail = new HashMap<String, String>();
835
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
836
					minPriceItem = item;
837
				}
838
				double sellingPrice = item.getSellingPrice();
839
				double mrp = item.getMrp();
840
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
841
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
842
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
843
				itemDetail.put("MRP", ((int)item.getMrp())+"");
844
				itemDetail.put("SAVING", ((int)saving)+"");
845
				itemDetail.put("COLOR", item.getColor());
846
				itemDetails.add(itemDetail);
847
			}
848
			boolean showmrp = true;
849
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
850
				showmrp = false;
851
			}
852
 
853
			params.put("TITLE", title);
2197 rajveer 854
			params.put("PROD_NAME", prodName);
2171 rajveer 855
			params.put("URL", url);
856
			params.put("IMAGE_PREFIX", imagePrefix);
857
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
858
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
859
			params.put("SHOWMRP", showmrp +"");
860
			params.put("CATALOG_ID", ((int)catalogId)+"");
861
			params.put("ITEM_ID", minPriceItem.getId()+"");
862
			params.put("staticurl", staticurl);
863
			params.put("contentVersion", contentVersion);
864
 
865
			VelocityContext context = new VelocityContext();
866
			context.put("itemDetails", itemDetails);
867
			context.put("params", params);
868
 
869
 
870
			String exportFileName = exportPath + catalogId + File.separator + "HomeSnippet.html";
871
			File exportFile = new File(exportFileName);
872
			if(!exportFile.exists()) {
873
				exportFile.createNewFile();
874
			}
875
 
876
			BufferedWriter writer = new BufferedWriter(
877
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
878
 
879
			Template template = Velocity.getTemplate(templateFile);
880
			template.merge(context, writer);
881
 
882
			writer.flush();
883
			writer.close();
884
			Utils.info("Export Complete!");
885
 
886
		} catch (Exception e) {
887
			e.printStackTrace();
888
		}
889
	}
890
 
891
	private  void getEntityCategorySnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
892
		Map<String,String> params = new HashMap<String, String>();
893
		long catalogId = expEntity.getID();
894
 
895
		try {
896
			expEntity = CreationUtils.getExpandedEntity(catalogId);
897
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
2197 rajveer 898
			String prodName = title;
899
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
900
				prodName = expEntity.getBrand() + " " + expEntity.getModelName();
901
			}
2171 rajveer 902
			catalogServiceClient = new CatalogServiceClient();
903
			client = catalogServiceClient.getClient();
904
			String url = getEntityURL(expEntity);
905
			String imagePrefix = getImagePrefix(expEntity);
906
 
907
			String tinySnippet = "";
908
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
909
			for(Feature feature: features){
910
				if(feature.getFeatureDefinitionID() == 120081){
911
					List<Bullet> bullets = feature.getBullets();
912
					int count = 1;
913
					for(Bullet bullet: bullets){
914
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
915
						tinySnippet = dataObject.getValue();
916
						params.put("SNIPPET_"+count++, tinySnippet);
917
						System.out.println("Tiny Snippets is " + dataObject.getValue());
918
					}
919
 				}
920
			}
921
 
922
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
923
 
924
			Item minPriceItem = null;
925
			for(Item item: items){
926
				Map<String, String> itemDetail = new HashMap<String, String>();
927
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
928
					minPriceItem = item;
929
				}
930
				double sellingPrice = item.getSellingPrice();
931
				double mrp = item.getMrp();
932
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
933
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
934
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
935
				itemDetail.put("MRP", ((int)item.getMrp())+"");
936
				itemDetail.put("SAVING", ((int)saving)+"");
937
				itemDetail.put("COLOR", item.getColor());
938
				itemDetails.add(itemDetail);
939
			}
940
 
941
			boolean showmrp = true;
942
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
943
				showmrp = false;
944
			}
945
			params.put("TITLE", title);
2197 rajveer 946
			params.put("PROD_NAME", prodName);
2171 rajveer 947
			params.put("URL", url);
948
			params.put("IMAGE_PREFIX", imagePrefix);
949
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
950
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
951
			params.put("SHOWMRP", showmrp +"");
952
			params.put("CATALOG_ID", ((int)catalogId)+"");
953
			params.put("ITEM_ID", minPriceItem.getId()+"");
954
			params.put("staticurl", staticurl);
955
			params.put("contentVersion", contentVersion);
956
 
957
			VelocityContext context = new VelocityContext();
958
			context.put("itemDetails", itemDetails);
959
			context.put("params", params);
960
 
961
			String exportFileName = exportPath + catalogId + File.separator + "CategorySnippet.html";
962
			File exportFile = new File(exportFileName);
963
			if(!exportFile.exists()) {
964
				exportFile.createNewFile();
965
			}
966
 
967
			BufferedWriter writer = new BufferedWriter(
968
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
969
 
970
			Template template = Velocity.getTemplate(templateFile);
971
			template.merge(context, writer);
972
 
973
			writer.flush();
974
			writer.close();
975
			Utils.info("Export Complete!");
976
 
977
 
978
		} catch (Exception e) {
979
			e.printStackTrace();
980
		}
981
	}
982
 
983
	private  void getEntityTitleSnippetHtml(ExpandedEntity expEntity, String exportPath) {
984
		long catalogId = expEntity.getID();
985
 
986
		try {
987
			expEntity = CreationUtils.getExpandedEntity(catalogId);
2197 rajveer 988
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
989
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
990
			title = expEntity.getBrand() + " " + expEntity.getModelName();
991
			}
2171 rajveer 992
			if(expEntity.getCategory().getParentCategory().getID() == 10001) {
993
				title +=  " | " + expEntity.getBrand() + " Mobile Phones";
994
			}
995
			if(expEntity.getCategory().getParentCategory().getID() == 10011) {
996
				title += " " + expEntity.getCategory().getLabel()
997
		            + " | " + expEntity.getBrand() + " Mobile Phone Accessories";
998
			}
999
			title += " | Saholic.com";
1000
 
1001
			String exportFileName = exportPath + catalogId + File.separator + "TitleSnippet.html";
1002
			File exportFile = new File(exportFileName);
1003
			if(!exportFile.exists()) {
1004
				exportFile.createNewFile();
1005
			}
1006
 
1007
			BufferedWriter writer = new BufferedWriter(
1008
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
1009
 
1010
			writer.write(title);
1011
 
1012
			writer.flush();
1013
			writer.close();
1014
		} catch (Exception e) {
1015
			e.printStackTrace();
1016
		}
1017
	}
1018
 
1019
 
1020
	private void getEntitySearchSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1021
		long catalogId = expEntity.getID();
1022
		Map<String,String> params = new HashMap<String, String>();
1023
 
1024
		try {
1025
			expEntity = CreationUtils.getExpandedEntity(catalogId);
1026
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
2197 rajveer 1027
			String prodName = title;
1028
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
1029
				prodName = expEntity.getBrand() + " " + expEntity.getModelName();
1030
			}
2171 rajveer 1031
 
1032
			String url = getEntityURL(expEntity);
1033
			String imagePrefix = getImagePrefix(expEntity);
1034
 
1035
			catalogServiceClient = new CatalogServiceClient();
1036
			client = catalogServiceClient.getClient();
1037
 
1038
			String tinySnippet = "";
1039
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1040
			for(Feature feature: features){
1041
				if(feature.getFeatureDefinitionID() == 120081){
1042
					List<Bullet> bullets = feature.getBullets();
1043
					int count = 1;
1044
					for(Bullet bullet: bullets){
1045
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1046
						tinySnippet = dataObject.getValue();
1047
						params.put("SNIPPET_"+count++, tinySnippet);
1048
						System.out.println("Tiny Snippets is " + dataObject.getValue());
1049
					}
1050
 				}
1051
			}
1052
 
1053
 
1054
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1055
 
1056
			Item minPriceItem = null;
1057
			for(Item item: items){
1058
				Map<String, String> itemDetail = new HashMap<String, String>();
1059
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1060
					minPriceItem = item;
1061
				}
1062
				double sellingPrice = item.getSellingPrice();
1063
				double mrp = item.getMrp();
1064
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
1065
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
1066
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1067
				itemDetail.put("MRP", ((int)item.getMrp())+"");
1068
				itemDetail.put("SAVING", ((int)saving)+"");
1069
				itemDetail.put("COLOR", item.getColor());
1070
				itemDetails.add(itemDetail);
1071
			}
1072
 
1073
			boolean showmrp = true;
1074
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1075
				showmrp = false;
1076
			}
1077
			params.put("TITLE", title);
2197 rajveer 1078
			params.put("PROD_NAME", prodName);
2171 rajveer 1079
			params.put("URL", url);
1080
			params.put("IMAGE_PREFIX", imagePrefix);
1081
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1082
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
1083
			params.put("SHOWMRP", showmrp +"");
1084
			params.put("CATALOG_ID", ((int)catalogId)+"");
1085
			params.put("ITEM_ID", minPriceItem.getId()+"");
1086
			params.put("staticurl", staticurl);
1087
			params.put("contentVersion", contentVersion);
1088
 
1089
			VelocityContext context = new VelocityContext();
1090
			context.put("itemDetails", itemDetails);
1091
			context.put("params", params);
1092
 
1093
			String exportFileName = exportPath + catalogId + File.separator + "SearchSnippet.html";
1094
			File exportFile = new File(exportFileName);
1095
			if(!exportFile.exists()) {
1096
				exportFile.createNewFile();
1097
			}
1098
 
1099
			BufferedWriter writer = new BufferedWriter(
1100
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
1101
 
1102
			Template template = Velocity.getTemplate(templateFile);
1103
			template.merge(context, writer);
1104
 
1105
			writer.flush();
1106
			writer.close();
1107
			Utils.info("Export Complete!");
1108
 
1109
 
1110
		} catch (Exception e) {
1111
			e.printStackTrace();
1112
		}
1113
	}
1114
 
1115
 
1116
    private void getEntityCompareSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1117
        long catalogId = expEntity.getID();
1118
        Map<String,String> params = new HashMap<String, String>();
1119
 
1120
        try {
1121
            expEntity = CreationUtils.getExpandedEntity(catalogId);
1122
            String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
2197 rajveer 1123
            String prodName = title;
1124
            if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
1125
            prodName = expEntity.getBrand() + " " + expEntity.getModelName();
1126
            }
2171 rajveer 1127
            String url = getEntityURL(expEntity);
1128
            String imagePrefix = getImagePrefix(expEntity);
1129
 
1130
            catalogServiceClient = new CatalogServiceClient();
1131
            client = catalogServiceClient.getClient();
1132
 
1133
            String tinySnippet = "";
1134
            List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1135
            for(Feature feature: features){
1136
                if(feature.getFeatureDefinitionID() == 120081){
1137
                    List<Bullet> bullets = feature.getBullets();
1138
                    int count = 1;
1139
                    for(Bullet bullet: bullets){
1140
                        PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1141
                        tinySnippet = dataObject.getValue();
1142
                        params.put("SNIPPET_"+count++, tinySnippet);
1143
                        System.out.println("Tiny Snippets is " + dataObject.getValue());
1144
                    }
1145
                }
1146
            }
1147
 
1148
 
1149
            List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1150
 
1151
            Item minPriceItem = null;
1152
            for(Item item: items){
1153
                Map<String, String> itemDetail = new HashMap<String, String>();
1154
                if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1155
                    minPriceItem = item;
1156
                }
1157
                double sellingPrice = item.getSellingPrice();
1158
                double mrp = item.getMrp();
1159
                double saving = Math.round((mrp-sellingPrice)/mrp*100);
1160
                itemDetail.put("ITEM_ID", ((int)item.getId())+"");  
1161
                itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1162
                itemDetail.put("MRP", ((int)item.getMrp())+"");
1163
                itemDetail.put("SAVING", ((int)saving)+"");
1164
                itemDetail.put("COLOR", item.getColor());
1165
                itemDetails.add(itemDetail);
1166
            }
1167
 
1168
            boolean showmrp = true;
1169
            if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1170
                showmrp = false;
1171
            }
1172
            params.put("TITLE", title);
2197 rajveer 1173
            params.put("PROD_NAME", prodName);
2171 rajveer 1174
            params.put("URL", url);
1175
            params.put("IMAGE_PREFIX", imagePrefix);
1176
            params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1177
            params.put("MRP", ((int)minPriceItem.getMrp())+"");
1178
            params.put("SHOWMRP", showmrp +"");
1179
            params.put("CATALOG_ID", ((int)catalogId)+"");
1180
            params.put("ITEM_ID", minPriceItem.getId()+"");
1181
            params.put("staticurl", staticurl);
1182
            params.put("contentVersion", contentVersion);
1183
 
1184
            VelocityContext context = new VelocityContext();
1185
            context.put("itemDetails", itemDetails);
1186
            context.put("params", params);
1187
 
1188
            String exportFileName = exportPath + catalogId + File.separator + "CompareProductSnippet.html";
1189
            File exportFile = new File(exportFileName);
1190
            if(!exportFile.exists()) {
1191
                exportFile.createNewFile();
1192
            }
1193
 
1194
            BufferedWriter writer = new BufferedWriter(
1195
                new OutputStreamWriter(new FileOutputStream(exportFile)));
1196
 
1197
            Template template = Velocity.getTemplate(templateFile);
1198
            template.merge(context, writer);
1199
 
1200
            writer.flush();
1201
            writer.close();
1202
            Utils.info("Export Complete!");
1203
 
1204
 
1205
        } catch (Exception e) {
1206
            e.printStackTrace();
1207
        }
1208
    }
1209
 
1210
 
1211
    private void getEntityCompareSummarySnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1212
        long catalogId = expEntity.getID();
1213
        Map<String,String> params = new HashMap<String, String>();
1214
 
1215
        try {
1216
            expEntity = CreationUtils.getExpandedEntity(catalogId);
1217
            String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
2197 rajveer 1218
            String prodName = title;
1219
            if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
1220
            prodName = expEntity.getBrand() + " " + expEntity.getModelName();
1221
            }
2171 rajveer 1222
            String url = getEntityURL(expEntity);
1223
            String imagePrefix = getImagePrefix(expEntity);
1224
 
1225
            catalogServiceClient = new CatalogServiceClient();
1226
            client = catalogServiceClient.getClient();
1227
 
1228
            String tinySnippet = "";
1229
            List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1230
            for(Feature feature: features){
1231
                if(feature.getFeatureDefinitionID() == 120081){
1232
                    List<Bullet> bullets = feature.getBullets();
1233
                    int count = 1;
1234
                    for(Bullet bullet: bullets){
1235
                        PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1236
                        tinySnippet = dataObject.getValue();
1237
                        params.put("SNIPPET_"+count++, tinySnippet);
1238
                        System.out.println("Tiny Snippets is " + dataObject.getValue());
1239
                    }
1240
                }
1241
            }
1242
 
1243
 
1244
            List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1245
 
1246
            Item minPriceItem = null;
1247
            for(Item item: items){
1248
                Map<String, String> itemDetail = new HashMap<String, String>();
1249
                if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1250
                    minPriceItem = item;
1251
                }
1252
                double sellingPrice = item.getSellingPrice();
1253
                double mrp = item.getMrp();
1254
                double saving = Math.round((mrp-sellingPrice)/mrp*100);
1255
                itemDetail.put("ITEM_ID", ((int)item.getId())+"");  
1256
                itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1257
                itemDetail.put("MRP", ((int)item.getMrp())+"");
1258
                itemDetail.put("SAVING", ((int)saving)+"");
1259
                itemDetail.put("COLOR", item.getColor());
1260
                itemDetails.add(itemDetail);
1261
            }
1262
 
1263
            boolean showmrp = true;
1264
            if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1265
                showmrp = false;
1266
            }
1267
            params.put("TITLE", title);
2197 rajveer 1268
            params.put("PROD_NAME", prodName);
2171 rajveer 1269
            params.put("URL", url);
1270
            params.put("IMAGE_PREFIX", imagePrefix);
1271
            params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1272
            params.put("MRP", ((int)minPriceItem.getMrp())+"");
1273
            params.put("SHOWMRP", showmrp +"");
1274
            params.put("CATALOG_ID", ((int)catalogId)+"");
1275
            params.put("ITEM_ID", minPriceItem.getId()+"");
1276
            params.put("staticurl", staticurl);
1277
            params.put("contentVersion", contentVersion);
1278
 
1279
            VelocityContext context = new VelocityContext();
1280
            context.put("itemDetails", itemDetails);
1281
            context.put("params", params);
1282
 
1283
            String exportFileName = exportPath + catalogId + File.separator + "CompareProductSummarySnippet.html";
1284
            File exportFile = new File(exportFileName);
1285
            if(!exportFile.exists()) {
1286
                exportFile.createNewFile();
1287
            }
1288
 
1289
            BufferedWriter writer = new BufferedWriter(
1290
                new OutputStreamWriter(new FileOutputStream(exportFile)));
1291
 
1292
            Template template = Velocity.getTemplate(templateFile);
1293
            template.merge(context, writer);
1294
 
1295
            writer.flush();
1296
            writer.close();
1297
            Utils.info("Export Complete!");
1298
 
1299
 
1300
        } catch (Exception e) {
1301
            e.printStackTrace();
1302
        }
1303
    }
1304
 
1305
	private void getEntityPhonesIOwnSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1306
		long catalogId = expEntity.getID();
1307
		Map<String,String> params = new HashMap<String, String>();
1308
 
1309
		try {
1310
			expEntity = CreationUtils.getExpandedEntity(catalogId);
1311
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
2197 rajveer 1312
			String prodName = title;
1313
			if (expEntity.getModelName() != null && !expEntity.getModelName().isEmpty() && (expEntity.getBrand().equals("Samsung") || expEntity.getBrand().equals("Sony Ericsson"))) {
1314
				 prodName = expEntity.getBrand() + " " + expEntity.getModelName();
1315
			}
2171 rajveer 1316
			String url = getEntityURL(expEntity);
2197 rajveer 1317
 
2171 rajveer 1318
			String imagePrefix = getImagePrefix(expEntity);
1319
 
1320
			catalogServiceClient = new CatalogServiceClient();
1321
			client = catalogServiceClient.getClient();
1322
 
1323
			String tinySnippet = "";
1324
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1325
			for(Feature feature: features){
1326
				if(feature.getFeatureDefinitionID() == 120081){
1327
					List<Bullet> bullets = feature.getBullets();
1328
					int count = 1;
1329
					for(Bullet bullet: bullets){
1330
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1331
						tinySnippet = dataObject.getValue();
1332
						params.put("SNIPPET_"+count++, tinySnippet);
1333
						System.out.println("Tiny Snippets is " + dataObject.getValue());
1334
					}
1335
 				}
1336
			}
1337
 
1338
 
1339
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1340
 
1341
			Item minPriceItem = null;
1342
			for(Item item: items){
1343
				Map<String, String> itemDetail = new HashMap<String, String>();
1344
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1345
					minPriceItem = item;
1346
				}
1347
				double sellingPrice = item.getSellingPrice();
1348
				double mrp = item.getMrp();
1349
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
1350
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
1351
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1352
				itemDetail.put("MRP", ((int)item.getMrp())+"");
1353
				itemDetail.put("SAVING", ((int)saving)+"");
1354
				itemDetail.put("COLOR", item.getColor());
1355
				itemDetails.add(itemDetail);
1356
			}
1357
 
1358
			boolean showmrp = true;
1359
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1360
				showmrp = false;
1361
			}
1362
			params.put("TITLE", title);
2197 rajveer 1363
			params.put("PROD_NAME", prodName);
2171 rajveer 1364
			params.put("URL", url);
1365
			params.put("IMAGE_PREFIX", imagePrefix);
1366
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1367
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
1368
			params.put("SHOWMRP", showmrp +"");
1369
			params.put("CATALOG_ID", ((int)catalogId)+"");
1370
			params.put("ITEM_ID", minPriceItem.getId()+"");
1371
			params.put("staticurl", staticurl);
1372
			params.put("contentVersion", contentVersion);
1373
 
1374
			VelocityContext context = new VelocityContext();
1375
			context.put("itemDetails", itemDetails);
1376
			context.put("params", params);
1377
 
1378
			String exportFileName = exportPath + catalogId + File.separator + "PhonesIOwnSnippet.html";
1379
			File exportFile = new File(exportFileName);
1380
			if(!exportFile.exists()) {
1381
				exportFile.createNewFile();
1382
			}
1383
 
1384
			BufferedWriter writer = new BufferedWriter(
1385
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
1386
 
1387
			Template template = Velocity.getTemplate(templateFile);
1388
			template.merge(context, writer);
1389
 
1390
			writer.flush();
1391
			writer.close();
1392
			Utils.info("Export Complete!");
1393
 
1394
 
1395
		} catch (Exception e) {
1396
			e.printStackTrace();
1397
		}
1398
	}
1399
 
1400
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
1401
		try {
1402
			Velocity.init("velocity/velocity.properties");
1403
			Template template = Velocity.getTemplate(templateFile);
1404
			if(template != null) {
1405
				StringWriter writer = new StringWriter();
1406
				template.merge(context, writer);
1407
				writer.flush();
1408
				writer.close();
1409
				return writer.toString();
1410
			}
1411
 
1412
			} catch (ResourceNotFoundException e) {
1413
				e.printStackTrace();
1414
			} catch (ParseErrorException e) {
1415
				e.printStackTrace();
1416
			} catch (MethodInvocationException e) {
1417
				e.printStackTrace();
1418
			} catch (IOException e) {
1419
				e.printStackTrace();
1420
			} catch (Exception e) {
1421
				e.printStackTrace();
1422
			}
1423
 
1424
		return null;
1425
	}
1426
 
1427
 
1428
}
1429