private DocumentReference getOwner(XWikiDocument doc) {
   String owner = null;
   BaseObject ownerObj = doc.getXObject(Owner.CLASS_REFERENCE);
   if (ownerObj != null) {
     owner = ownerObj.getStringValue("owner");
   }
   if (StringUtils.isNotBlank(owner)) {
     return this.stringEntityResolver.resolve(owner);
   } else if (doc.getCreatorReference() != null) {
     return doc.getCreatorReference();
   }
   return null;
 }
  @Override
  public SolrInputDocument getSolrDocument(EntityReference entityReference)
      throws SolrIndexException, IllegalArgumentException {
    DocumentReference documentReference = new DocumentReference(entityReference);

    XWikiContext context = getXWikiContext();

    try {
      SolrInputDocument solrDocument = new SolrInputDocument();

      XWikiDocument translatedDocument = getTranslatedDocument(documentReference);

      solrDocument.addField(Fields.ID, getId(documentReference));
      addDocumentFields(documentReference, solrDocument);
      solrDocument.addField(Fields.TYPE, documentReference.getType().name());
      solrDocument.addField(Fields.FULLNAME, compactSerializer.serialize(documentReference));

      // Convert the XWiki syntax of document to plain text.
      WikiPrinter printer = new DefaultWikiPrinter();
      renderer.render(translatedDocument.getXDOM(), printer);

      // Same for document title
      String plainTitle = translatedDocument.getRenderedTitle(Syntax.PLAIN_1_0, context);

      // Get the rendered plain text title.
      solrDocument.addField(Fields.TITLE /* + USCORE + language */, plainTitle);
      solrDocument.addField(Fields.DOCUMENT_CONTENT /* + USCORE + language */, printer.toString());
      solrDocument.addField(Fields.VERSION, translatedDocument.getVersion());

      solrDocument.addField(
          Fields.AUTHOR, serializer.serialize(translatedDocument.getAuthorReference()));
      solrDocument.addField(
          Fields.CREATOR, serializer.serialize(translatedDocument.getCreatorReference()));
      solrDocument.addField(Fields.CREATIONDATE, translatedDocument.getCreationDate());
      solrDocument.addField(Fields.DATE, translatedDocument.getContentUpdateDate());

      // Document translations have their own hidden fields
      solrDocument.setField(Fields.HIDDEN, translatedDocument.isHidden());

      // Index the Comments and Objects in general. Use the original document to get the comment
      // objects since the
      // translated document is just a lightweight object containing the translated content and
      // title.

      // FIXME: What about the fact that all translations have the same comments? The field will get
      // copied to
      // each version. Maybe we should handle specially the original document.
      XWikiDocument originalDocument = getDocument(documentReference);

      // Comments. TODO: Is this particular handling of comments actually useful?
      addComments(solrDocument, originalDocument);

      // Objects
      for (Map.Entry<DocumentReference, List<BaseObject>> objects :
          originalDocument.getXObjects().entrySet()) {
        for (BaseObject object : objects.getValue()) {
          this.addObjectContent(solrDocument, object);
        }
      }

      return solrDocument;
    } catch (Exception e) {
      throw new SolrIndexException(
          String.format(
              "Failed to get input document for '%s'", serializer.serialize(documentReference)),
          e);
    }
  }
  @Before
  public void setUp() throws Exception {
    EntityReferenceSerializer<String> localSerializer =
        mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    EntityReferenceSerializer<String> serializer =
        mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "default");

    // No locale provided.
    documentReference = new DocumentReference("wiki", "space", "name");
    documentReferenceString = serializer.serialize(documentReference);
    documentReferenceLocalString = localSerializer.serialize(documentReference);

    language = "en";
    renderedContent = "content";
    title = "title";
    version = "1.1";
    hidden = false;
    date = new Date();
    creationDate = new Date();

    authorReference = new DocumentReference("wiki", "space", "author");
    authorString = serializer.serialize(authorReference);
    authorDisplay = "Au Thor";

    creatorReference = new DocumentReference("wiki", "space", "Creator");
    creatorString = serializer.serialize(creatorReference);
    creatorDisplay = "Crea Tor";

    // Mock

    mockContext = mock(XWikiContext.class);
    Execution mockExecution = mocker.getInstance(Execution.class);
    ExecutionContext mockExecutionContext = new ExecutionContext();
    mockExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, mockContext);

    when(mockExecution.getContext()).thenReturn(mockExecutionContext);

    mockXWiki = mock(XWiki.class);
    mockDocument = mock(XWikiDocument.class);

    when(mockContext.getWiki()).thenReturn(mockXWiki);
    when(mockXWiki.getDocument(documentReference, mockContext)).thenReturn(mockDocument);

    when(mockDocument.getRealLanguage()).thenReturn(language);
    when(mockDocument.getTranslatedDocument(any(String.class), eq(mockContext)))
        .thenReturn(mockDocument);

    mockDab = mocker.getInstance(DocumentAccessBridge.class);
    when(mockDab.getDocument(documentReference)).thenReturn(mockDocument);

    BlockRenderer mockPlainRenderer = mocker.getInstance(BlockRenderer.class, "plain/1.0");
    doAnswer(
            new Answer<Object>() {
              public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();

                WikiPrinter printer = (WikiPrinter) args[1];
                printer.print(renderedContent);

                return null;
              }
            })
        .when(mockPlainRenderer)
        .render(any(Block.class), any(WikiPrinter.class));

    when(mockDocument.getRenderedTitle(any(Syntax.class), eq(mockContext))).thenReturn(title);

    when(mockDocument.getVersion()).thenReturn(version);

    when(mockDocument.getAuthorReference()).thenReturn(authorReference);
    when(mockXWiki.getUserName(authorString, null, false, mockContext)).thenReturn(authorDisplay);

    when(mockDocument.getCreatorReference()).thenReturn(creatorReference);
    when(mockXWiki.getUserName(creatorString, null, false, mockContext)).thenReturn(creatorDisplay);

    when(mockDocument.getCreationDate()).thenReturn(creationDate);
    when(mockDocument.getContentUpdateDate()).thenReturn(date);

    when(mockDocument.isHidden()).thenReturn(hidden);
  }
  @Test
  public void testImportUsersAndGroupsPreserveVersion()
      throws WikiStreamException, XWikiException, ParseException {
    UserInstanceOutputProperties outputProperties = new UserInstanceOutputProperties();

    outputProperties.setPreserveVersion(true);

    importFromXML("user1", outputProperties);

    // XWiki.user1

    XWikiDocument userDocument =
        this.oldcore
            .getMockXWiki()
            .getDocument(
                new DocumentReference("wiki1", "XWiki", "user1"), this.oldcore.getXWikiContext());

    Assert.assertFalse(userDocument.isNew());

    Assert.assertEquals(
        new DocumentReference("wiki1", "XWiki", "user1"), userDocument.getCreatorReference());
    Assert.assertEquals(toDate("2000-01-10 00:00:00.0 UTC"), userDocument.getCreationDate());
    Assert.assertEquals(
        new DocumentReference("wiki1", "XWiki", "user1"), userDocument.getAuthorReference());
    Assert.assertEquals(toDate("2000-01-11 00:00:00.0 UTC"), userDocument.getDate());
    Assert.assertEquals(toDate("2000-01-11 00:00:00.0 UTC"), userDocument.getContentUpdateDate());
    Assert.assertEquals(
        new DocumentReference("wiki1", "XWiki", "user1"), userDocument.getContentAuthorReference());
    Assert.assertEquals(false, userDocument.isMinorEdit());
    Assert.assertEquals("Import", userDocument.getComment());

    BaseObject userObject = userDocument.getXObject(USER_CLASS);
    Assert.assertEquals(0, userObject.getNumber());
    Assert.assertEquals("user1 first name", userObject.getStringValue("first_name"));
    Assert.assertEquals("user1 last name", userObject.getStringValue("last_name"));
    Assert.assertEquals("*****@*****.**", userObject.getStringValue("email"));
    Assert.assertEquals(1, userObject.getIntValue("active"));

    // XWiki.user2

    userDocument =
        this.oldcore
            .getMockXWiki()
            .getDocument(
                new DocumentReference("wiki1", "XWiki", "user2"), this.oldcore.getXWikiContext());

    Assert.assertFalse(userDocument.isNew());

    Assert.assertEquals(
        new DocumentReference("wiki1", "XWiki", "user2"), userDocument.getCreatorReference());
    Assert.assertEquals(toDate("2000-01-20 00:00:00.0 UTC"), userDocument.getCreationDate());
    Assert.assertEquals(
        new DocumentReference("wiki1", "XWiki", "user2"), userDocument.getAuthorReference());
    Assert.assertEquals(toDate("2000-01-21 00:00:00.0 UTC"), userDocument.getDate());
    Assert.assertEquals(toDate("2000-01-21 00:00:00.0 UTC"), userDocument.getContentUpdateDate());
    Assert.assertEquals(
        new DocumentReference("wiki1", "XWiki", "user2"), userDocument.getContentAuthorReference());
    Assert.assertEquals(false, userDocument.isMinorEdit());
    Assert.assertEquals("Import", userDocument.getComment());

    userObject = userDocument.getXObject(USER_CLASS);
    Assert.assertEquals(0, userObject.getNumber());
    Assert.assertEquals("user2 first name", userObject.getStringValue("first_name"));
    Assert.assertEquals("user2 last name", userObject.getStringValue("last_name"));
    Assert.assertEquals("*****@*****.**", userObject.getStringValue("email"));
    Assert.assertEquals(0, userObject.getIntValue("active"));

    // XWiki.group1

    XWikiDocument groupDocument =
        this.oldcore
            .getMockXWiki()
            .getDocument(
                new DocumentReference("wiki1", "XWiki", "group1"), this.oldcore.getXWikiContext());

    Assert.assertFalse(groupDocument.isNew());

    BaseObject groupMemberObject0 = groupDocument.getXObject(GROUP_CLASS, 0);
    Assert.assertEquals("XWiki.user1", groupMemberObject0.getStringValue("member"));
    BaseObject groupMemberObject1 = groupDocument.getXObject(GROUP_CLASS, 1);
    Assert.assertEquals("XWiki.user2", groupMemberObject1.getStringValue("member"));

    // XWiki.group2

    groupDocument =
        this.oldcore
            .getMockXWiki()
            .getDocument(
                new DocumentReference("wiki1", "XWiki", "group2"), this.oldcore.getXWikiContext());

    Assert.assertFalse(groupDocument.isNew());

    groupMemberObject0 = groupDocument.getXObject(GROUP_CLASS, 0);
    Assert.assertEquals("XWiki.group1", groupMemberObject0.getStringValue("member"));

    // XWiki.emptygroup

    groupDocument =
        this.oldcore
            .getMockXWiki()
            .getDocument(
                new DocumentReference("wiki1", "XWiki", "emptygroup"),
                this.oldcore.getXWikiContext());

    Assert.assertFalse(groupDocument.isNew());

    groupMemberObject0 = groupDocument.getXObject(GROUP_CLASS, 0);
    Assert.assertEquals("", groupMemberObject0.getStringValue("member"));
  }