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