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