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