Handling Document Events
Document events allow you to be informed of newly created documents and documents that have been edited, modified, or deleted. The icc.db.File class provides additional functionality to help you in storing and maintaining a hashtable of documents.
1. Creation events
A document CREATION event is posted when a document is newly created. The event object is an Object array that includes the cube and identifier of the document. The event source is the address card of the author of this creation. This author will not receive this event.
The following example assumes the user is interested in retrieving each newly created document he or she has read access to. Alternatively, the user could maintain a list of cubes (or folders) of interest.
public void handleFileEvent(Event event) {
switch (event.type) {
case Event.CREATION:
Object[] objs = (Object[]) event.object;
Cube cube = (Cube) objs[0];
Integer id = (Integer) objs[1];
// retrieve the document if the user has
// read access
if (Access.mayRead(Project.permission(cube,
this.userId)))
this.send(new Package(IDCard.SERVICE,
File.retrieve(id, cube)));
break;
}
}
Note: All of the following assumptions were made. The instance variable userId contains the user's identifier. The instance method send sends a package through the speaker and catches any IOException that may be thrown. Project information, including project partners and access rights are known and stored in the Project class.
2. Deletion events
A document DELETION event is posted when a document is deleted. The event object is an Object array that includes the cube and identifier of the document. The event source is the address card of the author of this deletion. This author will not receive this event.
The following example removes the corresponding document information from the File class. Additonally, if the user maintains a list of document folders, these must also be checked for the deleted document.
public void handleFileEvent(Event event) {
switch (event.type) {
case Event.DELETION:
Object[] objs = (Object[]) event.object;
Integer id = (Integer) objs[1];
File.remove(id);
break;
}
}
3. Edit and modification events
A document EDIT event is posted when a document is edited, with the exception of the document's content. A document MODIFICATION event is posted when a document's content (URL, path, or text) is modified. The event object in both cases is the document's identifier. The event source is the address card of the author of this deletion. This author will not receive this event.
A document EDIT event is also posted when a number of documents have their links to a particular document altered as the result of a creation, deletion, modification or edit of this document. The event object is the array of document identifiers. The event source is null.
The following example retrieves the corresponding documents if these were already present in the File class.
public void handleFileEvent(Event event) {
switch (event.type) {
case Event.EDIT: case Event.MODIFICATION:
if (event.object instanceof Integer) {
Integer id = (Integer) event.object;
// retrieve the document if it is
// already known
if (File.get(id) != null)
this.send(new Package(IDCard.SERVICE,
File.retrieve(id)));
break;
}
if (event.object instanceof Integer[]) {
Integer[] ids = (Integer[]) event.object;
// remove identifiers of documents that
// aren't known yet
int count = 0;
for (int i = 0; i < ids.length; i++)
if (File.get(ids[i]) != null)
ids[count++] = ids[i];
if (count == 0) break;
if (count < ids.length) {
Integer[] origids = ids;
ids = new Integer[count];
for (int i = 0; i < ids.length; i++)
ids[i] = origids[i];
}
// retrieve the edited documents
this.send(new Package(IDCard.SERVICE,
File.retrieve(ids)));
}
break;
}
}
Note: It is assumed that the instance method send sends a package through the speaker and catches any IOException that may be thrown.
Last modified on 5 May 1999 by Rudi Stouffs |