java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 214 1fb3c7953d8a
parent 212 d154d6012cbe
child 215 42880d38ad3e
equal deleted inserted replaced
213:39d154429f7a 214:1fb3c7953d8a
    45 import java.sql.SQLException;
    45 import java.sql.SQLException;
    46 import java.util.ArrayList;
    46 import java.util.ArrayList;
    47 import java.util.Collections;
    47 import java.util.Collections;
    48 import java.util.Comparator;
    48 import java.util.Comparator;
    49 import java.util.EnumSet;
    49 import java.util.EnumSet;
       
    50 import java.util.HashMap;
    50 import java.util.HashSet;
    51 import java.util.HashSet;
    51 import java.util.List;
    52 import java.util.List;
       
    53 import java.util.Map;
    52 import java.util.Map.Entry;
    54 import java.util.Map.Entry;
    53 import java.util.ServiceLoader;
    55 import java.util.ServiceLoader;
    54 import java.util.Set;
    56 import java.util.Set;
    55 import java.util.concurrent.ExecutorService;
    57 import java.util.concurrent.ExecutorService;
    56 import java.util.concurrent.Executors;
    58 import java.util.concurrent.Executors;
   184 		for (String formatterName : options.getFormatterNamesToListProperties()) {
   186 		for (String formatterName : options.getFormatterNamesToListProperties()) {
   185 			listFormatterProperties(formatterName);
   187 			listFormatterProperties(formatterName);
   186 		}
   188 		}
   187 	}
   189 	}
   188 
   190 
       
   191 	private PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
       
   192 		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
       
   193 
       
   194 		if (properties == null) {
       
   195 			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
       
   196 			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
       
   197 		} else {
       
   198 			return properties.value();
       
   199 		}
       
   200 	}
       
   201 
   189 	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
   202 	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
   190 		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
   203 		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
   191 		try {
   204 		try {
   192 
   205 
   193 			ColumnsHeader header = constructHeader(
   206 			ColumnsHeader header = constructHeader(
   194 					new HeaderField("name", SQLType.VARCHAR),
   207 					new HeaderField("name", SQLType.VARCHAR),
   195 					new HeaderField("type", SQLType.VARCHAR),
   208 					new HeaderField("type", SQLType.VARCHAR),
   196 					new HeaderField("default", SQLType.VARCHAR),
   209 					new HeaderField("default", SQLType.VARCHAR),
   197 					new HeaderField("description", SQLType.VARCHAR)
   210 					new HeaderField("description", SQLType.VARCHAR)
   198 			);
   211 			);
   199 			List<Object[]> data = new ArrayList<>();
   212 
   200 
   213 			Map<String, Object[]> data = new HashMap<>();
   201 			Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
   214 			Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
   202 
   215 			List<Class<? extends Formatter>> hierarchy = Functions.getClassHierarchy(formatterClass, Formatter.class);
   203 			// TOOD: formatterClass.getDeclaredAnnotation(PropertyDeclarations.class); and separate inherited properties
   216 			Collections.reverse(hierarchy);
   204 			// Repeated PropertyDeclaration wrapped in PropertyDeclarations
   217 			hierarchy.stream().forEach((c) -> {
   205 			PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
   218 				for (PropertyDeclaration p : getPropertyDeclarations(c)) {
   206 			if (properties == null) {
   219 					data.put(p.name(), propertyDeclarationToRow(p));
   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 				}
   220 				}
   212 			}
   221 			});
   213 
       
   214 			// Single PropertyDeclaration
       
   215 			PropertyDeclaration property = formatterClass.getAnnotation(PropertyDeclaration.class);
       
   216 			if (property != null) {
       
   217 				data.add(propertyDeclarationToRow(property));
       
   218 			}
       
   219 
   222 
   220 			List<Parameter> parameters = new ArrayList<>();
   223 			List<Parameter> parameters = new ArrayList<>();
   221 			parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
   224 			parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
   222 
   225 
   223 			printTable(formatter, header, "-- formatter properties", parameters, data);
   226 			printTable(formatter, header, "-- formatter properties", parameters, new ArrayList<>(data.values()));
   224 		} catch (ClassNotFoundException e) {
   227 		} catch (ClassNotFoundException e) {
   225 			throw new ConfigurationException("Unable to find class " + fd.getClassName() + " of formatter" + fd.getName(), e);
   228 			throw new ConfigurationException("Unable to find class " + fd.getClassName() + " of formatter" + fd.getName(), e);
   226 		}
   229 		}
   227 	}
   230 	}
   228 
   231