001: package vqwiki.servlets;
002:
003: import org.apache.log4j.Logger;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007: import javax.servlet.ServletException;
008: import java.io.IOException;
009: import java.util.List;
010: import java.util.Collection;
011: import java.util.Iterator;
012:
013: import vqwiki.file.*;
014: import vqwiki.db.DatabaseHandler;
015: import vqwiki.db.DatabaseWikiMembers;
016: import vqwiki.db.DatabaseNotify;
017: import vqwiki.db.DatabaseVersionManager;
018: import vqwiki.WikiMembers;
019: import vqwiki.WikiMember;
020: import vqwiki.VersionManager;
021: import vqwiki.TopicVersion;
022:
023: /**
024: * Servlet for migrating a file-based wiki into a database-based one
025: *
026: * @author garethc
027: * Date: Apr 28, 2003
028: */
029: public class ImportServlet extends VQWikiServlet {
030:
031: private static final Logger logger = Logger
032: .getLogger(ImportServlet.class);
033:
034: /**
035: *
036: */
037: protected void doGet(HttpServletRequest request,
038: HttpServletResponse response) throws ServletException,
039: IOException {
040: logger.debug("Importing...");
041: try {
042: request.setAttribute("results", importAll());
043: } catch (Exception e) {
044: error(request, response, e);
045: return;
046: }
047: dispatch("/jsp/afterImport.jsp", request, response);
048: }
049:
050: /**
051: *
052: */
053: private String importAll() throws Exception {
054: StringBuffer buffer = new StringBuffer();
055: // language does not matter here
056: FileHandler fileHandler = new FileHandler();
057: DatabaseHandler databaseHandler = new DatabaseHandler();
058: FileSearchEngine fileSearchEngine = FileSearchEngine
059: .getInstance();
060: VersionManager fileVersionManager = FileVersionManager
061: .getInstance();
062: VersionManager databaseVersionManager = DatabaseVersionManager
063: .getInstance();
064: Collection virtualWikis = fileHandler.getVirtualWikiList();
065: for (Iterator virtualWikiIterator = virtualWikis.iterator(); virtualWikiIterator
066: .hasNext();) {
067: String virtualWiki = (String) virtualWikiIterator.next();
068: logger.info("importing for virtual wiki " + virtualWiki);
069: buffer.append("imported for virtual wiki " + virtualWiki);
070: buffer.append("<br/>");
071: databaseHandler.addVirtualWiki(virtualWiki);
072: // Versions
073: Collection topics = fileSearchEngine
074: .getAllTopicNames(virtualWiki);
075: for (Iterator topicIterator = topics.iterator(); topicIterator
076: .hasNext();) {
077: String topicName = (String) topicIterator.next();
078: List versions = fileVersionManager.getAllVersions(
079: virtualWiki, topicName);
080: logger.info("importing " + versions.size()
081: + " versions of topic " + topicName);
082: buffer.append("imported " + versions.size()
083: + " versions of topic " + topicName);
084: buffer.append("<br/>");
085: for (Iterator topicVersionIterator = versions
086: .iterator(); topicVersionIterator.hasNext();) {
087: TopicVersion topicVersion = (TopicVersion) topicVersionIterator
088: .next();
089: databaseVersionManager.addVersion(virtualWiki,
090: topicVersion.getTopicName(), topicVersion
091: .getRawContents(), topicVersion
092: .getRevisionDate());
093: }
094: }
095: // Topics
096: for (Iterator topicIterator = topics.iterator(); topicIterator
097: .hasNext();) {
098: String topicName = (String) topicIterator.next();
099: logger.info("importing topic " + topicName);
100: buffer.append("imported topic " + topicName);
101: buffer.append("<br/>");
102: databaseHandler.write(virtualWiki, fileHandler.read(
103: virtualWiki, topicName), false, topicName);
104: }
105: // Read-only topics
106: Collection readOnlys = fileHandler
107: .getReadOnlyTopics(virtualWiki);
108: for (Iterator readOnlyIterator = readOnlys.iterator(); readOnlyIterator
109: .hasNext();) {
110: String topicName = (String) readOnlyIterator.next();
111: logger.info("import read-only topicname " + topicName);
112: buffer.append("imported read-only topicname "
113: + topicName);
114: buffer.append("<br/>");
115: databaseHandler
116: .addReadOnlyTopic(virtualWiki, topicName);
117: }
118: // Members
119: WikiMembers fileMembers = new FileWikiMembers(virtualWiki);
120: WikiMembers databaseMembers = new DatabaseWikiMembers(
121: virtualWiki);
122: Collection members = fileMembers.getAllMembers();
123: for (Iterator memberIterator = members.iterator(); memberIterator
124: .hasNext();) {
125: WikiMember wikiMember = (WikiMember) memberIterator
126: .next();
127: logger.info("importing member " + wikiMember);
128: buffer.append("imported member " + wikiMember);
129: buffer.append("<br/>");
130: databaseMembers.addMember(wikiMember.getUserName(),
131: wikiMember.getEmail(), wikiMember.getKey());
132: }
133: // Notifications
134: Collection fileNotifications = FileNotify
135: .getAll(virtualWiki);
136: for (Iterator iterator = fileNotifications.iterator(); iterator
137: .hasNext();) {
138: FileNotify fileNotify = (FileNotify) iterator.next();
139: logger.info("importing notification " + fileNotify);
140: buffer.append("imported notification " + fileNotify);
141: buffer.append("<br/>");
142: DatabaseNotify databaseNotify = new DatabaseNotify(
143: virtualWiki, fileNotify.getTopicName());
144: Collection notifyMembers = fileNotify.getMembers();
145: for (Iterator notifyMemberIterator = notifyMembers
146: .iterator(); notifyMemberIterator.hasNext();) {
147: String memberName = (String) notifyMemberIterator
148: .next();
149: databaseNotify.addMember(memberName);
150: }
151: }
152: // Templates
153: Collection templates = fileHandler
154: .getTemplateNames(virtualWiki);
155: for (Iterator templateIterator = templates.iterator(); templateIterator
156: .hasNext();) {
157: String templateName = (String) templateIterator.next();
158: logger.info("importing template " + templateName);
159: buffer.append("imported template " + templateName);
160: buffer.append("<br/>");
161: databaseHandler.saveAsTemplate(virtualWiki,
162: templateName, fileHandler.getTemplate(
163: virtualWiki, templateName));
164: }
165: }
166: return buffer.toString();
167: }
168: }
|