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