01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.wiki;
04:
05: import java.util.*;
06:
07: public class PageVersionPruner {
08: public static int daysTillVersionsExpire = 14;
09:
10: public static void pruneVersions(FileSystemPage page,
11: Collection versions) throws Exception {
12: List versionsList = makeSortedVersionList(versions);
13: if (versions.size() > 0) {
14: VersionInfo lastVersion = (VersionInfo) versionsList
15: .get(versionsList.size() - 1);
16: GregorianCalendar expirationDate = makeVersionExpirationDate(lastVersion);
17: for (Iterator iterator = versionsList.iterator(); iterator
18: .hasNext();) {
19: VersionInfo version = (VersionInfo) iterator.next();
20: removeVersionIfExpired(page, version, expirationDate);
21: }
22: }
23: }
24:
25: private static List makeSortedVersionList(Collection versions)
26: throws Exception {
27: List versionsList = new ArrayList(versions);
28: Collections.sort(versionsList);
29: return versionsList;
30: }
31:
32: private static GregorianCalendar makeVersionExpirationDate(
33: VersionInfo lastVersion) throws Exception {
34: Date dateOfLastVersion = lastVersion.getCreationTime();
35: GregorianCalendar expirationDate = new GregorianCalendar();
36: expirationDate.setTime(dateOfLastVersion);
37: expirationDate.add(Calendar.DAY_OF_MONTH,
38: -(daysTillVersionsExpire));
39: return expirationDate;
40: }
41:
42: private static void removeVersionIfExpired(FileSystemPage page,
43: VersionInfo version, GregorianCalendar expirationDate)
44: throws Exception {
45: Calendar this Date = new GregorianCalendar();
46: thisDate.setTime(version.getCreationTime());
47: if (thisDate.before(expirationDate)
48: || thisDate.equals(expirationDate))
49: page.removeVersion(version.getName());
50: }
51: }
|