KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > ncsql > NCDatabase


1 package jimm.datavision.source.ncsql;
2 import jimm.datavision.*;
3 import jimm.datavision.source.*;
4 import jimm.util.XMLWriter;
5 import java.util.*;
6
7 /**
8  * An <code>NCDatabase</code> a data source that acts like a SQL
9  * database data source but can't run reports. It gets its column
10  * descriptions from metadata described in the report XML file.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class NCDatabase extends DataSource {
15
16 protected static final String JavaDoc ORPHANS_TABLE = "no_table";
17
18 protected TreeMap tables;
19
20 public NCDatabase(Report report) {
21     super(report, new NCQuery(report));
22     tables = new TreeMap();
23 }
24
25 public boolean canRunReports() { return false; }
26 public boolean canJoinTables() { return true; }
27 public boolean isSQLGenerated() { return true; }
28 public boolean isConnectionEditable() { return false; }
29 public boolean areRecordsSelectable() { return true; }
30 public boolean areRecordsSortable() { return true; }
31 public boolean canGroupRecords() { return true; }
32
33 /**
34  * This override not only remembers the column but also hands it to the
35  * query for cacheing.
36  *
37  * @param col a column
38  */

39 public void addColumn(Column col) {
40     // We need to turn the column into a NCColumn and perhaps add an
41
// NCTable, too.
42

43     // Parse the column's full name, looking for the table name and column
44
// name.
45
String JavaDoc fullName = col.fullName();
46     int lastDotPos = fullName.lastIndexOf('.');
47     String JavaDoc tableName = null;
48     String JavaDoc colName = null;
49     if (lastDotPos == -1) {
50     tableName = ORPHANS_TABLE;
51     colName = fullName;
52     }
53     else {
54     tableName = fullName.substring(0, lastDotPos);
55     colName = fullName.substring(lastDotPos + 1);
56     }
57
58     // Find the table. Create one if we don't already have one.
59
NCTable table = (NCTable)tables.get(tableName);
60     if (table == null) {
61     table = new NCTable(this, tableName);
62     tables.put(tableName, table);
63     }
64
65     table.addColumn(new NCColumn(table, colName, col.getType()));
66 }
67
68 /**
69  * Given an id (a column name), returns the column that has that id. If no
70  * column with the specified id exists, returns <code>null</code>. Uses
71  * <code>Table.findColumn</code>.
72  *
73  * @param id a column id
74  * @return a column, or <code>null</code> if no column with the specified
75  * id exists
76  * @see Table#findColumn
77  */

78 public Column findColumn(Object JavaDoc id) {
79     if (tables == null)
80     return null;
81
82     for (Iterator iter = tables.values().iterator(); iter.hasNext(); ) {
83     Column col = ((Table)iter.next()).findColumn(id);
84     if (col != null)
85         return col;
86     }
87     return null;
88 }
89
90 public Iterator tables() {
91     return tables.values().iterator();
92 }
93
94 public Iterator tablesUsedInReport() {
95     return ((NCQuery)query).getTablesUsed().iterator();
96 }
97
98 public Iterator columns() {
99     return new ColumnIterator(tables.values().iterator());
100 }
101
102 public DataCursor execute() { return null; }
103
104 /**
105  * Writes this database and all its tables as an XML tag.
106  *
107  * @param out a writer that knows how to write XML
108  */

109 protected void doWriteXML(XMLWriter out) {
110     out.startElement("nc-database");
111     if (metadataURL != null)
112     out.textElement("metadata-url", metadataURL);
113     else
114     for (Iterator iter = columns(); iter.hasNext(); )
115         ((Column)iter.next()).writeXML(out);
116     out.endElement();
117 }
118
119 }
120
Popular Tags