Subversion Repositories SmartDukaan

Rev

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