Subversion Repositories SmartDukaan

Rev

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