Source Code Cross Referenced for ExportWebHelper.java in  » Forum » mvnforum-1.1 » com » mvnforum » admin » 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 » Forum » mvnforum 1.1 » com.mvnforum.admin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ExportWebHelper.java,v 1.12 2007/10/09 11:09:12 lexuanttkhtn Exp $
003:         * $Author: lexuanttkhtn $
004:         * $Revision: 1.12 $
005:         * $Date: 2007/10/09 11:09:12 $
006:         *
007:         * ====================================================================
008:         *
009:         * Copyright (C) 2002-2007 by MyVietnam.net
010:         *
011:         * All copyright notices regarding mvnForum MUST remain
012:         * intact in the scripts and in the outputted HTML.
013:         * The "powered by" text/logo with a link back to
014:         * http://www.mvnForum.com and http://www.MyVietnam.net in
015:         * the footer of the pages MUST remain visible when the pages
016:         * are viewed on the internet or intranet.
017:         *
018:         * This program is free software; you can redistribute it and/or modify
019:         * it under the terms of the GNU General Public License as published by
020:         * the Free Software Foundation; either version 2 of the License, or
021:         * any later version.
022:         *
023:         * This program is distributed in the hope that it will be useful,
024:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
025:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
026:         * GNU General Public License for more details.
027:         *
028:         * You should have received a copy of the GNU General Public License
029:         * along with this program; if not, write to the Free Software
030:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031:         *
032:         * Support can be obtained from support forums at:
033:         * http://www.mvnForum.com/mvnforum/index
034:         *
035:         * Correspondence and Marketing Questions can be sent to:
036:         * info at MyVietnam net
037:         *
038:         * @author: Igor Manic
039:         */
040:        package com.mvnforum.admin;
041:
042:        import java.io.*;
043:        import java.sql.*;
044:        import java.util.*;
045:        import java.util.zip.ZipEntry;
046:        import java.util.zip.ZipOutputStream;
047:
048:        import javax.servlet.http.HttpServletRequest;
049:
050:        import com.mvnforum.*;
051:        import com.mvnforum.admin.importexport.XMLUtil;
052:        import com.mvnforum.admin.importexport.XMLWriter;
053:        import net.myvietnam.mvncore.db.DBUtils;
054:        import net.myvietnam.mvncore.exception.*;
055:        import net.myvietnam.mvncore.util.I18nUtil;
056:        import org.apache.commons.logging.Log;
057:        import org.apache.commons.logging.LogFactory;
058:
059:        /**
060:         * @author Igor Manic
061:         * @version $Revision: 1.12 $, $Date: 2007/10/09 11:09:12 $
062:         * <br/>
063:         * <code>ExportWebHelper</code> todo Igor: enter description
064:         *
065:         */
066:        public class ExportWebHelper {
067:
068:            /** Message log. */
069:            private static Log log = LogFactory.getLog(ExportWebHelper.class);
070:
071:            /** Cannot instantiate. */
072:            private ExportWebHelper() {
073:                super ();
074:            }
075:
076:            // =================================================================
077:            // ===================== MAIN PUBLIC METHODS =======================
078:            // =================================================================
079:            public static void exportXml(String filename,
080:                    HttpServletRequest request, int logonMemberID,
081:                    String logonMemberName, Calendar backupTime, String backupIP)
082:                    throws ExportException {
083:
084:                log.debug("Making backup xml = \"" + filename + "\".");
085:                String filepath = MVNForumConfig.getBackupDir()
086:                        + File.separatorChar + filename;
087:                File resultFile = new File(filepath);
088:                XMLWriter xmlWriter = new XMLWriter("   "/*indentString*/,
089:                        resultFile);
090:                try {
091:                    createBackupXml(xmlWriter, backupTime, backupIP);
092:                } catch (ExportException e) {
093:                    /* xmlWriter.close() will be called again in finally section, but,
094:                     * nevermind, I'll ignore IOException then. Anyway, the most
095:                     * important is that it is called in any case, and here, in case
096:                     * of exception, I must first close xmlWriter, before trying
097:                     * to delete resultFile.
098:                     */
099:                    try {
100:                        xmlWriter.close();
101:                    } catch (IOException ee) {
102:                        log
103:                                .error("I/O error while closing created XML. Ignoring...");
104:                        //ignore this
105:                    } finally {
106:                        xmlWriter = null;
107:                    }
108:                    if (resultFile != null) {
109:                        resultFile.delete();
110:                        resultFile = null;
111:                    }
112:                    throw e; //rethrow; just wanted to ensure bad resultFile is deleted
113:                } finally {
114:                    //see above section "catch ExportException"
115:                    try {
116:                        xmlWriter.close();
117:                    } catch (IOException e) {
118:                        log
119:                                .error("I/O error while closing created XML. Ignoring...");
120:                        //ignore this
121:                    } finally {
122:                        xmlWriter = null;
123:                    }
124:                }
125:                /*return resultFile;*/resultFile = null;
126:            }
127:
128:            public static void exportZip(String filename,
129:                    HttpServletRequest request, int logonMemberID,
130:                    String logonMemberName, Calendar backupTime, String backupIP)
131:                    throws ExportException {
132:
133:                log.debug("Making backup zip = \"" + filename + "\".");
134:                // First, make the main XML file, in "temp" directory.
135:                File mainXmlFile = null;
136:
137:                Locale locale = I18nUtil.getLocaleInRequest(request);
138:
139:                try {
140:                    mainXmlFile = File.createTempFile("IMPORT", ".xml",
141:                            new File(MVNForumConfig.getTempDir()));
142:                    XMLWriter xmlWriter = new XMLWriter("   "/*indentString*/,
143:                            mainXmlFile);
144:                    try {
145:                        createBackupXml(xmlWriter, backupTime, backupIP);
146:                    } finally {
147:                        xmlWriter.close();
148:                        xmlWriter = null;
149:                    }
150:                } catch (IOException e) {
151:                    log.error("I/O error while creating XML.");
152:                    if (mainXmlFile != null) {
153:                        mainXmlFile.delete();
154:                        mainXmlFile = null;
155:                    }
156:                    String localizedMessage = MVNForumResourceBundle
157:                            .getString(locale,
158:                                    "mvncore.exception.ExportException.io_error.create_mail_xml_file");
159:                    throw new ExportException(localizedMessage);
160:                    //throw new ExportException("I/O error while creating main XML file.", e);
161:                } catch (ExportException e) {
162:                    if (mainXmlFile != null) {
163:                        mainXmlFile.delete();
164:                        mainXmlFile = null;
165:                    }
166:                    throw e; //rethrow; just wanted to ensure mainXmlFile is deleted
167:                }
168:
169:                /* Second, create ZIP file in "backup" directory and add avatarsDir, attachsDir, mainXmlFile.
170:                 * I am adding XML file at the end, since it might be better for import to process
171:                 * XML after the other files are copied. It's not so important, but it's maybe
172:                 * more convenient for the admin to see those file extraction prior to
173:                 * importing XML, since when that XML processing starts, he will be
174:                 * "bombed" with dozens of messages about database items...
175:                 */
176:                String zipFilePath = MVNForumConfig.getBackupDir()
177:                        + File.separatorChar + filename;
178:                String avatarsDir = request.getSession().getServletContext()
179:                        .getRealPath(MVNForumGlobal.UPLOADED_AVATAR_DIR);
180:                String attachsDir = MVNForumConfig.getAttachmentDir();
181:
182:                ZipOutputStream out = null;
183:                try {
184:                    //now create ZIP file
185:                    out = new ZipOutputStream(new FileOutputStream(zipFilePath));
186:                    //Select your choice of STORED (not compressed) or DEFLATED (compressed).
187:                    out.setMethod(ZipOutputStream.DEFLATED);
188:
189:                    //add avatarsDir and attachsDir
190:                    addDirectory(out, new File(avatarsDir),
191:                            MVNForumConfig.BACKUP_FILE_AvatarsDirNameInZip);
192:                    addDirectory(out, new File(attachsDir),
193:                            MVNForumConfig.BACKUP_FILE_AttachsDirNameInZip);
194:                    //add mainXmlFile
195:                    addFile(out, mainXmlFile,
196:                            MVNForumConfig.BACKUP_FILE_MainXmlFileNameInZip);
197:                } catch (FileNotFoundException e) {
198:                    log.error("Can't find a file that has been created.");
199:                    String localizedMessage = MVNForumResourceBundle
200:                            .getString(locale,
201:                                    "mvncore.exception.ExportException.cannot_file_created");
202:                    throw new ExportException(localizedMessage);
203:                    //throw new ExportException("Can't find a file that has been created.", e);
204:                } catch (IOException e) {
205:                    log.error("I/O error while making ZIP file.");
206:                    try {
207:                        out.close();
208:                    } catch (Exception ee) {
209:                    }
210:                    out = null;
211:                    try {
212:                        new File(zipFilePath).delete();
213:                    } catch (Exception ee) {
214:                    }
215:                    String localizedMessage = MVNForumResourceBundle
216:                            .getString(locale,
217:                                    "mvncore.exception.ExportException.io_error.make_zip_file");
218:                    throw new ExportException(localizedMessage);
219:                    //throw new ExportException("I/O error while making ZIP file.", e);
220:                } finally {
221:                    if (out != null)
222:                        try {
223:                            out.flush();
224:                            out.close();
225:                        } catch (IOException e) {
226:                        }
227:                    out = null;
228:                    //delete mainXmlFile, so I don't get both XML and ZIP in the backup repository
229:                    if (mainXmlFile != null) {
230:                        mainXmlFile.delete();
231:                        mainXmlFile = null;
232:                    }
233:                }
234:                /*return new File(zipFilePath);*/
235:            }
236:
237:            private static void addFile(ZipOutputStream out, File file,
238:                    String relativePath) throws IOException {
239:                byte[] buf = new byte[1024];
240:                FileInputStream in = new FileInputStream(file);
241:
242:                // Add ZIP entry to output stream.
243:                out.putNextEntry(new ZipEntry(relativePath));
244:                //ZipEntry zipentry = new ZipEntry(rgstring[i]);
245:                //zipentry.setSize(file.length());
246:                //zipentry.setTime(file.lastModified());
247:                //out.putNextEntry(zipentry);
248:
249:                //Transfer bytes from the file to the ZIP file
250:                int len;
251:                while ((len = in.read(buf)) > 0) {
252:                    out.write(buf, 0, len);
253:                }
254:
255:                // Complete the entry
256:                in.close();
257:                in = null;
258:                out.closeEntry();
259:            }
260:
261:            private static void addDirectory(ZipOutputStream out, File dir,
262:                    String relativePath) throws IOException {
263:                if (dir.isFile()) {
264:                    log.error("Called addDirectory on a file \""
265:                            + dir.getAbsolutePath() + "\".");
266:                    throw new IOException(
267:                            "I/O error while adding directory to ZIP.");
268:                }
269:                if (relativePath.length() > 0) { //ignore starting directory
270:                    if (!relativePath.endsWith("/"))
271:                        relativePath += "/";
272:                    // Add ZIP entry to output stream.
273:                    out.putNextEntry(new ZipEntry(relativePath));
274:                    out.closeEntry();
275:                }
276:                File[] files = dir.listFiles();
277:                if (files != null) {
278:                    for (int i = 0; i < files.length; i++) {
279:                        File file = files[i];
280:                        if (file.isFile()) {
281:                            addFile(out, file, relativePath + file.getName());
282:                        } else {
283:                            addDirectory(out, file, relativePath
284:                                    + file.getName() + "/");
285:                        }
286:                    }
287:                }
288:            }
289:
290:            // =================================================================
291:            // ================= MAIN CREATING XML/ZIP METHOD ==================
292:            // =================================================================
293:            private static void createBackupXml(XMLWriter xmlWriter,
294:                    Calendar backupTime, String backupIP)
295:                    throws ExportException {
296:
297:                try {
298:                    xmlWriter.startDocument(MVNForumConstant.dtdschemaDecl);
299:                    String strExportDate = XMLUtil
300:                            .sqlTimestampToStringDefNow(new Timestamp(
301:                                    backupTime.getTime().getTime()));
302:                    xmlWriter.startElement("mvnforum", new String[] {
303:                            "version", "1.0", "exportDate", strExportDate });
304:
305:                    MemberXML.exportMemberList(xmlWriter);
306:                    GroupXML.exportGroupList(xmlWriter);
307:                    CategoryXML.exportCategoryList(xmlWriter);
308:                    RankXML.exportRankList(xmlWriter);
309:
310:                    xmlWriter.endElement("mvnforum");
311:                    xmlWriter.endDocument();
312:                } catch (IOException e) {
313:                    log
314:                            .error(
315:                                    "I/O error while trying to create backup XML file on server.",
316:                                    e);
317:                    throw new ExportException("", e);
318:                } catch (ObjectNotFoundException e) {
319:                    log
320:                            .error(
321:                                    "Object not found error while trying to createBackupXml()",
322:                                    e);
323:                    throw new ExportException(
324:                            "Database object not found error while trying to create backup XML file on server.",
325:                            e);
326:                } catch (DatabaseException e) {
327:                    log.error(
328:                            "Database error while trying to createBackupXml()",
329:                            e);
330:                    throw new ExportException(
331:                            "Database error while trying to create backup XML file on server.",
332:                            e);
333:                }
334:            }
335:
336:            // =================================================================
337:            // ======================= UTILITY METHODS =========================
338:            // =================================================================
339:            public static Collection execSqlQuery(String query)
340:                    throws DatabaseException {
341:                Connection connection = null;
342:                PreparedStatement statement = null;
343:                ResultSet resultSet = null;
344:                Collection retValue = new ArrayList();
345:                try {
346:                    connection = DBUtils.getConnection();
347:                    statement = connection.prepareStatement(query);
348:                    resultSet = statement.executeQuery();
349:                    int numCols = resultSet.getMetaData().getColumnCount();
350:                    while (resultSet.next()) {
351:                        String[] this Result = new String[numCols];
352:                        for (int i = 0; i < numCols; i++) {
353:                            this Result[i] = resultSet.getString(i + 1);
354:                        }
355:                        retValue.add(this Result);
356:                    }
357:                    return retValue;
358:                } catch (SQLException sqle) {
359:                    log.error("Sql Execution Error!", sqle);
360:                    throw new DatabaseException(
361:                            "Error executing SQL in ExportWebHelper.execSqlQuery.");
362:                } finally {
363:                    DBUtils.closeResultSet(resultSet);
364:                    DBUtils.closeStatement(statement);
365:                    DBUtils.closeConnection(connection);
366:                }
367:            }
368:
369:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.