java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 21:47:33 +0100
branchv_0
changeset 70 02c8eaa425e8
parent 69 0befec5034c2
child 72 fc9fc1f26b88
permissions -rw-r--r--
use formatter also for printing info! --list-types
franta-hg@16
     1
/**
franta-hg@16
     2
 * SQL-DK
franta-hg@16
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16
     4
 *
franta-hg@16
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@16
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@14
    18
package info.globalcode.sql.dk;
franta-hg@14
    19
franta-hg@67
    20
import info.globalcode.sql.dk.configuration.Configuration;
franta-hg@65
    21
import info.globalcode.sql.dk.configuration.ConfigurationException;
franta-hg@26
    22
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
franta-hg@65
    23
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
franta-hg@67
    24
import info.globalcode.sql.dk.configuration.FormatterDefinition;
franta-hg@67
    25
import static info.globalcode.sql.dk.Functions.rpad;
franta-hg@69
    26
import info.globalcode.sql.dk.formatting.ColumnsHeader;
franta-hg@69
    27
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@69
    28
import info.globalcode.sql.dk.formatting.FormatterContext;
franta-hg@69
    29
import info.globalcode.sql.dk.formatting.FormatterException;
franta-hg@17
    30
import java.io.BufferedReader;
franta-hg@17
    31
import java.io.InputStreamReader;
franta-hg@14
    32
import java.io.PrintStream;
franta-hg@65
    33
import java.sql.SQLException;
franta-hg@70
    34
import java.util.ArrayList;
franta-hg@70
    35
import java.util.EnumSet;
franta-hg@66
    36
import java.util.List;
franta-hg@17
    37
import java.util.logging.Level;
franta-hg@17
    38
import java.util.logging.Logger;
franta-hg@69
    39
import javax.sql.rowset.RowSetMetaDataImpl;
franta-hg@14
    40
franta-hg@14
    41
/**
franta-hg@14
    42
 * Displays info like help, version etc.
franta-hg@14
    43
 *
franta-hg@14
    44
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@14
    45
 */
franta-hg@14
    46
public class InfoLister {
franta-hg@14
    47
franta-hg@17
    48
	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
franta-hg@17
    49
	private PrintStream out;
franta-hg@20
    50
	private ConfigurationProvider configurationProvider;
franta-hg@69
    51
	private CLIOptions options;
franta-hg@70
    52
	private Formatter formatter;
franta-hg@17
    53
franta-hg@69
    54
	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
franta-hg@17
    55
		this.out = out;
franta-hg@20
    56
		this.configurationProvider = configurationProvider;
franta-hg@69
    57
		this.options = options;
franta-hg@17
    58
	}
franta-hg@17
    59
franta-hg@70
    60
	public void showInfo() throws ConfigurationException, FormatterException {
franta-hg@70
    61
		EnumSet<InfoType> commands = options.getShowInfo();
franta-hg@70
    62
franta-hg@70
    63
		for (InfoType infoType : commands) {
franta-hg@70
    64
			switch (infoType) {
franta-hg@70
    65
				// only these needs formatted output
franta-hg@70
    66
				case CONNECTION:
franta-hg@70
    67
				case DATABASES:
franta-hg@70
    68
				case FORMATTERS:
franta-hg@70
    69
				case TYPES:
franta-hg@70
    70
					formatter = getFormatter();
franta-hg@70
    71
					formatter.writeStartDatabase(new DatabaseDefinition());
franta-hg@70
    72
			}
franta-hg@70
    73
		}
franta-hg@70
    74
franta-hg@70
    75
		for (InfoType infoType : commands) {
franta-hg@70
    76
			infoType.showInfo(this);
franta-hg@70
    77
		}
franta-hg@70
    78
franta-hg@70
    79
		if (formatter != null) {
franta-hg@70
    80
			formatter.writeEndDatabase();
franta-hg@70
    81
		}
franta-hg@70
    82
	}
franta-hg@70
    83
franta-hg@70
    84
	private void listFormatters() throws ConfigurationException {
franta-hg@69
    85
		for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
franta-hg@69
    86
			log.log(Level.INFO, "Built-in formatter:   {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
franta-hg@69
    87
		}
franta-hg@69
    88
		List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters();
franta-hg@69
    89
		for (FormatterDefinition fd : configuredFormatters) {
franta-hg@69
    90
			log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
franta-hg@69
    91
		}
franta-hg@69
    92
		if (configuredFormatters.isEmpty()) {
franta-hg@69
    93
			log.log(Level.INFO, "No other formatters are configured");
franta-hg@69
    94
		}
franta-hg@69
    95
		String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
franta-hg@69
    96
		if (configuredDefaultFormatter == null) {
franta-hg@69
    97
			log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER);
franta-hg@69
    98
		} else {
franta-hg@69
    99
			log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter);
franta-hg@69
   100
		}
franta-hg@69
   101
	}
franta-hg@67
   102
franta-hg@70
   103
	public void listTypes() throws FormatterException, ConfigurationException {
franta-hg@70
   104
		ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
franta-hg@70
   105
		List<Object[]> data = new ArrayList<>();
franta-hg@70
   106
		for (SQLType sqlType : SQLType.values()) {
franta-hg@70
   107
			data.add(new Object[]{sqlType.name(), sqlType.getCode()});
franta-hg@70
   108
		}
franta-hg@70
   109
		printTable(formatter, header, data);
franta-hg@69
   110
	}
franta-hg@67
   111
franta-hg@69
   112
	public void listDatabases() throws ConfigurationException {
franta-hg@69
   113
		final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
franta-hg@69
   114
		if (configuredDatabases.isEmpty()) {
franta-hg@69
   115
			log.log(Level.WARNING, "No databases are configured.");
franta-hg@69
   116
		} else {
franta-hg@69
   117
			for (DatabaseDefinition dd : configuredDatabases) {
franta-hg@69
   118
				log.log(Level.INFO, "Configured database: {0}", dd.getName());
franta-hg@14
   119
			}
franta-hg@14
   120
		}
franta-hg@14
   121
	}
franta-hg@17
   122
franta-hg@69
   123
	public void testConnection() {
franta-hg@69
   124
		boolean connectionTestResult = false;
franta-hg@69
   125
		String dbName = options.getDatabaseNameToTest();
franta-hg@69
   126
		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
franta-hg@69
   127
		try {
franta-hg@69
   128
			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
franta-hg@69
   129
			if (dd == null) {
franta-hg@69
   130
				log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
franta-hg@69
   131
			} else {
franta-hg@69
   132
				log.log(Level.FINE, "Database definition was loaded from configuration");
franta-hg@69
   133
				DatabaseConnection dc = dd.connect();
franta-hg@69
   134
				connectionTestResult = dc.test();
franta-hg@69
   135
			}
franta-hg@69
   136
		} catch (ConfigurationException | SQLException e) {
franta-hg@69
   137
			log.log(Level.SEVERE, "Error during testing connection", e);
franta-hg@69
   138
		}
franta-hg@69
   139
		log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
franta-hg@69
   140
	}
franta-hg@69
   141
franta-hg@69
   142
	public void printResource(String fileName) {
franta-hg@18
   143
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
franta-hg@17
   144
			while (true) {
franta-hg@18
   145
				String line = reader.readLine();
franta-hg@17
   146
				if (line == null) {
franta-hg@17
   147
					break;
franta-hg@17
   148
				} else {
franta-hg@17
   149
					println(line);
franta-hg@17
   150
				}
franta-hg@17
   151
			}
franta-hg@17
   152
		} catch (Exception e) {
franta-hg@18
   153
			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
franta-hg@17
   154
		}
franta-hg@17
   155
	}
franta-hg@17
   156
franta-hg@17
   157
	private void println(String line) {
franta-hg@17
   158
		out.println(line);
franta-hg@17
   159
	}
franta-hg@69
   160
franta-hg@69
   161
	private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
franta-hg@69
   162
		formatter.writeStartResultSet();
franta-hg@69
   163
		formatter.writeColumnsHeader(header);
franta-hg@69
   164
franta-hg@69
   165
		for (Object[] row : data) {
franta-hg@69
   166
			formatter.writeStartRow();
franta-hg@69
   167
			for (Object cell : row) {
franta-hg@69
   168
				formatter.writeColumnValue(cell);
franta-hg@69
   169
			}
franta-hg@69
   170
			formatter.writeEndRow();
franta-hg@69
   171
		}
franta-hg@69
   172
franta-hg@69
   173
		formatter.writeEndResultSet();
franta-hg@69
   174
	}
franta-hg@69
   175
franta-hg@69
   176
	private Formatter getFormatter() throws ConfigurationException, FormatterException {
franta-hg@69
   177
		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(options.getFormatterName());
franta-hg@69
   178
		FormatterContext context = new FormatterContext(out);
franta-hg@69
   179
		return fd.getInstance(context);
franta-hg@69
   180
	}
franta-hg@69
   181
franta-hg@69
   182
	private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
franta-hg@69
   183
		try {
franta-hg@69
   184
			RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
franta-hg@69
   185
			metaData.setColumnCount(fields.length);
franta-hg@69
   186
franta-hg@69
   187
			for (int i = 0; i < fields.length; i++) {
franta-hg@69
   188
				HeaderField hf = fields[i];
franta-hg@69
   189
				int sqlIndex = i + 1;
franta-hg@69
   190
				metaData.setColumnName(sqlIndex, hf.name);
franta-hg@69
   191
				metaData.setColumnLabel(sqlIndex, hf.name);
franta-hg@69
   192
				metaData.setColumnType(sqlIndex, hf.type.getCode());
franta-hg@69
   193
				metaData.setColumnTypeName(sqlIndex, hf.type.name());
franta-hg@69
   194
			}
franta-hg@69
   195
franta-hg@69
   196
			return new ColumnsHeader(metaData);
franta-hg@69
   197
		} catch (SQLException e) {
franta-hg@69
   198
			throw new FormatterException("Error while constructing table headers", e);
franta-hg@69
   199
		}
franta-hg@69
   200
	}
franta-hg@69
   201
franta-hg@69
   202
	private static class HeaderField {
franta-hg@69
   203
franta-hg@69
   204
		String name;
franta-hg@69
   205
		SQLType type;
franta-hg@69
   206
franta-hg@69
   207
		public HeaderField(String name, SQLType type) {
franta-hg@69
   208
			this.name = name;
franta-hg@69
   209
			this.type = type;
franta-hg@69
   210
		}
franta-hg@69
   211
	}
franta-hg@69
   212
franta-hg@69
   213
	public enum InfoType {
franta-hg@69
   214
franta-hg@69
   215
		HELP {
franta-hg@69
   216
			@Override
franta-hg@69
   217
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   218
				infoLister.printResource(Constants.HELP_FILE);
franta-hg@69
   219
			}
franta-hg@69
   220
		},
franta-hg@69
   221
		VERSION {
franta-hg@69
   222
			@Override
franta-hg@69
   223
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   224
				infoLister.printResource(Constants.VERSION_FILE);
franta-hg@69
   225
			}
franta-hg@69
   226
		},
franta-hg@69
   227
		LICENSE {
franta-hg@69
   228
			@Override
franta-hg@69
   229
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   230
				infoLister.printResource(Constants.LICENSE_FILE);
franta-hg@69
   231
			}
franta-hg@69
   232
		},
franta-hg@69
   233
		FORMATTERS {
franta-hg@69
   234
			@Override
franta-hg@70
   235
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   236
				infoLister.listFormatters();
franta-hg@69
   237
			}
franta-hg@69
   238
		},
franta-hg@69
   239
		TYPES {
franta-hg@69
   240
			@Override
franta-hg@70
   241
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   242
				infoLister.listTypes();
franta-hg@69
   243
			}
franta-hg@69
   244
		},
franta-hg@69
   245
		DATABASES {
franta-hg@69
   246
			@Override
franta-hg@70
   247
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   248
				infoLister.listDatabases();
franta-hg@69
   249
			}
franta-hg@69
   250
		},
franta-hg@69
   251
		CONNECTION {
franta-hg@69
   252
			@Override
franta-hg@69
   253
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   254
				infoLister.testConnection();
franta-hg@69
   255
			}
franta-hg@69
   256
		};
franta-hg@69
   257
franta-hg@69
   258
		public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
franta-hg@69
   259
	}
franta-hg@14
   260
}