java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Dec 2013 23:17:34 +0100
branchv_0
changeset 20 e225bdcd260e
parent 17 d8ab8aece6f2
child 21 d42ed0d10a10
permissions -rw-r--r--
refactor, configuration
franta-hg@16
     1
/**
franta-hg@16
     2
 * SQL-DK
franta-hg@16
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16
     4
 *
franta-hg@16
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@16
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@1
    18
package info.globalcode.sql.dk;
franta-hg@1
    19
franta-hg@20
    20
import info.globalcode.sql.dk.CLIOptions.MODE;
franta-hg@13
    21
import java.util.logging.Level;
franta-hg@13
    22
import java.util.logging.Logger;
franta-hg@13
    23
franta-hg@1
    24
/**
franta-hg@1
    25
 *
franta-hg@1
    26
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    27
 */
franta-hg@20
    28
public class CLIStarter implements ConfigurationProvider {
franta-hg@1
    29
franta-hg@13
    30
	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
franta-hg@20
    31
	private CLIOptions options;
franta-hg@20
    32
	private Configuration configuration;
franta-hg@13
    33
franta-hg@1
    34
	public static void main(String[] args) {
franta-hg@13
    35
		try {
franta-hg@13
    36
			CLIParser parser = new CLIParser();
franta-hg@13
    37
			CLIOptions options = parser.parseOptions(args);
franta-hg@14
    38
			options.validate();
franta-hg@20
    39
			CLIStarter starter = new CLIStarter(options);
franta-hg@20
    40
			starter.process();
franta-hg@13
    41
		} catch (CLIParserException e) {
franta-hg@14
    42
			log.log(Level.SEVERE, "Unable to parse CLI options", e);
franta-hg@14
    43
		} catch (InvalidOptionsException e) {
franta-hg@14
    44
			log.log(Level.SEVERE, "Invalid CLI options", e);
franta-hg@13
    45
		}
franta-hg@4
    46
	}
franta-hg@20
    47
franta-hg@20
    48
	public CLIStarter(CLIOptions options) {
franta-hg@20
    49
		this.options = options;
franta-hg@20
    50
	}
franta-hg@20
    51
franta-hg@20
    52
	private void process() {
franta-hg@20
    53
		/** Show info */
franta-hg@20
    54
		if (!options.getShowInfo().isEmpty()) {
franta-hg@20
    55
			InfoLister infoLister = new InfoLister(System.err, this);
franta-hg@20
    56
			infoLister.showInfo(options);
franta-hg@20
    57
		}
franta-hg@20
    58
franta-hg@20
    59
		MODE mode = options.getMode();
franta-hg@20
    60
		switch (mode) {
franta-hg@20
    61
			case QUERY_NOW:
franta-hg@20
    62
				break;
franta-hg@20
    63
			case PREPARE_BATCH:
franta-hg@20
    64
				break;
franta-hg@20
    65
			case EXECUTE_BATCH:
franta-hg@20
    66
				break;
franta-hg@20
    67
			case JUST_SHOW_INFO:
franta-hg@20
    68
				// already done above
franta-hg@20
    69
				break;
franta-hg@20
    70
			default:
franta-hg@20
    71
				log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
franta-hg@20
    72
				break;
franta-hg@20
    73
		}
franta-hg@20
    74
	}
franta-hg@20
    75
franta-hg@20
    76
	@Override
franta-hg@20
    77
	public Configuration getConfiguration() {
franta-hg@20
    78
		if (configuration == null) {
franta-hg@20
    79
			installDefaultConfiguration();
franta-hg@20
    80
			configuration = loadConfiguration();
franta-hg@20
    81
		}
franta-hg@20
    82
		return configuration;
franta-hg@20
    83
	}
franta-hg@20
    84
franta-hg@20
    85
	private void installDefaultConfiguration() {
franta-hg@20
    86
		/**
franta-hg@20
    87
		 * TODO: check config folder/file and create it if missing
franta-hg@20
    88
		 */
franta-hg@20
    89
	}
franta-hg@20
    90
franta-hg@20
    91
	private Configuration loadConfiguration() {
franta-hg@20
    92
		/**
franta-hg@20
    93
		 * TODO: load configuration from XML
franta-hg@20
    94
		 */
franta-hg@20
    95
		return null;
franta-hg@20
    96
	}
franta-hg@1
    97
}