Subversion Repositories SmartDukaan

Rev

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

/**
 * 
 */
package in.shop2020.metamodel.definitions;

import in.shop2020.metamodel.util.DBUtils;
import in.shop2020.metamodel.util.MM;

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

/**
 * @author naveen
 *
 */
public class DefinitionsContainer implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private Map<Long, Category> categories;
        private Map<Long, SlideDefinition> slideDefinitions;
        private Map<Long, FeatureDefinition> featureDefinitions;
        private Map<Long, DatatypeDefinition> datatypeDefinitions;
        private Map<Long, Unit> units;
        
        /**
         * 
         */
        public DefinitionsContainer() {
                // Lazy initialization
        }
        
        /**
         * 
         * @return Map
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, Unit> getUnits() throws Exception {            
                // De-serialize
                if(this.units == null) {
                        String dbFile = MM.DEFINITIONS_DB_PATH + "units" + ".ser";
                        
                        this.units = (Map<Long, Unit>) DBUtils.read(dbFile);
                }
                
                return this.units;
        }

        /**
         * 
         * @param unitID
         * @return Unit
         * @throws Exception 
         */
        public Unit getUnit(long unitID) throws Exception {             
                // Initialize 
                if(this.units == null) {
                        this.getUnits();
                }
                
                return this.units.get(new Long(unitID));
        }

        /**
         * @return the datatypeDefinitions
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, DatatypeDefinition> getDatatypeDefinitions() 
                throws Exception {
                
                // De-serialize
                if(this.datatypeDefinitions == null) {
                        String dbFile = MM.DEFINITIONS_DB_PATH + "datatypedefinitions" + ".ser";
                        
                        this.datatypeDefinitions = 
                                (Map<Long, DatatypeDefinition>) DBUtils.read(dbFile);
                }
                return this.datatypeDefinitions;
        }

        /**
         * 
         * @param unitID
         * @return Unit
         * @throws Exception 
         */
        public DatatypeDefinition getDatatypeDefinition(long id) throws Exception {
                
                // Initialize 
                if(this.datatypeDefinitions == null) {
                        this.getDatatypeDefinitions();
                }
                
                return this.datatypeDefinitions.get(new Long(id));
        }
        
        /**
         * @return the categories
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, Category> getCategories() throws Exception {
                
                // De-serialize
                if(this.categories == null) {
                        String dbFile = MM.DEFINITIONS_DB_PATH + "categories" + ".ser";
                        
                        this.categories = (Map<Long, Category>) DBUtils.read(dbFile);
                }
                return this.categories;
        }
        
        /**
         * 
         * @param categoryID
         * @return Category
         * @throws Exception 
         */
        public Category getCategory(long categoryID) throws Exception {
                
                // Initialize 
                if(this.categories == null) {
                        this.getCategories();
                }
                
                return this.categories.get(new Long(categoryID));
        }
        
        /**
         * 
         * @param categoryID
         * @return List<CategorySlideDefinition> 
         * @throws Exception 
         */
        public List<CategorySlideDefinition> getCategorySlideDefinitions(
                        long categoryID) throws Exception {
                Category category = this.getCategory(categoryID);
                
                return category.getCategorySlideDefintions();
        }
        
        /**
         * @return the slideDefinitions
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, SlideDefinition> getSlideDefinitions() throws Exception {
                
                // De-serialize
                if(this.slideDefinitions == null) {
                        String dbFile = MM.DEFINITIONS_DB_PATH + "slidedefinitions" + 
                                ".ser";
                        
                        this.slideDefinitions = 
                                (Map<Long, SlideDefinition>) DBUtils.read(dbFile);
                }
                return this.slideDefinitions;
        }
        
        /**
         * 
         * @param id
         * @return SlideDefinition
         * @throws Exception
         */
        public SlideDefinition getSlideDefinition(long id) throws Exception {
                
                // Initialize 
                if(this.slideDefinitions == null) {
                        this.getSlideDefinitions();
                }
                
                return this.slideDefinitions.get(new Long(id));
        }
        
        /**
         * 
         * @param categoryID
         * @param EditorialImportance imp
         * @return List<SlideDefinition>
         * @throws Exception 
         */
        public List<SlideDefinition> getSlides(long categoryID, 
                        EditorialImportance imp) 
                throws Exception {
                
                List<CategorySlideDefinition> catSlideDefs = 
                        this.getCategorySlideDefinitions(categoryID);
                
                Iterator<CategorySlideDefinition> itCatSlideDefs = 
                        catSlideDefs.iterator();
                
                List<SlideDefinition> slideDefs = new ArrayList<SlideDefinition>();
                while(itCatSlideDefs.hasNext()) {
                        CategorySlideDefinition catSlideDef = itCatSlideDefs.next();
                        if(catSlideDef.getEditorialImportance() == imp) {
                                long slideDefID = catSlideDef.getSlideDefintionID();
                                slideDefs.add(this.getSlideDefinition(slideDefID));
                        }
                }
                return slideDefs;
        }
        
        /**
         * 
         * @return Map<Long, FeatureDefinition>
         * @throws Exception
         */
        @SuppressWarnings("unchecked")
        public Map<Long, FeatureDefinition> getFeatureDefinitions() 
                throws Exception {
                
                // De-serialize
                if(this.featureDefinitions == null) {
                        String dbFile = MM.DEFINITIONS_DB_PATH + "featuredefinitions" + 
                                ".ser";
                        
                        this.featureDefinitions = 
                                (Map<Long, FeatureDefinition>) DBUtils.read(dbFile);
                }
                return this.featureDefinitions;
        }

        /**
         * 
         * @param id
         * @return
         * @throws Exception
         */
        public FeatureDefinition getFeatureDefinition(long id) throws Exception {
                
                // Initialize 
                if(this.featureDefinitions == null) {
                        this.getFeatureDefinitions();
                }
                
                return this.featureDefinitions.get(new Long(id));
        }       
}