package org.obridge;

import com.thoughtworks.xstream.XStream;
import org.apache.commons.cli.*;
import org.obridge.context.OBridgeConfiguration;
import org.obridge.generators.ConverterObjectGenerator;
import org.obridge.generators.EntityObjectGenerator;
import org.obridge.generators.PackageObjectGenerator;
import org.obridge.generators.ProcedureContextGenerator;
import org.obridge.util.XStreamFactory;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

public class OBridge {

    public static void main(String... args) throws SQLException, IOException, PropertyVetoException, ParseException {

        Options o = new Options();
        o.addOption("v", "version", false, "print version number");
        o.addOption("h", "help", false, "prints this help");
        o.addOption(OptionBuilder
                .withArgName("file")
                .hasArg()
                .withDescription("OBridge XML config file")
                .withLongOpt("config")
                .create("c"));

        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(o, args);

        if (cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("obridge", o, true);
            return;
        }

        if (cmd.hasOption("v")) {
            final Properties properties = new Properties();
            properties.load(new OBridge().getClass().getResourceAsStream("obridge-project.properties"));
            System.out.println("OBridge version " + properties.getProperty("version"));
            return;
        }

        if (cmd.hasOption("c")) {
            new OBridge().generate(new File(cmd.getOptionValue("c")));
        } else {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("obridge", o, true);
        }
    }

    public void generate(OBridgeConfiguration c) throws SQLException, IOException, PropertyVetoException {
        // generate objects
        EntityObjectGenerator.generate(c);

        // generate converters
        ConverterObjectGenerator.generate(c);

        // generate contexts
        ProcedureContextGenerator.generate(c);

        // generate packages
        PackageObjectGenerator.generate(c);

    }

    public OBridgeConfiguration loadConfiguration(File f) {
        XStream xs = XStreamFactory.createXStream();
        Object config = xs.fromXML(f);
        return (OBridgeConfiguration) config;
    }

    public void generate(File f) throws IOException, SQLException, PropertyVetoException {
        this.generate(loadConfiguration(f));
    }
}

@@ -739,604 +739,109 @@
     
-o.addOption(%22v%22, %22version%22, false, %22print version number%22);%0A        o.addOption(%22h%22, %22help%22, false, %22prints this help%22);%0A        o.addOption(OptionBuilder%0A                .withArgName(%22file%22)%0A                .hasArg()%0A                .withDescription(%22OBridge XML config file%22)%0A                .withLongOpt(%22config%22)%0A                .create(%22c%22));%0A%0A        CommandLineParser parser = new PosixParser();%0A        CommandLine cmd = parser.parse(o, args);%0A%0A        if (cmd.hasOption(%22h%22)) %7B%0A            HelpFormatter formatter = new HelpFormatter();%0A            formatter.printHelp(%22obridge%22, o, true
+CommandLine cmd = getCommandLine(o, args);%0A%0A        if (cmd.hasOption(%22h%22)) %7B%0A            printHelp(o
 );%0A 
@@ -1310,24 +1310,110 @@
  else %7B%0A    
+        printHelp(o);%0A        %7D%0A    %7D%0A%0A    private static void printHelp(Options o) %7B%0A
         Help
@@ -1451,28 +1451,24 @@
 ormatter();%0A
-    
         form
@@ -1504,29 +1504,571 @@
  true);%0A    
+%7D%0A%0A
     
-%7D
+private static CommandLine getCommandLine(Options o, String%5B%5D args) throws ParseException %7B%0A        o.addOption(%22v%22, %22version%22, false, %22print version number%22);%0A        o.addOption(%22h%22, %22help%22, false, %22prints this help%22);%0A        o.addOption(OptionBuilder%0A                .withArgName(%22file%22)%0A                .hasArg()%0A                .withDescription(%22OBridge XML config file%22)%0A                .withLongOpt(%22config%22)%0A                .create(%22c%22));%0A%0A        CommandLineParser parser = new PosixParser();%0A        return parser.parse(o, args);
 %0A    %7D%0A%0A    

refactor main