| 2071 |
rajveer |
1 |
package in.shop2020.utils;
|
|
|
2 |
|
|
|
3 |
import java.io.Serializable;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.HashMap;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
import java.util.Map;
|
|
|
8 |
|
|
|
9 |
import in.shop2020.model.v1.catalog.Category;
|
|
|
10 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
|
|
11 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
|
|
12 |
|
|
|
13 |
public class CategoryManager implements Serializable{
|
|
|
14 |
|
|
|
15 |
private static final long serialVersionUID = 1L;
|
|
|
16 |
|
|
|
17 |
private static CategoryManager categoryManager;
|
|
|
18 |
|
|
|
19 |
private Map<Long, Category> categories;
|
|
|
20 |
|
|
|
21 |
static{
|
|
|
22 |
synchronized(CategoryManager.class){
|
|
|
23 |
categoryManager = new CategoryManager();
|
|
|
24 |
}
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
private CategoryManager(){
|
|
|
28 |
Logger.log("Initializing category manager", this);
|
|
|
29 |
categories = new HashMap<Long, Category>();
|
|
|
30 |
try {
|
|
|
31 |
CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
|
|
|
32 |
Client client = catalogServiceClient.getClient();
|
|
|
33 |
List<Category> t_categories = client.getAllCategories();
|
|
|
34 |
for(Category category: t_categories){
|
|
|
35 |
categories.put(category.getId(), category);
|
|
|
36 |
}
|
| 2551 |
rajveer |
37 |
System.out.println(categories);
|
| 2071 |
rajveer |
38 |
for(Category category: t_categories){
|
|
|
39 |
if(category.getParent_category_id() != 0){
|
| 2551 |
rajveer |
40 |
List<Long> childCats = categories.get(category.getParent_category_id()).getChildren_category_ids();
|
|
|
41 |
if(childCats==null){
|
|
|
42 |
childCats = new ArrayList<Long>();
|
|
|
43 |
categories.get(category.getParent_category_id()).setChildren_category_ids(childCats);
|
|
|
44 |
}
|
|
|
45 |
childCats.add(category.getId());
|
| 2071 |
rajveer |
46 |
}
|
|
|
47 |
}
|
| 2551 |
rajveer |
48 |
System.out.println(categories);
|
| 2071 |
rajveer |
49 |
} catch (Exception e) {
|
|
|
50 |
Logger.log("Unable to load categories from Catalog.", this);
|
|
|
51 |
|
|
|
52 |
categories = null;
|
|
|
53 |
e.printStackTrace();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
Logger.log("Initialized category manager", this);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public static CategoryManager getCategoryManager(){
|
|
|
60 |
return categoryManager;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public Map<Long, Category> getCategories(){
|
|
|
64 |
return this.categories;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public String getCategoryLabel(long categoryId){
|
|
|
68 |
Category category = this.categories.get(categoryId);
|
|
|
69 |
if(category == null){
|
|
|
70 |
return null;
|
|
|
71 |
}
|
|
|
72 |
return category.getLabel();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public Category getCategory(long categoryId){
|
|
|
76 |
return this.categories.get(categoryId);
|
|
|
77 |
}
|
|
|
78 |
}
|