| 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");
|
|
|
96 |
|
| 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");
|
| 2743 |
rajveer |
104 |
filenames.add("MostComparedProducts");
|
| 2367 |
rajveer |
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 |
|