1 40 package com.mvnforum.admin; 41 42 import java.io.*; 43 import java.sql.*; 44 45 import javax.servlet.ServletContext ; 46 import javax.servlet.http.HttpServletRequest ; 47 48 import com.mvnforum.*; 49 import com.mvnforum.auth.MVNForumPermission; 50 import com.mvnforum.db.*; 51 import net.myvietnam.mvncore.db.DBUtils; 52 import net.myvietnam.mvncore.exception.*; 53 import org.apache.commons.logging.Log; 54 import org.apache.commons.logging.LogFactory; 55 56 63 public class ImportWebHelper { 64 65 66 private static Log log = LogFactory.getLog(ImportWebHelper.class); 67 68 protected ImportWebHelper() { 69 } 70 71 72 79 protected static int execUpdateQuery(String query) throws DatabaseException { 80 81 Connection connection = null; 82 PreparedStatement statement = null; 83 ResultSet resultSet = null; 84 try { 85 connection = DBUtils.getConnection(); 86 statement = connection.prepareStatement(query); 87 return statement.executeUpdate(); 88 } catch (SQLException sqle) { 89 log.error("Sql Execution Error!", sqle); 90 throw new DatabaseException("Error executing SQL in ImportWebHelper.execUpdateQuery."); 91 } finally { 92 DBUtils.closeResultSet(resultSet); 93 DBUtils.closeStatement(statement); 94 DBUtils.closeConnection(connection); 95 } 96 } 97 98 private static void clearTable(String tableName) throws DatabaseException { 99 100 execUpdateQuery("DELETE FROM " + tableName); 101 } 102 103 private static void emptyDirectory(File dir) throws IOException { 104 105 if (dir.isFile()) { 106 log.error("Called emptyDirectory on a file \""+dir.getAbsolutePath()+"\"."); 107 throw new IOException("IOException: not a directory."); 108 } 109 File[] files = dir.listFiles(); 110 if (files != null) { 111 for (int i = 0; i < files.length; i++) { 112 File file = files[i]; 113 if (file.isFile()) { 114 file.delete(); 115 } else { 116 emptyDirectory(file); 117 } 118 } 119 } 120 } 121 122 private static void emptyDirectory(String path) throws IOException { 123 124 emptyDirectory(new File(path)); 125 } 126 127 protected static void clearFiles(ServletContext context) throws IOException { 128 129 addImportantMessage("Deleting previous files..."); 130 if (MVNForumConfig.getSearchPostIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 131 emptyDirectory(MVNForumConfig.getSearchPostIndexDirName()); 132 } 133 if (MVNForumConfig.getSearchMemberIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 134 emptyDirectory(MVNForumConfig.getSearchMemberIndexDirName()); 135 } 136 if (MVNForumConfig.getSearchCompanyIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 137 emptyDirectory(MVNForumConfig.getSearchCompanyIndexDirName()); 138 } 139 emptyDirectory(MVNForumConfig.getAttachmentDir()); 140 emptyDirectory(context.getRealPath(MVNForumGlobal.UPLOADED_AVATAR_DIR)); 141 } 143 144 protected static void clearDatabase() throws DatabaseException { 145 146 addImportantMessage("Clearing previous database contents..."); 147 clearTable(RankDAO.TABLE_NAME); 148 clearTable(FavoriteThreadDAO.TABLE_NAME); 149 150 clearTable(AttachmentDAO.TABLE_NAME); 151 clearTable(WatchDAO.TABLE_NAME); 152 clearTable(PostDAO.TABLE_NAME); 153 clearTable(ThreadDAO.TABLE_NAME); 154 155 clearTable(MemberForumDAO.TABLE_NAME); 156 clearTable(GroupForumDAO.TABLE_NAME); 157 clearTable(ForumDAO.TABLE_NAME); 158 clearTable(CategoryDAO.TABLE_NAME); 159 160 clearTable(MemberGroupDAO.TABLE_NAME); 161 clearTable(GroupPermissionDAO.TABLE_NAME); 162 clearTable(GroupsDAO.TABLE_NAME); 163 clearTable(MemberPermissionDAO.TABLE_NAME); 164 clearTable(MessageFolderDAO.TABLE_NAME); 165 clearTable(MemberDAO.TABLE_NAME); 166 } 167 168 169 public static void createDefaultContents() throws DuplicateKeyException, 173 ObjectNotFoundException, CreateException, DatabaseException, ForeignKeyNotFoundException { 174 createDefaultAdminMember(); 175 createDefaultGuestMember(); 176 createDefaultRegisteredMembersGroup(); 177 createDefaultRanks(); 178 } 179 180 public static void createDefaultGuestMember() throws DuplicateKeyException, 181 ObjectNotFoundException, CreateException, DatabaseException, ForeignKeyNotFoundException { 182 createDefaultGuestMember(MVNForumConfig.getDefaultGuestName()); 183 } 184 185 public static void createDefaultGuestMember(String guestName) throws DuplicateKeyException, 186 ObjectNotFoundException, CreateException, DatabaseException, ForeignKeyNotFoundException { 187 if ((guestName==null) || (guestName.length()<=0)) { 188 guestName=MVNForumConfig.getDefaultGuestName(); 189 } 190 addImportantMessage("Adding virtual guest member \""+guestName+"\"..."); 191 MemberXML memberXML=new MemberXML(); 194 memberXML.addMember(Integer.toString(MVNForumConstant.MEMBER_ID_OF_GUEST), guestName, 195 "N/A", 196 "N/A", 197 "N/A", 198 "0", "1", "127.0.0.1", "127.0.0.1", "0", "0", 199 null , null , null , 200 null , "0", "0", "" , 201 "" , "0" , 202 "0" , "10" , 203 "0" , "0" , "0" , 204 "0" , "" , "0" , 205 "" , "" , "" , 206 "" , guestName , "" , 207 "1" , null , "" , 208 "" , "" , "" , "" , 209 "" , "" , "" , "" , 210 "" , "" , "" , "" , 211 "" , "" ); 212 memberXML.addMemberPermission(Integer.toString(MVNForumPermission.PERMISSION_LIMITED_USER)); 213 } 215 216 public static void createDefaultAdminMember() throws ObjectNotFoundException, 217 CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 218 createDefaultAdminMember("Admin"); 219 } 220 221 public static void createDefaultAdminMember(String adminName) throws ObjectNotFoundException, 222 CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 223 if ((adminName==null) || (adminName.length()<=0)) { 224 adminName="Admin"; 225 } 226 addImportantMessage("Adding system administrator \""+adminName+"\"..."); 227 MemberXML memberXML = new MemberXML(); 228 229 memberXML.addMember(Integer.toString(MVNForumConstant.MEMBER_ID_OF_ADMIN), adminName, 230 "ISMvKXpXpadDiUoOSoAfww==", "admin@yourdomain.com", "admin@yourdomain.com", 231 "0", "1", "127.0.0.1", "127.0.0.1", "0", "0", 232 null , null ,null , 233 null , "0", "0", "" , 234 "" , "0" , 235 "0" , "10" , 236 "0" , "0" , "0" , 237 "0" , "" , "0" , 238 "" , "" , "" , 239 "" , adminName , "" , 240 "1" , null , "" , 241 "" , "" , "" , "" , 242 "" , "" , "" , "" , 243 "" , "" , "" , "" , 244 "" , "" ); 245 memberXML.addMemberPermission(Integer.toString(MVNForumPermission.PERMISSION_SYSTEM_ADMIN)); 246 memberXML.addMessageFolder("Inbox", "0" , 247 null , null ); 248 memberXML.addMessageFolder("Sent", "0" , 249 null , null ); 250 251 } 252 253 public static void createDefaultRegisteredMembersGroup() throws CreateException, 254 DuplicateKeyException, ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException { 255 createDefaultRegisteredMembersGroup(null); 256 } 257 258 public static void createDefaultRegisteredMembersGroup(String groupOwnerName) throws CreateException, 259 DuplicateKeyException, ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException { 260 addImportantMessage("Adding default virtual group of all registered members..."); 261 GroupXML groupXML = new GroupXML(); 262 groupXML.addGroup(Integer.toString(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS), 263 groupOwnerName, "Registered Members", 264 "All registered users belong to this group.", 265 null, 266 null, null); 267 groupXML.addGroupPermission(Integer.toString(MVNForumPermission.PERMISSION_NORMAL_USER)); 268 } 270 271 public static void createDefaultRanks() throws CreateException, 272 DuplicateKeyException, ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException { 273 addImportantMessage("Adding default rank titles \"Stranger\", \"Newbie\", \"Member\" and \"Advanced Member\"..."); 274 (new RankXML()).addRank("0", "0", "Stranger", "", "0", "0"); 275 (new RankXML()).addRank("20", "0", "Newbie", "", "0", "0"); 276 (new RankXML()).addRank("50", "0", "Member", "", "0", "0"); 277 (new RankXML()).addRank("100", "0", "Advanced Member", "", "0", "0"); 278 } 279 280 protected static void setOutputHtmlWriter(PrintWriter outWriter) { 284 ImportWebHandler.setOutputHtmlWriter(outWriter); 285 } 286 287 protected static void setMessageOutputLevel(int messageLevel) { 288 ImportWebHandler.setMessageOutputLevel(messageLevel); 289 } 290 291 protected static void startHtml(HttpServletRequest request) { 292 ImportWebHandler.startHtml(request); 293 } 294 295 protected static void endHtml() { 296 ImportWebHandler.endHtml(); 297 } 298 299 protected static void addMessage(String message) { 300 ImportWebHandler.addMessage(message); 301 } 302 303 protected static void addErrorMessage(String message) { 304 ImportWebHandler.addErrorMessage(message); 305 } 306 307 protected static void addSuccessMessage() { 308 ImportWebHandler.addSuccessMessage(); 309 } 310 311 protected static void addImportantMessage(String message) { 312 ImportWebHandler.addImportantMessage(message); 313 } 314 315 protected static void addFinalErrorHandling(HttpServletRequest request, boolean clearIfError) { 316 ImportWebHandler.addFinalErrorHandling(request, clearIfError); 317 } 318 319 320 } 321 322
| Popular Tags
|