| 5522 |
varun.gupt |
1 |
package in.shop2020.util;
|
|
|
2 |
|
|
|
3 |
import java.io.File;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.HashMap;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
import java.util.Map;
|
|
|
8 |
|
| 5564 |
varun.gupt |
9 |
import org.apache.commons.lang.StringEscapeUtils;
|
| 5522 |
varun.gupt |
10 |
import org.apache.commons.lang.StringUtils;
|
|
|
11 |
|
|
|
12 |
import in.shop2020.metamodel.core.Bullet;
|
|
|
13 |
import in.shop2020.metamodel.core.Entity;
|
|
|
14 |
import in.shop2020.metamodel.core.Feature;
|
|
|
15 |
import in.shop2020.metamodel.core.PrimitiveDataObject;
|
|
|
16 |
import in.shop2020.metamodel.core.Slide;
|
|
|
17 |
import in.shop2020.metamodel.definitions.Catalog;
|
|
|
18 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
|
|
19 |
import in.shop2020.metamodel.util.CreationUtils;
|
|
|
20 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
21 |
import in.shop2020.model.v1.catalog.status;
|
| 5945 |
mandeep.dh |
22 |
import in.shop2020.model.v1.catalog.CatalogService.Client;
|
| 5522 |
varun.gupt |
23 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
24 |
|
|
|
25 |
public class MobileSiteDataXMLGenerator {
|
|
|
26 |
|
|
|
27 |
private List<Entity> entities;
|
|
|
28 |
private List<Item> items;
|
|
|
29 |
private Map<Long, Item> entityItemMap = new HashMap<Long, Item>();
|
|
|
30 |
private String[] xmlIndentation = {"", " ", " ", " "," "};
|
|
|
31 |
|
|
|
32 |
private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
|
|
|
33 |
private CatalogClient csc;
|
|
|
34 |
private Client client;
|
|
|
35 |
|
|
|
36 |
public MobileSiteDataXMLGenerator(List<Entity> entities) throws Exception {
|
|
|
37 |
this.entities = entities;
|
|
|
38 |
csc = new CatalogClient();
|
|
|
39 |
client = csc.getClient();
|
|
|
40 |
|
|
|
41 |
items = client.getAllItemsByStatus(status.ACTIVE);
|
|
|
42 |
items.addAll(client.getAllItemsByStatus(status.COMING_SOON));
|
|
|
43 |
items.addAll(client.getAllItemsByStatus(status.PAUSED));
|
|
|
44 |
|
|
|
45 |
for (Item item: items) {
|
|
|
46 |
if(!entityItemMap.containsKey(item.getCatalogItemId())) {
|
|
|
47 |
|
|
|
48 |
entityItemMap.put(item.getCatalogItemId(), item);
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
void generate() throws Exception {
|
|
|
54 |
List<String> productXMLSnippets = new ArrayList<String>();
|
| 11706 |
amit.gupta |
55 |
productXMLSnippets.add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
|
|
56 |
productXMLSnippets.add("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
|
| 5522 |
varun.gupt |
57 |
|
|
|
58 |
for (Entity entity: entities) {
|
|
|
59 |
try {
|
|
|
60 |
long categoryId = defContainer.getCategory(entity.getCategoryID()).getParentCategory().getID();
|
|
|
61 |
} catch (NullPointerException e) {
|
|
|
62 |
continue;
|
|
|
63 |
}
|
|
|
64 |
if (! entityItemMap.containsKey(entity.getID())) continue;
|
|
|
65 |
|
|
|
66 |
Item item = entityItemMap.get(entity.getID());
|
|
|
67 |
String url = getProductURL(entity.getID(), item);
|
| 11706 |
amit.gupta |
68 |
productXMLSnippets.add(this.xmlIndentation[2] + "<url><loc>" + url + "</loc></url>");
|
| 5522 |
varun.gupt |
69 |
}
|
| 11723 |
amit.gupta |
70 |
productXMLSnippets.add("</urlset>");
|
| 5522 |
varun.gupt |
71 |
String productDataXML = StringUtils.join(productXMLSnippets, "\n");
|
|
|
72 |
|
|
|
73 |
Utils.info(productDataXML);
|
|
|
74 |
|
|
|
75 |
// Write it to file
|
|
|
76 |
String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "msitedata.xml";
|
|
|
77 |
DBUtils.store(productDataXML, productXMLFilename);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private String getProductURL(long entityId, Item item){
|
|
|
81 |
//long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();
|
|
|
82 |
in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
|
|
|
83 |
|
| 11723 |
amit.gupta |
84 |
String url = "http://m.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
|
| 5522 |
varun.gupt |
85 |
String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
|
|
|
86 |
+ "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-')
|
|
|
87 |
+ "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
|
|
|
88 |
+ "-" + entityId;
|
|
|
89 |
productUrl = productUrl.replaceAll("/", "-");
|
|
|
90 |
url = url + productUrl;
|
|
|
91 |
url = url.replaceAll("-+", "-");
|
|
|
92 |
return url;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public static void main(String[] args) {
|
|
|
96 |
try {
|
|
|
97 |
List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
|
|
|
98 |
MobileSiteDataXMLGenerator generator = new MobileSiteDataXMLGenerator(entities);
|
|
|
99 |
generator.generate();
|
|
|
100 |
} catch (Exception e) {
|
|
|
101 |
e.printStackTrace();
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|