Subversion Repositories SmartDukaan

Rev

Rev 22 | Blame | Last modification | View Log | RSS feed

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

import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.util.CN;
import in.shop2020.metamodel.util.DBUtils;
import in.shop2020.metamodel.util.ExpandedEntity;

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

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

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        private Map<Long, Entity> entities;
        private Map<Long, List<Entity>> entitiesbycategory;
        
        /**
         * 
         */
        public EntityContainer() {
                // Lazy initialization
        }
        
        /**
         * 
         * @return Map
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, Entity> getEntities() throws Exception {
                // De-serialize
                if(this.entities == null) {
                        String entitiesDBFile = CN.CONTENT_DB_PATH + "entities" + ".ser";
                        String entitiesbycategoryDBFile = CN.CONTENT_DB_PATH + 
                                "entitiesbycategory" + ".ser";
                        
                        this.entities = (Map<Long, Entity>) DBUtils.read(entitiesDBFile);
                        this.entitiesbycategory = (Map<Long, List<Entity>>) 
                                DBUtils.read(entitiesbycategoryDBFile);
                }
                
                if(this.entities == null) {
                        // A sorted map to ensure sequence
                        this.entities = new TreeMap<Long, Entity>();
                        this.entitiesbycategory = new TreeMap<Long, List<Entity>>();
                }
                
                return this.entities;
        }
        
        /**
         * @return the entitiesbycategory
         * @throws Exception 
         */
        public Map<Long, List<Entity>> getEntitiesbycategory() throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                
                return this.entitiesbycategory;
        }

        /**
         * 
         * @param entityID
         * @return Entity
         * @throws Exception 
         */
        public Entity getEntity(long entityID) throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                
                return this.entities.get(new Long(entityID));
        }
        
        /**
         * 
         * @param categoryID
         * @return List<Entity>
         * @throws Exception 
         */
        public List<Entity> getEntities(long categoryID) throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                
                return this.entitiesbycategory.get(new Long(categoryID));
        }
        
        /**
         * 
         * @param newEntity
         * @throws Exception 
         */
        public void addEntity(Entity newEntity) throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                
                this.entities.put(new Long(newEntity.getID()), newEntity);
                
                // Keep index by category ID
                List<Entity> catentities = 
                        this.entitiesbycategory.get(newEntity.getCategoryID());
                
                if(catentities == null) {
                        catentities = new ArrayList<Entity>();
                }
                
                catentities.add(newEntity);
                this.entitiesbycategory.put(newEntity.getCategoryID(), catentities);
        }
        
        /**
         * 
         * @param entityID
         * @return ExpandedEntity 
         * @throws Exception 
         */
        public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
                Entity entity = this.getEntity(entityID);
                
                ExpandedEntity expEntity = new ExpandedEntity(entity);
                
                return expEntity;
        }
}