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);
}
}
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;
}
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);
}
}
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();
}
}
Aggregations