private void removeExistingDescriptor(XWikiDocument document) {
   List<BaseObject> existingServerClassObjects = document.getXObjects(SERVER_CLASS);
   if (existingServerClassObjects != null && !existingServerClassObjects.isEmpty()) {
     String wikiId =
         wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(
             document.getDocumentReference());
     DefaultWikiDescriptor existingDescriptor = cache.getFromId(wikiId);
     if (existingDescriptor != null) {
       cache.remove(existingDescriptor);
     }
   }
 }
  @Override
  public void onEvent(Event event, Object source, Object data) {
    XWikiDocument document = (XWikiDocument) source;

    // If the document is deleted then check the original document to see if it had XWiki Server
    // objects and if
    // so then unregister them
    if (event instanceof DocumentDeletedEvent || event instanceof DocumentUpdatedEvent) {
      removeExistingDescriptor(document.getOriginalDocument());
    }

    // Register the new XWiki Server objects if any
    List<BaseObject> serverClassObjects = document.getXObjects(SERVER_CLASS);
    if (serverClassObjects != null && !serverClassObjects.isEmpty()) {
      DefaultWikiDescriptor descriptor =
          this.builder.buildDescriptorObject(serverClassObjects, document);
      if (descriptor != null) {
        cache.add(descriptor);
      }
    }
  }