Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2367 rajveer 1
package in.shop2020.ui.util;
2
 
3
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.util.Utils;
5
import in.shop2020.utils.CategoryManager;
6
 
7
import java.io.BufferedWriter;
8
import java.io.File;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.io.OutputStreamWriter;
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Properties;
17
 
18
import org.apache.commons.io.FileUtils;
19
import org.apache.velocity.Template;
20
import org.apache.velocity.VelocityContext;
21
import org.apache.velocity.app.Velocity;
22
import org.apache.velocity.exception.ParseErrorException;
23
import org.apache.velocity.exception.ResourceNotFoundException;
24
 
25
 
26
/**
27
 * utility class to generated all html stuff from java objects
28
 * Driver class to merge Java data objects with VTL script. 
29
 * 
30
 * @author rajveer
31
 *
32
 */
33
public class PriceInsertor {
34
 
35
	public void insertPriceInSolrData(long catalogId, double price) throws IOException{
36
		String irSolrFilename = Utils.EXPORT_PATH + "xml/final/" + catalogId + "_irdata_solr.xml";
37
		String finalFile = Utils.EXPORT_PATH + "solr/" + catalogId + "_irdata_solr.xml";
38
		File f = new File(irSolrFilename);
39
		String s = FileUtils.readFileToString(f);
40
		s = s.replaceAll("<field name=\"F_50002\">.*</field>", "<field name=\"F_50002\">"+price+"</field>");
41
		File f1 = new File(finalFile);
42
		FileUtils.writeStringToFile(f1, s);
43
	}
44
 
45
	public void insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath){
46
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
47
		Item minPriceItem = null;
48
		for(Item item: items){
49
			Map<String, String> itemDetail = new HashMap<String, String>();
50
			boolean showmrp = true;
51
			if (item.getMrp() <= item.getSellingPrice()) {
52
				showmrp = false;
53
			}
54
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
55
				minPriceItem = item;
56
			}
57
			double sellingPrice = item.getSellingPrice();
58
			double mrp = item.getMrp();
59
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
60
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
61
			itemDetail.put("SP", ((int)item.getSellingPrice())+"");
62
			itemDetail.put("MRP", ((int)item.getMrp())+"");
63
			itemDetail.put("SAVING", ((int)saving)+"");
64
			itemDetail.put("COLOR", item.getColor());
65
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
66
			itemDetail.put("SHOWMRP", showmrp +"");
67
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
68
			itemDetails.add(itemDetail);
69
		}
70
 
71
		File exportDir = new File(exportPath + catalogId);
72
		if(!exportDir.exists()) {
73
			exportDir.mkdir();
74
		}
75
 
76
		// This URL is used to ensure that images such as icon and thumbnail for
77
		// a particular entity are always downloaded from the same location.
78
		String staticurl = "http://static" + catalogId%3 + "." + domain;
79
 
80
 
81
		VelocityContext context = new VelocityContext();
82
		context.put("itemDetails", itemDetails);
83
		context.put("minPriceItem", minPriceItem);
84
		context.put("domain", domain);
85
		context.put("staticurl", staticurl);
86
 
87
		List<String> filenames = new ArrayList<String>();
88
		filenames.add("ProductDetail");
89
		filenames.add("WidgetSnippet");
90
		filenames.add("HomeSnippet");
91
		filenames.add("SearchSnippet");
92
		filenames.add("CategorySnippet");
93
		filenames.add("SlideGuide");
94
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 95
		filenames.add("MyResearchSnippet");
3018 rajveer 96
		filenames.add("AfterSales");
2716 varun.gupt 97
 
2547 rajveer 98
		if(CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory()).getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY){
2488 rajveer 99
			filenames.add("PhonesIOwnSnippet");
2367 rajveer 100
			filenames.add("CompareProductSnippet");
101
			filenames.add("ComparisonSnippet");
102
			filenames.add("CompareProductSummarySnippet");
103
			filenames.add("SlideNamesSnippet");
2651 rajveer 104
			filenames.add("RelatedAccessories");
2743 rajveer 105
			filenames.add("MostComparedProducts");
2367 rajveer 106
		}
107
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
108
	}
109
 
110
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
111
		try {
112
			Properties p = new Properties();
113
			p.setProperty("resource.loader", "file");
114
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
115
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
116
			Velocity.init(p);
117
			for(String filename: filenames){
118
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
119
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + catalogId + File.separator +filename+".html")));
120
				template.merge(context, writer);
121
				writer.flush();
122
				writer.close();	
123
			}
124
		}catch (ResourceNotFoundException e) {
125
			// TODO Auto-generated catch block
126
			e.printStackTrace();
127
		} catch (IOException e) {
128
			// TODO Auto-generated catch block
129
			e.printStackTrace();
130
		} catch (ParseErrorException e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		} catch (Exception e) {
134
			// TODO Auto-generated catch block
135
			e.printStackTrace();
136
		}
137
	}
138
}
139