001: /*
002: Very Quick Wiki - WikiWikiWeb clone
003: Copyright (C) 2001-2002 Gareth Cronin
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the latest version of the GNU Lesser General
007: Public License as published by the Free Software Foundation;
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public License
015: along with this program (gpl.txt); if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018: package vqwiki;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Comparator;
024: import java.util.Date;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.log4j.Logger;
029:
030: /**
031: * Class, which represents one topic
032: *
033: * @author $author$
034: * @version $Revision: 644 $
035: */
036: public class Topic {
037:
038: /** Reference to the wiki base class */
039: protected WikiBase wb;
040: /** Name of this topic */
041: protected String topicName;
042: /** Contents of this topic */
043: protected String contents;
044: /** Author of this topic */
045: protected String author;
046: /** Last modification date of this topic */
047: protected Date lastRevisionDate;
048: /** revision number of this topic */
049: protected int revision;
050: private static Logger logger = Logger.getLogger(Topic.class);
051:
052: /**
053: * Creates a new Topic object.
054: *
055: * @param topicName The name of this topic
056: *
057: * @throws Exception If the topic cannot be retrieved
058: */
059: public Topic(String topicName) throws Exception {
060: this .topicName = topicName;
061: wb = WikiBase.getInstance();
062: this .contents = null;
063: this .author = null;
064: this .lastRevisionDate = null;
065: this .revision = -1;
066: }
067:
068: /**
069: * Find the most recent revision before the current
070: *
071: * @param virtualWiki The virtualWiki, which contains the topic
072: *
073: * @return the last revision date, or null if versioning is off
074: */
075: public Date getMostRecentRevisionDate(String virtualWiki)
076: throws Exception {
077: if (Environment.getInstance().isVersioningOn()) {
078: this .lastRevisionDate = wb.getVersionManagerInstance()
079: .lastRevisionDate(virtualWiki, this .topicName);
080: return this .lastRevisionDate;
081: } else {
082: return null;
083: }
084: }
085:
086: /**
087: * Return a diff for the current vs the most recent revision before it.
088: *
089: * @param virtualWiki The virtualWiki, which contains the topic
090: * @param useHtml Set to true if the diff should be returned as HTML.
091: * Returns the diff as text otherwise.
092: *
093: * @return a diff to the last revision
094: */
095: public String mostRecentDiff(String virtualWiki, boolean useHtml)
096: throws Exception {
097: return wb.getVersionManagerInstance().diff(virtualWiki,
098: topicName, 0, 1, useHtml);
099: }
100:
101: /**
102: * Get a diff for two arbitrary versions of a topic
103: * @param virtualWiki the virtual wiki
104: * @param firstVersion the first version number
105: * @param secondVersion the second version number
106: * @param useHtml Set to true if the diff should be returned as HTML.
107: * Returns the diff as text otherwise.
108: * @return
109: */
110: public String getDiff(String virtualWiki, int firstVersion,
111: int secondVersion, boolean useHtml) throws Exception {
112: return wb.getVersionManagerInstance().diff(virtualWiki,
113: topicName, firstVersion, secondVersion, useHtml);
114: }
115:
116: /**
117: * Find the most recent author.
118: *
119: * @param virtualWiki The virtualWiki, which contains the topic
120: *
121: * @return the author, who editied the last revision, or null if versioning is off
122: */
123: public String getMostRecentAuthor(String virtualWiki)
124: throws Exception {
125: this .author = null;
126: if (Environment.getInstance().isVersioningOn()) {
127: // get list of versions:
128: List allVersions = wb.getVersionManagerInstance()
129: .getAllVersions(virtualWiki, this .topicName);
130: // sort the list so that the most recent version is on top:
131: Collections.sort(allVersions, new Comparator() {
132: public int compare(Object o1, Object o2) {
133: TopicVersion bean1 = (TopicVersion) o1;
134: TopicVersion bean2 = (TopicVersion) o2;
135: return bean2.getRevisionDate().compareTo(
136: (Date) bean1.getRevisionDate());
137: }
138: });
139: logger.debug("Having " + allVersions.size() + " versions: "
140: + allVersions);
141: String foundAuthor = null;
142: // go through all the versions
143: for (Iterator iter = allVersions.iterator(); iter.hasNext()
144: && foundAuthor == null;) {
145: TopicVersion version = (TopicVersion) iter.next();
146: if (version.getRevisionDate() != null) {
147: logger.debug("Checking version date "
148: + version.getRevisionDate());
149: // get list of changes for that date
150: Collection c = WikiBase.getInstance()
151: .getChangeLogInstance().getChanges(
152: virtualWiki,
153: version.getRevisionDate());
154: if (c != null) {
155: // remove all changes, which do not apply for this topic
156: logger.debug("Got " + c.size()
157: + " changes for that date");
158: Collection cGood = new ArrayList();
159: for (Iterator iterChange = c.iterator(); iterChange
160: .hasNext();) {
161: Change this change = (Change) iterChange
162: .next();
163: if (this change.getTopic().equals(
164: this .topicName)) {
165: cGood.add(this change);
166: }
167: }
168: logger.debug("Got " + cGood.size()
169: + " changes left after filtering");
170: if (!cGood.isEmpty()) {
171: // find author on that day; check, if there are multiple
172: // modifications on the same day
173: Iterator it = cGood.iterator();
174: Date authorDate = null;
175: while (it.hasNext()) {
176: Change this change = (Change) it.next();
177: if (authorDate == null) {
178: foundAuthor = this change.getUser();
179: authorDate = this change.getTime();
180: } else {
181: if (authorDate.before(this change
182: .getTime())) {
183: foundAuthor = this change
184: .getUser();
185: authorDate = this change
186: .getTime();
187: }
188: }
189: }
190: }
191: }
192: }
193: }
194: this .author = foundAuthor;
195: } else {
196: this .author = null;
197: }
198: return this .author;
199: }
200:
201: /**
202: * Find the revision number.
203: *
204: * @param virtualWiki The virtualWiki, which contains the topic
205: *
206: * @return the revision number
207: */
208: public int getRevision(String virtualWiki) throws Exception {
209: this .revision = wb.getVersionManagerInstance()
210: .getNumberOfVersions(virtualWiki, this .topicName);
211: return this .revision;
212: }
213:
214: /**
215: * Make a topic read-only
216: *
217: * @param virtualWiki The virtualWiki, which contains the topic
218: */
219: public synchronized void makeTopicReadOnly(String virtualWiki)
220: throws Exception {
221: wb.addReadOnlyTopic(virtualWiki, topicName);
222: }
223:
224: /**
225: * Return whether a topic is read-only
226: *
227: * @param virtualWiki The virtualWiki, which contains the topic
228: */
229: public boolean isReadOnlyTopic(String virtualWiki) throws Exception {
230: return wb.isTopicReadOnly(virtualWiki, topicName);
231: }
232:
233: /**
234: * Make a previously read-only topic writable
235: *
236: * @param virtualWiki The virtualWiki, which contains the topic
237: */
238: public synchronized void makeTopicWritable(String virtualWiki)
239: throws Exception {
240: wb.removeReadOnlyTopic(virtualWiki, topicName);
241: }
242: }
|