KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > DataSource


1 package jimm.datavision.source;
2 import jimm.datavision.*;
3 import jimm.util.XMLWriter;
4 import jimm.util.I18N;
5 import java.io.IOException JavaDoc;
6 import java.io.FileNotFoundException JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import org.xml.sax.InputSource JavaDoc;
10
11 /**
12  * An abstract data source.
13  *
14  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
15  * @see Column
16  * @see Table
17  * @see Query
18  */

19 public abstract class DataSource implements Writeable {
20
21 protected Report report;
22 protected Query query;
23 protected String JavaDoc metadataURL;
24
25 public DataSource(Report r, Query q) {
26     report = r;
27     query = q;
28 }
29
30 /**
31  * Used to enable/disable the &quot;Table Linker&quot; menu item.
32  *
33  * @return <code>true</code> if the &quot;Table Linker&quot; menu item
34  * should be enabled.
35  */

36 public abstract boolean canJoinTables();
37
38 /**
39  * Used to enable/disable the &quot;SQL Query Text&quot; menu item.
40  *
41  * @return <code>true</code> if the &quot;SQL Query Text&quot; menu item
42  * should be enabled.
43  */

44 public abstract boolean isSQLGenerated();
45
46 /**
47  * Used to enable/disable the &quot;Connection&quot; menu item.
48  *
49  * @return <code>true</code> if the &quot;Connection&quot; menu item
50  * should be enabled.
51  */

52 public abstract boolean isConnectionEditable();
53
54 /**
55  * Used to enable/disable the &quot;Select Records&quot; menu item.
56  *
57  * @return <code>true</code> if the &quot;Select Records&quot; menu item
58  * should be enabled.
59  */

60 public abstract boolean areRecordsSelectable();
61
62 /**
63  * Used to enable/disable the &quot;Sort By&quot; menu item.
64  *
65  * @return <code>true</code> if the &quot;Sort By&quot; menu item
66  * should be enabled.
67  */

68 public abstract boolean areRecordsSortable();
69
70 /**
71  * Used to enable/disable the &quot;Group By&quot; menu item.
72  *
73  * @return <code>true</code> if the &quot;Group By&quot; menu item
74  * should be enabled.
75  */

76 public abstract boolean canGroupRecords();
77
78 /**
79  * Used to enable/disable the &quot;Run&quot; and &quot;Export&quot; menu
80  * items. Most data sources will enable these, of course.
81  *
82  * @return <code>true</code> if the &quot;Run&quot; and &quot;Export&quot;
83  * menu items should be enabled.
84  */

85 public boolean canRunReports() { return true; }
86
87 /**
88  * Returns <code>true</code> if this data source uses a file to retrieve
89  * data. The default implementation returns false.
90  *
91  * @return <code>true</code> if this data source uses a file to retrieve
92  * data
93  */

94 public boolean needsSourceFile() { return false; }
95
96 /**
97  * Accepts the path to a data source file. The default implementation does
98  * nothing.
99  *
100  * @param filePath the full path to a file
101  */

102 public void setSourceFile(String JavaDoc filePath) throws FileNotFoundException JavaDoc { }
103
104 /**
105  * Tells this data source to re-use (perhaps re-open) the current data
106  * source file. The default implementation does nothing.
107  */

108 public void reuseSourceFile() throws FileNotFoundException JavaDoc { }
109
110 public Report getReport() { return report; }
111
112 public Query getQuery() { return query; }
113
114 public abstract DataCursor execute() throws Exception JavaDoc;
115
116 /**
117  * Called from <code>ReportReader.column</code> to add a column to a
118  * data source.
119  * <p>
120  * The default implementation does nothing.
121  *
122  * @param col a column
123  */

124 public void addColumn(Column col) {}
125
126 /**
127  * Called from <code>Report.reloadColumns/code>, this method gives the
128  * data source a chance to tell its ancillary objects (such as the query)
129  * to reload column objects.
130  * <p>
131  * This is necessary, for example, after a SQL database data source has
132  * reloaded all of its table and column information. The old column
133  * objects no longer exist. New ones (with the same ids, we assume) have
134  * taken their place.
135  */

136 public void reloadColumns() {
137     if (query != null)
138     query.reloadColumns(this);
139 }
140
141 /**
142  * Reads metadata from a URL. We save the URL string so we can write it
143  * back out when we write to XML.
144  *
145  * @param urlString where to get the metadata
146  * @see MetadataReader
147  */

148 public void readMetadataFrom(String JavaDoc urlString) throws Exception JavaDoc {
149     try {
150     metadataURL = urlString;
151     new MetadataReader(this).read(new InputSource JavaDoc(urlString));
152     }
153     catch (IOException JavaDoc ioe) {
154     ErrorHandler.error(I18N.get("DataSource.metadata_err"), ioe,
155                I18N.get("DataSource.metadata_err_title"));
156     }
157 }
158
159 /**
160  * Given an id, returns the column that has that id. If no column with the
161  * specified id exists, returns <code>null</code>.
162  *
163  * @return a column, or <code>null</code> if no column with the specified
164  * id exists
165  */

166 public abstract Column findColumn(Object JavaDoc id);
167
168 /**
169  * Returns the index of the specified selectable.
170  *
171  * @param sel a selectable
172  */

173 public int indexOfSelectable(Selectable sel) {
174     return query.indexOfSelectable(sel);
175 }
176
177 /**
178  * Returns an iterator over all tables, or <code>null</code> if the
179  * data source does not have tables (for example, a character-separated
180  * file data source).
181  *
182  * @return a possibly <code>null</code> iterator over all tables
183  */

184 public abstract Iterator JavaDoc tables();
185
186 /**
187  * Returns an iterator over all tables actually used in the report, or
188  * <code>null</code> if the data source does not have tables (for example,
189  * a character-separated file data source).
190  *
191  * @return a possibly <code>null</code> iterator over all tables used
192  * in the report
193  */

194 public abstract Iterator JavaDoc tablesUsedInReport();
195
196 /**
197  * Returns an iterator over all columns.
198  *
199  * @return an iterator over all columns
200  */

201 public abstract Iterator JavaDoc columns();
202
203 /**
204  * Returns an iterator over all the columns in only the tables used by the
205  * report, or over all columns if this data source does not have tables.
206  *
207  * @return an iterator over all columns in only tables used by the
208  * report, or over all columns if this data source does not have tables
209  */

210 public Iterator JavaDoc columnsInTablesUsedInReport() {
211     Iterator JavaDoc iter = tablesUsedInReport();
212     if (iter == null)
213     return columns();
214
215     // Create a new collection and return an iterator over it.
216
ArrayList JavaDoc list = new ArrayList JavaDoc();
217     while (iter.hasNext()) {
218     Table t = (Table)iter.next();
219     list.addAll(t.columns.values());
220     }
221     return list.iterator();
222 }
223
224 public void removeSort(Selectable s) {
225     query.removeSort(s);
226 }
227
228 /**
229  * Returns <code>true</code> if the specified parameter exists within this
230  * data source's query.
231  *
232  * @param p a parameter
233  * @return <code>true</code> if the specified parameter exists within
234  * the query
235  * @see Query#containsReferenceTo
236  */

237 public boolean containsReferenceTo(Parameter p) {
238     return query.containsReferenceTo(p);
239 }
240
241 /**
242  * Writes this data source and its query as an XML tag.
243  *
244  * @param out a writer that knows how to write XML
245  */

246 public void writeXML(XMLWriter out) {
247     out.startElement("source");
248     doWriteXML(out);
249     query.writeXML(out);
250     out.endElement();
251 }
252
253 protected abstract void doWriteXML(XMLWriter out);
254
255 }
256
Popular Tags