Subversion Repositories SmartDukaan

Rev

Rev 2298 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
	@SuppressWarnings("unchecked")
28
	private CategoryManager(){
29
	    Logger.log("Initializing category manager", this);
30
		categories = new HashMap<Long, Category>();
31
		try {
32
		    CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
33
	        Client client = catalogServiceClient.getClient();
34
	        List<Category> t_categories =  client.getAllCategories();
35
	        for(Category category: t_categories){
36
	            categories.put(category.getId(), category);
37
	        }
38
	        for(Category category: t_categories){
39
	            if(category.getParent_category_id() != 0){
40
	                if(category.getParent_category_id()!=0){
41
	                    List<Long> childCats = categories.get(category.getParent_category_id()).getChildren_category_ids();
42
	                    if(childCats==null){
43
	                        childCats = new ArrayList<Long>();
44
	                    }
45
	                    childCats.add(category.getId());
46
	                }
47
	             }
48
            }
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
}