Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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