Search in sources :

Example 1 with AutomaticWatchMode

use of org.xwiki.watchlist.internal.api.AutomaticWatchMode in project xwiki-platform by xwiki.

the class DefaultWatchListStore method getAutomaticWatchMode.

@Override
public AutomaticWatchMode getAutomaticWatchMode(String user) {
    XWikiContext context = contextProvider.get();
    AutomaticWatchMode mode = null;
    try {
        BaseObject watchObject = getWatchListObject(user);
        String value = watchObject.getStringValue(WatchListClassDocumentInitializer.AUTOMATICWATCH_PROPERTY);
        if (StringUtils.isNotBlank(value) && !WatchListClassDocumentInitializer.AUTOMATICWATCH_DEFAULT_VALUE.equals(value)) {
            mode = AutomaticWatchMode.valueOf(value);
        }
    } catch (Exception e) {
        // Failed for some reason, now try getting it from xwiki.cfg
        logger.error("Failed to get automatic watch mode for user [{}], using fallbacks", user, e);
    }
    if (mode == null) {
        String value = context.getWiki().Param("xwiki.plugin.watchlist.automaticwatch");
        if (value != null) {
            try {
                mode = AutomaticWatchMode.valueOf(value.toUpperCase());
            } catch (Exception e) {
                logger.warn("Invalid configuration in xwiki.plugin.watchlist.automaticwatch", e);
            }
        }
    }
    return mode != null ? mode : AutomaticWatchMode.MAJOR;
}
Also used : AutomaticWatchMode(org.xwiki.watchlist.internal.api.AutomaticWatchMode) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 2 with AutomaticWatchMode

use of org.xwiki.watchlist.internal.api.AutomaticWatchMode in project xwiki-platform by xwiki.

the class AutomaticWatchModeListener method documentModifiedHandler.

/**
 * Automatically watch modified document depending on the configuration.
 *
 * @param event the observation event we check for a deleted document event
 * @param currentDoc document version after event occurred
 * @param context the XWiki context
 */
private void documentModifiedHandler(Event event, XWikiDocument currentDoc, XWikiContext context) {
    String user = currentDoc.getContentAuthor();
    DocumentReference userReference = currentDoc.getContentAuthorReference();
    // Avoid handling guests or the superadmin user.
    if (userReference == null || !context.getWiki().exists(userReference, context)) {
        return;
    }
    // Determine if the current event should be registered, based on the user's prefereces.
    boolean register = false;
    AutomaticWatchMode mode = this.store.getAutomaticWatchMode(user);
    switch(mode) {
        case ALL:
            register = true;
            break;
        case MAJOR:
            register = !currentDoc.isMinorEdit();
            break;
        case NEW:
            register = event instanceof DocumentCreatedEvent;
            break;
        default:
            break;
    }
    if (register) {
        try {
            this.store.addWatchedElement(user, currentDoc.getPrefixedFullName(), WatchedElementType.DOCUMENT);
        } catch (XWikiException e) {
            logger.warn("Failed to watch document [{}] for user [{}]", currentDoc.getPrefixedFullName(), user, e);
        }
    }
}
Also used : AutomaticWatchMode(org.xwiki.watchlist.internal.api.AutomaticWatchMode) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)2 AutomaticWatchMode (org.xwiki.watchlist.internal.api.AutomaticWatchMode)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1