Subversion Repositories SmartDukaan

Rev

Rev 2197 | Go to most recent revision | Details | 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);
214
 
215
 
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);
226
 
227
            templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_comparisonpage_summary.vm";
228
            getEntityCompareSummarySnippetHtml(expEntity, items, templateFile, staticurl, exportPath);
229
 
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);
314
		//copyDirectory(sourceFile, destinationFile, imagePrefix);
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
		 */
320
		   // copyFile(imageDirPath + "thumbnail.jpg", exportPath + File.separator + "thumbnail.jpg");
321
		   // copyFile(imageDirPath + "icon.jpg", exportPath + File.separator + "icon.jpg");
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);
428
			String keywords = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + ", ";
429
			if(expEntity.getCategory().getParentCategory().getID() == 10001) {
430
				keywords += expEntity.getBrand() + " mobile phones, ";
431
			}
432
			if(expEntity.getCategory().getParentCategory().getID() == 10011) {
433
				keywords += "phone accessories, ";
434
			}
435
			keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " Price, ";
436
			keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " India, ";
437
			if(expEntity.getCategory().getParentCategory().getID() == 10001) {
438
    			keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " Review, ";
439
			}
440
 
441
			String exportFileName = exportPath + catalogId + File.separator + "MetaKeywordsSnippet.html";
442
			File exportFile = new File(exportFileName);
443
			if(!exportFile.exists()) {
444
				exportFile.createNewFile();
445
			}
446
 
447
			BufferedWriter writer = new BufferedWriter(
448
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
449
 
450
			writer.write(keywords);
451
 
452
			writer.flush();
453
			writer.close();
454
		} catch (Exception e) {
455
			e.printStackTrace();
456
		}
457
	}
458
 
459
 
460
	private void getSlidenamesSnippet(ExpandedEntity expEntity, String exportPath) throws ResourceNotFoundException, ParseErrorException, Exception{
461
	    long catalogId = expEntity.getID();
462
 
463
	    StringBuilder slideNames = new StringBuilder();
464
 
465
	    slideNames.append(expEntity.getBrand() + " " + expEntity.getModelNumber() + " " + expEntity.getModelName() + "\n");
466
 
467
	    Map<Long, Double> slideScores = CreationUtils.getSlideComparisonScores(catalogId);
468
 
469
	    for(ExpandedSlide expSlide: expEntity.getExpandedSlides()){
470
	        if(expSlide.getSlideDefinitionID() == 130054){
471
	            continue;
472
	        }
473
	        slideNames.append(expSlide.getSlideDefinition().getLabel() + "!!!");
474
 
475
	        String bucketName = "None";
476
	        if(Catalog.getInstance().getDefinitionsContainer().getComparisonBucketName(expEntity.getCategoryID(), expSlide.getSlideDefinitionID()) != null){
477
	            bucketName = Catalog.getInstance().getDefinitionsContainer().getComparisonBucketName(expEntity.getCategoryID(), expSlide.getSlideDefinitionID());
478
	        }
479
	        slideNames.append(bucketName + "!!!");
480
 
481
	        double score = 0;
482
	        if(slideScores.get(expSlide.getSlideDefinitionID())!=null){
483
	            score = slideScores.get(expSlide.getSlideDefinitionID());
484
	        }
485
 
486
	        DecimalFormat oneDForm = new DecimalFormat("#.#");
487
	        score = Double.valueOf(oneDForm.format(score));
488
 
489
	        slideNames.append(score + "\n");
490
	    }
491
 
492
        String exportFileName = exportPath + catalogId + File.separator + "SlideNamesSnippet.html";
493
        File exportFile = new File(exportFileName);
494
        if(!exportFile.exists()) {
495
            exportFile.createNewFile();
496
        }
497
 
498
        BufferedWriter writer = new BufferedWriter(
499
            new OutputStreamWriter(new FileOutputStream(exportFile)));
500
 
501
        writer.write(slideNames.toString());
502
        writer.flush();
503
        writer.close();
504
 
505
	}
506
 
507
 
508
	private  void getEntitySnippetForComparison(ExpandedEntity expEntity, String templateFile, String domain, String exportPath) throws Exception {
509
		long catalogId = expEntity.getID();
510
		VelocityContext context = new VelocityContext();
511
 
512
		context.put("imagePrefix", getImagePrefix(expEntity));
513
		context.put("expentity", expEntity);
514
		context.put("domain", domain);
515
		context.put("contentVersion", this.contentVersion);
516
 
517
		String exportFileName = null;
518
 
519
		Template template = Velocity.getTemplate(templateFile);
520
		exportFileName = exportPath + catalogId + File.separator + "ComparisonSnippet.html";
521
 
522
		File exportFile = new File(exportFileName);
523
		if(!exportFile.exists()) {
524
			exportFile.createNewFile();
525
		}
526
 
527
		BufferedWriter writer = new BufferedWriter(
528
		    new OutputStreamWriter(new FileOutputStream(exportFile)));
529
 
530
		//Template template = Velocity.getTemplate(templateFile);
531
		template.merge(context, writer);
532
 
533
		writer.flush();
534
		writer.close();
535
		Utils.info("Export Complete!");
536
 
537
	}
538
 
539
	private  void getEntitySlideGuideHtml(ExpandedEntity expEntity, String templateFile, String domain, String exportPath) throws Exception {
540
        long catalogId = expEntity.getID();
541
        VelocityContext context = new VelocityContext();
542
 
543
        context.put("imagePrefix", getImagePrefix(expEntity));
544
        context.put("expentity", expEntity);
545
        context.put("domain", domain);
546
        context.put("contentVersion", this.contentVersion);
547
        context.put("defs", Catalog.getInstance().getDefinitionsContainer());
548
 
549
        String exportFileName = null;
550
 
551
        Template template = Velocity.getTemplate(templateFile);
552
        exportFileName = exportPath + catalogId + File.separator + "SlideGuide.html";
553
 
554
        File exportFile = new File(exportFileName);
555
        if(!exportFile.exists()) {
556
            exportFile.createNewFile();
557
        }
558
 
559
        BufferedWriter writer = new BufferedWriter(
560
            new OutputStreamWriter(new FileOutputStream(exportFile)));
561
 
562
        //Template template = Velocity.getTemplate(templateFile);
563
        template.merge(context, writer);
564
 
565
        writer.flush();
566
        writer.close();
567
        Utils.info("Export Complete!");
568
 
569
    }
570
 
571
	public void getProductSummaryHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String exportPath) {
572
		long catalogId = expEntity.getID();
573
		try {
574
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
575
			for(Item item: items){
576
				Map<String, String> itemDetail = new HashMap<String, String>();
577
				double sellingPrice = item.getSellingPrice();
578
				double mrp = item.getMrp();
579
				boolean showmrp = true;
580
				if (mrp <= sellingPrice) {
581
					showmrp = false;
582
				}
583
 
584
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
585
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");
586
				itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
587
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
588
				itemDetail.put("MRP", ((int)item.getMrp())+"");
589
				itemDetail.put("SAVING", ((int)saving)+"");
590
				itemDetail.put("COLOR", item.getColor());
591
				itemDetail.put("SHOWMRP", showmrp +"");
592
				itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
593
				itemDetails.add(itemDetail);
594
			}
595
 
596
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
597
			String categoryName = expEntity.getCategory().getLabel();
598
			String tagline = "";
599
			String warranty = "";
600
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
601
			for(Feature feature: features){
602
				if(feature.getFeatureDefinitionID() == 120084){
603
					PrimitiveDataObject dataObject= (PrimitiveDataObject)feature.getBullets().get(0).getDataObject();
604
					tagline = dataObject.getValue(); 
605
				}
606
				if(feature.getFeatureDefinitionID() == 120125){
607
					ExpandedFeature expFeature = new ExpandedFeature(feature);
608
					ExpandedBullet expBullet =expFeature.getExpandedBullets().get(0);
609
					if(expBullet != null){
610
						warranty = expBullet.getValue() + " "+ expBullet.getUnit().getShortForm();
611
					}
612
				}
613
			}
614
 
615
 
616
 
617
			long categoryId = expEntity.getCategory().getID();
618
			Map<String,String> params = new HashMap<String, String>();
619
			params.put("TITLE", title);
620
			params.put("CATEGORY_ID", ((int)categoryId)+"");
621
			params.put("CATEGORY_NAME", categoryName);
622
			params.put("CATEGORY_URL", categoryName.replaceAll(" ", "-").toLowerCase() + "/" + categoryId);
623
 
624
			params.put("CATALOG_ID", ((int)catalogId)+"");
625
			params.put("TAGLINE", tagline);
626
			params.put("WARRANTY", warranty);
627
 
628
			VelocityContext context = new VelocityContext();
629
 
630
			context.put("itemDetails", itemDetails);
631
 
632
			context.put("params", params);
633
 
634
			String exportFileName = exportPath + catalogId + File.separator + "ProductDetail.html";
635
 
636
			File exportFile = new File(exportFileName);
637
			if(!exportFile.exists()) {
638
				exportFile.createNewFile();
639
			}
640
 
641
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));
642
 
643
			Template template = Velocity.getTemplate(templateFile);
644
			template.merge(context, writer);
645
 
646
			writer.flush();
647
			writer.close();
648
			Utils.info("Export Complete!");
649
 
650
		} catch (Exception e) {
651
			e.printStackTrace();
652
		}
653
	}
654
 
655
	private String getEntityURL(ExpandedEntity expEntity){
656
		String url = "/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
657
		String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')
658
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
659
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')
660
        + "-" + expEntity.getID();
661
		productUrl = productUrl.replaceAll("/", "-");
662
		url = url + productUrl;
663
		url = url.replaceAll("--", "-");
664
		return url;
665
	}
666
 
667
	private String getImagePrefix(ExpandedEntity expEntity){
668
		String imagePrefix = expEntity.getBrand().toLowerCase().replace(' ', '-')
669
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
670
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-');
671
		imagePrefix = imagePrefix.replaceAll("/", "-");
672
		imagePrefix = imagePrefix.replaceAll("--", "-");
673
		return imagePrefix;
674
	}
675
 
676
	private void getEntityWidgetSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
677
		long catalogId = expEntity.getID();
678
		try {
679
			expEntity = CreationUtils.getExpandedEntity(catalogId);
680
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
681
			String imagePrefix = getImagePrefix(expEntity);
682
			String url = getEntityURL(expEntity);
683
 
684
			String tinySnippet = "";
685
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
686
			for(Feature feature: features){
687
				if(feature.getFeatureDefinitionID() == 120089){
688
					PrimitiveDataObject dataObject= (PrimitiveDataObject)feature.getBullets().get(0).getDataObject();
689
					tinySnippet = dataObject.getValue(); 
690
				}
691
			}
692
 
693
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
694
 
695
			Item minPriceItem = null; 
696
			for(Item item: items){
697
				Map<String, String> itemDetail = new HashMap<String, String>();
698
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
699
					minPriceItem = item;
700
				}
701
				double sellingPrice = item.getSellingPrice();
702
				double mrp = item.getMrp();
703
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
704
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
705
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
706
				itemDetail.put("MRP", ((int)item.getMrp())+"");
707
				itemDetail.put("SAVING", ((int)saving)+"");
708
				itemDetail.put("COLOR", item.getColor());
709
				itemDetails.add(itemDetail);
710
			}
711
 
712
			boolean showmrp = true;
713
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
714
				showmrp = false;
715
			}
716
			Map<String,String> params = new HashMap<String, String>();
717
			params.put("TITLE", title);
718
			params.put("URL", url);
719
			params.put("IMAGE_PREFIX", imagePrefix);
720
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
721
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
722
			params.put("SHOWMRP", showmrp +"");
723
			params.put("CATALOG_ID", ((int)catalogId)+"");
724
			params.put("ITEM_ID", minPriceItem.getId()+"");
725
			params.put("TINY_SNIPPET", tinySnippet);
726
			params.put("staticurl", staticurl);
727
			params.put("contentVersion", contentVersion);
728
			VelocityContext context = new VelocityContext();
729
			context.put("itemDetails", itemDetails);
730
			context.put("params", params);
731
 
732
			String exportFileName = exportPath + catalogId + File.separator + "WidgetSnippet.html";
733
			File exportFile = new File(exportFileName);
734
			if(!exportFile.exists()) {
735
				exportFile.createNewFile();
736
			}
737
 
738
			BufferedWriter writer = new BufferedWriter(
739
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
740
 
741
			Template template = Velocity.getTemplate(templateFile);
742
			template.merge(context, writer);
743
 
744
			writer.flush();
745
			writer.close();
746
			Utils.info("Export Complete!");
747
 
748
 
749
		} catch (Exception e) {
750
			e.printStackTrace();
751
		}
752
	}
753
 
754
 
755
	private  void getEntityHomeSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
756
		long catalogId = expEntity.getID();
757
 
758
		Map<String,String> params = new HashMap<String, String>();
759
 
760
		try {
761
			expEntity = CreationUtils.getExpandedEntity(catalogId);
762
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
763
			String url = getEntityURL(expEntity);
764
			String imagePrefix = getImagePrefix(expEntity);
765
 
766
			catalogServiceClient = new CatalogServiceClient();
767
			client = catalogServiceClient.getClient();
768
 
769
			String tinySnippet = "";
770
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
771
			for(Feature feature: features){
772
				if(feature.getFeatureDefinitionID() == 120081){
773
					List<Bullet> bullets = feature.getBullets();
774
					int count = 1;
775
					for(Bullet bullet: bullets){
776
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
777
						tinySnippet = dataObject.getValue();
778
						params.put("SNIPPET_"+count++, tinySnippet);
779
						System.out.println("Tiny Snippets is " + dataObject.getValue());
780
					}
781
 				}
782
			}
783
 
784
 
785
 
786
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
787
 
788
			Item minPriceItem = null;
789
 
790
			for(Item item: items){
791
				Map<String, String> itemDetail = new HashMap<String, String>();
792
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
793
					minPriceItem = item;
794
				}
795
				double sellingPrice = item.getSellingPrice();
796
				double mrp = item.getMrp();
797
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
798
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
799
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
800
				itemDetail.put("MRP", ((int)item.getMrp())+"");
801
				itemDetail.put("SAVING", ((int)saving)+"");
802
				itemDetail.put("COLOR", item.getColor());
803
				itemDetails.add(itemDetail);
804
			}
805
			boolean showmrp = true;
806
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
807
				showmrp = false;
808
			}
809
 
810
			params.put("TITLE", title);
811
			params.put("URL", url);
812
			params.put("IMAGE_PREFIX", imagePrefix);
813
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
814
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
815
			params.put("SHOWMRP", showmrp +"");
816
			params.put("CATALOG_ID", ((int)catalogId)+"");
817
			params.put("ITEM_ID", minPriceItem.getId()+"");
818
			params.put("staticurl", staticurl);
819
			params.put("contentVersion", contentVersion);
820
 
821
			VelocityContext context = new VelocityContext();
822
			context.put("itemDetails", itemDetails);
823
			context.put("params", params);
824
 
825
 
826
			String exportFileName = exportPath + catalogId + File.separator + "HomeSnippet.html";
827
			File exportFile = new File(exportFileName);
828
			if(!exportFile.exists()) {
829
				exportFile.createNewFile();
830
			}
831
 
832
			BufferedWriter writer = new BufferedWriter(
833
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
834
 
835
			Template template = Velocity.getTemplate(templateFile);
836
			template.merge(context, writer);
837
 
838
			writer.flush();
839
			writer.close();
840
			Utils.info("Export Complete!");
841
 
842
		} catch (Exception e) {
843
			e.printStackTrace();
844
		}
845
	}
846
 
847
	private  void getEntityCategorySnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
848
		Map<String,String> params = new HashMap<String, String>();
849
		long catalogId = expEntity.getID();
850
 
851
		try {
852
			expEntity = CreationUtils.getExpandedEntity(catalogId);
853
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
854
			catalogServiceClient = new CatalogServiceClient();
855
			client = catalogServiceClient.getClient();
856
			String url = getEntityURL(expEntity);
857
			String imagePrefix = getImagePrefix(expEntity);
858
 
859
			String tinySnippet = "";
860
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
861
			for(Feature feature: features){
862
				if(feature.getFeatureDefinitionID() == 120081){
863
					List<Bullet> bullets = feature.getBullets();
864
					int count = 1;
865
					for(Bullet bullet: bullets){
866
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
867
						tinySnippet = dataObject.getValue();
868
						params.put("SNIPPET_"+count++, tinySnippet);
869
						System.out.println("Tiny Snippets is " + dataObject.getValue());
870
					}
871
 				}
872
			}
873
 
874
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
875
 
876
			Item minPriceItem = null;
877
			for(Item item: items){
878
				Map<String, String> itemDetail = new HashMap<String, String>();
879
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
880
					minPriceItem = item;
881
				}
882
				double sellingPrice = item.getSellingPrice();
883
				double mrp = item.getMrp();
884
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
885
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
886
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
887
				itemDetail.put("MRP", ((int)item.getMrp())+"");
888
				itemDetail.put("SAVING", ((int)saving)+"");
889
				itemDetail.put("COLOR", item.getColor());
890
				itemDetails.add(itemDetail);
891
			}
892
 
893
			boolean showmrp = true;
894
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
895
				showmrp = false;
896
			}
897
			params.put("TITLE", title);
898
			params.put("URL", url);
899
			params.put("IMAGE_PREFIX", imagePrefix);
900
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
901
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
902
			params.put("SHOWMRP", showmrp +"");
903
			params.put("CATALOG_ID", ((int)catalogId)+"");
904
			params.put("ITEM_ID", minPriceItem.getId()+"");
905
			params.put("staticurl", staticurl);
906
			params.put("contentVersion", contentVersion);
907
 
908
			VelocityContext context = new VelocityContext();
909
			context.put("itemDetails", itemDetails);
910
			context.put("params", params);
911
 
912
			String exportFileName = exportPath + catalogId + File.separator + "CategorySnippet.html";
913
			File exportFile = new File(exportFileName);
914
			if(!exportFile.exists()) {
915
				exportFile.createNewFile();
916
			}
917
 
918
			BufferedWriter writer = new BufferedWriter(
919
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
920
 
921
			Template template = Velocity.getTemplate(templateFile);
922
			template.merge(context, writer);
923
 
924
			writer.flush();
925
			writer.close();
926
			Utils.info("Export Complete!");
927
 
928
 
929
		} catch (Exception e) {
930
			e.printStackTrace();
931
		}
932
	}
933
 
934
	private  void getEntityTitleSnippetHtml(ExpandedEntity expEntity, String exportPath) {
935
		long catalogId = expEntity.getID();
936
 
937
		try {
938
			expEntity = CreationUtils.getExpandedEntity(catalogId);
939
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " 
940
	            + expEntity.getModelNumber();
941
			if(expEntity.getCategory().getParentCategory().getID() == 10001) {
942
				title +=  " | " + expEntity.getBrand() + " Mobile Phones";
943
			}
944
			if(expEntity.getCategory().getParentCategory().getID() == 10011) {
945
				title += " " + expEntity.getCategory().getLabel()
946
		            + " | " + expEntity.getBrand() + " Mobile Phone Accessories";
947
			}
948
			title += " | Saholic.com";
949
 
950
			String exportFileName = exportPath + catalogId + File.separator + "TitleSnippet.html";
951
			File exportFile = new File(exportFileName);
952
			if(!exportFile.exists()) {
953
				exportFile.createNewFile();
954
			}
955
 
956
			BufferedWriter writer = new BufferedWriter(
957
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
958
 
959
			writer.write(title);
960
 
961
			writer.flush();
962
			writer.close();
963
		} catch (Exception e) {
964
			e.printStackTrace();
965
		}
966
	}
967
 
968
 
969
	private void getEntitySearchSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
970
		long catalogId = expEntity.getID();
971
		Map<String,String> params = new HashMap<String, String>();
972
 
973
		try {
974
			expEntity = CreationUtils.getExpandedEntity(catalogId);
975
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
976
 
977
			String url = getEntityURL(expEntity);
978
			String imagePrefix = getImagePrefix(expEntity);
979
 
980
			catalogServiceClient = new CatalogServiceClient();
981
			client = catalogServiceClient.getClient();
982
 
983
			String tinySnippet = "";
984
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
985
			for(Feature feature: features){
986
				if(feature.getFeatureDefinitionID() == 120081){
987
					List<Bullet> bullets = feature.getBullets();
988
					int count = 1;
989
					for(Bullet bullet: bullets){
990
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
991
						tinySnippet = dataObject.getValue();
992
						params.put("SNIPPET_"+count++, tinySnippet);
993
						System.out.println("Tiny Snippets is " + dataObject.getValue());
994
					}
995
 				}
996
			}
997
 
998
 
999
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1000
 
1001
			Item minPriceItem = null;
1002
			for(Item item: items){
1003
				Map<String, String> itemDetail = new HashMap<String, String>();
1004
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1005
					minPriceItem = item;
1006
				}
1007
				double sellingPrice = item.getSellingPrice();
1008
				double mrp = item.getMrp();
1009
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
1010
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
1011
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1012
				itemDetail.put("MRP", ((int)item.getMrp())+"");
1013
				itemDetail.put("SAVING", ((int)saving)+"");
1014
				itemDetail.put("COLOR", item.getColor());
1015
				itemDetails.add(itemDetail);
1016
			}
1017
 
1018
			boolean showmrp = true;
1019
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1020
				showmrp = false;
1021
			}
1022
			params.put("TITLE", title);
1023
			params.put("URL", url);
1024
			params.put("IMAGE_PREFIX", imagePrefix);
1025
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1026
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
1027
			params.put("SHOWMRP", showmrp +"");
1028
			params.put("CATALOG_ID", ((int)catalogId)+"");
1029
			params.put("ITEM_ID", minPriceItem.getId()+"");
1030
			params.put("staticurl", staticurl);
1031
			params.put("contentVersion", contentVersion);
1032
 
1033
			VelocityContext context = new VelocityContext();
1034
			context.put("itemDetails", itemDetails);
1035
			context.put("params", params);
1036
 
1037
			String exportFileName = exportPath + catalogId + File.separator + "SearchSnippet.html";
1038
			File exportFile = new File(exportFileName);
1039
			if(!exportFile.exists()) {
1040
				exportFile.createNewFile();
1041
			}
1042
 
1043
			BufferedWriter writer = new BufferedWriter(
1044
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
1045
 
1046
			Template template = Velocity.getTemplate(templateFile);
1047
			template.merge(context, writer);
1048
 
1049
			writer.flush();
1050
			writer.close();
1051
			Utils.info("Export Complete!");
1052
 
1053
 
1054
		} catch (Exception e) {
1055
			e.printStackTrace();
1056
		}
1057
	}
1058
 
1059
 
1060
    private void getEntityCompareSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1061
        long catalogId = expEntity.getID();
1062
        Map<String,String> params = new HashMap<String, String>();
1063
 
1064
        try {
1065
            expEntity = CreationUtils.getExpandedEntity(catalogId);
1066
            String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
1067
 
1068
            String url = getEntityURL(expEntity);
1069
            String imagePrefix = getImagePrefix(expEntity);
1070
 
1071
            catalogServiceClient = new CatalogServiceClient();
1072
            client = catalogServiceClient.getClient();
1073
 
1074
            String tinySnippet = "";
1075
            List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1076
            for(Feature feature: features){
1077
                if(feature.getFeatureDefinitionID() == 120081){
1078
                    List<Bullet> bullets = feature.getBullets();
1079
                    int count = 1;
1080
                    for(Bullet bullet: bullets){
1081
                        PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1082
                        tinySnippet = dataObject.getValue();
1083
                        params.put("SNIPPET_"+count++, tinySnippet);
1084
                        System.out.println("Tiny Snippets is " + dataObject.getValue());
1085
                    }
1086
                }
1087
            }
1088
 
1089
 
1090
            List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1091
 
1092
            Item minPriceItem = null;
1093
            for(Item item: items){
1094
                Map<String, String> itemDetail = new HashMap<String, String>();
1095
                if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1096
                    minPriceItem = item;
1097
                }
1098
                double sellingPrice = item.getSellingPrice();
1099
                double mrp = item.getMrp();
1100
                double saving = Math.round((mrp-sellingPrice)/mrp*100);
1101
                itemDetail.put("ITEM_ID", ((int)item.getId())+"");  
1102
                itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1103
                itemDetail.put("MRP", ((int)item.getMrp())+"");
1104
                itemDetail.put("SAVING", ((int)saving)+"");
1105
                itemDetail.put("COLOR", item.getColor());
1106
                itemDetails.add(itemDetail);
1107
            }
1108
 
1109
            boolean showmrp = true;
1110
            if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1111
                showmrp = false;
1112
            }
1113
            params.put("TITLE", title);
1114
            params.put("URL", url);
1115
            params.put("IMAGE_PREFIX", imagePrefix);
1116
            params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1117
            params.put("MRP", ((int)minPriceItem.getMrp())+"");
1118
            params.put("SHOWMRP", showmrp +"");
1119
            params.put("CATALOG_ID", ((int)catalogId)+"");
1120
            params.put("ITEM_ID", minPriceItem.getId()+"");
1121
            params.put("staticurl", staticurl);
1122
            params.put("contentVersion", contentVersion);
1123
 
1124
            VelocityContext context = new VelocityContext();
1125
            context.put("itemDetails", itemDetails);
1126
            context.put("params", params);
1127
 
1128
            String exportFileName = exportPath + catalogId + File.separator + "CompareProductSnippet.html";
1129
            File exportFile = new File(exportFileName);
1130
            if(!exportFile.exists()) {
1131
                exportFile.createNewFile();
1132
            }
1133
 
1134
            BufferedWriter writer = new BufferedWriter(
1135
                new OutputStreamWriter(new FileOutputStream(exportFile)));
1136
 
1137
            Template template = Velocity.getTemplate(templateFile);
1138
            template.merge(context, writer);
1139
 
1140
            writer.flush();
1141
            writer.close();
1142
            Utils.info("Export Complete!");
1143
 
1144
 
1145
        } catch (Exception e) {
1146
            e.printStackTrace();
1147
        }
1148
    }
1149
 
1150
 
1151
    private void getEntityCompareSummarySnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1152
        long catalogId = expEntity.getID();
1153
        Map<String,String> params = new HashMap<String, String>();
1154
 
1155
        try {
1156
            expEntity = CreationUtils.getExpandedEntity(catalogId);
1157
            String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
1158
 
1159
            String url = getEntityURL(expEntity);
1160
            String imagePrefix = getImagePrefix(expEntity);
1161
 
1162
            catalogServiceClient = new CatalogServiceClient();
1163
            client = catalogServiceClient.getClient();
1164
 
1165
            String tinySnippet = "";
1166
            List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1167
            for(Feature feature: features){
1168
                if(feature.getFeatureDefinitionID() == 120081){
1169
                    List<Bullet> bullets = feature.getBullets();
1170
                    int count = 1;
1171
                    for(Bullet bullet: bullets){
1172
                        PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1173
                        tinySnippet = dataObject.getValue();
1174
                        params.put("SNIPPET_"+count++, tinySnippet);
1175
                        System.out.println("Tiny Snippets is " + dataObject.getValue());
1176
                    }
1177
                }
1178
            }
1179
 
1180
 
1181
            List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1182
 
1183
            Item minPriceItem = null;
1184
            for(Item item: items){
1185
                Map<String, String> itemDetail = new HashMap<String, String>();
1186
                if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1187
                    minPriceItem = item;
1188
                }
1189
                double sellingPrice = item.getSellingPrice();
1190
                double mrp = item.getMrp();
1191
                double saving = Math.round((mrp-sellingPrice)/mrp*100);
1192
                itemDetail.put("ITEM_ID", ((int)item.getId())+"");  
1193
                itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1194
                itemDetail.put("MRP", ((int)item.getMrp())+"");
1195
                itemDetail.put("SAVING", ((int)saving)+"");
1196
                itemDetail.put("COLOR", item.getColor());
1197
                itemDetails.add(itemDetail);
1198
            }
1199
 
1200
            boolean showmrp = true;
1201
            if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1202
                showmrp = false;
1203
            }
1204
            params.put("TITLE", title);
1205
            params.put("URL", url);
1206
            params.put("IMAGE_PREFIX", imagePrefix);
1207
            params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1208
            params.put("MRP", ((int)minPriceItem.getMrp())+"");
1209
            params.put("SHOWMRP", showmrp +"");
1210
            params.put("CATALOG_ID", ((int)catalogId)+"");
1211
            params.put("ITEM_ID", minPriceItem.getId()+"");
1212
            params.put("staticurl", staticurl);
1213
            params.put("contentVersion", contentVersion);
1214
 
1215
            VelocityContext context = new VelocityContext();
1216
            context.put("itemDetails", itemDetails);
1217
            context.put("params", params);
1218
 
1219
            String exportFileName = exportPath + catalogId + File.separator + "CompareProductSummarySnippet.html";
1220
            File exportFile = new File(exportFileName);
1221
            if(!exportFile.exists()) {
1222
                exportFile.createNewFile();
1223
            }
1224
 
1225
            BufferedWriter writer = new BufferedWriter(
1226
                new OutputStreamWriter(new FileOutputStream(exportFile)));
1227
 
1228
            Template template = Velocity.getTemplate(templateFile);
1229
            template.merge(context, writer);
1230
 
1231
            writer.flush();
1232
            writer.close();
1233
            Utils.info("Export Complete!");
1234
 
1235
 
1236
        } catch (Exception e) {
1237
            e.printStackTrace();
1238
        }
1239
    }
1240
 
1241
	private void getEntityPhonesIOwnSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {
1242
		long catalogId = expEntity.getID();
1243
		Map<String,String> params = new HashMap<String, String>();
1244
 
1245
		try {
1246
			expEntity = CreationUtils.getExpandedEntity(catalogId);
1247
			String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();
1248
			String url = getEntityURL(expEntity);
1249
			String imagePrefix = getImagePrefix(expEntity);
1250
 
1251
			catalogServiceClient = new CatalogServiceClient();
1252
			client = catalogServiceClient.getClient();
1253
 
1254
			String tinySnippet = "";
1255
			List<Feature>  features = expEntity.getSlide(130054).getFeatures();
1256
			for(Feature feature: features){
1257
				if(feature.getFeatureDefinitionID() == 120081){
1258
					List<Bullet> bullets = feature.getBullets();
1259
					int count = 1;
1260
					for(Bullet bullet: bullets){
1261
						PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();
1262
						tinySnippet = dataObject.getValue();
1263
						params.put("SNIPPET_"+count++, tinySnippet);
1264
						System.out.println("Tiny Snippets is " + dataObject.getValue());
1265
					}
1266
 				}
1267
			}
1268
 
1269
 
1270
			List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
1271
 
1272
			Item minPriceItem = null;
1273
			Date todate = new Date();
1274
			for(Item item: items){
1275
				Map<String, String> itemDetail = new HashMap<String, String>();
1276
				if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
1277
					minPriceItem = item;
1278
				}
1279
				double sellingPrice = item.getSellingPrice();
1280
				double mrp = item.getMrp();
1281
				double saving = Math.round((mrp-sellingPrice)/mrp*100);
1282
				itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
1283
				itemDetail.put("SP", ((int)item.getSellingPrice())+"");
1284
				itemDetail.put("MRP", ((int)item.getMrp())+"");
1285
				itemDetail.put("SAVING", ((int)saving)+"");
1286
				itemDetail.put("COLOR", item.getColor());
1287
				itemDetails.add(itemDetail);
1288
			}
1289
 
1290
			boolean showmrp = true;
1291
			if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {
1292
				showmrp = false;
1293
			}
1294
			params.put("TITLE", title);
1295
			params.put("URL", url);
1296
			params.put("IMAGE_PREFIX", imagePrefix);
1297
			params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");
1298
			params.put("MRP", ((int)minPriceItem.getMrp())+"");
1299
			params.put("SHOWMRP", showmrp +"");
1300
			params.put("CATALOG_ID", ((int)catalogId)+"");
1301
			params.put("ITEM_ID", minPriceItem.getId()+"");
1302
			params.put("staticurl", staticurl);
1303
			params.put("contentVersion", contentVersion);
1304
 
1305
			VelocityContext context = new VelocityContext();
1306
			context.put("itemDetails", itemDetails);
1307
			context.put("params", params);
1308
 
1309
			String exportFileName = exportPath + catalogId + File.separator + "PhonesIOwnSnippet.html";
1310
			File exportFile = new File(exportFileName);
1311
			if(!exportFile.exists()) {
1312
				exportFile.createNewFile();
1313
			}
1314
 
1315
			BufferedWriter writer = new BufferedWriter(
1316
			    new OutputStreamWriter(new FileOutputStream(exportFile)));
1317
 
1318
			Template template = Velocity.getTemplate(templateFile);
1319
			template.merge(context, writer);
1320
 
1321
			writer.flush();
1322
			writer.close();
1323
			Utils.info("Export Complete!");
1324
 
1325
 
1326
		} catch (Exception e) {
1327
			e.printStackTrace();
1328
		}
1329
	}
1330
 
1331
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
1332
		try {
1333
			Velocity.init("velocity/velocity.properties");
1334
			Template template = Velocity.getTemplate(templateFile);
1335
			if(template != null) {
1336
				StringWriter writer = new StringWriter();
1337
				template.merge(context, writer);
1338
				writer.flush();
1339
				writer.close();
1340
				return writer.toString();
1341
			}
1342
 
1343
			} catch (ResourceNotFoundException e) {
1344
				e.printStackTrace();
1345
			} catch (ParseErrorException e) {
1346
				e.printStackTrace();
1347
			} catch (MethodInvocationException e) {
1348
				e.printStackTrace();
1349
			} catch (IOException e) {
1350
				e.printStackTrace();
1351
			} catch (Exception e) {
1352
				e.printStackTrace();
1353
			}
1354
 
1355
		return null;
1356
	}
1357
 
1358
 
1359
}
1360