001: package vqwiki.file;
002:
003: import vqwiki.*;
004:
005: import java.io.*;
006: import java.util.Date;
007: import java.util.Hashtable;
008: import java.util.Iterator;
009:
010: /**
011: * Implementation of Reminders which stores reminder records in text files.
012: * Reminder files use the same filename as the associated topic and use
013: * the extension ".rmd". Only one reminder is allowed per user per topic;
014: * the user must either create a new page to set an additional reminder,
015: * or plan to alter the existing reminder on a recurring basis to simulate
016: * recurring reminders. Each Reminders object represents a single topic,
017: * which must be specified at instantiation, either by name or by File.
018: *
019: * @author Robert E Brewer
020: * @version 0.1
021: */
022: public class FileReminders implements Reminders {
023:
024: private Hashtable curReminders = new Hashtable();
025: private String topicName;
026: private File remindFile;
027:
028: /**
029: * No-arg constructor for compatibility only; always use FileReminders(newTopicName)
030: * or FileReminders(newRemindFile) instead.
031: */
032: public FileReminders() {
033: }
034:
035: /**
036: * Opens and reads a Reminders object using the topic name.
037: *
038: * @param newTopicName the name of the topic
039: * @exception vqwiki.WikiException if the reminder file could not be opened or read
040: */
041: public FileReminders(String newTopicName) throws WikiException {
042: topicName = newTopicName;
043: remindFile = makeRemindFile();
044: if (remindFile.exists())
045: readRemindFile();
046: }
047:
048: /**
049: * Reads a Reminders object from a File which has already been opened.
050: *
051: * @param newRemindFile the open File from which to read Reminders data.
052: * @exception vqwiki.WikiException if the reminder file could not be read
053: */
054: public FileReminders(File newRemindFile) throws WikiException {
055: remindFile = newRemindFile;
056: topicName = remindFile.getName();
057: topicName = topicName.substring(0, topicName.length() - 4);
058: if (remindFile.exists())
059: readRemindFile();
060: }
061:
062: /**
063: *
064: */
065: private File makeRemindFile() {
066: return new File(fileBase()
067: + System.getProperty("file.separator") + topicName
068: + ".rmd");
069: }
070:
071: /**
072: *
073: */
074: private synchronized boolean createRemindFile()
075: throws WikiException {
076: try {
077: remindFile.createNewFile();
078: if (remindFile.exists())
079: return true;
080: } catch (IOException e) {
081: }
082: throw new WikiException("Remind File could not be created.");
083: }
084:
085: /**
086: *
087: */
088: private synchronized boolean readRemindFile() throws WikiException {
089: try {
090: ObjectInputStream in = null;
091: in = new ObjectInputStream(new FileInputStream(remindFile));
092: curReminders = (Hashtable) in.readObject();
093: in.close();
094: } catch (Exception e) {
095: throw new WikiException("Remind File could not be read.");
096: }
097: return true;
098: }
099:
100: /**
101: *
102: */
103: private synchronized boolean writeRemindFile() throws WikiException {
104: try {
105: if (!remindFile.exists())
106: createRemindFile();
107: ObjectOutputStream out = new ObjectOutputStream(
108: new FileOutputStream(remindFile));
109: out.writeObject(curReminders);
110: out.close();
111: } catch (IOException e) {
112: throw new WikiException("Remind File could not be written");
113: }
114: return true;
115: }
116:
117: /**
118: * Adds a reminder to this topic. If a reminder already exists for this user, it will be
119: * replaced with the new reminder date.
120: *
121: * @param userName the name of the user to add
122: * @param dateToRemind the date on which to send a reminder to the specified user
123: * @exception vqwiki.WikiException if the file could not be written
124: */
125: public void addReminder(String userName, Date dateToRemind)
126: throws WikiException {
127: WikiReminder tReminder = new WikiReminder(userName,
128: dateToRemind);
129: curReminders.put(userName, tReminder);
130: writeRemindFile();
131: }
132:
133: /**
134: * Removes reminder for the specified user for this topic. If the removal
135: * empties the reminder list for this topic, the reminder file is deleted.
136: *
137: * @param userName the name of the user for whom to remove reminders
138: * @exception vqwiki.WikiException if the file could not be written
139: */
140: public synchronized void removeReminder(String userName)
141: throws WikiException {
142: curReminders.remove(userName);
143: if (curReminders.isEmpty()) {
144: remindFile.delete();
145: } else {
146: writeRemindFile();
147: }
148: }
149:
150: /**
151: * Checks whether the specified user has a reminder set for this topic.
152: *
153: * @param userName the name of the user to check
154: * @return boolean True if the specified user has a reminder set for this topic.
155: */
156: public boolean hasReminder(String userName) {
157: return curReminders.containsKey(userName);
158: }
159:
160: /**
161: * Returns the date specified for a reminder for this user for this topic.
162: *
163: * @param userName the name of the user to check
164: * @return Date the date of the reminder, or null if no reminder is set
165: */
166: public Date dateToRemind(String userName) {
167: if (!hasReminder(userName))
168: return null;
169: WikiReminder aReminder = (WikiReminder) curReminders
170: .get(userName);
171: return aReminder.getRemindDate();
172: }
173:
174: /**
175: * Retrieves the home directory of the VQWiki installation.
176: *
177: * @return String the home directory
178: */
179: protected String fileBase() {
180: return Environment.getInstance().getHomeDir();
181: }
182:
183: /**
184: * Sends reminders via email to all users who have requested a reminder
185: * on the specified date for this topic.
186: *
187: * @param remindDate the date of reminders to send
188: * @return boolean true if the operation completes successfully
189: * @exception java.lang.ClassNotFoundException, IOException if the mailer could not be instantiated
190: */
191: public boolean sendReminders(Date remindDate) throws Exception {
192: WikiMembers members = WikiBase.getInstance()
193: .getWikiMembersInstance(null);
194: WikiMail mailer = WikiMail.getInstance();
195: Iterator anIterator = curReminders.values().iterator();
196: while (anIterator.hasNext()) {
197: WikiReminder aReminder = (WikiReminder) anIterator.next();
198: if (aReminder.remindDateEquals(remindDate)) {
199: WikiMember aMember = members.findMemberByName(aReminder
200: .getUserName());
201: String replyAddress = Environment.getInstance()
202: .getStringSetting(
203: Environment.PROPERTY_REPLY_ADDRESS);
204: mailer.sendMail(replyAddress, aMember.getEmail(),
205: "Wiki Reminder", "Please visit the topic, '"
206: + topicName + "'.");
207: }
208: }
209: return true;
210: }
211: }
|