001: /*
002: * WbSchemaReport.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.sql.wbcommands;
013:
014: import java.io.File;
015: import java.io.IOException;
016: import java.sql.SQLException;
017: import java.util.List;
018: import java.util.Properties;
019: import workbench.db.ConnectionProfile;
020: import workbench.db.TableIdentifier;
021: import workbench.db.report.SchemaReporter;
022: import workbench.db.report.Workbench2Designer;
023: import workbench.interfaces.ScriptGenerationMonitor;
024: import workbench.log.LogMgr;
025: import workbench.resource.ResourceMgr;
026: import workbench.sql.SqlCommand;
027: import workbench.sql.StatementRunnerResult;
028: import workbench.storage.RowActionMonitor;
029: import workbench.util.ArgumentParser;
030: import workbench.util.ArgumentType;
031: import workbench.util.ExceptionUtil;
032: import workbench.util.StringUtil;
033: import workbench.util.StringUtil;
034: import workbench.util.WbFile;
035: import workbench.util.XsltTransformer;
036:
037: /**
038: *
039: * @author support@sql-workbench.net
040: */
041: public class WbSchemaReport extends SqlCommand implements
042: RowActionMonitor {
043: public static final String PARAM_INCLUDE_TABLES = "includeTables";
044: public static final String PARAM_INCLUDE_PROCS = "includeProcedures";
045: public static final String PARAM_INCLUDE_GRANTS = "includeTableGrants";
046: public static final String PARAM_INCLUDE_SEQUENCES = "includeSequences";
047: public static final String PARAM_INCLUDE_VIEWS = "includeViews";
048:
049: public static final String VERB = "WBREPORT";
050: private SchemaReporter reporter;
051: private int currentTable = 0;
052:
053: public WbSchemaReport() {
054: cmdLine = new ArgumentParser();
055: cmdLine.addArgument("types");
056: cmdLine.addArgument("file");
057: cmdLine.addArgument("namespace");
058: cmdLine.addArgument("tables", ArgumentType.TableArgument);
059: cmdLine.addArgument("schemas");
060: cmdLine.addArgument("format", StringUtil
061: .stringToList("wb,dbdesigner"));
062: cmdLine.addArgument("useSchemaName", ArgumentType.BoolArgument);
063: cmdLine.addArgument(PARAM_INCLUDE_VIEWS,
064: ArgumentType.BoolArgument);
065: cmdLine.addArgument(PARAM_INCLUDE_PROCS,
066: ArgumentType.BoolArgument);
067: cmdLine.addArgument(PARAM_INCLUDE_TABLES,
068: ArgumentType.BoolArgument);
069: cmdLine.addArgument(PARAM_INCLUDE_GRANTS,
070: ArgumentType.BoolArgument);
071: cmdLine.addArgument(PARAM_INCLUDE_SEQUENCES,
072: ArgumentType.BoolArgument);
073: cmdLine.addArgument(WbXslt.ARG_STYLESHEET);
074: cmdLine.addArgument(WbXslt.ARG_OUTPUT);
075: }
076:
077: public String getVerb() {
078: return VERB;
079: }
080:
081: public StatementRunnerResult execute(final String sql)
082: throws SQLException {
083: boolean dbDesigner = false;
084: StatementRunnerResult result = new StatementRunnerResult();
085:
086: cmdLine.parse(getCommandLine(sql));
087:
088: if (cmdLine.hasUnknownArguments()) {
089: setUnknownMessage(result, cmdLine, ResourceMgr
090: .getString("ErrSchemaReportWrongParameters"));
091: return result;
092: }
093:
094: WbFile output = evaluateFileArgument(cmdLine.getValue("file"));
095:
096: if (output == null) {
097: result.addMessage(ResourceMgr
098: .getString("ErrSchemaReportWrongParameters"));
099: result.setFailure();
100: return result;
101: }
102:
103: String format = cmdLine.getValue("format");
104: if (StringUtil.isEmptyString(format))
105: format = "xml";
106:
107: if ("dbdesigner".equalsIgnoreCase(format)) {
108: dbDesigner = true;
109: } else if (!"xml".equalsIgnoreCase(format)
110: && !"wb".equalsIgnoreCase(format)
111: && !"wbxml".equalsIgnoreCase(format)) {
112: result.addMessage(ResourceMgr
113: .getString("ErrSchemaReportWrongParameters"));
114: result.setFailure();
115: return result;
116: }
117: String namespace = cmdLine.getValue("namespace");
118: this .reporter = new SchemaReporter(currentConnection);
119: this .reporter.setNamespace(namespace);
120: this .reporter.setDbDesigner(dbDesigner);
121: this .reporter.setIncludeViews(cmdLine.getBoolean(
122: PARAM_INCLUDE_VIEWS, true));
123: TableIdentifier[] tables = this .parseTables();
124: if (tables != null) {
125: this .reporter.setTableList(tables);
126: } else {
127: String arg = cmdLine.getValue("schemas");
128: List<String> schemas = StringUtil.stringToList(arg, ",");
129: this .reporter.setSchemas(schemas);
130: }
131:
132: String alternateSchema = cmdLine.getValue("useschemaname");
133: this .reporter.setSchemaNameToUse(alternateSchema);
134:
135: this .reporter.setProgressMonitor(this );
136:
137: if (this .rowMonitor != null) {
138: this .rowMonitor
139: .setMonitorType(RowActionMonitor.MONITOR_PROCESS);
140: }
141:
142: this .reporter.setIncludeTables(cmdLine.getBoolean(
143: PARAM_INCLUDE_TABLES, true));
144: this .reporter.setIncludeProcedures(cmdLine.getBoolean(
145: PARAM_INCLUDE_PROCS, false));
146: this .reporter.setIncludeGrants(cmdLine.getBoolean(
147: PARAM_INCLUDE_GRANTS, false));
148: this .reporter.setIncludeSequences(cmdLine.getBoolean(
149: PARAM_INCLUDE_SEQUENCES, false));
150:
151: if (currentConnection != null
152: && currentConnection.getMetadata().isOracle()) {
153: // check if remarksReporting is turned on for Oracle, if not issue a warning.
154: ConnectionProfile prof = currentConnection.getProfile();
155: Properties props = prof.getConnectionProperties();
156: String value = "false";
157: if (props != null) {
158: value = props.getProperty("remarksReporting", "false");
159: }
160: if (!"true".equals(value)) {
161: result
162: .addMessage(ResourceMgr
163: .getString("MsgSchemaReporterOracleRemarksWarning"));
164: result.addMessage("");
165: }
166: }
167:
168: // currentTable will be incremented as we have registered
169: // this object as the RowActionMonitor of the SchemaReporter
170: // see setCurrentObject()
171: this .currentTable = 0;
172:
173: // String wbReportFilename = output.getFullPath();
174: // if (dbDesigner)
175: // {
176: // WbFile f = new WbFile(wbReportFilename);
177: // String dir = f.getParent();
178: // String fname = f.getName();
179: // WbFile nf = new WbFile(dir, "__wb_" + fname);
180: // wbReportFilename = nf.getFullPath();
181: // }
182: this .reporter.setOutputFilename(output.getFullPath());
183:
184: try {
185: this .reporter.writeXml();
186: } catch (IOException e) {
187: result.setFailure();
188: result.addMessage(e.getMessage());
189: }
190:
191: String xslt = cmdLine.getValue(WbXslt.ARG_STYLESHEET);
192: String xsltOutput = cmdLine.getValue(WbXslt.ARG_OUTPUT);
193:
194: // if (dbDesigner && result.isSuccess())
195: // {
196: // try
197: // {
198: // this.setCurrentObject(ResourceMgr.getString("MsgConvertReport2Designer"), -1, -1);
199: // Workbench2Designer converter = new Workbench2Designer(new File(wbReportFilename));
200: // converter.transformWorkbench2Designer();
201: // converter.writeOutputFile(output);
202: // }
203: // catch (Exception e)
204: // {
205: // result.setFailure();
206: // LogMgr.logError("WbSchemaReport.execute()", "Error generating DBDesigner file", e);
207: // String msg = ResourceMgr.getString("ErrGeneratingDbDesigner");
208: // msg = StringUtil.replace(msg, "%wbfile%", output.getFullPath());
209: // msg = StringUtil.replace(msg, "%error%", ExceptionUtil.getDisplay(e));
210: // result.addMessage(msg);
211: // }
212: // }
213: if (result.isSuccess()) {
214: String msg = ResourceMgr.getFormattedString(
215: "MsgSchemaReportTablesWritten", currentTable,
216: output.getFullPath());
217: result.addMessage(msg);
218: result.setSuccess();
219: }
220:
221: if (!StringUtil.isEmptyString(xslt)
222: && !StringUtil.isEmptyString(xsltOutput)) {
223: try {
224: XsltTransformer.transformFile(output.getFullPath(),
225: xsltOutput, xslt);
226: result.addMessage(ResourceMgr.getFormattedString(
227: "MsgXsltSuccessful", xsltOutput));
228: result.setSuccess();
229: } catch (Exception e) {
230: LogMgr.logError("WbSchemaReport.execute()",
231: "Error when transforming '"
232: + output.getFullPath() + "' to '"
233: + xsltOutput + "' using " + xslt, e);
234: result.addMessage(e.getMessage());
235: }
236: }
237:
238: return result;
239: }
240:
241: private TableIdentifier[] parseTables() {
242: String tables = this .cmdLine.getValue("tables");
243: if (tables == null)
244: return null;
245: List l = StringUtil.stringToList(tables, ",");
246: int count = l.size();
247: TableIdentifier[] result = new TableIdentifier[count];
248:
249: for (int i = 0; i < count; i++) {
250: String table = (String) l.get(i);
251: if (table == null)
252: continue;
253: if (table.trim().length() == 0)
254: continue;
255: table = this .currentConnection.getMetadata()
256: .adjustObjectnameCase(table);
257: result[i] = new TableIdentifier(table);
258: }
259: return result;
260: }
261:
262: public void cancel() throws SQLException {
263: if (this .reporter != null) {
264: this .reporter.cancelExecution();
265: }
266: }
267:
268: public void setCurrentObject(String anObject, long number,
269: long total) {
270: if (anObject == null) {
271: this .currentTable = 0;
272: return;
273: } else {
274: this .currentTable++;
275: if (this .rowMonitor != null) {
276: if (number > 0) {
277: this .rowMonitor.setCurrentObject(anObject, number,
278: total);
279: } else if (rowMonitor instanceof ScriptGenerationMonitor) {
280: ((ScriptGenerationMonitor) this .rowMonitor)
281: .setCurrentObject(anObject);
282: }
283:
284: }
285: }
286: }
287:
288: public void setCurrentRow(long number, long total) {
289: }
290:
291: public int getMonitorType() {
292: return RowActionMonitor.MONITOR_PLAIN;
293: }
294:
295: public void setMonitorType(int aType) {
296: }
297:
298: public void jobFinished() {
299: }
300:
301: public void saveCurrentType(String type) {
302: }
303:
304: public void restoreType(String type) {
305: }
306:
307: }
|