Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

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

import in.shop2020.metamodel.definitions.Category;
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
import in.shop2020.metamodel.definitions.EditorialImportance;
import in.shop2020.metamodel.definitions.Unit;
import in.shop2020.metamodel.jaxb.CategorySlideDefinitionType;
import in.shop2020.metamodel.jaxb.CategoryType;
import in.shop2020.metamodel.jaxb.DefinitionType;
import in.shop2020.metamodel.jaxb.EditorialImportanceType;
import in.shop2020.metamodel.jaxb.UnitType;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author naveen
 *
 */
public class MM {
        public static final String DEFINITIONS_SRC_PATH = "/home/naveen/workspace/eclipse/webapp/src/xml/model/";
        public static final String DEFINITIONS_DB_PATH = "/home/naveen/workspace/eclipse/webapp/db/definitions/";
        public static final String ENTITIES_DB_PATH = "/home/naveen/workspace/eclipse/webapp/db/entities/";
        
        private String definitionSetName;
        private String srcFile;
        private String dbFile;
        
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
                System.out.println("MM "+ args[0] + " " + args[1]);
                
                String[] definitionSetNames = new String[] {"units", "datatypedefinitions", "bulletdefinitions", 
                                "featuredefinitions", "slidedefinitions", "categories"};
                
                String[] commands = new String[] {"show", "import"};

                String usage = "Usage: MM ["+ StringUtils.join(commands, "|") +"] ["+ StringUtils.join(definitionSetNames, "|") +"]";
                if(args.length < 2) {
                        System.out.println(usage);
                        System.exit(-1);
                }
                String inputCommand = args[0];
                String inputDefinitionSet = args[1];
                
                if(!ArrayUtils.contains(commands, inputCommand)) {
                        System.out.println(usage);
                        System.exit(-1);
                }
                
                if(!ArrayUtils.contains(definitionSetNames, inputDefinitionSet)) {
                        System.out.println(usage);
                        System.exit(-1);
                }
                
                MM mm = new MM(inputDefinitionSet);
                if(inputCommand.equals("import")) {
                        mm.importDefinitionSet();
                }
                else if(inputCommand.equals("show")) {
                        mm.showDefinitionSet();
                }
        }
        
        /**
         * 
         * @param definitionSet
         */
        public MM(String definitionSetName) {
                this.definitionSetName = definitionSetName;
                this.srcFile = DEFINITIONS_SRC_PATH + this.definitionSetName + ".xml";
                this.dbFile = DEFINITIONS_DB_PATH + this.definitionSetName + ".ser";
        }
        
        /**
         * 
         */
        @SuppressWarnings("unchecked")
        public void importDefinitionSet() throws Exception {
                System.out.println("importDefinitionSet()");
                
                DefinitionType defType = this.unmarshallSrcFile(this.srcFile);
                
                // Units
                if(this.definitionSetName.equals("units")) {
                        Map unitsMap = this.convertUnits(defType);
                        
                        if(unitsMap != null) {
                                this.store(unitsMap, this.dbFile);
                        }
                }
                
                // Categories
                else if(this.definitionSetName.equals("categories")) {
                        Map<Long, Category> categoriesMap = new HashMap<Long, Category>();
                        Category root = this.convertCategories(defType, categoriesMap);
                        //System.out.println(categoriesMap);
                        //System.out.println(root);
                        //System.exit(0);
                        
                        // Tree
                        String categoryTreeDBFile = DEFINITIONS_DB_PATH + "categorytree" + ".ser";
                        if(root != null) {
                                this.store(root, categoryTreeDBFile);
                        }
                        
                        // Map
                        if(categoriesMap != null) {
                                this.store(categoriesMap, this.dbFile);
                        }
                }
        }

        /**
         * 
         */
        public void showDefinitionSet() throws Exception {
                System.out.println("showDefinitionSet()");
                
                Object obj = this.read(this.dbFile);
                System.out.println(obj);
                
                if(this.definitionSetName.equals("categories")) {
                        String categoryTreeDBFile = DEFINITIONS_DB_PATH + "categorytree" + ".ser";
                        Category root = (Category)this.read(categoryTreeDBFile);
                        System.out.println(root);
                        System.out.println("root.getChildrenCategory().size: " + root.getChildrenCategory().size());
                }
        }
        
        /**
         * 
         * @return Map
         */
        private Category convertCategories(DefinitionType defType, Map<Long, Category> catMap) {
                System.out.println("convertCategories()");
                
                List<CategoryType> jaxbCats = defType.getCategory();
                Iterator<CategoryType> it = jaxbCats.iterator();
                
                // Get root category
                CategoryType jaxbCat = it.next();
                if(jaxbCat == null) {
                        System.out.println("Invalid file: " + this.srcFile);
                        return null;
                }
                
                Category root = this.convertCategoryType(jaxbCat, null, catMap);
                return root;
        }
        
        /**
         * 
         * @param jaxbCat
         * @return Category
         */
        private Category convertCategoryType(CategoryType jaxbCat, Category parentCat, Map<Long, Category> catMap) {
                Category cat = new Category(jaxbCat.getID());
                cat.setLabel(jaxbCat.getName());
                cat.setDescription(jaxbCat.getDescription());
                if(parentCat != null) {
                        cat.setParentCategory(parentCat);
                }
                
                // Category Slide Definition
                List<CategorySlideDefinitionType> jaxbDefs = jaxbCat.getCategorySlideDefinition();
                Iterator<CategorySlideDefinitionType> it = jaxbDefs.iterator();
                while(it.hasNext()) {
                        CategorySlideDefinitionType jaxbDef = it.next();
                        CategorySlideDefinition catSlideDef = this.convertCategorySlideDefinitionType(jaxbCat.getID(), jaxbDef);
                        cat.addCategorySlideDefintion(catSlideDef);
                }
                
                // Children
                List<CategoryType> jaxbChildren = jaxbCat.getChildCategory();
                //System.out.println("jaxbChildren.size: " + jaxbChildren.size());
                
                Iterator<CategoryType> itChildren = jaxbChildren.iterator();
                while(itChildren.hasNext()) {
                        CategoryType jaxbChildCat = itChildren.next();
                        Category childCat = this.convertCategoryType(jaxbChildCat, cat, catMap);
                        cat.addChild(childCat);
                }
                //System.out.println("Parent cat: " + cat);
                catMap.put(new Long(jaxbCat.getID()), cat);
                return cat;
        }
        
        /**
         * 
         * @param jaxbDef
         * @return
         */
        private CategorySlideDefinition convertCategorySlideDefinitionType(long categoryID, CategorySlideDefinitionType jaxbDef) {
                CategorySlideDefinition catSlideDef = new CategorySlideDefinition(
                                categoryID, jaxbDef.getSlideDefinitionID());
                
                catSlideDef.setDescription(jaxbDef.getDescription());
                
                EditorialImportanceType jaxbEdImp = jaxbDef.getEditorialImportance();
                EditorialImportance edImp = this.convertEditorialImportance(jaxbEdImp);
                catSlideDef.setEditorialImportance(edImp);
                
                return catSlideDef;
        }
        
        /**
         * 
         * @param jaxbEdImp
         * @return EditorialImportance
         */
        private EditorialImportance convertEditorialImportance(EditorialImportanceType jaxbEdImp) {
                String strEdImp = jaxbEdImp.value();
                EditorialImportance edImp = null;
                
                if(strEdImp.equals("Mandatory"))
                        edImp = EditorialImportance.MANDATORY;
                else if(strEdImp.equals("Optional"))
                        edImp = EditorialImportance.OPTIONAL;
                else 
                        edImp = EditorialImportance.RECOMMENDED;
                
                return edImp;
        }
        /**
         * 
         * @return Map
         */
        private Map<Long, Unit> convertUnits(DefinitionType defType) {
                System.out.println("convertUnits()");
                
                Map<Long, Unit> allUnits = new HashMap<Long, Unit>();
                List<UnitType> jaxbUnits = defType.getUnit();
                Iterator<UnitType> it = jaxbUnits.iterator();
                
                while(it.hasNext()) {
                        UnitType jaxbUnit = it.next();
                        
                        Unit unit = new Unit(jaxbUnit.getID());
                        unit.setDescription(jaxbUnit.getDescription());
                        unit.setFullForm(jaxbUnit.getFullform());
                        unit.setShortForm(jaxbUnit.getShortform());
                        System.out.println(unit);
                        
                        allUnits.put(new Long(jaxbUnit.getID()), unit);
                }
                
                return allUnits;
        }
        
        /**
         * 
         * @return DefinitionType
         */
        private DefinitionType unmarshallSrcFile(String srcFile) throws Exception {
                System.out.println("unmarshallSrcFile()");
                
        JAXBContext jc = JAXBContext.newInstance( "in.shop2020.metamodel.jaxb" );
        
        // create an Unmarshaller
        Unmarshaller u = jc.createUnmarshaller();
        
        JAXBElement<?> element = (JAXBElement<?>)u.unmarshal( new FileInputStream(srcFile) );
        
        return (DefinitionType)element.getValue();
        }
        
        /**
         * 
         * @param objectToStore
         */
        private void store(Object objectToStore, String dbFile) throws Exception {
                System.out.println("store()");
                
                FileOutputStream fos = new FileOutputStream(new File(dbFile));
                ObjectOutputStream out = new ObjectOutputStream(fos);
                
                out.writeObject(objectToStore);
                out.close();
                System.out.println("Serialization complete");
        }
        
        /**
         * 
         */
        private Object read(String dbFile) throws Exception {
                System.out.println("read()");

                FileInputStream fis = new FileInputStream(new File(dbFile));
                ObjectInputStream in = new ObjectInputStream(fis);
                Object obj = in.readObject();
                in.close();
                
                System.out.println("De-serialization complete");
                return obj;
        }
}