Subversion Repositories SmartDukaan

Rev

Rev 512 | 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.Media;
import in.shop2020.metamodel.core.Slide;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.SlideDefinition;
import in.shop2020.metamodel.util.CN;
import in.shop2020.util.DBUtils;
import in.shop2020.util.IR;
import in.shop2020.util.Utils;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

/**
 * @author naveen
 *
 */
public class CreationUtils {

        /**
         * 
         */
        private static Log log = LogFactory.getLog(CreationUtils.class);
        
        /**
         * 
         * @param entityID
         * @return
         * @throws Exception 
         */
        public static List<String> getMediaLabels(long entityID) throws Exception {
                Map<String, Media> rawMedia = getRawMedia(entityID);
                
                List<String> labels = new ArrayList<String>();
                for(Media media : rawMedia.values()) {
                        labels.add(media.getLabel());
                }
                
                return labels;
        }
        
        /**
         * 
         * @param entityID
         * @param type
         * @return
         * @throws Exception
         */
        public static List<String> getMediaLabels(long entityID, String type) 
                throws Exception {
                Map<String, Media> rawMedia = getRawMedia(entityID);
                
                List<String> labels = new ArrayList<String>();
                for(Media media : rawMedia.values()) {
                        if(media.getType().equals(type)) {
                                labels.add(media.getLabel());
                        }
                }
                
                return labels;
        }
        
        /**
         * 
         * @param entityID
         * @return
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public static Map<String, Media> getRawMedia(long entityID) 
                throws Exception {
                String dbFile = Utils.CONTENT_DB_PATH + "media" + File.separator + entityID + ".ser";
                return (Map<String, Media>)DBUtils.read(dbFile);
        }
        
        /**
         * 
         * @param media
         * @throws Exception
         */
        public static void addMedia(long entityID,  Media media) throws Exception {
                Map<String, Media> rawMedia = CreationUtils.getRawMedia(entityID);
                
                if(rawMedia == null) {
                        rawMedia = new HashMap<String, Media>();
                }
                rawMedia.put(media.getLabel(), media);
                
                DBUtils.store(rawMedia, Utils.CONTENT_DB_PATH + "media" + File.separator + entityID + 
                                ".ser");
        }
        
        /**
         * 
         * @param entityID
         * @param label
         * @throws Exception
         */
        public static void removeMedia(long entityID, String label) 
                throws Exception {
                String dbFile = Utils.CONTENT_DB_PATH + "media" + File.separator + entityID + ".ser";
                
                Map<String, Media> media = getRawMedia(entityID);
                media.remove(label);
                
                DBUtils.store(media, dbFile);
        }
        /**
         * 
         * @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 entityID
                 * @param slideDefIDs
                 */
                public static void storeSlideSequence(long entityID, 
                                Map<Long, List<Long>> catSlideDefIDs) throws Exception {
                String entityDirPath = Utils.CONTENT_DB_PATH + "entities" + File.separator + entityID;
                log.info("entityDirPath:" + entityDirPath);
                
                File entityDir = new File(entityDirPath);
                if(!entityDir.exists()) {
                        entityDir.mkdir();
                }
                
                String slideSeqFileName = entityDirPath + "/slidesequence.ser";
                log.info("slideSeqFileName:" + slideSeqFileName);
                
                File slideSeqFile = new File(slideSeqFileName);
                if(slideSeqFile.exists()) {
                        slideSeqFile.delete();
                }

                slideSeqFile.createNewFile();
                
                DBUtils.store(catSlideDefIDs, slideSeqFileName);
        }
        
        /**
         * 
         * @param entityID
         * @return
         * @throws Exception
         */
        @SuppressWarnings("unchecked")
        public static Map<Long, List<Long>> getRawSlideSequence(long entityID) 
                throws Exception {
                String entityDirPath = Utils.CONTENT_DB_PATH + "entities" + File.separator + entityID;
                String slideSeqFileName = entityDirPath + "/slidesequence.ser";
                return (Map<Long, List<Long>>)DBUtils.read(slideSeqFileName);
        }
        
        /**
         * 
         * @param entityID
         * @return
         */
        public static List<Long> getSlideSequence(long entityID) throws Exception {
                Map<Long, List<Long>> catSlides = getRawSlideSequence(entityID);
                
                if(catSlides !=null &&  catSlides.containsKey(new Long(-1))){
                        return catSlides.get(new Long(-1));
                }else{
                        return getSlideSequence(catSlides);
                }

                
        }
        
        /**
         * 
         * @param entityID
         * @return
         */
        public static List<Long> getSlideSequence(long entityID, long categoryID) throws Exception {
                Map<Long, List<Long>> catSlides = getRawSlideSequence(entityID);
                
                if(catSlides !=null && catSlides.containsKey(new Long(-1))){
                        return catSlides.get(new Long(-1));
                }else{
                        return getOrderedSlideSequence(catSlides, categoryID);
                }
        }
        
        /**
         * 
         * @param catSlides
         * @return
         * @throws Exception
         */
        public static List<Long> getSlideSequence(Map<Long, List<Long>> catSlides) 
                throws Exception {
                if(catSlides == null) {
                        return null;
                }
                log.info(" IN GetSlideSequence" );
                List<Long> slideDefIDs = new ArrayList<Long>();
                
                for(Long catID : catSlides.keySet()) {
                        slideDefIDs.addAll(catSlides.get(catID));
                }

                return slideDefIDs;
        }
        
        /**
         * 
         * @param catSlides
         * @param categoryID
         * @return
         * @throws Exception
         */
        public static List<Long> getOrderedSlideSequence(Map<Long, List<Long>> catSlides, Long categoryID) 
                throws Exception {
                if(catSlides == null) {
                        return null;
                }
                
                List<Long> slideDefIDs = new ArrayList<Long>();
                List<Long> orderedSlideDefIDs = new ArrayList<Long>();
                
                DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
                List<Long> categorySlideSequence = defs.getCategorySlideSequence(categoryID);
                
                
                for(Long catID : catSlides.keySet()) {
                        slideDefIDs.addAll(catSlides.get(catID));
                }
                log.info("slideDefIDs:" + slideDefIDs);
                
                Map<String,Long> labelIDMap = new HashMap<String, Long>();
                for(long slideDefID : slideDefIDs){
                        labelIDMap.put(defs.getSlideDefinition(slideDefID).getLabel(), slideDefID);
                }
                log.info("labelIDMap:" + labelIDMap);
                
                for(Long slideID : categorySlideSequence){
                        String currentSlideLabel = defs.getSlideDefinition(slideID).getLabel();
                        if(labelIDMap.containsKey(currentSlideLabel)){
                                orderedSlideDefIDs.add(labelIDMap.get(currentSlideLabel));
                                labelIDMap.remove(currentSlideLabel);
                        }
                }
                log.info("orderedSlideDefIDs:" + orderedSlideDefIDs);
                
                List<String> removeLabels = new ArrayList<String>();
                
                for(String label : labelIDMap.keySet()){
                        orderedSlideDefIDs.add(labelIDMap.get(label));
                        removeLabels.add(label);
                }
                for(String label: removeLabels){
                        labelIDMap.remove(label);       
                }
                
                
                log.info(" orderedSlideDefIDs:" + orderedSlideDefIDs);
                return orderedSlideDefIDs;
        }
        
        /**
         * 
         * @param entities
         * @param entitiesByCategory
         * @throws Exception
         */
        public static void rewriteRepository(Map<Long, Entity> entities, 
                        Map<Long, List<Entity>> entitiesByCategory) throws Exception {

                String entityDBFile = Utils.CONTENT_DB_PATH + "entities" + File.separator + "entities.ser";
                
                // Remove existing
                DBUtils.delete(entityDBFile);
                
                DBUtils.store(entities, entityDBFile);
                
                String entitiesbycategoryDBFile = Utils.CONTENT_DB_PATH + "entities" + File.separator +  
                        "entitiesbycategory.ser";
        
                // Remove existing
                DBUtils.delete(entitiesbycategoryDBFile);

                DBUtils.store(entitiesByCategory, entitiesbycategoryDBFile);
                
                // Added automatic learning once someone rewrites repository
//              CN cn = new CN();
//              cn.learn();
                
                //update Solr data feed according to updated definition
                //FIXME - Commented for now . Will be looked into later.
                //long categoryID = 0L;
                //IR ir = new IR(categoryID);
                //ir.exportIRData();
                //ir.transformIrDataXMLtoSolrXML();
        }
        
        /**
         * 
         * @param entityID
         * @return
         * @throws Exception
         */
        public static Entity getEntity(long entityID) throws Exception {
                String entityDBFile = Utils.CONTENT_DB_PATH + "entities" + File.separator + entityID + 
                        "/entity.ser";
                
                return (Entity)DBUtils.read(entityDBFile);
        }
        
        /**
         * 
         * @param entity
         * @throws Exception
         */
        public static void storeEntity(Entity entity) throws Exception {
                String entityDBDir = Utils.CONTENT_DB_PATH + "entities" + File.separator + entity.getID();
                
                File entityDBDirFile = new File(entityDBDir);
                if(!entityDBDirFile.exists()) {
                        entityDBDirFile.mkdir();
                }
                
                String entityDBFile =  entityDBDir + "/entity.ser";
                
                File entityFile = new File(entityDBFile);
                if(entityFile.exists()) {
                        entityFile.delete();
                }
                else {
                        entityFile.createNewFile();
                }
                
                DBUtils.store(entity, entityDBFile);            
        }
        
        /**
         * 
         * @param entityID
         */
        public static void deleteEntity(long entityID) {
                String entityDBDir = Utils.CONTENT_DB_PATH + "entities" + File.separator + entityID;
                String entityDBFile = entityDBDir + "/entity.ser";

                File entityFile = new File(entityDBFile);
                if(entityFile.exists()) {
                        entityFile.delete();
                }
                
                String slideSeqFileName = entityDBDir + "/slidesequence.ser";
                File slideSeqFileNameFile = new File(slideSeqFileName);
                if(slideSeqFileNameFile.exists()) {
                        slideSeqFileNameFile.delete();
                }
                
                File entityDBDirFile = new File(entityDBDir);
                if(entityDBDirFile.exists()) {
                        entityDBDirFile.delete();
                }
        }
        
        /**
         * 
         * @param entity
         * @throws Exception 
         */
        public static void addToIncomplete(Entity entity) throws Exception {
                List<Entity> incompleteList = getIncomplete();
                if(incompleteList == null) {
                        incompleteList = new ArrayList<Entity>();
                }
                
                if(!incompleteList.contains(entity)) {
                        String incompleteFileName = Utils.CONTENT_DB_PATH + "entities" + File.separator  + "/incomplete.ser";
                        File incompleteFile = new File(incompleteFileName);
                        if(!incompleteFile.exists()) {
                                incompleteFile.createNewFile();
                        }
                        
                        incompleteList.add(entity);
                        
                        DBUtils.store(incompleteList, incompleteFileName);
                }
        }
        
        /**
         * 
         * @param entity
         * @throws Exception 
         */
        public static void deleteFromIncomplete(Entity entity) throws Exception {               
                List<Entity> incompleteList = getIncomplete();
                
                if(incompleteList != null) {
                        if(incompleteList.contains(entity)) {
                                incompleteList.remove(entity);

                                String incompleteFileName = Utils.CONTENT_DB_PATH + "entities" + File.separator + 
                                        "/incomplete.ser";
                                DBUtils.store(incompleteList, incompleteFileName);
                        }
                }
        }
        
        /**
         * 
         * @return
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public static List<Entity> getIncomplete() throws Exception {
                String incompleteFileName = Utils.CONTENT_DB_PATH + "entities" + File.separator + "/incomplete.ser";

                File incompleteFile = new File(incompleteFileName);
                if(!incompleteFile.exists()) {
                        return null;
                }
                
                return (List<Entity>)DBUtils.read(incompleteFileName);
        }
        
}