synchronized (INSTANCE) {
final Set foldersToUpdate = SynchronizedSet.decorate(new HashSet());
final Set foldersToDelete = SynchronizedSet.decorate(new HashSet());
SystemTransactionTemplate st = new SystemTransactionTemplate() {
protected Object doInTransaction(Object trSession) throws Exception {
Session session = (Session) trSession;
while (events.hasNext()) {
Event e = events.nextEvent();
String path = e.getPath();
System.out.println(path);
if (e.getType() == Event.NODE_REMOVED) {
if (isFolderNode(path)) {
removeFolder(path);
foldersToDelete.remove(path);
continue;
}
}
if (e.getType() == Event.NODE_ADDED) {
Item item = session.getItem(path);
if (item.isNode()) {
Node node = (Node) item;
if (node.getPrimaryNodeType().getName().equals(NodesTypes.PREFIX + FolderDao.NAME)) {
// Ajout l'index
if (e.getType() == Event.NODE_ADDED) {
indexFolder(node);
continue;
}
}
}
}
if (isPathInFolder(path)) {
Node folderNode = (Node) session.getItem(getFolderPath(path));
foldersToUpdate.add(folderNode);
}
// if (e.getPath().indexOf(NodesTypes.PREFIX + CollectionDao.FOLDERS_NODE_NAME + "/") > - 1) {
// We presume that we only listen for changes in folder nodes
// Node folderNode = getFolderNode(item);
// this.foldersToUpdate.remove(folderNode);
// this.foldersToUpdate.add(folderNode);
// }
}
for (Iterator it = foldersToUpdate.iterator(); it.hasNext(); ) {
Node folderNode = (Node) it.next();
String folderNodePath = folderNode.getPath();
if (!foldersToDelete.contains(folderNode.getPath())) {
//FIXME DECOMMENT
updateFolder(folderNode);
}
}
return null;
}
};
try {
st.execute();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}