java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 21 Jun 2015 16:21:51 +0200
branchv_0
changeset 202 01078e09b85b
parent 188 54bacc7ed42b
child 224 36db9fd27436
permissions -rw-r--r--
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
     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.configuration;
    19 
    20 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
    21 import static info.globalcode.sql.dk.Functions.findByName;
    22 import info.globalcode.sql.dk.formatting.DsvFormatter;
    23 import info.globalcode.sql.dk.formatting.SilentFormatter;
    24 import info.globalcode.sql.dk.formatting.SingleRecordFormatter;
    25 import info.globalcode.sql.dk.formatting.SingleValueFormatter;
    26 import info.globalcode.sql.dk.formatting.TabularFormatter;
    27 import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
    28 import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
    29 import info.globalcode.sql.dk.formatting.TeXFormatter;
    30 import info.globalcode.sql.dk.formatting.XhtmlFormatter;
    31 import info.globalcode.sql.dk.formatting.XmlFormatter;
    32 import java.util.ArrayList;
    33 import java.util.Collection;
    34 import java.util.Collections;
    35 import java.util.List;
    36 import javax.xml.bind.annotation.XmlElement;
    37 import javax.xml.bind.annotation.XmlRootElement;
    38 import javax.xml.bind.annotation.XmlTransient;
    39 
    40 /**
    41  * Object representation of user configuration loaded from XML.
    42  *
    43  * @author Ing. František Kučera (frantovo.cz)
    44  */
    45 @XmlRootElement(name = "configuration", namespace = CONFIGURATION)
    46 public class Configuration {
    47 
    48 	private List<DatabaseDefinition> databases = new ArrayList<>();
    49 	private List<FormatterDefinition> formatters = new ArrayList<>();
    50 	/**
    51 	 * is used if no formatter is specified on CLI nor in user configuration
    52 	 */
    53 	public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
    54 	/**
    55 	 * Can be used as default if prefetching is ok – for configuration listings (config is alread in
    56 	 * memory, so this does not matter)
    57 	 */
    58 	public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME;
    59 	private String defaultFormatter;
    60 	/**
    61 	 * Default list of formatters. Is used if particular name is not found in user configuration.
    62 	 */
    63 	private static final Collection<FormatterDefinition> buildInFormatters;
    64 
    65 	static {
    66 		Collection<FormatterDefinition> l = new ArrayList<>();
    67 		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
    68 		l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
    69 		l.add(new FormatterDefinition(SingleRecordFormatter.NAME, SingleRecordFormatter.class.getName()));
    70 		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
    71 		l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
    72 		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
    73 		l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
    74 		l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
    75 		l.add(new FormatterDefinition(TeXFormatter.NAME, TeXFormatter.class.getName()));
    76 		l.add(new FormatterDefinition(DsvFormatter.NAME, DsvFormatter.class.getName()));
    77 		buildInFormatters = Collections.unmodifiableCollection(l);
    78 	}
    79 
    80 	@XmlElement(name = "database", namespace = CONFIGURATION)
    81 	public List<DatabaseDefinition> getDatabases() {
    82 		return databases;
    83 	}
    84 
    85 	public void setDatabases(List<DatabaseDefinition> databases) {
    86 		this.databases = databases;
    87 	}
    88 
    89 	/**
    90 	 * @param name
    91 	 * @return 
    92 	 * @throws ConfigurationException if no database with this name is configured
    93 	 */
    94 	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
    95 		DatabaseDefinition dd = findByName(databases, name);
    96 		if (dd == null) {
    97 			throw new ConfigurationException("Database is not configured: " + name);
    98 		} else {
    99 			return dd;
   100 		}
   101 	}
   102 
   103 	/**
   104 	 * @return only configured formatters
   105 	 * @see #getBuildInFormatters()
   106 	 * @see #getAllFormatters()
   107 	 */
   108 	@XmlElement(name = "formatter", namespace = CONFIGURATION)
   109 	public List<FormatterDefinition> getFormatters() {
   110 		return formatters;
   111 	}
   112 
   113 	public void setFormatters(List<FormatterDefinition> formatters) {
   114 		this.formatters = formatters;
   115 	}
   116 
   117 	/**
   118 	 * @param name name of desired formatter. Looking for this name in user configuration, then in
   119 	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
   120 	 * default is used.
   121 	 * @return formatter definition
   122 	 * @throws ConfigurationException if no formatter with this name was found
   123 	 */
   124 	public FormatterDefinition getFormatter(String name) throws ConfigurationException {
   125 		if (name == null) {
   126 			return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
   127 		} else {
   128 			FormatterDefinition fd = findByName(formatters, name);
   129 			fd = fd == null ? findByName(buildInFormatters, name) : fd;
   130 			if (fd == null) {
   131 				throw new ConfigurationException("Formatter is not configured: " + name);
   132 			} else {
   133 				return fd;
   134 			}
   135 		}
   136 	}
   137 
   138 	/**
   139 	 * @return only built-in formatters
   140 	 * @see #getAllFormatters()
   141 	 * @see #getFormatters()
   142 	 */
   143 	@XmlTransient
   144 	public Collection<FormatterDefinition> getBuildInFormatters() {
   145 		return buildInFormatters;
   146 	}
   147 
   148 	/**
   149 	 * @return built-in + configured formatters
   150 	 * @see #getFormatters()
   151 	 */
   152 	@XmlTransient
   153 	public Collection<FormatterDefinition> getAllFormatters() {
   154 		Collection<FormatterDefinition> allFormatters = new ArrayList<>();
   155 		allFormatters.addAll(buildInFormatters);
   156 		allFormatters.addAll(formatters);
   157 		return allFormatters;
   158 	}
   159 
   160 	/**
   161 	 * @return name of default formatter, is used if name is not specified on CLI
   162 	 */
   163 	@XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
   164 	public String getDefaultFormatter() {
   165 		return defaultFormatter;
   166 	}
   167 
   168 	public void setDefaultFormatter(String defaultFormatter) {
   169 		this.defaultFormatter = defaultFormatter;
   170 	}
   171 }