List of usage examples for javax.swing.table TableModel getColumnName
public String getColumnName(int columnIndex);
columnIndex
. From source file:pt.webdetails.cda.exporter.XmlExporter.java
public void export(final OutputStream out, final TableModel tableModel) throws ExporterException { final Document document = DocumentHelper.createDocument(); // Generate metadata final Element root = document.addElement("CdaExport"); final Element metadata = root.addElement("MetaData"); final int columnCount = tableModel.getColumnCount(); final int rowCount = tableModel.getRowCount(); for (int i = 0; i < columnCount; i++) { final Element columnInfo = metadata.addElement("ColumnMetaData"); columnInfo.addAttribute("index", (String.valueOf(i))); columnInfo.addAttribute("type", getColType(tableModel.getColumnClass(i))); columnInfo.addAttribute("name", tableModel.getColumnName(i)); }//from w w w. j a va 2s . c o m SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US); final Element resultSet = root.addElement("ResultSet"); for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) { final Element row = resultSet.addElement("Row"); for (int colIdx = 0; colIdx < columnCount; colIdx++) { final Element col = row.addElement("Col"); final Object value = tableModel.getValueAt(rowIdx, colIdx); if (value instanceof Date) { col.setText(format.format(value)); } else if (value != null) { // numbers can be safely converted via toString, as they use a well-defined format there col.setText(value.toString()); } else { col.addAttribute("isNull", "true"); } } } try { final Writer writer = new BufferedWriter(new OutputStreamWriter(out)); document.setXMLEncoding("UTF-8"); document.write(writer); writer.flush(); } catch (IOException e) { throw new ExporterException("IO Exception converting to utf-8", e); } }
From source file:pt.webdetails.cda.tests.ColumnDefinitionIT.java
@Test public void testColumnNames() throws Exception { final CdaSettings cdaSettings = parseSettingsFile("sample-columnDefinition.cda"); logger.debug("Doing query on Cda - Initializing CdaEngine"); QueryOptions queryOptions = new QueryOptions(); queryOptions.setDataAccessId("1"); queryOptions.addParameter("status", "Shipped"); DataAccess dataAccess = cdaSettings.getDataAccess("1"); logger.info("Doing query"); TableModel table = TableModelUtils.postProcessTableModel(dataAccess, queryOptions, doQuery(cdaSettings, queryOptions)); assertEquals(table.getColumnCount(), 3); assertEquals(table.getColumnName(0), "Year"); assertEquals(table.getColumnName(1), "STATUS"); assertEquals(table.getColumnName(2), "PriceInK"); }
From source file:pt.webdetails.cda.tests.OutputColumnsIT.java
public void testSingleOutputColumn() throws Exception { final CdaSettings cdaSettings = getSettingsManager().parseSettingsFile("sample-sql.cda"); final CdaEngine engine = getEngine(); QueryOptions queryOptions = new QueryOptions(); queryOptions.setDataAccessId("1"); List<String> outputColumnNames = new LinkedList<String>(); outputColumnNames.add("Year"); queryOptions.setOutputColumnName(outputColumnNames); TableModel tm = engine.doQuery(cdaSettings, queryOptions); Assert.assertEquals(tm.getColumnCount(), 1); Assert.assertEquals(tm.getColumnName(0), "Year"); }
From source file:pt.webdetails.cda.tests.OutputColumnsIT.java
public void testMultipleOutputColumn() throws Exception { final CdaSettings cdaSettings = getSettingsManager().parseSettingsFile("sample-sql.cda"); final CdaEngine engine = getEngine(); QueryOptions queryOptions = new QueryOptions(); queryOptions.setDataAccessId("1"); List<String> outputColumnNames = new LinkedList<String>(); outputColumnNames.add("Year"); outputColumnNames.add("STATUS"); queryOptions.setOutputColumnName(outputColumnNames); TableModel tm = engine.doQuery(cdaSettings, queryOptions); Assert.assertEquals(tm.getColumnCount(), 2); Assert.assertEquals(tm.getColumnName(0), "Year"); Assert.assertEquals(tm.getColumnName(1), "STATUS"); }
From source file:pt.webdetails.cda.tests.OutputColumnsIT.java
public void testMultipleOutputColumnOrder() throws Exception { final CdaSettings cdaSettings = getSettingsManager().parseSettingsFile("sample-sql.cda"); final CdaEngine engine = getEngine(); QueryOptions queryOptions = new QueryOptions(); queryOptions.setDataAccessId("1"); List<String> outputColumnNames = new LinkedList<String>(); outputColumnNames.add("Year"); outputColumnNames.add("STATUS"); outputColumnNames.add("PRICE"); queryOptions.setOutputColumnName(outputColumnNames); TableModel tm = engine.doQuery(cdaSettings, queryOptions); Assert.assertEquals(tm.getColumnName(0), "Year"); Assert.assertEquals(tm.getColumnName(1), "STATUS"); Assert.assertEquals(tm.getColumnName(2), "PRICE"); queryOptions = new QueryOptions(); queryOptions.setDataAccessId("1"); outputColumnNames = new LinkedList<String>(); outputColumnNames.add("PRICE"); outputColumnNames.add("Year"); outputColumnNames.add("STATUS"); queryOptions.setOutputColumnName(outputColumnNames); tm = engine.doQuery(cdaSettings, queryOptions); Assert.assertEquals(tm.getColumnName(0), "PRICE"); Assert.assertEquals(tm.getColumnName(1), "Year"); Assert.assertEquals(tm.getColumnName(2), "STATUS"); }
From source file:pt.webdetails.cda.utils.kettle.SortTableModel.java
private String getSortXmlStep(TableModel unsorted, List<String> sortBy) throws SortException { StringBuilder sortXML = new StringBuilder(" <step>\n" + " <name>sort</name>\n" + " <type>SortRows</type>\n" + " <description/>\n" + " <distribute>Y</distribute>\n" + " <copies>1</copies>\n" + " <partitioning>\n" + " <method>none</method>\n" + " <schema_name/>\n" + " </partitioning>\n" + " <directory>%%java.io.tmpdir%%</directory>\n" + " <prefix>out</prefix>\n" + " <sort_size>1000000</sort_size>\n" + " <free_memory>25</free_memory>\n" + " <compress>N</compress>\n" + " <compress_variable/>\n" + " <unique_rows>N</unique_rows>\n" + " <fields>\n"); for (String s : sortBy) { SortDescriptor sort = new SortDescriptor((s)); sortXML.append(" <field>\n" + " <name>" + unsorted.getColumnName(sort.getIndex()) + "</name>\n" + " <ascending>" + sort.getIsAscendingString() + "</ascending>\n" + " <case_sensitive>N</case_sensitive>\n" + " </field>\n"); }// w w w. ja va 2 s . c o m sortXML.append(" </fields>\n" + " <cluster_schema/>\n" + " <remotesteps> <input> </input> <output> </output> </remotesteps> <GUI>\n" + " <xloc>615</xloc>\n" + " <yloc>188</yloc>\n" + " <draw>Y</draw>\n" + " </GUI>\n" + " </step>\n"); return sortXML.toString(); }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
public static TableModel postProcessTableModel(final DataAccess dataAccess, final QueryOptions queryOptions, final TableModel rawTableModel) throws SortException, InvalidOutputIndexException { if (rawTableModel == null) { throw new IllegalArgumentException("Cannot process null table."); }/* w ww .java2 s . co m*/ // We will: // 1. Evaluate Calculated columns // 2. Show only the output columns we want; // 3. Sort // 4. Pagination TableModel table; // 1 Evaluate Calculated columns table = evaluateCalculatedColumns(dataAccess, rawTableModel); // 2. Show only the output columns we want, filter rows List<Integer> outputIndexes = getOutputIndexes(dataAccess, queryOptions, table); DataTableFilter rowFilter = getRowFilter(queryOptions, outputIndexes); //mdx and denormalizedMdx queries with an empty result set can return different metadata (less columns), //in this cases, the output indexes will be ignored boolean useOutputIndexes = true; if (table.getRowCount() == 0 && (dataAccess.getType().equals("mdx") || dataAccess.getType().equals("denormalizedMdx"))) { useOutputIndexes = false; logger.warn("Mdx query returned empty result set, output indexes will be ignored."); } table = useOutputIndexes ? filterTable(table, outputIndexes, rowFilter) : filterTable(table, new ArrayList<Integer>(), rowFilter); // 3. Sort if (!queryOptions.getSortBy().isEmpty()) { // no action table = (new SortTableModel()).doSort(table, queryOptions.getSortBy()); } // Create a metadata-aware table model final Class<?>[] colTypes = new Class[table.getColumnCount()]; final String[] colNames = new String[table.getColumnCount()]; for (int i = 0; i < table.getColumnCount(); i++) { colTypes[i] = table.getColumnClass(i); colNames[i] = table.getColumnName(i); } final int rowCount = table.getRowCount(); MetadataTableModel result = new MetadataTableModel(colNames, colTypes, rowCount); result.setMetadata("totalRows", rowCount); for (int r = 0; r < rowCount; r++) { for (int j = 0; j < table.getColumnCount(); j++) { result.setValueAt(table.getValueAt(r, j), r, j); } } // 4. Pagination return paginateTableModel(result, queryOptions); }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
/** * @param table/*from w ww .j av a2 s .c o m*/ * @param outputIndexes * @param rowFilter (optional) * @return * @throws InvalidOutputIndexException */ private static TableModel filterTable(final TableModel table, List<Integer> outputIndexes, final DataTableFilter rowFilter) throws InvalidOutputIndexException { int columnCount = outputIndexes.size(); if (columnCount == 0 && rowFilter != null) { //still have to go through the motions if we need to filter rows for (int i = 0; i < table.getColumnCount(); i++) { outputIndexes.add(i); } columnCount = outputIndexes.size(); } if (columnCount != 0) { //logger.info(Collections.max(outputIndexes)+" "+table.getColumnCount()); if ((Collections.max(outputIndexes) > table.getColumnCount() - 1)) { String errorMessage = String.format( "Output index higher than number of columns in tableModel. %s > %s", Collections.max(outputIndexes), table.getColumnCount()); logger.error(errorMessage); if (table.getColumnCount() > 0) { throw new InvalidOutputIndexException(errorMessage, null); } else { logger.warn( "Unable to validate output indexes because table metadata is empty. Returning table."); return table; } } final int rowCount = table.getRowCount(); logger.debug(rowCount == 0 ? "No data found" : "Found " + rowCount + " rows"); final Class<?>[] colTypes = new Class[columnCount]; final String[] colNames = new String[columnCount]; //just set the number of rows/columns final TypedTableModel typedTableModel = new TypedTableModel(colNames, colTypes, rowCount); for (int rowIn = 0, rowOut = 0; rowIn < rowCount; rowIn++, rowOut++) { //filter rows if (rowFilter != null && !rowFilter.rowContainsSearchTerms(table, rowIn)) { rowOut--; continue; } //filter columns for (int j = 0; j < outputIndexes.size(); j++) { final int outputIndex = outputIndexes.get(j); typedTableModel.setValueAt(table.getValueAt(rowIn, outputIndex), rowOut, j); } } //since we set the calculated table model to infer types, they will be available after rows are evaluated for (int i = 0; i < outputIndexes.size(); i++) { final int outputIndex = outputIndexes.get(i); typedTableModel.setColumnName(i, table.getColumnName(outputIndex)); typedTableModel.setColumnType(i, table.getColumnClass(outputIndex)); } return typedTableModel; } return table; }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
public static TableModel copyTableModel(final DataAccess dataAccess, final TableModel t) { // We're removing the ::table-by-index:: cols // Build an array of column indexes whose name is different from ::table-by-index::.* ArrayList<String> namedColumns = new ArrayList<String>(); ArrayList<Class<?>> namedColumnsClasses = new ArrayList<Class<?>>(); for (int i = 0; i < t.getColumnCount(); i++) { String colName = t.getColumnName(i); if (!colName.startsWith("::table-by-index::") && !colName.startsWith("::column::")) { namedColumns.add(colName);//from ww w .java 2 s .co m namedColumnsClasses.add(t.getColumnClass(i)); } } final int count = namedColumns.size(); final Class<?>[] colTypes = namedColumnsClasses.toArray(new Class[] {}); final String[] colNames = namedColumns.toArray(new String[] {}); for (int i = 0; i < count; i++) { colTypes[i] = t.getColumnClass(i); final ColumnDefinition col = dataAccess.getColumnDefinition(i); colNames[i] = col != null ? col.getName() : t.getColumnName(i); } final int rowCount = t.getRowCount(); logger.debug(rowCount == 0 ? "No data found" : "Found " + rowCount + " rows"); //if the first row has no values, the class will be Object, however, next rows can have values, we evaluate those for (int i = 0; i < colTypes.length; i++) { if (colTypes[i] == Object.class) { for (int k = 0; k < t.getRowCount(); k++) { if (t.getValueAt(k, i) != null) { colTypes[i] = t.getValueAt(k, i).getClass(); break; } } } } final TypedTableModel typedTableModel = new TypedTableModel(colNames, colTypes, rowCount); for (int r = 0; r < rowCount; r++) { for (int c = 0; c < count; c++) { typedTableModel.setValueAt(t.getValueAt(r, c), r, c); } } return typedTableModel; }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
/** * Method to append a tablemodel into another. We'll make no guarantees about the types * * @param tableModelA TableModel to be modified * @param tableModelB Contents to be appended # *//*from w w w. j av a2 s . c o m*/ public static TableModel appendTableModel(final TableModel tableModelA, final TableModel tableModelB) { // We will believe the data is correct - no type checking int colCountA = tableModelA.getColumnCount(), colCountB = tableModelB.getColumnCount(); boolean usingA = colCountA > colCountB; int colCount = usingA ? colCountA : colCountB; TableModel referenceTable = (usingA ? tableModelA : tableModelB); final Class<?>[] colTypes = new Class[colCount]; final String[] colNames = new String[colCount]; for (int i = 0; i < referenceTable.getColumnCount(); i++) { colTypes[i] = referenceTable.getColumnClass(i); colNames[i] = referenceTable.getColumnName(i); } int rowCount = tableModelA.getRowCount() + tableModelB.getRowCount(); // Table A final TypedTableModel typedTableModel = new TypedTableModel(colNames, colTypes, rowCount); for (int r = 0; r < tableModelA.getRowCount(); r++) { for (int c = 0; c < colTypes.length; c++) { typedTableModel.setValueAt(tableModelA.getValueAt(r, c), r, c); } } // Table B int rowCountOffset = tableModelA.getRowCount(); for (int r = 0; r < tableModelB.getRowCount(); r++) { for (int c = 0; c < colTypes.length; c++) { typedTableModel.setValueAt(tableModelB.getValueAt(r, c), r + rowCountOffset, c); } } return typedTableModel; }