Subversion Repositories SmartDukaan

Rev

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