Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.metamodel.util;

import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.EntityState;
import in.shop2020.metamodel.util.CN;
import in.shop2020.storage.bdb.StorageManager;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * @author rajveer
 * @description 
 *
 */
public class CreationUtils {
        
        private static final String LEARNED_BULLETS = "LEARNED_BULLETS";
        private static final String DEFAULT_ENTITY_SCORES = "DEFAULT_ENTITY_SCORES";
        private static final String FACET_VALUES = "FACET_VALUES";
        private static final String SLIDE_SCORES = "SLIDE_SCORES";
        

        /**
         * 
         * @param aThrowable
         * @return
         */
        public static String getStackTrace(Throwable aThrowable) {
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(result);
            aThrowable.printStackTrace(printWriter);
            return result.toString();
        }
        
        /**
         * 
         * @param entity
         * @throws Exception
         */
        public static void storeLearnedBullets(Map<Long, List<ExpandedBullet>> learnedBullets) throws Exception {
                StorageManager.getStorageManager().storeDataObject(CreationUtils.LEARNED_BULLETS, learnedBullets);              
        }
        
        @SuppressWarnings("unchecked")
        public static Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception {
                return (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);               
        }
        
        @SuppressWarnings("unchecked")
        public static List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) throws Exception {
                Map<Long, List<ExpandedBullet>> learnedBullets = (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);
                if(learnedBullets==null){
                        return null;
                }
                return learnedBullets.get(featureDefinitionID);
        }

        public static void storeFacetValues(Map<Long, List<String>> facetIDFacetValues) throws Exception {
                StorageManager.getStorageManager().storeDataObject(CreationUtils.FACET_VALUES, facetIDFacetValues);
        }

        @SuppressWarnings("unchecked")
        public static Map<Long, List<String>>  getFacetValues() throws Exception {
                return (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
        }

        @SuppressWarnings("unchecked")
        public static List<String>  getFacetValues(long facetDefinitionID) throws Exception {
                Map<Long, List<String>> facetValues = (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
                return facetValues.get(facetDefinitionID);
        }

        
        public static void storeSlideScores( Map<Long, Map<Long, Integer>> slideScoresByEntity) throws Exception {
                StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES, slideScoresByEntity);
        }

        @SuppressWarnings("unchecked")
        public static Map<Long, Map<Long, Integer>> getSlideScores() throws Exception {
                return (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
        }

        @SuppressWarnings("unchecked")
        public static Map<Long, Integer> getSlideComparisonScores(long entityID) throws Exception{
                Map<Long, Map<Long, Integer>> slideScores = (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
                return slideScores.get(entityID);
        }
        
        
        public static void storeDefaultEntityScores(Map<Long, Integer> finalScoreByEntityID) throws Exception {
                StorageManager.getStorageManager().storeDataObject(CreationUtils.DEFAULT_ENTITY_SCORES, finalScoreByEntityID);
        }
        
        @SuppressWarnings("unchecked")
        public static Map<Long, Integer> getDefaultEntityScores() throws Exception {
                return (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
        }
        
        @SuppressWarnings("unchecked")
        public static Integer getEntityComparisonScores(long entityID) throws Exception {
                Map<Long, Integer> scoresMap =  (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
                Integer objScore = scoresMap.get(entityID);
                if(objScore==null){
                        return -1;
                }
                return objScore;
        }
        
        
        
        /**
         * Resolves Entity ID into Entity object
         * 
         * @param entityID
         * @return Entity
         * @throws Exception 
         */
        public static Entity getEntity(long entityID) throws Exception{
                return StorageManager.getStorageManager().getEntity(entityID);
        }
        
        public static void updateEntity(Entity entity) throws Exception {
                entity.reorderSlides(entity.getSlideSequence());
                StorageManager.getStorageManager().updateEntity(entity);
                CreationUtils.learn(entity);
        }

        public static void createEntity(Entity entity, EntityState entityMetadata) throws Exception {
                StorageManager.getStorageManager().createEntity(entity, entityMetadata);
        }

        public static void deleteEntity(long entityId) throws Exception  {
                StorageManager.getStorageManager().deleteEntity(entityId);
        }


        public static Map<Long, EntityState> getEntitiesState() throws Exception {
                return StorageManager.getStorageManager().getEntitiesMetadata();
        }
        
        public static EntityState getEntityState(long entityId) throws Exception {
                return StorageManager.getStorageManager().getEntityMetadata(entityId);
        }

        public static void updateEntityState(EntityState entityMetadata) throws Exception {
                StorageManager.getStorageManager().updateEntityMetadata(entityMetadata);
        }

        private static void learn(Entity entity) throws Exception{
                CN cn = new CN();
                cn.learn(entity);       
        }

        public static Map<Long, Entity> getEntities() throws Exception {
                return StorageManager.getStorageManager().getEntities();
        }

        public static Collection<Entity> getEntities(long categoryId) throws Exception {
                return StorageManager.getStorageManager().getEntitisByCategory(categoryId);
        }
        
}