Search in sources :

Example 21 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultModelBridge method setFilterPreferenceEnabled.

@Override
public void setFilterPreferenceEnabled(DocumentReference user, String filterPreferenceName, boolean enabled) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
    boolean shouldSave = false;
    try {
        XWikiDocument doc = xwiki.getDocument(user, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null && StringUtils.equals(filterPreferenceName, obj.getStringValue(FILTER_PREFERENCE_NAME)) && (obj.getIntValue(FIELD_IS_ENABLED) != 0) != enabled) {
                    obj.setIntValue(FIELD_IS_ENABLED, enabled ? 1 : 0);
                    shouldSave = true;
                }
            }
        }
        if (shouldSave) {
            // Make this change a minor edit so it's not displayed, by default, in notifications
            xwiki.saveDocument(doc, String.format("%s filter preference [%s].", enabled ? "Enable" : "Disable", filterPreferenceName), true, context);
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to update enabled state filters [%s] for user [%s].", filterPreferenceName, user), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 22 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultModelBridge method getToggeableFilterActivations.

@Override
public Map<String, Boolean> getToggeableFilterActivations(DocumentReference user) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationPreferencesScopeClass = TOGGLEABLE_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
    Map<String, Boolean> filterStatus = new HashMap<>();
    try {
        XWikiDocument doc = xwiki.getDocument(user, context);
        for (NotificationFilter filter : componentManager.<NotificationFilter>getInstanceList(NotificationFilter.class)) {
            if (filter instanceof ToggleableNotificationFilter) {
                ToggleableNotificationFilter toggleableFilter = (ToggleableNotificationFilter) filter;
                boolean status = toggleableFilter.isEnabledByDefault();
                BaseObject obj = doc.getXObject(notificationPreferencesScopeClass, FIELD_FILTER_NAME, filter.getName());
                if (obj != null) {
                    status = obj.getIntValue(FIELD_IS_ENABLED, status ? 1 : 0) != 0;
                }
                filterStatus.put(filter.getName(), status);
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the toggleable filters preferences for the user [%s].", user), e);
    }
    return filterStatus;
}
Also used : HashMap(java.util.HashMap) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 23 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultNotificationDisplayer method renderNotification.

@Override
public Block renderNotification(CompositeEvent eventNotification) throws NotificationException {
    try {
        velocityManager.getCurrentVelocityContext().put(EVENT_BINDING_NAME, eventNotification);
        String templateName = String.format("notification/%s.vm", eventNotification.getType().replaceAll("\\/", "."));
        Template template = templateManager.getTemplate(templateName);
        return (template != null) ? templateManager.execute(template) : templateManager.execute("notification/default.vm");
    } catch (Exception e) {
        throw new NotificationException("Failed to render the notification.", e);
    } finally {
        velocityManager.getCurrentVelocityContext().remove(EVENT_BINDING_NAME);
    }
}
Also used : NotificationException(org.xwiki.notifications.NotificationException) NotificationException(org.xwiki.notifications.NotificationException) Template(org.xwiki.template.Template)

Example 24 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultNotificationFilterDisplayer method display.

@Override
public Block display(NotificationFilter filter, NotificationFilterPreference preference) throws NotificationException {
    try {
        setUpContext(scriptContextManager, filter, preference);
        // Try to get a template using the filter name ; if no template is found, fallback on the default one.
        String templateName = String.format("notification/filters/%s.vm", filter.getName().replaceAll("\\/", "."));
        Template template = templateManager.getTemplate(templateName);
        return (template != null) ? templateManager.execute(template) : templateManager.execute("notification/filters/default.vm");
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to display the notification filter [%s] with the filter preference [%s].", filter, preference), e);
    } finally {
        cleanUpContext();
    }
}
Also used : NotificationException(org.xwiki.notifications.NotificationException) NotificationException(org.xwiki.notifications.NotificationException) Template(org.xwiki.template.Template)

Aggregations

NotificationException (org.xwiki.notifications.NotificationException)24 DocumentReference (org.xwiki.model.reference.DocumentReference)14 XWikiContext (com.xpn.xwiki.XWikiContext)10 XWikiException (com.xpn.xwiki.XWikiException)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 ArrayList (java.util.ArrayList)6 List (java.util.List)4 NotificationFormat (org.xwiki.notifications.NotificationFormat)4 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)4 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)3 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 Map (java.util.Map)2 CompositeEvent (org.xwiki.notifications.CompositeEvent)2