Subversion Repositories SmartDukaan

Rev

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

/**
 * 
 */
package in.shop2020.creation.util;

import in.shop2020.metamodel.core.Entity;
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.util.DBUtils;
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.MEDIA_DB_PATH + entityID + ".ser";
                return (Map<String, Media>)DBUtils.read(dbFile);
        }
        
        /**
         * 
         * @param entityID
         * @param label
         * @throws Exception
         */
        public static void removeMedia(long entityID, String label) 
                throws Exception {
                String dbFile = Utils.MEDIA_DB_PATH + 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.ENTITIES_DB_PATH + 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.ENTITIES_DB_PATH + 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);
                
                return getSlideSequence(catSlides);
        }
        
        /**
         * 
         * @param entityID
         * @return
         */
        public static List<Long> getSlideSequence(long entityID, long categoryID) throws Exception {
                Map<Long, List<Long>> catSlides = getRawSlideSequence(entityID);
                
                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);
                
                for(String label : labelIDMap.keySet()){
                        orderedSlideDefIDs.add(labelIDMap.get(label));
                        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.ENTITIES_DB_PATH + "entities.ser";
                
                // Remove existing
                DBUtils.delete(entityDBFile);
                
                DBUtils.store(entities, entityDBFile);
                
                String entitiesbycategoryDBFile = Utils.ENTITIES_DB_PATH + 
                        "entitiesbycategory.ser";
        
                // Remove existing
                DBUtils.delete(entitiesbycategoryDBFile);

                DBUtils.store(entitiesByCategory, entitiesbycategoryDBFile);            
        }
        
        /**
         * 
         * @param entityID
         * @return
         * @throws Exception
         */
        public static Entity getEntity(long entityID) throws Exception {
                String entityDBFile = Utils.ENTITIES_DB_PATH + entityID + 
                        "/entity.ser";
                
                return (Entity)DBUtils.read(entityDBFile);
        }
        
        /**
         * 
         * @param entity
         * @throws Exception
         */
        public static void storeEntity(Entity entity) throws Exception {
                String entityDBDir = Utils.ENTITIES_DB_PATH + 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.ENTITIES_DB_PATH + 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.ENTITIES_DB_PATH + "/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.ENTITIES_DB_PATH + 
                                        "/incomplete.ser";
                                DBUtils.store(incompleteList, incompleteFileName);
                        }
                }
        }
        
        /**
         * 
         * @return
         * @throws Exception 
         */
        @SuppressWarnings("unchecked")
        public static List<Entity> getIncomplete() throws Exception {
                String incompleteFileName = Utils.ENTITIES_DB_PATH + "/incomplete.ser";

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