| 5117 |
varun.gupt |
1 |
package in.shop2020.util;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.metamodel.core.Entity;
|
|
|
4 |
import in.shop2020.metamodel.core.Slide;
|
|
|
5 |
import in.shop2020.metamodel.definitions.Catalog;
|
|
|
6 |
import in.shop2020.metamodel.definitions.Category;
|
|
|
7 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
|
|
8 |
import in.shop2020.metamodel.util.CreationUtils;
|
|
|
9 |
|
| 5404 |
amit.gupta |
10 |
import java.util.ArrayList;
|
|
|
11 |
import java.util.Arrays;
|
|
|
12 |
import java.util.HashMap;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
|
|
|
16 |
import org.apache.commons.lang.StringUtils;
|
|
|
17 |
|
| 5117 |
varun.gupt |
18 |
public class CompatibleAccessoriesIndexGenerator {
|
|
|
19 |
|
|
|
20 |
private String[] indentation = {"", " ", " ", " "," "};
|
|
|
21 |
private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
|
|
|
22 |
List<Entity> entities;
|
|
|
23 |
private Map<Long, Long> categorySlides = new HashMap<Long, Long>();
|
|
|
24 |
private Map<Long, String> categoryLabels = new HashMap<Long, String>();
|
| 5404 |
amit.gupta |
25 |
|
|
|
26 |
private Map<Long, List<String>> categorySynonyms = new HashMap<Long, List<String>>();
|
|
|
27 |
private Map<String, List<String>> brandSynonyms = new HashMap<String, List<String>>();
|
| 5117 |
varun.gupt |
28 |
|
| 5155 |
varun.gupt |
29 |
public CompatibleAccessoriesIndexGenerator(List<Entity> entities) throws Exception {
|
| 5117 |
varun.gupt |
30 |
|
|
|
31 |
categorySlides.put((long) 10014, (long) 130067);
|
|
|
32 |
categorySlides.put((long) 10018, (long) 130073);
|
|
|
33 |
|
|
|
34 |
categoryLabels.put((long) 10014, "Battery");
|
|
|
35 |
categoryLabels.put((long) 10018, "Carrying Case");
|
| 5404 |
amit.gupta |
36 |
|
|
|
37 |
Map<String, String> brSynonyms = new HashMap<String, String>();
|
|
|
38 |
Map<String, String> catSynonyms = new HashMap<String, String>();
|
| 5117 |
varun.gupt |
39 |
|
| 5404 |
amit.gupta |
40 |
|
|
|
41 |
try {
|
|
|
42 |
brSynonyms = CreationUtils.getSynonyms().get("brand");
|
|
|
43 |
catSynonyms = CreationUtils.getSynonyms().get("subcategory");
|
|
|
44 |
|
|
|
45 |
for(String brand : brSynonyms.keySet()){
|
|
|
46 |
List<String> brandSyns = Arrays.asList((brand + "," + StringUtils.defaultString(brSynonyms.get(brand))).split(","));
|
|
|
47 |
brandSynonyms.put(brand, brandSyns);
|
|
|
48 |
}
|
|
|
49 |
} catch (Exception e) {
|
|
|
50 |
// TODO: handle exception
|
|
|
51 |
}
|
|
|
52 |
catSynonyms.put("Battery", StringUtils.defaultString("Battery," + StringUtils.defaultString(catSynonyms.get("Battery"))));
|
|
|
53 |
catSynonyms.put("Carrying Case", StringUtils.defaultString("Carrying Case," + StringUtils.defaultString(catSynonyms.get("Carrying Case"))));
|
|
|
54 |
|
|
|
55 |
categorySynonyms.put((long) 10014, Arrays.asList(catSynonyms.get("Battery").split(",")));
|
|
|
56 |
categorySynonyms.put((long) 10018, Arrays.asList(catSynonyms.get("Carrying Case").split(",")));
|
| 5155 |
varun.gupt |
57 |
this.entities = entities;
|
| 5117 |
varun.gupt |
58 |
}
|
|
|
59 |
|
|
|
60 |
public void generate() {
|
|
|
61 |
StringBuilder sb = new StringBuilder();
|
|
|
62 |
|
|
|
63 |
for(Entity entity: entities) {
|
|
|
64 |
|
|
|
65 |
long categoryId = entity.getCategoryID();
|
|
|
66 |
String url = getProductURL(entity);
|
|
|
67 |
|
|
|
68 |
if (url != null && categorySlides.keySet().contains(categoryId)) {
|
|
|
69 |
|
|
|
70 |
Slide compatibilitySlide = entity.getSlide(categorySlides.get(categoryId));
|
|
|
71 |
|
|
|
72 |
try {
|
|
|
73 |
String text = compatibilitySlide.getFreeformContent().getFreeformText();
|
|
|
74 |
|
|
|
75 |
if(text.indexOf("<ul>") > -1) continue;
|
|
|
76 |
for(String name: getHandsetNames(text)) {
|
| 5404 |
amit.gupta |
77 |
for(String categoryLabel : getCategorySynonyms(categoryId)){
|
|
|
78 |
sb.append(indentation[1] + "<div>\n");
|
|
|
79 |
sb.append(indentation[2] + "<a href='" + url + "'>" + name + " " + categoryLabel + "</a>\n");
|
|
|
80 |
sb.append(indentation[1] + "</div>\n");
|
|
|
81 |
}
|
| 5117 |
varun.gupt |
82 |
}
|
|
|
83 |
|
|
|
84 |
} catch (NullPointerException e) {
|
|
|
85 |
// TODO: handle exception
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "compatible-accessories-index.html";
|
|
|
91 |
|
|
|
92 |
try {
|
|
|
93 |
DBUtils.store(sb.toString(), indexFilename);
|
|
|
94 |
|
|
|
95 |
} catch (Exception e) {
|
|
|
96 |
e.printStackTrace();
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
private String getProductURL(Entity entity) {
|
|
|
101 |
|
|
|
102 |
Category category = defContainer.getCategory(entity.getCategoryID());
|
|
|
103 |
try {
|
|
|
104 |
Category parentCategory = category.getParentCategory();
|
|
|
105 |
|
|
|
106 |
String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
|
|
|
107 |
String productUrl = ((entity.getBrand() != null) ? entity.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
|
|
|
108 |
+ "-" + ((entity.getModelName() != null) ? entity.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-')
|
|
|
109 |
+ "-" + ((entity.getModelNumber() != null ) ? entity.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
|
|
|
110 |
+ "-" + entity.getID();
|
|
|
111 |
productUrl = productUrl.replaceAll("/", "-");
|
|
|
112 |
url = url + productUrl;
|
|
|
113 |
url = url.replaceAll("-+", "-");
|
|
|
114 |
return url;
|
|
|
115 |
|
|
|
116 |
} catch (NullPointerException e) {
|
|
|
117 |
return null;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private List<String> getHandsetNames(String rawText) {
|
|
|
122 |
|
|
|
123 |
List<String> names = new ArrayList<String>();
|
|
|
124 |
|
|
|
125 |
for (String namesWithSameBrand: rawText.trim().split("\n")) {
|
|
|
126 |
boolean isFirst = true;
|
| 5404 |
amit.gupta |
127 |
List<String> brandSynonyms = null;
|
| 5117 |
varun.gupt |
128 |
|
|
|
129 |
for(String name: namesWithSameBrand.trim().split(",")) {
|
|
|
130 |
name = name.trim();
|
| 5404 |
amit.gupta |
131 |
String [] brandName;
|
| 5117 |
varun.gupt |
132 |
if(isFirst) {
|
| 5404 |
amit.gupta |
133 |
brandName = name.split(" ", 2);
|
|
|
134 |
if(brandName.length==1){
|
|
|
135 |
continue;
|
|
|
136 |
}
|
|
|
137 |
if(brandName[1].startsWith("Ericsson")) {
|
|
|
138 |
brandName[0] = "Sony Ericsson";
|
|
|
139 |
brandName[1] = brandName[1].substring(9);
|
|
|
140 |
}
|
|
|
141 |
brandSynonyms = getBrandSynonyms(brandName[0]);
|
|
|
142 |
name = brandName[1];
|
| 5117 |
varun.gupt |
143 |
isFirst = false;
|
| 5404 |
amit.gupta |
144 |
}
|
|
|
145 |
for(String brand : brandSynonyms){
|
| 5117 |
varun.gupt |
146 |
names.add(brand + " " + name);
|
|
|
147 |
}
|
| 5404 |
amit.gupta |
148 |
|
| 5117 |
varun.gupt |
149 |
}
|
|
|
150 |
}
|
|
|
151 |
return names;
|
|
|
152 |
}
|
|
|
153 |
|
| 5404 |
amit.gupta |
154 |
private List<String> getBrandSynonyms(String brand) {
|
|
|
155 |
if(!brandSynonyms.containsKey(brand)){
|
|
|
156 |
List<String> list = new ArrayList<String>();
|
|
|
157 |
list.add(brand);
|
|
|
158 |
brandSynonyms.put(brand, list);
|
|
|
159 |
}
|
|
|
160 |
return brandSynonyms.get(brand);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
private List<String> getCategorySynonyms(Long categoryId) {
|
|
|
164 |
return categorySynonyms.get(categoryId);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
| 5117 |
varun.gupt |
169 |
public static void main(String[] args) {
|
|
|
170 |
try {
|
| 5155 |
varun.gupt |
171 |
List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
|
|
|
172 |
CompatibleAccessoriesIndexGenerator generator = new CompatibleAccessoriesIndexGenerator(entities);
|
| 5117 |
varun.gupt |
173 |
generator.generate();
|
|
|
174 |
} catch (Exception e) {
|
|
|
175 |
e.printStackTrace();
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
}
|