Subversion Repositories SmartDukaan

Rev

Rev 2488 | Rev 2651 | 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");
103
		}
104
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
105
	}
106
 
107
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
108
		try {
109
			Properties p = new Properties();
110
			p.setProperty("resource.loader", "file");
111
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
112
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
113
			Velocity.init(p);
114
			for(String filename: filenames){
115
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
116
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + catalogId + File.separator +filename+".html")));
117
				template.merge(context, writer);
118
				writer.flush();
119
				writer.close();	
120
			}
121
		}catch (ResourceNotFoundException e) {
122
			// TODO Auto-generated catch block
123
			e.printStackTrace();
124
		} catch (IOException e) {
125
			// TODO Auto-generated catch block
126
			e.printStackTrace();
127
		} catch (ParseErrorException e) {
128
			// TODO Auto-generated catch block
129
			e.printStackTrace();
130
		} catch (Exception e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		}
134
	}
135
 
136
	public static void main(String[] args) throws Exception{
137
		PriceInsertor generator = new PriceInsertor();
138
		CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
139
		Client client = catalogServiceClient.getClient();
140
		List<Item> items = client.getItemsByCatalogId(1001210);
141
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
142
		Item minPriceItem = null;
143
		for(Item item: items){
144
			Map<String, String> itemDetail = new HashMap<String, String>();
145
			boolean showmrp = true;
146
			if (item.getMrp() <= item.getSellingPrice()) {
147
				showmrp = false;
148
			}
149
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
150
				minPriceItem = item;
151
			}
152
			double sellingPrice = item.getSellingPrice();
153
			double mrp = item.getMrp();
154
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
155
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
156
			itemDetail.put("SP", ((int)item.getSellingPrice())+"");
157
			itemDetail.put("MRP", ((int)item.getMrp())+"");
158
			itemDetail.put("SAVING", ((int)saving)+"");
159
			itemDetail.put("COLOR", item.getColor());
160
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
161
			itemDetail.put("SHOWMRP", showmrp +"");
162
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
163
			itemDetails.add(itemDetail);
164
		}
165
 
166
		String staticurl = "http://static" + 1001210%3 + "." + "saholic.com";
167
		VelocityContext context = new VelocityContext();
168
		context.put("itemDetails", itemDetails);
169
		context.put("minPriceItem", minPriceItem);
170
		context.put("domain", "saholic.com");
171
		context.put("staticurl", staticurl);
172
 
173
		List<String> filenames = new ArrayList<String>();
174
		filenames.add("ProductDetail");
175
		filenames.add("WidgetSnippet");
176
		filenames.add("HomeSnippet");
177
		filenames.add("SearchSnippet");
178
		filenames.add("CategorySnippet");
179
		filenames.add("SlideGuide");
180
		filenames.add("PhonesIOwnSnippet");
181
		filenames.add("ProductPropertiesSnippet");
2547 rajveer 182
		if(CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory()).getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY){
2367 rajveer 183
			filenames.add("CompareProductSnippet");
184
			filenames.add("ComparisonSnippet");
185
			filenames.add("CompareProductSummarySnippet");
186
			filenames.add("SlideNamesSnippet");
187
		}		
188
		generator.getHtmlFromVelocity(filenames,Utils.EXPORT_ENTITIES_PATH_SAHOLIC,context,1001210);
189
	}
190
}
191