private XWikiDocument addDocument(String documentName, String author, Date date, int vote)
     throws RatingsException {
   try {
     String ratingsClassName = RatingsManager.RATINGS_CLASSNAME;
     String pageName = getPageName(documentName);
     String parentDocName = documentName;
     XWiki xwiki = context.getWiki();
     XWikiDocument doc = xwiki.getDocument(pageName, context);
     doc.setParent(parentDocName);
     BaseObject obj = new BaseObject();
     obj.setClassName(ratingsClassName);
     obj.setName(pageName);
     obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR, author);
     obj.setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date);
     obj.setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote);
     obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_PARENT, parentDocName);
     doc.addObject(ratingsClassName, obj);
     return doc;
   } catch (XWikiException e) {
     throw new RatingsException(e);
   }
 }
  /**
   * Convert the old WorkspaceManager.WorkspaceCandidateMemberClass objects to the new candidacies
   * format.
   *
   * @param wikiId id of the wiki to upgrade
   * @throws XWikiException if problems occur
   */
  private void upgradeWorkspaceCandidacies(String wikiId) throws XWikiException {
    XWikiContext xcontext = getXWikiContext();
    XWiki xwiki = xcontext.getWiki();

    // We need to get the document that holds the candidacies
    DocumentReference candidaciesDocumentReference =
        new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "XWikiAllGroup");
    XWikiDocument candidaciesDocument = xwiki.getDocument(candidaciesDocumentReference, xcontext);

    // We need to get all the old candidacies
    DocumentReference oldCandidateClassReference =
        new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "WorkspaceCandidateMemberClass");
    List<BaseObject> candidacyObjects = candidaciesDocument.getXObjects(oldCandidateClassReference);
    if (candidacyObjects != null) {
      DocumentReference newCandidateClassReference =
          new DocumentReference(
              wikiId,
              WikiCandidateMemberClassInitializer.DOCUMENT_SPACE,
              WikiCandidateMemberClassInitializer.DOCUMENT_NAME);

      for (BaseObject oldObject : candidacyObjects) {
        if (oldObject == null) {
          continue;
        }
        // Transform the candidacy to the new class
        BaseObject newObject = candidaciesDocument.newXObject(newCandidateClassReference, xcontext);
        newObject.setStringValue(
            WikiCandidateMemberClassInitializer.FIELD_TYPE, oldObject.getStringValue("type"));
        newObject.setStringValue(
            WikiCandidateMemberClassInitializer.FIELD_STATUS, oldObject.getStringValue("status"));
        newObject.setStringValue(
            WikiCandidateMemberClassInitializer.FIELD_USER, oldObject.getStringValue("userName"));
        newObject.setLargeStringValue(
            WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT,
            oldObject.getLargeStringValue("userComment"));
        newObject.setStringValue(
            WikiCandidateMemberClassInitializer.FIELD_ADMIN, oldObject.getStringValue("reviewer"));
        newObject.setLargeStringValue(
            WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT,
            oldObject.getLargeStringValue("reviewerComment"));
        newObject.setLargeStringValue(
            WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT,
            oldObject.getLargeStringValue("reviewerPrivateComment"));
        newObject.setDateValue(
            WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION,
            oldObject.getDateValue("date"));
        newObject.setDateValue(
            WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE,
            oldObject.getDateValue("resolutionDate"));

        // Remove the old object
        candidaciesDocument.removeXObject(oldObject);
      }

      // Save
      xwiki.saveDocument(
          candidaciesDocument,
          "Upgrade candidacies from the old Workspace Application to the "
              + "new Wiki Application.",
          xcontext);
    }
  }