Search in sources :

Example 56 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class DefaultWikiComponentManagerEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    if (source instanceof DocumentModelBridge) {
        // Get the document reference
        DocumentModelBridge document = (DocumentModelBridge) source;
        DocumentReference documentReference = document.getDocumentReference();
        if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
            registerDocumentComponents(document.getDocumentReference());
        } else if (event instanceof DocumentDeletedEvent) {
            // Unregister components from the deleted document, if any
            this.wikiComponentManagerEventListenerHelper.unregisterComponents(documentReference);
        }
    /* If we are at application startup time, we have to instanciate every document or object that we can find
         * in the wiki */
    } else if (event instanceof ApplicationReadyEvent || event instanceof WikiReadyEvent) {
        // These 2 events are created when the database is ready. We register all wiki components.
        registerAllDocumentComponents();
    }
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) ApplicationReadyEvent(org.xwiki.bridge.event.ApplicationReadyEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) WikiReadyEvent(org.xwiki.bridge.event.WikiReadyEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 57 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class LinkCheckerEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    DocumentModelBridge doc = (DocumentModelBridge) source;
    final String reference = this.serializer.serialize(doc.getDocumentReference());
    Map<String, Map<String, LinkState>> states = this.linkStateManager.getLinkStates();
    for (Map.Entry<String, Map<String, LinkState>> entry : states.entrySet()) {
        Map<String, LinkState> state = entry.getValue();
        state.remove(reference);
        if (state.size() == 0) {
            states.remove(entry.getKey());
        }
    }
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Map(java.util.Map) LinkState(org.xwiki.rendering.transformation.linkchecker.LinkState)

Example 58 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class LinkCheckerEventListenerTest method testOnEvent.

@Test
public void testOnEvent() throws Exception {
    final DocumentModelBridge documentModelBridge = getMockery().mock(DocumentModelBridge.class);
    final EntityReferenceSerializer serializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    final DocumentReference reference = new DocumentReference("wiki", "space", "page");
    final LinkStateManager linkStateManager = getComponentManager().getInstance(LinkStateManager.class);
    final Map<String, Map<String, LinkState>> states = new HashMap<String, Map<String, LinkState>>();
    Map<String, LinkState> referenceStates1 = new HashMap<String, LinkState>();
    referenceStates1.put("wiki1:space1.page1", new LinkState(200, System.currentTimeMillis()));
    referenceStates1.put("wiki2:space2.page2", new LinkState(200, System.currentTimeMillis()));
    states.put("url1", referenceStates1);
    Map<String, LinkState> referenceStates2 = new HashMap<String, LinkState>();
    referenceStates2.put("wiki1:space1.page1", new LinkState(200, System.currentTimeMillis()));
    states.put("url2", referenceStates2);
    getMockery().checking(new Expectations() {

        {
            oneOf(documentModelBridge).getDocumentReference();
            will(returnValue(reference));
            oneOf(serializer).serialize(reference);
            will(returnValue("wiki1:space1.page1"));
            oneOf(linkStateManager).getLinkStates();
            will(returnValue(states));
        }
    });
    this.listener.onEvent(new DocumentUpdatingEvent(), documentModelBridge, null);
    Assert.assertEquals(1, states.size());
    Assert.assertEquals(1, states.get("url1").size());
    Assert.assertNull(states.get("url2"));
}
Also used : EntityReferenceSerializer(org.xwiki.model.reference.EntityReferenceSerializer) Expectations(org.jmock.Expectations) DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) LinkStateManager(org.xwiki.rendering.transformation.linkchecker.LinkStateManager) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) LinkState(org.xwiki.rendering.transformation.linkchecker.LinkState) Test(org.junit.Test)

Aggregations

DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)58 DocumentReference (org.xwiki.model.reference.DocumentReference)43 Test (org.junit.Test)39 Expectations (org.jmock.Expectations)18 XDOM (org.xwiki.rendering.block.XDOM)18 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)14 Syntax (org.xwiki.rendering.syntax.Syntax)11 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)9 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)7 Block (org.xwiki.rendering.block.Block)6 HashMap (java.util.HashMap)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)5 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetaData (org.xwiki.rendering.listener.MetaData)5 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)5 StringReader (java.io.StringReader)4