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