Rev 8865 | Rev 22647 | 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.core.ExpertReview;import in.shop2020.metamodel.core.ExpertReviewSource;import in.shop2020.metamodel.core.Feature;import in.shop2020.metamodel.core.Helpdoc;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.core.SpecialPage;import in.shop2020.storage.mongo.StorageManager;import in.shop2020.util.Utils;import java.io.File;import java.io.PrintWriter;import java.io.StringWriter;import java.io.Writer;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import com.google.gson.reflect.TypeToken;/*** All storage and retrival utility methods* @author rajveer**/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";private static final String CUSTOM_SLIDE_SCORES = "CUSTOM_SLIDE_SCORES";private static final String CONTENT_GENERATION_TIME = "CONTENT_GENERATION_TIME";private static final String INCONSISTENT_ENTITIES = "INCONSISTENT_ENTITIES";private static final String HELPDOC_ENTITYIDS = "HELPDOC_ENTITYIDS";private static final String RELATED_ACCESSORIES = "RELATED_ACCESSORIES";private static final String COMPARISON_STATS = "COMPARISON_STATS";private static final String SEARCH_STATS = "SEARCH_STATS";private static final String SYNONYMS = "SYNONYMS";private static final String EXPERT_REVIEWS = "EXPERT_REVIEWS";private static final String EXPERT_REVIEW_SOURCES = "EXPERT_REVIEW_SOURCES";private static long helpDocId;private static long specialPageId;/**** @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 storeInconsistentEntities(List<Long> inconsistentEntities) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.INCONSISTENT_ENTITIES, inconsistentEntities);}/**** @return* @throws Exception*/@SuppressWarnings("unchecked")public static List<Long> getInconsistentEntities() throws Exception {Type t = new TypeToken<List<Long>>() {}.getType();return (List<Long>) StorageManager.getStorageManager().getDataObject(CreationUtils.INCONSISTENT_ENTITIES,t);}/**** @param entity* @throws Exception*/public static void storeSearchStats(String date, List<String> searchStats) throws Exception {Map<String,List<String>> map = new HashMap<String, List<String>>();map.put(date, searchStats);StorageManager.getStorageManager().storeSearchStats(Long.parseLong(date), map);}/**** @param entity* @throws Exception*/public static void deleteSearchStats(String date) throws Exception {StorageManager.getStorageManager().deleteSearchStats(Long.parseLong(date));}/**** @return* @throws Exception*/public static List<String> getSearchStats(String date) throws Exception {return StorageManager.getStorageManager().getSearchStats(Long.parseLong(date));}/**** @param slideScoresByEntity* @throws Exception*/public static void storeSlideScores( Map<Long, Map<Long, Double>> slideScoresByEntity) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES, slideScoresByEntity);}/**** @param entityID* @return* @throws Exception*/@SuppressWarnings("unchecked")public static Map<Long, Double> getSlideComparisonScores(long entityID) throws Exception{Type t = new TypeToken<Map<Long, Map<Long,Double>>>() {}.getType();return ((Map<Long, Map<Long,Double>>)StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES, t)).get(entityID);}/**** @param entityID* @return* @throws Exception*/@SuppressWarnings("unchecked")public static Map<Long, Double> getCustomSlideComparisonScores(long entityID) throws Exception{return StorageManager.getStorageManager().getCustomSlideScores(entityID);}/**** @param entityID* @return* @throws Exception*/public static void storeCustomSlideComparisonScores(long entityID, Map<Long, Double> customSlideScores) throws Exception{StorageManager.getStorageManager().storeCustomSlideScores(entityID, customSlideScores);}/**** @return* @throws Exception*/public static Long getLastContentGenerationTime() throws Exception {return (Long) StorageManager.getStorageManager().getDataObject(CreationUtils.CONTENT_GENERATION_TIME, Long.class);}/**** @param storageTime* @throws Exception*/public static void storeLastContentGenerationTime(Long storageTime) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.CONTENT_GENERATION_TIME, storageTime);}/*** 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);}/**** @param entity* @throws Exception*/public static void updateEntity(Entity entity) throws Exception {//FIXME This should not happen here.entity.reorderSlides(entity.getSlideSequence());//CreationUtils.learn(entity);StorageManager.getStorageManager().updateEntity(entity);}/**** @param entity* @param entityMetadata* @throws Exception*/public static void createEntity(Entity entity, EntityState entityMetadata) throws Exception {/** Creating media directory by default. Even if there is no media uploaded yet.*/String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + entity.getID();File mediaDir = new File(mediaDirPath);if(!mediaDir.exists()) {mediaDir.mkdir();}StorageManager.getStorageManager().createEntity(entity, entityMetadata);}/**** @param entityId* @throws Exception*/public static void deleteEntity(long entityId) throws Exception {StorageManager.getStorageManager().deleteEntity(entityId);}/**** @return* @throws Exception*/public static Map<Long, EntityState> getEntitiesState() {return StorageManager.getStorageManager().getEntitiesMetadata();}/**** @param entityId* @return* @throws Exception*/public static EntityState getEntityState(long entityId) {return StorageManager.getStorageManager().getEntityMetadata(entityId);}/**** @param entityMetadata* @throws Exception*/public static void updateEntityState(EntityState entityMetadata) {StorageManager.getStorageManager().updateEntityMetadata(entityMetadata);}/**** @param entity* @throws Exception*/private static void learn(Entity entity) throws Exception{CN cn = new CN();cn.learn(entity);cn.learnHelpdocs(entity);}/**** @return* @throws Exception*/public static Map<Long, Entity> getEntities() throws Exception {return StorageManager.getStorageManager().getEntities();}/**** @param categoryId* @return* @throws Exception*/public static Collection<Entity> getEntities(long categoryId) throws Exception {return StorageManager.getStorageManager().getEntitisByCategory(categoryId);}public static void storeHelpdoc(Helpdoc helpdoc) throws Exception {StorageManager.getStorageManager().updateHelpdoc(helpdoc);}public static Helpdoc getHelpdoc(Long helpdocId) throws Exception {return StorageManager.getStorageManager().getHelpdoc(helpdocId);}public static Map<Long, Helpdoc> getHelpdocs() throws Exception {return StorageManager.getStorageManager().getHelpdocs();}public static void deleteHelpdoc(Long helpdocId) throws Exception {StorageManager.getStorageManager().deleteHelpdoc(helpdocId);}public static long getNewHelpdocId() {if(helpDocId == 0){helpDocId = 200000;Map<Long, Helpdoc> helpdocs = StorageManager.getStorageManager().getHelpdocs();if(helpdocs != null){for(long id : StorageManager.getStorageManager().getHelpdocs().keySet()){if(helpDocId <= id){helpDocId = id;}}}}return ++helpDocId;}/**** @param slide* @param featureDefinitionID* @return Feature*/public static 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 = CreationUtils.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 static Slide getSlide(long entityID, long slideDefinitionID)throws Exception {Entity entity = CreationUtils.getEntity(entityID);List<Slide> slides = entity.getSlides();Slide resultSlide = null;if(slides != null) {for(Slide slide : slides) {if(slide.getSlideDefinitionID() == slideDefinitionID) {return slide;}resultSlide = CreationUtils.getSlide(slide, slideDefinitionID);if(resultSlide == null) {continue;}else {break;}}}return resultSlide;}/**** @param slide* @param slideDefinitionID* @return*/public static 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 = CreationUtils.getSlide(childSlide, slideDefinitionID);if(resultSlide == null) {continue;}else {break;}}}return resultSlide;}/*** Utility method to find out Feature object in Entity instance** @param entityID* @param featureDefinitionID* @return Feature* @throws Exception*/public static Feature getFeature(long entityID, long featureDefinitionID)throws Exception {Entity entity = CreationUtils.getEntity(entityID);Feature feature = null;List<Slide> slides = entity.getSlides();for(Slide slide : slides) {feature = CreationUtils.getFeature(slide, featureDefinitionID);// Until all slides are searchedif(feature == null) {continue;}else {break;}}return feature;}/*** Returns expand form of entity object. All references are resolved into* corresponding detail object** @param entityID* @return ExpandedEntity* @throws Exception*/public static ExpandedEntity getExpandedEntity(long entityID) throws Exception {Entity entity = CreationUtils.getEntity(entityID);if(entity==null){System.out.println("Entity is null");return null;}System.out.println( entity.getCategoryID());ExpandedEntity expEntity = new ExpandedEntity(entity);return expEntity;}/*** Returns first parent slide with matching label** @param expEntity2* @param slideLabel* @return Null if not found*/public static 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;}/*** Special page ids start from 300000. Here, we pick the last one used and return* a value one more than it.*/public static long getNewSpecialPageId() {if (specialPageId == 0) {specialPageId = StorageManager.getStorageManager().getMaxPageId();if(specialPageId == 0) {specialPageId = 300000;}}return ++specialPageId;}/*** Updates a given special page object in database.*/public static void storeSpecialPage(SpecialPage specialPage) {StorageManager.getStorageManager().updateSpecialPage(specialPage);}/*** Looks up a special page given its Id from database and returns the same.*/public static SpecialPage getSpecialPage(long specialPageId) {return StorageManager.getStorageManager().getSpecialPage(specialPageId);}/*** Returns all special pages in the database.*/public static Map<Long, SpecialPage> getSpecialPages() {return StorageManager.getStorageManager().getSpecialPages();}/*** delete the special pages from database.*/public static void deleteSpecialPage(long specialPageId) {StorageManager.getStorageManager().deleteSpecialPage(specialPageId);}/**** @param Synonym that contains map of brand/category name as keys and their synonyms as values.* @throws Exception*/public static List<ExpertReview> getExpertReviewByEntity(long entityId) throws Exception {return StorageManager.getStorageManager().getExpertReviewByEntity(entityId);}public static Set<ExpertReviewSource> getExpertReviewSources() throws Exception {return StorageManager.getStorageManager().getExpertReviewSources();}public static void addExpertReviewSource(ExpertReviewSource expertReviewSource) throws Exception {StorageManager.getStorageManager().storeExpertReviewSource(expertReviewSource);}public static void deleteExpertReviewSource(ExpertReviewSource expertReviewSource) throws Exception {StorageManager.getStorageManager().deleteExpertReviewSource(expertReviewSource);}public static void storeExpertReview(long entityId, List<ExpertReview> expertReviews) throws Exception {StorageManager.getStorageManager().storeExpertReview(entityId, expertReviews);}public static void storeExpertReview(long entityId, ExpertReview expertReview) throws Exception {StorageManager.getStorageManager().storeExpertReview(entityId, expertReview);}public static void deleteExpertReview(long entityId, int index) throws Exception {StorageManager.getStorageManager().deleteExpertReview(entityId, index);}public static Map<Long, List<ExpertReview>> getExpertReviews() throws Exception {return StorageManager.getStorageManager().getExpertReviews();}/**** @param Synonym that contains map of brand/category name as keys and their synonyms as values.* @throws Exception*/public static void deleteExpertReviews(long entityId) throws Exception {StorageManager.getStorageManager().deleteExpertReview(entityId);}/**** @return* @throws Exception*/@SuppressWarnings("unchecked")public static Map<Long, Map<Long, Long>> getComparisonStats() throws Exception {Type t = new TypeToken<Map<Long, Map<Long, Long>>>() {}.getType();return (Map<Long, Map<Long, Long>>) StorageManager.getStorageManager().getDataObject(CreationUtils.COMPARISON_STATS, t);}/**** @param entity* @throws Exception*/public static void storeComparisonStats(Map<Long, Map<Long, Long>> comparisonStats) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.COMPARISON_STATS, comparisonStats);}public static Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception {return StorageManager.getStorageManager().getLearnedBullets();}/**** @param entity* @throws Exception*/public static void storeLearnedBullets(Map<Long, List<ExpandedBullet>> learnedBullets) throws Exception {StorageManager.getStorageManager().storeLearnedBullets(learnedBullets);}/**** @param featureDefinitionID* @return* @throws Exception*/public static List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) throws Exception {Set<ExpandedBullet> expBullets = StorageManager.getStorageManager().getLearnedBulletsByFeatureId(featureDefinitionID);if(expBullets == null) return null;return new ArrayList<ExpandedBullet>(expBullets);}/**** @param entity* @throws Exception*/public static void storeHelpdocEntityIds(Map<Long, List<Long>> helpdocEntityIds) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.HELPDOC_ENTITYIDS, helpdocEntityIds);}/**** @return* @throws Exception*/public static Map<Long,List<Long>> getHelpdocEntityIds() throws Exception {return StorageManager.getStorageManager().getHelpdocEntityIds();}/**** @return* @throws Exception*/@SuppressWarnings("unchecked")public static Map<Long, Map<Long, List<Long>>> getRelatedAccessories() throws Exception {Type t = new TypeToken<Map<Long, Map<Long, List<Long>>>>() {}.getType();return (Map<Long, Map<Long, List<Long>>>) StorageManager.getStorageManager().getDataObject(CreationUtils.RELATED_ACCESSORIES, t);}/**** @param entity* @throws Exception*/public static void storeRelatedAccessories(Map<Long, Map<Long, List<Long>>> relatedAccessories) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.RELATED_ACCESSORIES, relatedAccessories);}/**** @param Synonym that contains map of brand/category name as keys and their synonyms as values.* @throws Exception*/@SuppressWarnings("unchecked")public static Map<String, Map<String, String>> getSynonyms() throws Exception {Type t = new TypeToken<Map<String, Map<String, String>>>() {}.getType();return (Map<String, Map<String,String>>)StorageManager.getStorageManager().getDataObject(CreationUtils.SYNONYMS, t);}/**** @param Synonym that contains map of brand/category name as keys and their synonyms as values.* @throws Exception*/public static void storeSynonyms(Map<String, Map<String, String>> synonyms) throws Exception {StorageManager.getStorageManager().storeDataObject(CreationUtils.SYNONYMS, synonyms);}}