Subversion Repositories SmartDukaan

Rev

Rev 2551 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.shop2020.model.v1.catalog.Category;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.thrift.clients.CatalogClient;

public class CategoryManager implements Serializable{

        private static final long serialVersionUID = 1L;

        private static CategoryManager categoryManager;
        
        private Map<Long, Category> categories;
        
        static{
                synchronized(CategoryManager.class){
                        categoryManager = new CategoryManager();
                }
        }
        
        private CategoryManager(){
            Logger.log("Initializing category manager", this);
                categories = new HashMap<Long, Category>();
                try {
                    CatalogClient catalogServiceClient = new CatalogClient();
                Client client = catalogServiceClient.getClient();
                List<Category> t_categories =  client.getAllCategories();
                for(Category category: t_categories){
                    categories.put(category.getId(), category);
                }
                System.out.println(categories);
                for(Category category: t_categories){
                    if(category.getParent_category_id() != 0){
                    List<Long> childCats = categories.get(category.getParent_category_id()).getChildren_category_ids();
                    if(childCats==null){
                        childCats = new ArrayList<Long>();
                        categories.get(category.getParent_category_id()).setChildren_category_ids(childCats);
                    }
                    childCats.add(category.getId());
                     }
            }
                System.out.println(categories);
                } catch (Exception e) {
                        Logger.log("Unable to load categories from Catalog.", this);
                        
                        categories = null;
                        e.printStackTrace();
                }
                
                Logger.log("Initialized category manager", this);
        }
        
        public static CategoryManager getCategoryManager(){
                return categoryManager;
        }
        
        public Map<Long, Category> getCategories(){
                return this.categories;         
        }
        
        public String getCategoryLabel(long categoryId){
                Category category = this.categories.get(categoryId);
                if(category == null){
                        return null;
                }
                return category.getLabel();
        }

        public Category getCategory(long categoryId){
                return this.categories.get(categoryId);
        }
}