コード例 #1
0
  @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);
  }