001: /*
002: * ReportView.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.report;
013:
014: import java.io.IOException;
015: import java.io.Writer;
016: import java.sql.SQLException;
017: import java.util.Collection;
018: import java.util.List;
019:
020: import workbench.db.ColumnIdentifier;
021: import workbench.db.TableIdentifier;
022: import workbench.db.WbConnection;
023: import workbench.util.StrBuffer;
024: import java.util.Collections;
025: import workbench.db.IndexDefinition;
026: import workbench.util.StringUtil;
027:
028: /**
029: * A class to hold information about a database view that
030: * will eventually be stored in an XML report.
031: * It uses a {@link workbench.db.TableIdentifier} to store the
032: * view's name, and {@link workbench.db.ColumnIdentifier} to
033: * store the view's columns.
034: *
035: * @author support@sql-workbench.net
036: */
037: public class ReportView {
038: public static final String TAG_VIEW_DEF = "view-def";
039: public static final String TAG_VIEW_NAME = "view-name";
040: public static final String TAG_VIEW_CATALOG = "view-catalog";
041: public static final String TAG_VIEW_SCHEMA = "view-schema";
042: public static final String TAG_VIEW_COMMENT = "view-comment";
043: public static final String TAG_VIEW_SOURCE = "view-source";
044:
045: private TableIdentifier view;
046: private ReportColumn[] columns;
047: private String viewComment;
048: private TagWriter tagWriter = new TagWriter();
049: private IndexReporter index;
050:
051: /** The schema name to be used in the generated XML */
052: private String schemaNameToUse = null;
053:
054: private String namespace = null;
055: private CharSequence viewSource;
056:
057: public ReportView(TableIdentifier tbl) {
058: this (tbl, (String) null);
059: }
060:
061: public ReportView(TableIdentifier tbl, String nspace) {
062: this .view = tbl;
063: this .namespace = nspace;
064: tagWriter.setNamespace(this .namespace);
065: }
066:
067: /**
068: * Initialize this ReportView.
069: * This will read the following information for the table:
070: * <ul>
071: * <li>columns for the table using {@link workbench.db.DbMetadata#getTableColumns(workbench.db.TableIdentifier)}</li>
072: * <li>the comments for the view using {@link workbench.db.DbMetadata#getTableComment(workbench.db.TableIdentifier)}</li>
073: * <li>the source for the view using{@link workbench.db.DbMetadata#getViewSource(workbench.db.TableIdentifier)}</li>
074: *</ul>
075: */
076: public ReportView(TableIdentifier tbl, WbConnection conn,
077: boolean includeIndex, String nspace) throws SQLException {
078: this .view = tbl;
079: this .namespace = nspace;
080:
081: if (tbl.getSchema() == null) {
082: // This is important for e.g. Oracle. Otherwise the table definition
083: // will contain multiple columns if a table exists more then once in
084: // different schemas with the same name
085: tbl.setSchema(conn.getMetadata().getSchemaToUse());
086: }
087: List<ColumnIdentifier> cols = conn.getMetadata()
088: .getTableColumns(tbl);
089: Collections.sort(cols);
090:
091: this .viewComment = conn.getMetadata()
092: .getTableComment(this .view);
093: String schema = this .view.getSchema();
094: if (schema == null || schema.length() == 0) {
095: schema = conn.getMetadata().getSchemaToUse();
096: if (schema != null)
097: this .view.setSchema(schema);
098: }
099: this .viewSource = conn.getMetadata().getViewSource(tbl);
100: if (viewSource == null)
101: viewSource = StringUtil.EMPTY_STRING;
102: this .setColumns(cols);
103: this .tagWriter.setNamespace(namespace);
104: if (includeIndex) {
105: this .index = new IndexReporter(tbl, conn);
106: this .index.setNamespace(namespace);
107: }
108: }
109:
110: /**
111: * Return the list of IndexDefinitions for this view
112: * @return defined indexes, maybe null
113: */
114: public Collection<IndexDefinition> getIndexList() {
115: if (this .index == null)
116: return null;
117: return this .index.getIndexList();
118: }
119:
120: /**
121: * Define the columns that belong to this table
122: */
123: public void setColumns(List<ColumnIdentifier> cols) {
124: if (cols == null)
125: return;
126: int numCols = cols.size();
127: this .columns = new ReportColumn[numCols];
128: int i = 0;
129: for (ColumnIdentifier column : cols) {
130: this .columns[i] = new ReportColumn(column);
131: this .columns[i].setNamespace(this .namespace);
132: this .columns[i].setRealColumn(false);
133: i++;
134: }
135: }
136:
137: public void setSchemaNameToUse(String name) {
138: this .schemaNameToUse = name;
139: }
140:
141: public void writeXml(Writer out) throws IOException {
142: StrBuffer line = this .getXml();
143: line.writeTo(out);
144: }
145:
146: public StrBuffer getXml() {
147: return getXml(new StrBuffer(" "));
148: }
149:
150: public TableIdentifier getView() {
151: return this .view;
152: }
153:
154: public String getViewComment() {
155: return this .viewComment;
156: }
157:
158: public CharSequence getViewSource() {
159: return this .viewSource;
160: }
161:
162: public void appendTableNameXml(StrBuffer toAppend, StrBuffer indent) {
163: tagWriter.appendTag(toAppend, indent, TAG_VIEW_CATALOG,
164: this .view.getCatalog());
165: tagWriter.appendTag(toAppend, indent, TAG_VIEW_SCHEMA,
166: (this .schemaNameToUse == null ? this .view.getSchema()
167: : this .schemaNameToUse));
168: tagWriter.appendTag(toAppend, indent, TAG_VIEW_NAME, this .view
169: .getTableName());
170: }
171:
172: public StrBuffer getXml(StrBuffer indent) {
173: return getXml(indent, true);
174: }
175:
176: /**
177: * Return an XML representation of this view information.
178: * The columns will be listed alphabetically not in the order
179: * they were retrieved from the database.
180: */
181: public StrBuffer getXml(StrBuffer indent, boolean includeIndex) {
182: StrBuffer line = new StrBuffer(this .viewSource.length() + 200);
183: StrBuffer colindent = new StrBuffer(indent);
184: colindent.append(indent);
185:
186: tagWriter.appendOpenTag(line, indent, TAG_VIEW_DEF, "name",
187: this .view.getTableName());
188: line.append('\n');
189: appendTableNameXml(line, colindent);
190: tagWriter.appendTag(line, colindent, TAG_VIEW_COMMENT,
191: this .viewComment, true);
192: int cols = this .columns.length;
193: for (int i = 0; i < cols; i++) {
194: this .columns[i].appendXml(line, colindent);
195: }
196: writeSourceTag(tagWriter, line, colindent, viewSource);
197: if (includeIndex && this .index != null)
198: this .index.appendXml(line, colindent);
199: tagWriter.appendCloseTag(line, indent, TAG_VIEW_DEF);
200: return line;
201: }
202:
203: public static final void writeSourceTag(TagWriter tagWriter,
204: StrBuffer target, StrBuffer indent, CharSequence source) {
205: if (source == null)
206: return;
207: tagWriter.appendOpenTag(target, indent, TAG_VIEW_SOURCE);
208: target.append(TagWriter.CDATA_START);
209: target.append(source);
210: target.append(TagWriter.CDATA_END);
211: target.append('\n');
212: tagWriter.appendCloseTag(target, indent, TAG_VIEW_SOURCE);
213:
214: }
215:
216: /**
217: * The namespace to be used for the XML representation
218: */
219: public void setNamespace(String namespace) {
220: this.tagWriter.setNamespace(namespace);
221: }
222:
223: }
|