java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 212 d154d6012cbe
parent 211 b5148f646278
child 214 1fb3c7953d8a
equal deleted inserted replaced
211:b5148f646278 212:d154d6012cbe
    23 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    23 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
    25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
    26 import info.globalcode.sql.dk.configuration.Properties;
    26 import info.globalcode.sql.dk.configuration.Properties;
    27 import info.globalcode.sql.dk.configuration.Property;
    27 import info.globalcode.sql.dk.configuration.Property;
       
    28 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
       
    29 import info.globalcode.sql.dk.configuration.PropertyDeclarations;
    28 import info.globalcode.sql.dk.configuration.TunnelDefinition;
    30 import info.globalcode.sql.dk.configuration.TunnelDefinition;
    29 import info.globalcode.sql.dk.formatting.ColumnsHeader;
    31 import info.globalcode.sql.dk.formatting.ColumnsHeader;
       
    32 import info.globalcode.sql.dk.formatting.CommonProperties;
    30 import info.globalcode.sql.dk.formatting.FakeSqlArray;
    33 import info.globalcode.sql.dk.formatting.FakeSqlArray;
    31 import info.globalcode.sql.dk.formatting.Formatter;
    34 import info.globalcode.sql.dk.formatting.Formatter;
    32 import info.globalcode.sql.dk.formatting.FormatterContext;
    35 import info.globalcode.sql.dk.formatting.FormatterContext;
    33 import info.globalcode.sql.dk.formatting.FormatterException;
    36 import info.globalcode.sql.dk.formatting.FormatterException;
    34 import java.io.BufferedReader;
    37 import java.io.BufferedReader;
   182 			listFormatterProperties(formatterName);
   185 			listFormatterProperties(formatterName);
   183 		}
   186 		}
   184 	}
   187 	}
   185 
   188 
   186 	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
   189 	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
   187 		ColumnsHeader header = constructHeader(
   190 		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
   188 				new HeaderField("name", SQLType.VARCHAR),
   191 		try {
   189 				new HeaderField("type", SQLType.VARCHAR),
   192 
   190 				new HeaderField("default", SQLType.VARCHAR),
   193 			ColumnsHeader header = constructHeader(
   191 				new HeaderField("description", SQLType.VARCHAR)
   194 					new HeaderField("name", SQLType.VARCHAR),
   192 		);
   195 					new HeaderField("type", SQLType.VARCHAR),
   193 		List<Object[]> data = new ArrayList<>();
   196 					new HeaderField("default", SQLType.VARCHAR),
   194 
   197 					new HeaderField("description", SQLType.VARCHAR)
   195 		data.add(new Object[]{"TODO", "a", "b", "c"});
   198 			);
   196 		data.add(new Object[]{"TODO", "a", "b", "c"});
   199 			List<Object[]> data = new ArrayList<>();
   197 		data.add(new Object[]{"TODO", "a", "b", "c"});
   200 
   198 		data.add(new Object[]{"TODO", "a", "b", "c"});
   201 			Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
   199 
   202 
   200 		List<Parameter> parameters = new ArrayList<>();
   203 			// TOOD: formatterClass.getDeclaredAnnotation(PropertyDeclarations.class); and separate inherited properties
   201 		parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
   204 			// Repeated PropertyDeclaration wrapped in PropertyDeclarations
   202 
   205 			PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
   203 		printTable(formatter, header, "-- formatter properties", parameters, data);
   206 			if (properties == null) {
       
   207 				log.log(Level.WARNING, "Formatter „{0}“  has no declared properties", formatterName);
       
   208 			} else {
       
   209 				for (PropertyDeclaration p : properties.value()) {
       
   210 					data.add(propertyDeclarationToRow(p));
       
   211 				}
       
   212 			}
       
   213 
       
   214 			// Single PropertyDeclaration
       
   215 			PropertyDeclaration property = formatterClass.getAnnotation(PropertyDeclaration.class);
       
   216 			if (property != null) {
       
   217 				data.add(propertyDeclarationToRow(property));
       
   218 			}
       
   219 
       
   220 			List<Parameter> parameters = new ArrayList<>();
       
   221 			parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
       
   222 
       
   223 			printTable(formatter, header, "-- formatter properties", parameters, data);
       
   224 		} catch (ClassNotFoundException e) {
       
   225 			throw new ConfigurationException("Unable to find class " + fd.getClassName() + " of formatter" + fd.getName(), e);
       
   226 		}
       
   227 	}
       
   228 
       
   229 	private static Object[] propertyDeclarationToRow(PropertyDeclaration p) {
       
   230 		return new Object[]{
       
   231 			p.name(),
       
   232 			CommonProperties.getSimpleTypeName(p.type()),
       
   233 			p.defaultValue(),
       
   234 			p.description()
       
   235 		};
   204 	}
   236 	}
   205 
   237 
   206 	private void listTypes() throws FormatterException, ConfigurationException {
   238 	private void listTypes() throws FormatterException, ConfigurationException {
   207 		ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
   239 		ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
   208 		List<Object[]> data = new ArrayList<>();
   240 		List<Object[]> data = new ArrayList<>();