Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.Feature;
import in.shop2020.metamodel.core.Slide;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedBullet;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.metamodel.util.ExpandedSlide;
import in.shop2020.util.DBUtils;
import in.shop2020.util.Utils;

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

/**
 * Single point access to stored shop2020 core objects
 * 
 * @author naveen
 *
 */
public class OldEntityContainer implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        /**
         * Hashtable of Entity ID to Entity object
         */
        private Map<Long, Entity> entities;
        
        /**
         * Hashtable of Category ID to list of Entity objects
         */
        private Map<Long, List<Entity>> entitiesbycategory;
        
        /**
         * Hashtable of Feature Definition ID to list of learned Bullet objects
         */
        private Map<Long, List<ExpandedBullet>> learnedBullets;
        
        /**
         * Hashtable of Facet Definition ID to list of unique facet values found 
         * across entities
         */
        private Map<Long, List<String>> facetDefinitionIDFacetValues;

        /**
         * Hashtable of Entity ID > Map(Slide ID > Score)
         */
        private Map<Long, Map<Long, Integer>> entityIDSlideIDsScore;
        
        /**
         * Hashtable of Entity ID > Final Score
         */
        private Map<Long, Integer> entityIDFinalScore;
        
        /**
         * Database path. Just in case we want to override
         */
        private String dbPath =  Utils.CONTENT_DB_PATH;
        
        /**
         * Instantiates required data structures only when needed
         */
        public OldEntityContainer() {
                // Lazy initialization
        }

        public OldEntityContainer(String dbPath) {
                this.dbPath = dbPath;
                // Lazy initialization
        }
        
        /**
         * Returns all stored Entity objects 
         * 
         * @return Map
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, Entity> getEntities() throws Exception {
                // De-serialize
                if(this.entities == null) {
                        String entitiesDBFile = this.dbPath + "entities" + File.separator + "entities.ser";
                        String entitiesbycategoryDBFile = this.dbPath + "entities" + File.separator + 
                                "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;
        }
        
        /**
         * Returns hashtable of list bullet objects to feature definition ID
         * 
         * @return Map<Long, List<ExpandedBullet>> 
         * @throws Exception
         */
        @SuppressWarnings("unchecked")
        public Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception{
                // De-serialize
                if(this.learnedBullets == null) {
                        String dbFile = this.dbPath + "entities" + File.separator + "learnedbullets.ser";
                        
                        this.learnedBullets = 
                                (Map<Long, List<ExpandedBullet>>) DBUtils.read(dbFile);
                }
                
                if(this.learnedBullets == null) {
                        this.learnedBullets = new HashMap<Long, List<ExpandedBullet>>();
                }
                
                return this.learnedBullets;
        }

        /**
         * Resolves Feature Definition ID into list of learned Bullet objects
         * 
         * @param featureDefinitionID
         * @return list of ExpandedBullets
         * @throws Exception
         */
        public List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) 
                throws Exception {
                if(this.learnedBullets == null) {
                        this.getLearnedBullets();
                }
                
                return this.learnedBullets.get(new Long(featureDefinitionID));
        }
        
        /** 
         * Returns hashtable of list entities to category ID
         * 
         * @return the entitiesbycategory Entities by category
         * @throws Exception 
         */
        public Map<Long, List<Entity>> getEntitiesbyCategory() throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                String entitiesbycategoryDBFile = this.dbPath + "entities" + File.separator + 
                "entitiesbycategory.ser";
                this.entitiesbycategory = (Map<Long, List<Entity>>)DBUtils.read(entitiesbycategoryDBFile);
                
                
                return this.entitiesbycategory;
        }

        /**
         * Resolves Entity ID into Entity object
         * 
         * @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));
        }
        
        /**
         * Returns list of entities for a category ID
         * 
         * @param categoryID
         * @return List<Entity>
         * @throws Exception 
         */
        public List<Entity> getEntities(long categoryID) throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                //Utils.info("this.entitiesbycategory=" + this.entitiesbycategory);
                
                return this.entitiesbycategory.get(new Long(categoryID));
        }
        
        /**
         * Convenience method to add new Entity 
         * 
         * @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(new Long(newEntity.getCategoryID()), 
                                catentities);
        }

        
        /**
         * Convenience method to add new Entity 
         * 
         * @param newEntity
         * @throws Exception 
         */
        public void updateEntity(Entity newEntity) throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                
                this.entities.remove(new Long(newEntity.getID()));
                
                // Keep index by category ID
                List<Entity> catentities = 
                        this.entitiesbycategory.get(newEntity.getCategoryID());
                
                // As it is the reference, remove(obj) should work
                catentities.remove(newEntity);
                
                this.addEntity(newEntity);
        }
        
        /**
         * Convenience method to remove an Entity 
         * 
         * @param newEntity
         * @throws Exception 
         */
        public void deleteEntity(Entity newEntity) throws Exception {
                if(this.entities == null) {
                        this.getEntities();
                }
                
                this.entities.remove(new Long(newEntity.getID()));
                
                // Keep index by category ID
                List<Entity> catentities = 
                        this.entitiesbycategory.get(newEntity.getCategoryID());
                
                // As it is the reference, remove(obj) should work
                catentities.remove(newEntity);
        }
        
        /**
         * Returns expand form of entity object. All references are resolved into 
         * corresponding detail object
         * 
         * @param entityID
         * @return ExpandedEntity 
         * @throws Exception 
         */
        public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
                Entity entity = this.getEntity(entityID);
                // Changed by Rajveer to handle null value
                if(entity==null){
                        System.out.println("Entity is null");
                        return null;
                }
                System.out.println( entity.getCategoryID());
                ExpandedEntity expEntity = new ExpandedEntity(entity);
                
                return expEntity;
        }

        
        /**
         * Returns hashtable of list facet values to facet definition ID
         * 
         * @return
         * @throws Exception
         */
        @SuppressWarnings("unchecked")
        public Map<Long, List<String>> getFacetValues() throws Exception {
                // De-serialize
                if(this.facetDefinitionIDFacetValues == null) {
                        String dbFile = this.dbPath + "entities" + File.separator + "facetvalues.ser";
                        
                        this.facetDefinitionIDFacetValues = 
                                (Map<Long, List<String>>) DBUtils.read(dbFile);
                }
                
                if(this.facetDefinitionIDFacetValues == null) {
                        this.facetDefinitionIDFacetValues = new HashMap<Long, 
                                List<String>>();
                }
                
                return this.facetDefinitionIDFacetValues;
        }
        
        /**
         * Returns list of facet values for a facet definition ID
         * 
         * @param facetDefinitionID
         * @return List<String> 
         * @throws Exception
         */
        public List<String> getFacetValues(long facetDefinitionID) 
                throws Exception {
                this.getFacetValues();
                
                return this.facetDefinitionIDFacetValues.get(
                                new Long(facetDefinitionID));
        }
        
        /**
         * Utility method to find out Feature object in Entity instance
         * 
         * @param entityID
         * @param featureDefinitionID
         * @return Feature
         * @throws Exception 
         */
        public Feature getFeature(long entityID, long featureDefinitionID) 
                throws Exception {
                Entity entity = this.getEntity(entityID);
                
                Feature feature = null;
                
                List<Slide> slides = entity.getSlides();
                for(Slide slide : slides) {
                        feature = this.getFeature(slide, featureDefinitionID);
                        
                        // Until all slides are searched
                        if(feature == null) {
                                continue;
                        }
                        else {
                                break;
                        }
                }
                
                return feature;
        }
        
        /**
         * 
         * @param slide
         * @param featureDefinitionID
         * @return Feature
         */
        public Feature getFeature(Slide slide, long featureDefinitionID) {
                List<Feature> features = slide.getFeatures();
                
                if(features != null) {
                        for(Feature feature : features) {
                                if(feature.getFeatureDefinitionID() == featureDefinitionID) {
                                        return feature;
                                }
                        }
                }
                
                Feature feature = null;
                
                List<Slide> childrenSlides = slide.getChildrenSlides();
                if(childrenSlides != null) {
                        for(Slide childSlide : childrenSlides) {
                                
                                feature = this.getFeature(childSlide, featureDefinitionID);
                                if(feature == null) {
                                        continue;
                                }
                                else {
                                        break;
                                }
                        }
                }
                
                return feature;
        }
        
        /**
         * Utility method to find out Slide object in Entity instance
         * 
         * @param entityID
         * @param slideDefinitionID
         * @return
         * @throws Exception 
         */
        public Slide getSlide(long entityID, long slideDefinitionID) 
                throws Exception {
                Entity entity = this.getEntity(entityID);
                
                List<Slide> slides = entity.getSlides();
                
                Slide resultSlide = null;
                
                if(slides != null) {
                        for(Slide slide : slides) {

                                if(slide.getSlideDefinitionID() == slideDefinitionID) {
                                        return slide;
                                }
                                
                                resultSlide = this.getSlide(slide, slideDefinitionID);
                                
                                if(resultSlide == null) {
                                        continue;
                                }
                                else {
                                        break;
                                }
                        }
                }
                
                return resultSlide;
        }
        
        /**
         * 
         * @param slide
         * @param slideDefinitionID
         * @return
         */
        public Slide getSlide(Slide slide, long slideDefinitionID) {
                
                List<Slide> childrenSlides = slide.getChildrenSlides();
                
                Slide resultSlide = null;
                
                if(childrenSlides != null) {
                        for(Slide childSlide : childrenSlides) {
                                if(childSlide.getSlideDefinitionID() == slideDefinitionID) {
                                        return childSlide;
                                }
                                
                                resultSlide = this.getSlide(childSlide, slideDefinitionID);
                                if(resultSlide == null) {
                                        continue;
                                }
                                else {
                                        break;
                                }
                        }
                }
                
                return resultSlide;
        }
        
        /**
         * Returns list of borrowed slides
         * 
         * @param entity
         * @return list of borrowed slides
         */
        public List<Slide> getBorrowedSlides(Entity entity) {
                List<Slide> borrowedSlides = new ArrayList<Slide>();
                
                List<Slide> slides = entity.getSlides();
                
                for(Slide slide : slides) {
                        if(CreationUtils.isBorrowedSlide(slide.getSlideDefinitionID(), entity.getCategoryID())) {
                                borrowedSlides.add(slide);
                        }
                }
                
                return borrowedSlides;
        }
        
        /**
         * Returns first parent slide with matching label
         * 
         * @param expEntity2
         * @param slideLabel
         * @return Null if not found
         */
        public ExpandedSlide getParentExpandedSlide(ExpandedEntity expEntity2,
                        String slideLabel) {
                
                ExpandedSlide matchingSlide = null;
                List<ExpandedSlide> expSlides = expEntity2.getExpandedSlides();
                
                for(ExpandedSlide expSlide : expSlides) {
                        if(expSlide.getSlideDefinition().getLabel().equals(slideLabel)) {
                                matchingSlide = expSlide;
                                break;
                        }
                }
                
                return matchingSlide;
        }
        
        /**
         * Returns per slide comparison score for all entities in DB
         * 
         * @return Map<Long, Map<Long, Integer>> Entity ID > Map(Slide ID > Score)
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, Map<Long, Integer>> getEntitySlideComparisonScores() 
                throws Exception {
                // De-serialize
                if(this.entityIDSlideIDsScore == null) {
                        String dbFile = this.dbPath + "comparisons" + File.separator + "slidescores.ser";
                        
                        this.entityIDSlideIDsScore = 
                                (Map<Long, Map<Long, Integer>>) DBUtils.read(dbFile);
                }
                
                if(this.entityIDSlideIDsScore == null) {
                        this.entityIDSlideIDsScore = new HashMap<Long, 
                                Map<Long, Integer>>();
                }
                
                return this.entityIDSlideIDsScore;
        }
        
        /**
         * Returns final comparison score for all entities in DB
         * 
         * @return Map<Long, Integer> Entity ID > Final Score
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public Map<Long, Integer> getEntityComparisonScores() throws Exception {
                // De-serialize
                if(this.entityIDFinalScore == null) {
                        String dbFile = this.dbPath + "comparisons" + File.separator + 
                                "defaultentityscores.ser";
                        
                        this.entityIDFinalScore = 
                                (Map<Long, Integer>) DBUtils.read(dbFile);
                }
                
                if(this.entityIDFinalScore == null) {
                        this.entityIDFinalScore = new HashMap<Long, Integer>();
                }
                
                return this.entityIDFinalScore;
        }
        
        /**
         * Returns slide wise comparison scores for an entity
         * 
         * @param entityID
         * @return Map<Long, Integer> Slide ID to Comparison score, Null if no data
         * @throws Exception
         */
        public Map<Long, Integer> getSlideComparisonScores(long entityID) 
                throws Exception {
                this.getEntitySlideComparisonScores();
                
                return this.entityIDSlideIDsScore.get(new Long(entityID));
        }
        
        /**
         * 
         * @param entityID
         * @return int Comparison score
         * @throws Exception
         */
        public int getEntityComparisonScore(long entityID) throws Exception {
                this.getEntityComparisonScores();
                
                Integer objScore = this.entityIDFinalScore.get(new Long(entityID));
                if(objScore != null) {
                        return objScore.intValue();
                }
                
                return -1;
        }
}