java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 23:31:55 +0100
branchv_0
changeset 34 9335cf31c0f2
parent 33 04db6ccd6c48
child 36 025fbe816bbf
permissions -rw-r--r--
first working version
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    21 import info.globalcode.sql.dk.CLIOptions.MODE;
    22 import info.globalcode.sql.dk.configuration.Configuration;
    23 import info.globalcode.sql.dk.configuration.ConfigurationException;
    24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
    26 import info.globalcode.sql.dk.formatting.Formatter;
    27 import info.globalcode.sql.dk.formatting.FormatterContext;
    28 import info.globalcode.sql.dk.formatting.FormatterException;
    29 import java.io.IOException;
    30 import java.sql.SQLException;
    31 import java.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import javax.xml.bind.JAXBContext;
    34 import javax.xml.bind.Unmarshaller;
    35 
    36 /**
    37  *
    38  * @author Ing. František Kučera (frantovo.cz)
    39  */
    40 public class CLIStarter implements ConfigurationProvider {
    41 
    42 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    43 	private CLIOptions options;
    44 	private Configuration configuration;
    45 
    46 	public static void main(String[] args) {
    47 
    48 		if (args.length == 0) {
    49 			args = new String[]{CLIParser.Tokens.INFO_HELP};
    50 		}
    51 
    52 		try {
    53 			CLIParser parser = new CLIParser();
    54 			CLIOptions options = parser.parseOptions(args);
    55 			options.validate();
    56 			CLIStarter starter = new CLIStarter(options);
    57 			starter.installDefaultConfiguration();
    58 			starter.process();
    59 		} catch (CLIParserException e) {
    60 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    61 		} catch (InvalidOptionsException e) {
    62 			log.log(Level.SEVERE, "Invalid CLI options", e);
    63 		} catch (ConfigurationException e) {
    64 			log.log(Level.SEVERE, "Configuration problem", e);
    65 		} catch (SQLException e) {
    66 			log.log(Level.SEVERE, "SQL problem", e);
    67 		} catch (FormatterException e) {
    68 			log.log(Level.SEVERE, "Formatting problem", e);
    69 		}
    70 	}
    71 
    72 	public CLIStarter(CLIOptions options) {
    73 		this.options = options;
    74 	}
    75 
    76 	private void process() throws ConfigurationException, SQLException, FormatterException {
    77 		/** Show info */
    78 		if (!options.getShowInfo().isEmpty()) {
    79 			InfoLister infoLister = new InfoLister(System.err, this);
    80 			infoLister.showInfo(options);
    81 		}
    82 
    83 		MODE mode = options.getMode();
    84 		switch (mode) {
    85 			case QUERY_NOW:
    86 				processQueryNow();
    87 				break;
    88 			case PREPARE_BATCH:
    89 				processPrepareBatch();
    90 				break;
    91 			case EXECUTE_BATCH:
    92 				processExecuteBatch();
    93 				break;
    94 			case JUST_SHOW_INFO:
    95 				// already done above
    96 				break;
    97 			default:
    98 				log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
    99 				break;
   100 		}
   101 	}
   102 
   103 	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
   104 		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
   105 		if (dd == null) {
   106 			throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
   107 		} else {
   108 			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
   109 			if (fd == null) {
   110 				throw new ConfigurationException("Formatter is not configured: " + options.getDatabaseName());
   111 			} else {
   112 				DatabaseConnection c = dd.connect();
   113 				Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
   114 				c.executeQuery(options.getSQLCommand(), f);
   115 			}
   116 		}
   117 	}
   118 
   119 	private void processPrepareBatch() {
   120 	}
   121 
   122 	private void processExecuteBatch() {
   123 	}
   124 
   125 	@Override
   126 	public Configuration getConfiguration() throws ConfigurationException {
   127 		if (configuration == null) {
   128 			configuration = loadConfiguration();
   129 		}
   130 		return configuration;
   131 	}
   132 
   133 	private void installDefaultConfiguration() throws ConfigurationException {
   134 		Constants.DIR.mkdir();
   135 
   136 		if (Constants.CONFIG_FILE.exists()) {
   137 			log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
   138 		} else {
   139 			try {
   140 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
   141 			} catch (IOException e) {
   142 				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
   143 			}
   144 		}
   145 	}
   146 
   147 	private Configuration loadConfiguration() throws ConfigurationException {
   148 		try {
   149 			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
   150 			Unmarshaller u = jaxb.createUnmarshaller();
   151 			return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
   152 		} catch (Exception e) {
   153 			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
   154 		}
   155 	}
   156 }