Rev 80 | Blame | Last modification | View Log | RSS feed
/****/package in.shop2020.metamodel.definitions;import in.shop2020.metamodel.core.Bullet;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.Feature;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.util.ExpandedEntity;import in.shop2020.util.DBUtils;import in.shop2020.util.Utils;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 EntityContainer 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<Bullet>> learnedBullets;/*** Instantiates required data structures only when needed*/public EntityContainer() {// Lazy initialization}/*** Returns all stored Entity objects** @return Map* @throws Exception*/@SuppressWarnings("unchecked")public Map<Long, Entity> getEntities() throws Exception {// De-serializeif(this.entities == null) {String entitiesDBFile = Utils.ENTITIES_DB_PATH + "entities.ser";String entitiesbycategoryDBFile = Utils.ENTITIES_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 sequencethis.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<Bullet>>* @throws Exception*/@SuppressWarnings("unchecked")public Map<Long, List<Bullet>> getLearnedBullets() throws Exception {// De-serializeif(this.learnedBullets == null) {String dbFile = Utils.ENTITIES_DB_PATH + "learnedbullets.ser";this.learnedBullets =(Map<Long, List<Bullet>>) DBUtils.read(dbFile);}if(this.learnedBullets == null) {this.learnedBullets = new HashMap<Long, List<Bullet>>();}return this.learnedBullets;}/*** Resolves Feature Definition ID into list of learned Bullet objects** @param featureDefinitionID* @return* @throws Exception*/public List<Bullet> 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();}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));}/*** Convienience 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 IDList<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);}/*** 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);ExpandedEntity expEntity = new ExpandedEntity(entity);return expEntity;}/*** 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 searchedif(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;}}