Source Code Cross Referenced for ExportDelimitedWorker.java in  » Database-Client » executequery » org » executequery » gui » importexport » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » executequery » org.executequery.gui.importexport 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ExportDelimitedWorker.java
003:         *
004:         * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005:         *
006:         * This program is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU General Public License
008:         * as published by the Free Software Foundation; either version 2
009:         * of the License, or any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License
017:         * along with this program; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019:         *
020:         */
021:
022:        package org.executequery.gui.importexport;
023:
024:        import java.io.File;
025:        import java.io.FileWriter;
026:        import java.io.IOException;
027:        import java.io.PrintWriter;
028:        import java.sql.ResultSet;
029:        import java.sql.SQLException;
030:        import java.util.Vector;
031:
032:        import org.executequery.Constants;
033:        import org.executequery.util.Log;
034:        import org.underworldlabs.jdbc.DataSourceException;
035:        import org.underworldlabs.swing.util.SwingWorker;
036:
037:        /* ----------------------------------------------------------
038:         * CVS NOTE: Changes to the CVS repository prior to the 
039:         *           release of version 3.0.0beta1 has meant a 
040:         *           resetting of CVS revision numbers.
041:         * ----------------------------------------------------------
042:         */
043:
044:        /**
045:         *
046:         * @author   Takis Diakoumis
047:         * @version  $Revision: 1.7 $
048:         * @date     $Date: 2006/09/05 12:07:26 $
049:         */
050:        public class ExportDelimitedWorker extends AbstractImportExportWorker {
051:
052:            /** The thread worker object for this process */
053:            private SwingWorker worker;
054:
055:            /** 
056:             * Constructs a new instance with the specified parent object 
057:             * and progress output panel.
058:             *
059:             * @param parent - the parent for this process
060:             * @param progress - the progress panel
061:             */
062:            public ExportDelimitedWorker(ImportExportProcess parent,
063:                    ImportExportProgressPanel progress) {
064:                super (parent, progress);
065:                transferData();
066:            }
067:
068:            /** <p>Begins the transfer process setting up the
069:             *  <code>SwingWorker</code> and creating the progress
070:             *  dialog.
071:             */
072:            private void transferData() {
073:                reset();
074:
075:                // create the worker
076:                worker = new SwingWorker() {
077:                    public Object construct() {
078:                        return doWork();
079:                    }
080:
081:                    public void finished() {
082:                        String result = (String) get();
083:                        setResult(result);
084:
085:                        releaseResources(getParent().getDatabaseConnection());
086:
087:                        printResults();
088:                        setProgressStatus(-1);
089:                        getParent().setProcessComplete(result == SUCCESS);
090:                    }
091:                };
092:                worker.start();
093:            }
094:
095:            /** <p>Performs the actual processing for the worker. */
096:            private Object doWork() {
097:
098:                // counter variables
099:                int tableCount = 0;
100:                int totalRecordCount = 0;
101:                int errorCount = 0;
102:
103:                appendProgressText("Beginning export to delimited file process...");
104:                appendProgressText("Using connection: "
105:                        + getParent().getDatabaseConnection().getName());
106:
107:                // record the start time
108:                start();
109:
110:                // --------------------------------
111:                // --- begin the export process ---
112:                // --------------------------------
113:
114:                ResultSet rset = null;
115:                PrintWriter writer = null;
116:                try {
117:
118:                    // define the delimiter
119:                    char delim = getParent().getDelimiter();
120:
121:                    // whether to trim whitespace
122:                    boolean trimWhitespace = getParent().trimWhitespace();
123:
124:                    // include the column names
125:                    boolean includeColumnNames = getParent()
126:                            .includeColumnNames();
127:
128:                    // row data output buffer
129:                    StringBuffer rowData = new StringBuffer(5000);
130:
131:                    // retrieve the export to files
132:                    Vector files = getParent().getDataFileVector();
133:                    int fileCount = files.size();
134:
135:                    // ---------------------------
136:                    // --- initialise counters ---
137:                    // ---------------------------
138:
139:                    int columnCount = -1;
140:                    int recordCount = 0;
141:                    int totalRecords = 0;
142:
143:                    // ----------------------------------------
144:                    // --- begin looping through the tables ---
145:                    // ----------------------------------------
146:
147:                    for (int i = 0; i < fileCount; i++) {
148:
149:                        tableCount++;
150:                        setProgressStatus(0);
151:
152:                        DataTransferObject dto = (DataTransferObject) files
153:                                .elementAt(i);
154:
155:                        totalRecords = getTableRecordCount(dto.getTableName());
156:                        setProgressBarMaximum(totalRecords);
157:
158:                        // initialise the file object
159:                        File exportFile = new File(dto.getFileName());
160:
161:                        // append some output
162:                        outputBuffer
163:                                .append("---------------------------\nTable: ");
164:                        outputBuffer.append(dto.getTableName());
165:                        outputBuffer.append("\nRecords found: ");
166:                        outputBuffer.append(totalRecords);
167:                        outputBuffer.append("\nExport file: ");
168:                        outputBuffer.append(exportFile.getName());
169:                        appendProgressText(outputBuffer);
170:
171:                        // retrieve the columns to be exported (or all)
172:                        Vector columns = getColumns(dto.getTableName());
173:                        columnCount = columns.size();
174:
175:                        // initialise the writer
176:                        writer = new PrintWriter(new FileWriter(exportFile,
177:                                false), true);
178:
179:                        // print the column names if specified to do so
180:                        if (includeColumnNames) {
181:                            for (int k = 0, n = columnCount - 1; k < columnCount; k++) {
182:                                rowData.append(columns.elementAt(k));
183:                                if (k != n) {
184:                                    rowData.append(delim);
185:                                }
186:                            }
187:                            writer.println(rowData.toString());
188:                            rowData.setLength(0);
189:                        }
190:
191:                        appendProgressText("Exporting data...");
192:
193:                        // retrieve the result set
194:                        rset = getResultSet(dto.getTableName(), columns);
195:
196:                        // start the loop over results
197:                        while (rset.next()) {
198:
199:                            if (Thread.interrupted()) {
200:                                rset.close();
201:                                rset = null;
202:                                writer.close();
203:                                setProgressStatus(totalRecords);
204:                                throw new InterruptedException();
205:                            }
206:
207:                            setProgressStatus(recordCount);
208:
209:                            for (int j = 1; j <= columnCount; j++) {
210:                                String value = rset.getString(j);
211:                                if (value == null) {
212:                                    value = Constants.EMPTY;
213:                                } else if (trimWhitespace) {
214:                                    value = value.trim();
215:                                }
216:
217:                                rowData.append(value);
218:                                if (j != columnCount) {
219:                                    rowData.append(delim);
220:                                }
221:                            }
222:
223:                            writer.println(rowData.toString());
224:                            rowData.setLength(0);
225:                            totalRecordCount++;
226:                            recordCount++;
227:                        }
228:
229:                        rset.close();
230:                        stmnt.close();
231:                        writer.close();
232:
233:                        setProgressStatus(totalRecords);
234:
235:                        recordCount = 0;
236:                        outputBuffer.append("Export successful for table: ");
237:                        outputBuffer.append(dto.getTableName());
238:                        appendProgressText(outputBuffer);
239:
240:                        /*
241:                        if (tableCount != fileCount) {
242:                            setProgressStatus(0);
243:                        }
244:                         */
245:                    }
246:
247:                    return SUCCESS;
248:                }
249:
250:                catch (InterruptedException e) {
251:                    cancelStatement();
252:                    return CANCELLED;
253:                } catch (SQLException e) {
254:                    logException(e);
255:                    outputExceptionError(
256:                            "SQL error exporting table data to file", e);
257:                    return FAILED;
258:                } catch (DataSourceException e) {
259:                    logException(e);
260:                    outputExceptionError("Error exporting table data to file",
261:                            e);
262:                    return FAILED;
263:                } catch (IOException e) {
264:                    logException(e);
265:                    outputExceptionError(
266:                            "I/O error exporting table data to file", e);
267:                    return FAILED;
268:                } catch (OutOfMemoryError e) {
269:                    outputExceptionError("Error exporting table data to file",
270:                            e);
271:                    return FAILED;
272:                } finally {
273:
274:                    if (rset != null) {
275:                        try {
276:                            rset.close();
277:                        } catch (SQLException e) {
278:                        }
279:                    }
280:
281:                    finish();
282:                    setTableCount(tableCount);
283:                    setRecordCount(totalRecordCount + errorCount);
284:                    setErrorCount(errorCount);
285:                    setRecordCountProcessed(totalRecordCount);
286:                }
287:            }
288:
289:            private void logException(Throwable e) {
290:                if (Log.isDebugEnabled()) {
291:                    Log.debug("Error on delimited export.", e);
292:                }
293:            }
294:
295:            /** 
296:             * Cancels an in progress SQL statement. 
297:             */
298:            private void cancelStatement() {
299:                if (stmnt == null) {
300:                    return;
301:                }
302:                try {
303:                    stmnt.cancel();
304:                    stmnt.close();
305:                    stmnt = null;
306:                } catch (SQLException e) {
307:                }
308:            }
309:
310:            /**
311:             * Cancels the current in-process transfer. 
312:             */
313:            public void cancelTransfer() {
314:                worker.interrupt();
315:                getParent().cancelTransfer();
316:            }
317:
318:            /** 
319:             * Indicates that the process has completed. 
320:             */
321:            public void finished() {
322:            }
323:
324:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.