java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Configuration.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 248 7f81cfa150d0
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package info.globalcode.sql.dk.configuration;
    18 
    19 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
    20 import static info.globalcode.sql.dk.Functions.findByName;
    21 import info.globalcode.sql.dk.formatting.BarChartFormatter;
    22 import info.globalcode.sql.dk.formatting.RecfileFormatter;
    23 import info.globalcode.sql.dk.formatting.SilentFormatter;
    24 import info.globalcode.sql.dk.formatting.SingleValueFormatter;
    25 import info.globalcode.sql.dk.formatting.TabularFormatter;
    26 import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
    27 import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
    28 import info.globalcode.sql.dk.formatting.TeXFormatter;
    29 import info.globalcode.sql.dk.formatting.XhtmlFormatter;
    30 import info.globalcode.sql.dk.formatting.XmlFormatter;
    31 import java.util.ArrayList;
    32 import java.util.Collection;
    33 import java.util.Collections;
    34 import java.util.List;
    35 import javax.xml.bind.annotation.XmlElement;
    36 import javax.xml.bind.annotation.XmlRootElement;
    37 import javax.xml.bind.annotation.XmlTransient;
    38 
    39 /**
    40  * Object representation of user configuration loaded from XML.
    41  *
    42  * @author Ing. František Kučera (frantovo.cz)
    43  */
    44 @XmlRootElement(name = "configuration", namespace = CONFIGURATION)
    45 public class Configuration {
    46 
    47 	private List<DatabaseDefinition> databases = new ArrayList<>();
    48 	private List<FormatterDefinition> formatters = new ArrayList<>();
    49 	/**
    50 	 * is used if no formatter is specified on CLI nor in user configuration
    51 	 */
    52 	public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
    53 	/**
    54 	 * Can be used as default if prefetching is ok – for configuration listings (config is alread in
    55 	 * memory, so this does not matter)
    56 	 */
    57 	public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME;
    58 	private String defaultFormatter;
    59 	/**
    60 	 * Default list of formatters. Is used if particular name is not found in user configuration.
    61 	 */
    62 	private static final Collection<FormatterDefinition> buildInFormatters;
    63 
    64 	static {
    65 		Collection<FormatterDefinition> l = new ArrayList<>();
    66 		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
    67 		l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
    68 		l.add(new FormatterDefinition(RecfileFormatter.NAME, RecfileFormatter.class.getName()));
    69 		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
    70 		l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
    71 		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
    72 		l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
    73 		l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
    74 		l.add(new FormatterDefinition(TeXFormatter.NAME, TeXFormatter.class.getName()));
    75 		//l.add(new FormatterDefinition(DsvFormatter.NAME, DsvFormatter.class.getName()));
    76 		//l.add(new FormatterDefinition(SystemCommandExecutor.NAME, SystemCommandExecutor.class.getName()));
    77 		l.add(new FormatterDefinition(BarChartFormatter.NAME, BarChartFormatter.class.getName()));
    78 		buildInFormatters = Collections.unmodifiableCollection(l);
    79 	}
    80 
    81 	@XmlElement(name = "database", namespace = CONFIGURATION)
    82 	public List<DatabaseDefinition> getDatabases() {
    83 		return databases;
    84 	}
    85 
    86 	public void setDatabases(List<DatabaseDefinition> databases) {
    87 		this.databases = databases;
    88 	}
    89 
    90 	/**
    91 	 * @param name
    92 	 * @return
    93 	 * @throws ConfigurationException if no database with this name is configured
    94 	 */
    95 	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
    96 		DatabaseDefinition dd = findByName(databases, name);
    97 		if (dd == null) {
    98 			throw new ConfigurationException("Database is not configured: " + name);
    99 		} else {
   100 			return dd;
   101 		}
   102 	}
   103 
   104 	/**
   105 	 * @return only configured formatters
   106 	 * @see #getBuildInFormatters()
   107 	 * @see #getAllFormatters()
   108 	 */
   109 	@XmlElement(name = "formatter", namespace = CONFIGURATION)
   110 	public List<FormatterDefinition> getFormatters() {
   111 		return formatters;
   112 	}
   113 
   114 	public void setFormatters(List<FormatterDefinition> formatters) {
   115 		this.formatters = formatters;
   116 	}
   117 
   118 	/**
   119 	 * @param name name of desired formatter. Looking for this name in user configuration, then in
   120 	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
   121 	 * default is used.
   122 	 * @return formatter definition
   123 	 * @throws ConfigurationException if no formatter with this name was found
   124 	 */
   125 	public FormatterDefinition getFormatter(String name) throws ConfigurationException {
   126 		if (name == null) {
   127 			return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
   128 		} else {
   129 			FormatterDefinition fd = findByName(formatters, name);
   130 			fd = fd == null ? findByName(buildInFormatters, name) : fd;
   131 			if (fd == null) {
   132 				throw new ConfigurationException("Formatter is not configured: " + name);
   133 			} else {
   134 				return fd;
   135 			}
   136 		}
   137 	}
   138 
   139 	/**
   140 	 * @return only built-in formatters
   141 	 * @see #getAllFormatters()
   142 	 * @see #getFormatters()
   143 	 */
   144 	@XmlTransient
   145 	public Collection<FormatterDefinition> getBuildInFormatters() {
   146 		return buildInFormatters;
   147 	}
   148 
   149 	/**
   150 	 * @return built-in + configured formatters
   151 	 * @see #getFormatters()
   152 	 */
   153 	@XmlTransient
   154 	public Collection<FormatterDefinition> getAllFormatters() {
   155 		Collection<FormatterDefinition> allFormatters = new ArrayList<>();
   156 		allFormatters.addAll(buildInFormatters);
   157 		allFormatters.addAll(formatters);
   158 		return allFormatters;
   159 	}
   160 
   161 	/**
   162 	 * @return name of default formatter, is used if name is not specified on CLI
   163 	 */
   164 	@XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
   165 	public String getDefaultFormatter() {
   166 		return defaultFormatter;
   167 	}
   168 
   169 	public void setDefaultFormatter(String defaultFormatter) {
   170 		this.defaultFormatter = defaultFormatter;
   171 	}
   172 }