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