java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Configuration.java
branchv_0
changeset 238 4a1864c3e867
parent 224 36db9fd27436
child 248 7f81cfa150d0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Configuration.java	Mon Mar 04 20:15:24 2019 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.configuration;
    1.22 +
    1.23 +import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
    1.24 +import static info.globalcode.sql.dk.Functions.findByName;
    1.25 +import info.globalcode.sql.dk.formatting.BarChartFormatter;
    1.26 +import info.globalcode.sql.dk.formatting.SilentFormatter;
    1.27 +import info.globalcode.sql.dk.formatting.SingleRecordFormatter;
    1.28 +import info.globalcode.sql.dk.formatting.SingleValueFormatter;
    1.29 +import info.globalcode.sql.dk.formatting.TabularFormatter;
    1.30 +import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
    1.31 +import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
    1.32 +import info.globalcode.sql.dk.formatting.TeXFormatter;
    1.33 +import info.globalcode.sql.dk.formatting.XhtmlFormatter;
    1.34 +import info.globalcode.sql.dk.formatting.XmlFormatter;
    1.35 +import java.util.ArrayList;
    1.36 +import java.util.Collection;
    1.37 +import java.util.Collections;
    1.38 +import java.util.List;
    1.39 +import javax.xml.bind.annotation.XmlElement;
    1.40 +import javax.xml.bind.annotation.XmlRootElement;
    1.41 +import javax.xml.bind.annotation.XmlTransient;
    1.42 +
    1.43 +/**
    1.44 + * Object representation of user configuration loaded from XML.
    1.45 + *
    1.46 + * @author Ing. František Kučera (frantovo.cz)
    1.47 + */
    1.48 +@XmlRootElement(name = "configuration", namespace = CONFIGURATION)
    1.49 +public class Configuration {
    1.50 +
    1.51 +	private List<DatabaseDefinition> databases = new ArrayList<>();
    1.52 +	private List<FormatterDefinition> formatters = new ArrayList<>();
    1.53 +	/**
    1.54 +	 * is used if no formatter is specified on CLI nor in user configuration
    1.55 +	 */
    1.56 +	public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
    1.57 +	/**
    1.58 +	 * Can be used as default if prefetching is ok – for configuration listings (config is alread in
    1.59 +	 * memory, so this does not matter)
    1.60 +	 */
    1.61 +	public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME;
    1.62 +	private String defaultFormatter;
    1.63 +	/**
    1.64 +	 * Default list of formatters. Is used if particular name is not found in user configuration.
    1.65 +	 */
    1.66 +	private static final Collection<FormatterDefinition> buildInFormatters;
    1.67 +
    1.68 +	static {
    1.69 +		Collection<FormatterDefinition> l = new ArrayList<>();
    1.70 +		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
    1.71 +		l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
    1.72 +		l.add(new FormatterDefinition(SingleRecordFormatter.NAME, SingleRecordFormatter.class.getName()));
    1.73 +		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
    1.74 +		l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
    1.75 +		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
    1.76 +		l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
    1.77 +		l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
    1.78 +		l.add(new FormatterDefinition(TeXFormatter.NAME, TeXFormatter.class.getName()));
    1.79 +		//l.add(new FormatterDefinition(DsvFormatter.NAME, DsvFormatter.class.getName()));
    1.80 +		//l.add(new FormatterDefinition(SystemCommandExecutor.NAME, SystemCommandExecutor.class.getName()));
    1.81 +		l.add(new FormatterDefinition(BarChartFormatter.NAME, BarChartFormatter.class.getName()));
    1.82 +		buildInFormatters = Collections.unmodifiableCollection(l);
    1.83 +	}
    1.84 +
    1.85 +	@XmlElement(name = "database", namespace = CONFIGURATION)
    1.86 +	public List<DatabaseDefinition> getDatabases() {
    1.87 +		return databases;
    1.88 +	}
    1.89 +
    1.90 +	public void setDatabases(List<DatabaseDefinition> databases) {
    1.91 +		this.databases = databases;
    1.92 +	}
    1.93 +
    1.94 +	/**
    1.95 +	 * @param name
    1.96 +	 * @return
    1.97 +	 * @throws ConfigurationException if no database with this name is configured
    1.98 +	 */
    1.99 +	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
   1.100 +		DatabaseDefinition dd = findByName(databases, name);
   1.101 +		if (dd == null) {
   1.102 +			throw new ConfigurationException("Database is not configured: " + name);
   1.103 +		} else {
   1.104 +			return dd;
   1.105 +		}
   1.106 +	}
   1.107 +
   1.108 +	/**
   1.109 +	 * @return only configured formatters
   1.110 +	 * @see #getBuildInFormatters()
   1.111 +	 * @see #getAllFormatters()
   1.112 +	 */
   1.113 +	@XmlElement(name = "formatter", namespace = CONFIGURATION)
   1.114 +	public List<FormatterDefinition> getFormatters() {
   1.115 +		return formatters;
   1.116 +	}
   1.117 +
   1.118 +	public void setFormatters(List<FormatterDefinition> formatters) {
   1.119 +		this.formatters = formatters;
   1.120 +	}
   1.121 +
   1.122 +	/**
   1.123 +	 * @param name name of desired formatter. Looking for this name in user configuration, then in
   1.124 +	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
   1.125 +	 * default is used.
   1.126 +	 * @return formatter definition
   1.127 +	 * @throws ConfigurationException if no formatter with this name was found
   1.128 +	 */
   1.129 +	public FormatterDefinition getFormatter(String name) throws ConfigurationException {
   1.130 +		if (name == null) {
   1.131 +			return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
   1.132 +		} else {
   1.133 +			FormatterDefinition fd = findByName(formatters, name);
   1.134 +			fd = fd == null ? findByName(buildInFormatters, name) : fd;
   1.135 +			if (fd == null) {
   1.136 +				throw new ConfigurationException("Formatter is not configured: " + name);
   1.137 +			} else {
   1.138 +				return fd;
   1.139 +			}
   1.140 +		}
   1.141 +	}
   1.142 +
   1.143 +	/**
   1.144 +	 * @return only built-in formatters
   1.145 +	 * @see #getAllFormatters()
   1.146 +	 * @see #getFormatters()
   1.147 +	 */
   1.148 +	@XmlTransient
   1.149 +	public Collection<FormatterDefinition> getBuildInFormatters() {
   1.150 +		return buildInFormatters;
   1.151 +	}
   1.152 +
   1.153 +	/**
   1.154 +	 * @return built-in + configured formatters
   1.155 +	 * @see #getFormatters()
   1.156 +	 */
   1.157 +	@XmlTransient
   1.158 +	public Collection<FormatterDefinition> getAllFormatters() {
   1.159 +		Collection<FormatterDefinition> allFormatters = new ArrayList<>();
   1.160 +		allFormatters.addAll(buildInFormatters);
   1.161 +		allFormatters.addAll(formatters);
   1.162 +		return allFormatters;
   1.163 +	}
   1.164 +
   1.165 +	/**
   1.166 +	 * @return name of default formatter, is used if name is not specified on CLI
   1.167 +	 */
   1.168 +	@XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
   1.169 +	public String getDefaultFormatter() {
   1.170 +		return defaultFormatter;
   1.171 +	}
   1.172 +
   1.173 +	public void setDefaultFormatter(String defaultFormatter) {
   1.174 +		this.defaultFormatter = defaultFormatter;
   1.175 +	}
   1.176 +}