Skip to content

Commit

Permalink
Diagram installation remains on hold on a XWiki Cloud instance #148
Browse files Browse the repository at this point in the history
* synchronize the process of starting threads to be sure that there are not multiple renaming jobs trying to start the threads at once when they not initialized yet (to avoid creating multiple threads and keep only the last assignment)
* verify that an entry queue added after page rename could be a needed entry by checking it has backlinks
  • Loading branch information
oanalavinia committed May 26, 2020
2 parents c203ea3 + 5257663 commit 40df34c
Showing 1 changed file with 33 additions and 9 deletions.
Expand Up @@ -39,6 +39,8 @@
import org.xwiki.observation.ObservationContext;
import org.xwiki.observation.event.Event;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xwiki.diagram.internal.handlers.DiagramContentHandler;

Expand Down Expand Up @@ -98,11 +100,8 @@ public PageRenameListener()
@Override
public void onEvent(Event event, Object source, Object data)
{
if (this.diagramLinksThread == null) {
this.diagramLinksThread = startThread(this.diagramLinksRunnable, "Update Diagram Links Thread");
}
if (this.diagramMacroThread == null) {
this.diagramMacroThread = startThread(this.diagramMacroRunnable, "Update Diagram Macro Thread");
if (this.diagramLinksThread == null || this.diagramMacroThread == null) {
startThreads();
}

if (observationContext.isIn(new JobStartedEvent("refactoring/rename"))) {
Expand All @@ -117,7 +116,7 @@ public void onEvent(Event event, Object source, Object data)
DocumentReference currentDocRef = currentDoc.getDocumentReference();

if (destinationRef.equals(currentDocRef)) {
startContentUpdating(currentDoc, new DiagramQueueEntry(originalDocRef, currentDocRef));
startContentUpdating((XWikiContext) data, currentDoc, originalDocRef);
}
}
}
Expand All @@ -128,15 +127,40 @@ public void onEvent(Event event, Object source, Object data)
* content of a diagram after renaming pages that are linked in the content, update reference parameter of diagram
* macro after renaming a diagram.
*
* @param context the current context
* @param currentDoc current document
* @param queueEntry entry to be added in the thread queue
* @param originalDocRef the reference of the document before rename
*/
public void startContentUpdating(XWikiDocument currentDoc, DiagramQueueEntry queueEntry)
public void startContentUpdating(XWikiContext context, XWikiDocument currentDoc, DocumentReference originalDocRef)
{
DiagramQueueEntry queueEntry = new DiagramQueueEntry(originalDocRef, currentDoc.getDocumentReference());
if (currentDoc.getXObject(DiagramContentHandler.DIAGRAM_CLASS) != null) {
this.diagramMacroRunnable.addToQueue(queueEntry);
}
this.diagramLinksRunnable.addToQueue(queueEntry);

try {
// Restrain the number of documents added to queue to only those that have backlinks. We need to take
// backlinks from the original document because at this step they are not loaded to the new document.
if (!context.getWiki().getDocument(originalDocRef, context).getBackLinkedReferences(context).isEmpty()) {
this.diagramLinksRunnable.addToQueue(queueEntry);
}
} catch (XWikiException e) {
logger.warn("Error when getting backlinks of renamed document");
}
}

/**
* Multiple rename jobs could be started at very close dates in the moment when the threads were not initialized yet
* (for example, at installation step) and we need to be sure that only a single instance of each thread is created.
*/
public synchronized void startThreads()
{
if (this.diagramLinksThread == null) {
this.diagramLinksThread = startThread(this.diagramLinksRunnable, "Update Diagram Links Thread");
}
if (this.diagramMacroThread == null) {
this.diagramMacroThread = startThread(this.diagramMacroRunnable, "Update Diagram Macro Thread");
}
}

/**
Expand Down

0 comments on commit 40df34c

Please sign in to comment.