Subversion Repositories SmartDukaan

Rev

Rev 2071 | Rev 2551 | Go to most recent revision | Details | Compare with Previous | 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
	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
	        }
37
	        for(Category category: t_categories){
38
	            if(category.getParent_category_id() != 0){
39
	                if(category.getParent_category_id()!=0){
40
	                    List<Long> childCats = categories.get(category.getParent_category_id()).getChildren_category_ids();
41
	                    if(childCats==null){
42
	                        childCats = new ArrayList<Long>();
43
	                    }
44
	                    childCats.add(category.getId());
45
	                }
46
	             }
47
            }
48
		} catch (Exception e) {
49
			Logger.log("Unable to load categories from Catalog.", this);
50
 
51
			categories = null;
52
			e.printStackTrace();
53
		}
54
 
55
		Logger.log("Initialized category manager", this);
56
	}
57
 
58
	public static CategoryManager getCategoryManager(){
59
		return categoryManager;
60
	}
61
 
62
	public Map<Long, Category> getCategories(){
63
		return this.categories;		
64
	}
65
 
66
	public String getCategoryLabel(long categoryId){
67
		Category category = this.categories.get(categoryId);
68
		if(category == null){
69
			return null;
70
		}
71
		return category.getLabel();
72
	}
73
 
74
	public Category getCategory(long categoryId){
75
		return this.categories.get(categoryId);
76
	}
77
}