001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ImportWebHelper.java,v 1.18 2007/11/09 10:38:27 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.18 $
005: * $Date: 2007/11/09 10:38:27 $
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:
045: import javax.servlet.ServletContext;
046: import javax.servlet.http.HttpServletRequest;
047:
048: import com.mvnforum.*;
049: import com.mvnforum.auth.MVNForumPermission;
050: import com.mvnforum.db.*;
051: import com.mvnforum.service.MvnForumServiceFactory;
052: import com.mvnforum.service.SearchService;
053:
054: import net.myvietnam.mvncore.db.DBUtils;
055: import net.myvietnam.mvncore.exception.*;
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058:
059: /**
060: * @author Igor Manic
061: * @version $Revision: 1.18 $, $Date: 2007/11/09 10:38:27 $
062: * <br/>
063: * <code>ImportWebHelper</code> todo Igor: enter description
064: *
065: */
066: public class ImportWebHelper {
067:
068: /** Message log. */
069: private static Log log = LogFactory.getLog(ImportWebHelper.class);
070:
071: protected ImportWebHelper() {
072: }
073:
074: // =================================================================
075: // ======== UTILITY METHODS FOR CLEARING PREVIOUS CONTENTS =========
076: // =================================================================
077: /* I need this method because db package doesn't allow me to enter some
078: * values I need to (for example, to ensure creating predefined groups
079: * having GroupIDs I want them to be (like in .sql script)
080: */
081: protected static int execUpdateQuery(String query)
082: throws DatabaseException {
083:
084: Connection connection = null;
085: PreparedStatement statement = null;
086: ResultSet resultSet = null;
087: try {
088: connection = DBUtils.getConnection();
089: statement = connection.prepareStatement(query);
090: return statement.executeUpdate();
091: } catch (SQLException sqle) {
092: log.error("Sql Execution Error!", sqle);
093: throw new DatabaseException(
094: "Error executing SQL in ImportWebHelper.execUpdateQuery.");
095: } finally {
096: DBUtils.closeResultSet(resultSet);
097: DBUtils.closeStatement(statement);
098: DBUtils.closeConnection(connection);
099: }
100: }
101:
102: private static void clearTable(String tableName)
103: throws DatabaseException {
104:
105: execUpdateQuery("DELETE FROM " + tableName);
106: }
107:
108: private static void emptyDirectory(File dir) throws IOException {
109:
110: if (dir.isFile()) {
111: log.error("Called emptyDirectory on a file \""
112: + dir.getAbsolutePath() + "\".");
113: throw new IOException("IOException: not a directory.");
114: }
115: File[] files = dir.listFiles();
116: if (files != null) {
117: for (int i = 0; i < files.length; i++) {
118: File file = files[i];
119: if (file.isFile()) {
120: file.delete();
121: } else {
122: emptyDirectory(file);
123: }
124: }
125: }
126: }
127:
128: private static void emptyDirectory(String path) throws IOException {
129:
130: emptyDirectory(new File(path));
131: }
132:
133: protected static void clearFiles(ServletContext context)
134: throws IOException {
135:
136: addImportantMessage("Deleting previous files...");
137:
138: SearchService service = MvnForumServiceFactory
139: .getMvnForumService().getSearchService();
140: if (service.savePostOnDisk()) {
141: emptyDirectory(MVNForumConfig.getSearchPostIndexDirName());
142: }
143: if (service.saveMemberOnDisk()) {
144: emptyDirectory(MVNForumConfig.getSearchMemberIndexDirName());
145: }
146:
147: emptyDirectory(MVNForumConfig.getAttachmentDir());
148: emptyDirectory(context
149: .getRealPath(MVNForumGlobal.UPLOADED_AVATAR_DIR));
150: //DO NOT DELETE OR EMPTY getTempDir() BECAUSE IT MAY CONTAIN UPLOADED IMPORT FILE WHICH IS GOING TO BE PROCESSED
151: }
152:
153: protected static void clearDatabase() throws DatabaseException {
154:
155: addImportantMessage("Clearing previous database contents...");
156: clearTable(RankDAO.TABLE_NAME);
157: clearTable(FavoriteThreadDAO.TABLE_NAME);
158:
159: clearTable(AttachmentDAO.TABLE_NAME);
160: clearTable(WatchDAO.TABLE_NAME);
161: clearTable(PostDAO.TABLE_NAME);
162: clearTable(ThreadDAO.TABLE_NAME);
163:
164: clearTable(MemberForumDAO.TABLE_NAME);
165: clearTable(GroupForumDAO.TABLE_NAME);
166: clearTable(ForumDAO.TABLE_NAME);
167: clearTable(CategoryDAO.TABLE_NAME);
168:
169: clearTable(MemberGroupDAO.TABLE_NAME);
170: clearTable(GroupPermissionDAO.TABLE_NAME);
171: clearTable(GroupsDAO.TABLE_NAME);
172: clearTable(MemberPermissionDAO.TABLE_NAME);
173: clearTable(MessageFolderDAO.TABLE_NAME);
174: clearTable(MemberDAO.TABLE_NAME);
175: }
176:
177: // =================================================================
178: // =============== CREATING DEFAULT DATABASE RECORDS ===============
179: // =================================================================
180: public static void createDefaultContents()
181: throws DuplicateKeyException, ObjectNotFoundException,
182: CreateException, DatabaseException,
183: ForeignKeyNotFoundException {
184: createDefaultAdminMember();
185: createDefaultGuestMember();
186: createDefaultRegisteredMembersGroup();
187: createDefaultRanks();
188: }
189:
190: public static void createDefaultGuestMember()
191: throws DuplicateKeyException, ObjectNotFoundException,
192: CreateException, DatabaseException,
193: ForeignKeyNotFoundException {
194: createDefaultGuestMember(MVNForumConfig.getDefaultGuestName());
195: }
196:
197: public static void createDefaultGuestMember(String guestName)
198: throws DuplicateKeyException, ObjectNotFoundException,
199: CreateException, DatabaseException,
200: ForeignKeyNotFoundException {
201: if ((guestName == null) || (guestName.length() <= 0)) {
202: guestName = MVNForumConfig.getDefaultGuestName();
203: }
204: addImportantMessage("Adding virtual guest member \""
205: + guestName + "\"...");
206: //todo Igor: add an array of individual perms to the previous message
207: //e.g.: "... with READ_POST, ADD_POST and ADD_THREAD permissions.");
208: MemberXML memberXML = new MemberXML();
209: memberXML.addMember(Integer
210: .toString(MVNForumConstant.MEMBER_ID_OF_GUEST),
211: guestName,
212: "N/A"/*MemberPassword - not used - can't be empty*/,
213: "N/A"/*MemberFirstEmail - not used - can't be empty*/,
214: "N/A"/*MemberEmail - not used - can't be empty*/, "0",
215: "1", "127.0.0.1", "127.0.0.1", "0", "0",
216: null /*memberCreationDate*/,
217: null /*memberModifiedDate*/,
218: null /*memberExpireDate*/, null /*memberLastLogon*/,
219: "0", "0", "" /*memberActivateCode*/,
220: "" /*memberTempPassword*/,
221: "0" /*memberMessageCount*/,
222: "0" /*memberMessageOption*/,
223: "10" /*memberPostsPerPage*/, "0" /*memberWarnCount*/,
224: "0" /*memberVoteCount*/,
225: "0" /*memberVoteTotalStars*/,
226: "0" /*memberRewardPoints*/, "" /*memberTitle*/,
227: "0" /*memberTimeZone*/, "" /*memberSignature*/,
228: "" /*memberAvatar*/, "" /*memberSkin*/,
229: "" /*memberLanguage*/, guestName /*memberFirstname*/,
230: "" /*memberLastname*/, "1" /*memberGender*/,
231: null /*memberBirthday*/, "" /*memberAddress*/,
232: "" /*memberCity*/, "" /*memberState*/,
233: "" /*memberCountry*/, "" /*memberPhone*/,
234: "" /*memberMobile*/, "" /*memberFax*/,
235: "" /*memberCareer*/, "" /*memberHomepage*/,
236: "" /*memberYahoo*/, "" /*memberAol*/,
237: "" /*memberIcq*/, "" /*memberMsn*/,
238: "" /*memberCoolLink1*/, "" /*memberCoolLink2*/);
239: memberXML.addMemberPermission(Integer
240: .toString(MVNForumPermission.PERMISSION_LIMITED_USER));
241: //todo Igor: replace previous permission with an array of individual perms
242: }
243:
244: public static void createDefaultAdminMember()
245: throws ObjectNotFoundException, CreateException,
246: DatabaseException, DuplicateKeyException,
247: ForeignKeyNotFoundException {
248: createDefaultAdminMember("Admin");
249: }
250:
251: public static void createDefaultAdminMember(String adminName)
252: throws ObjectNotFoundException, CreateException,
253: DatabaseException, DuplicateKeyException,
254: ForeignKeyNotFoundException {
255: if ((adminName == null) || (adminName.length() <= 0)) {
256: adminName = "Admin";
257: }
258: addImportantMessage("Adding system administrator \""
259: + adminName + "\"...");
260: MemberXML memberXML = new MemberXML();
261:
262: memberXML.addMember(Integer
263: .toString(MVNForumConstant.MEMBER_ID_OF_ADMIN),
264: adminName, "ISMvKXpXpadDiUoOSoAfww==",
265: "admin@yourdomain.com", "admin@yourdomain.com", "0",
266: "1", "127.0.0.1", "127.0.0.1", "0", "0",
267: null /*memberCreationDate*/,
268: null /*memberModifiedDate*/,
269: null /*memberExpireDate*/, null /*memberLastLogon*/,
270: "0", "0", "" /*memberActivateCode*/,
271: "" /*memberTempPassword*/,
272: "0" /*memberMessageCount*/,
273: "0" /*memberMessageOption*/,
274: "10" /*memberPostsPerPage*/, "0" /*memberWarnCount*/,
275: "0" /*memberVoteCount*/,
276: "0" /*memberVoteTotalStars*/,
277: "0" /*memberRewardPoints*/, "" /*memberTitle*/,
278: "0" /*memberTimeZone*/, "" /*memberSignature*/,
279: "" /*memberAvatar*/, "" /*memberSkin*/,
280: "" /*memberLanguage*/, adminName /*memberFirstname*/,
281: "" /*memberLastname*/, "1" /*memberGender*/,
282: null /*memberBirthday*/, "" /*memberAddress*/,
283: "" /*memberCity*/, "" /*memberState*/,
284: "" /*memberCountry*/, "" /*memberPhone*/,
285: "" /*memberMobile*/, "" /*memberFax*/,
286: "" /*memberCareer*/, "" /*memberHomepage*/,
287: "" /*memberYahoo*/, "" /*memberAol*/,
288: "" /*memberIcq*/, "" /*memberMsn*/,
289: "" /*memberCoolLink1*/, "" /*memberCoolLink2*/);
290: memberXML.addMemberPermission(Integer
291: .toString(MVNForumPermission.PERMISSION_SYSTEM_ADMIN));
292: memberXML
293: .addMessageFolder("Inbox", "0" /*folderOrder*/,
294: null /*folderCreationDate*/, null /*folderModifiedDate*/);
295: memberXML
296: .addMessageFolder("Sent", "0" /*folderOrder*/,
297: null /*folderCreationDate*/, null /*folderModifiedDate*/);
298:
299: }
300:
301: public static void createDefaultRegisteredMembersGroup()
302: throws CreateException, DuplicateKeyException,
303: ObjectNotFoundException, DatabaseException,
304: ForeignKeyNotFoundException {
305: createDefaultRegisteredMembersGroup(null);
306: }
307:
308: public static void createDefaultRegisteredMembersGroup(
309: String groupOwnerName) throws CreateException,
310: DuplicateKeyException, ObjectNotFoundException,
311: DatabaseException, ForeignKeyNotFoundException {
312: addImportantMessage("Adding default virtual group of all registered members...");
313: GroupXML groupXML = new GroupXML();
314: groupXML
315: .addGroup(
316: Integer
317: .toString(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS),
318: groupOwnerName, "Registered Members",
319: "All registered users belong to this group.",
320: null/*GroupOption*/,
321: null/*GroupCreationDate*/, null/*GroupModifiedDate*/);
322: groupXML.addGroupPermission(Integer
323: .toString(MVNForumPermission.PERMISSION_NORMAL_USER));
324: //todo Igor: replace previous permission with an array of individual perms
325: }
326:
327: public static void createDefaultRanks() throws CreateException,
328: DuplicateKeyException, ObjectNotFoundException,
329: DatabaseException, ForeignKeyNotFoundException {
330: addImportantMessage("Adding default rank titles \"Stranger\", \"Newbie\", \"Member\" and \"Advanced Member\"...");
331: (new RankXML()).addRank("0", "0", "Stranger", "", "0", "0");
332: (new RankXML()).addRank("20", "0", "Newbie", "", "0", "0");
333: (new RankXML()).addRank("50", "0", "Member", "", "0", "0");
334: (new RankXML()).addRank("100", "0", "Advanced Member", "", "0",
335: "0");
336: }
337:
338: // =================================================================
339: // ===== PRINTING STATUS AND ERROR MESSAGES TO THE OUTPUT HTML =====
340: // =================================================================
341: protected static void setOutputHtmlWriter(PrintWriter outWriter) {
342: ImportWebHandler.setOutputHtmlWriter(outWriter);
343: }
344:
345: protected static void setMessageOutputLevel(int messageLevel) {
346: ImportWebHandler.setMessageOutputLevel(messageLevel);
347: }
348:
349: protected static void startHtml(HttpServletRequest request) {
350: ImportWebHandler.startHtml(request);
351: }
352:
353: protected static void endHtml() {
354: ImportWebHandler.endHtml();
355: }
356:
357: protected static void addMessage(String message) {
358: ImportWebHandler.addMessage(message);
359: }
360:
361: protected static void addErrorMessage(String message) {
362: ImportWebHandler.addErrorMessage(message);
363: }
364:
365: protected static void addSuccessMessage(HttpServletRequest request) {
366: ImportWebHandler.addSuccessMessage(request);
367: }
368:
369: protected static void addImportantMessage(String message) {
370: ImportWebHandler.addImportantMessage(message);
371: }
372:
373: protected static void addFinalErrorHandling(
374: HttpServletRequest request, boolean clearIfError) {
375: ImportWebHandler.addFinalErrorHandling(request, clearIfError);
376: }
377:
378: }
|