Search in sources :

Example 1 with AbstractDocumentEvent

use of org.xwiki.bridge.event.AbstractDocumentEvent in project xwiki-platform by xwiki.

the class SolrIndexAvailableLocalesListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiContext xcontext = (XWikiContext) data;
    String wiki = xcontext.getWikiId();
    Set<Locale> oldLocales = this.localesCache.get(wiki);
    List<Locale> availableLocales = xcontext.getWiki().getAvailableLocales(xcontext);
    // Update the cache
    this.localesCache.put(wiki, new HashSet<Locale>(availableLocales));
    try {
        // initialization
        if (oldLocales != null && event instanceof AbstractDocumentEvent) {
            Collection<Locale> newLocales = CollectionUtils.subtract(availableLocales, oldLocales);
            if (!newLocales.isEmpty()) {
                StringBuilder builder = new StringBuilder();
                for (Locale newLocale : newLocales) {
                    for (Locale locale : getParentLocales(newLocale)) {
                        if (builder.length() > 0) {
                            builder.append(" OR ");
                        }
                        builder.append(FieldUtils.DOCUMENT_LOCALE);
                        builder.append(':');
                        builder.append('"');
                        builder.append(locale.toString());
                        builder.append('"');
                    }
                }
                SolrQuery solrQuery = new SolrQuery(builder.toString());
                solrQuery.setFields(FieldUtils.WIKI, FieldUtils.SPACES, FieldUtils.NAME, FieldUtils.DOCUMENT_LOCALE);
                solrQuery.addFilterQuery(FieldUtils.TYPE + ':' + EntityType.DOCUMENT.name());
                StreamingResponseCallback callback = new StreamingResponseCallback() {

                    @Override
                    public void streamSolrDocument(SolrDocument doc) {
                        solrIndexer.get().index(solrDocumentReferenceResolver.resolve(doc), true);
                    }

                    @Override
                    public void streamDocListInfo(long numFound, long start, Float maxScore) {
                    // Do nothing.
                    }
                };
                this.solrInstanceProvider.get().queryAndStreamResponse(solrQuery, callback);
            }
        }
    } catch (Exception e) {
        this.logger.error("Failed to handle event [{}] with source [{}]", event, source, e);
    }
}
Also used : Locale(java.util.Locale) XWikiContext(com.xpn.xwiki.XWikiContext) SolrQuery(org.apache.solr.client.solrj.SolrQuery) StreamingResponseCallback(org.apache.solr.client.solrj.StreamingResponseCallback) SolrDocument(org.apache.solr.common.SolrDocument) AbstractDocumentEvent(org.xwiki.bridge.event.AbstractDocumentEvent)

Example 2 with AbstractDocumentEvent

use of org.xwiki.bridge.event.AbstractDocumentEvent in project xwiki-platform by xwiki.

the class WikiMacroEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    if (event instanceof AbstractDocumentEvent) {
        DocumentModelBridge document = (DocumentModelBridge) source;
        DocumentReference documentReference = document.getDocumentReference();
        if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
            registerMacro(documentReference);
        } else if (event instanceof DocumentDeletedEvent) {
            unregisterMacro(documentReference);
        }
    }
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) AbstractDocumentEvent(org.xwiki.bridge.event.AbstractDocumentEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

AbstractDocumentEvent (org.xwiki.bridge.event.AbstractDocumentEvent)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 Locale (java.util.Locale)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 StreamingResponseCallback (org.apache.solr.client.solrj.StreamingResponseCallback)1 SolrDocument (org.apache.solr.common.SolrDocument)1 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)1 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)1 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)1 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1