Subversion Repositories SmartDukaan

Rev

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

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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.EntityContainer;
import in.shop2020.metamodel.util.ExpandedCategory;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.util.Utils;

/**
 * Driver class to merge Java data objects with VTL script
 * 
 * @author naveen
 *
 */
public class VUI {
        public static String VTL_SRC_PATH = 
                "src/velocity/";
        
        public static String EXPORT_ENTITIES_PATH = 
                "/home/naveen/workspace/eclipse/export/html/entities/";
        
        public static String EXPORT_DEFINITIONS_PATH = 
                "/home/naveen/workspace/eclipse/export/html/definitions/";

        /**
         * Usage: VUI [entity|category] {Template file name}
         * 
         * @param args
         */
        public static void main(String[] args) throws Exception {
                String[] commands = new String[] {"entity", "category"};
                String[] datatypes = new String[] {"Entity ID", "Category ID"};

                String usage = "Usage: VUI ["+ StringUtils.join(commands, "|") +"] ["+ 
                        StringUtils.join(datatypes, "|") + "] {Template file name}";
                
                if(args.length < 3) {
                        System.out.println(usage);
                        System.exit(-1);
                }
                String inputCommand = args[0];
                String inputID = args[1];
                String templateFilename = args[2];
                
                long id = 0;
                try {
                        id = Long.parseLong(inputID);
                }
                catch (NumberFormatException nfe) {
                        System.out.println("Invalid ID - " + inputID);
                        System.exit(-1);
                }
                
                // Setup
                Velocity.init("src/in/shop2020/ui/util/velocity.properties");
                VelocityContext context = new VelocityContext();

                String templateFile = VTL_SRC_PATH + templateFilename + ".vm";
                
                Template template = null;
                String exportFileName = null;
                
                // Entities
                if(inputCommand.equals("entity")) {
                        EntityContainer entContainer = 
                                Catalog.getInstance().getEntityContainer();
                        
                        ExpandedEntity expEntity = entContainer.getExpandedEntity(id);
                        Utils.logger.info("expEntity=" + expEntity);
                        
                        context.put("expentity", expEntity);
                        template = Velocity.getTemplate(templateFile);
                        
                        exportFileName = EXPORT_ENTITIES_PATH + id + ".html";
                }
                
                // Category definitons
                else if(inputCommand.equals("category")) {
                        DefinitionsContainer defs = 
                                Catalog.getInstance().getDefinitionsContainer();
                        
                        ExpandedCategory expCategory = defs.getExpandedCategory(id);
                        Utils.info("expCategory=" + expCategory);
                        
                        context.put("expcategory", expCategory);
                        template = Velocity.getTemplate(templateFile);
                        
                        exportFileName = EXPORT_DEFINITIONS_PATH + id + ".html";
                }
                
                if(template != null) { 
                        File exportFile = new File(exportFileName);
                        if(!exportFile.exists()) {
                                exportFile.createNewFile();
                        }
                        
                        BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(new FileOutputStream(exportFile)));
                        
                        template.merge(context, writer);
                        
                        writer.flush();
                        writer.close();
                        Utils.info("Export Complete!");
                }
        }

}