Set removedEvents = new HashSet();
// separately collect the add events
Set addEvents = new HashSet();
for (Iterator it = events.iterator(); it.hasNext();) {
Event event = (Event) it.next();
int type = event.getType();
if (type == Event.NODE_REMOVED) {
// remember removed nodes separately for proper handling later on.
removedEvents.add(event.getItemId());
} else if (type == Event.NODE_ADDED || type == Event.PROPERTY_ADDED) {
addEvents.add(event);
it.remove();
}
}
/* Process ADD-events.
In case of persisting transients modifications, the event-set may
still contain events that are not covered by the changeLog such as
new version-history or other autocreated properties and nodes.
Add events need to be processed hierarchically, since its not possible
to add a new child reference to a state that is not yet present in
the state manager.
The 'progress' flag is used to make sure, that during each loop at
least one event has been processed and removed from the iterator.
If this is not the case, there are not parent states present in the
state manager that need to be updated and the remaining events may
be ignored.
*/
boolean progress = true;
while (!addEvents.isEmpty() && progress) {
progress = false;
for (Iterator it = addEvents.iterator(); it.hasNext();) {
Event ev = (Event) it.next();
NodeId parentId = ev.getParentId();
HierarchyEntry parent = null;
if (parentId != null) {
parent = hierarchyMgr.lookup(parentId);
if (parent == null && ev.getPath() != null && parentId.getUniqueID() != null) {
// parentID contains a uniqueID part -> try to lookup
// the parent by path.
try {
Path parentPath = ev.getPath().getAncestor(1);
parent = hierarchyMgr.lookup(parentPath);
} catch (RepositoryException e) {
// should not occur
log.debug(e.getMessage());
}
}
}
if (parent != null && parent.denotesNode()) {
((NodeEntry) parent).refresh(ev);
it.remove();
progress = true;
}
}
}
/* process all other events (removal, property changed) */
for (Iterator it = events.iterator(); it.hasNext(); ) {
Event event = (Event) it.next();
int type = event.getType();
NodeId parentId = event.getParentId();
NodeEntry parent = (parentId != null) ? (NodeEntry) hierarchyMgr.lookup(parentId) : null;
switch (type) {
case Event.NODE_REMOVED:
case Event.PROPERTY_REMOVED:
// notify parent about removal if its child-entry.
// - if parent is 'null' (i.e. not yet loaded) the child-entry does
// not exist either -> no need to inform child-entry
// - if parent got removed with the same event-bundle
// only remove the parent an skip this event.
if (parent != null && !removedEvents.contains(parentId)) {
parent.refresh(event);
}
break;
case Event.PROPERTY_CHANGED:
// notify parent in case jcr:mixintypes or jcr:uuid was changed.
// if parent is 'null' (i.e. not yet loaded) the prop-entry does
// not exist either -> no need to inform propEntry
if (parent != null) {
parent.refresh(event);
}
break;
case Event.NODE_MOVED:
// TODO: implementation missing
throw new UnsupportedOperationException("Implementation missing");
//break;
case Event.PERSIST:
// TODO: implementation missing
throw new UnsupportedOperationException("Implementation missing");
//break;
default:
// should never occur
throw new IllegalArgumentException("Invalid event type: " + event.getType());
}
}
}