use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultNotificationFilterManager method saveFilterPreferences.
@Override
public void saveFilterPreferences(Set<NotificationFilterPreference> filterPreferences) {
Map<String, Set<NotificationFilterPreference>> preferencesMapping = new HashMap<>();
for (NotificationFilterPreference filterPreference : filterPreferences) {
// Try to get the corresponding provider, if no provider can be found, discard the save of the preference
String providerHint = filterPreference.getProviderHint();
if (componentManager.hasComponent(NotificationFilterPreferenceProvider.class, providerHint)) {
if (!preferencesMapping.containsKey(providerHint)) {
preferencesMapping.put(providerHint, new HashSet<>());
}
preferencesMapping.get(providerHint).add(filterPreference);
}
}
// Once we have created the mapping, save all the preferences using their correct providers
for (String providerHint : preferencesMapping.keySet()) {
try {
NotificationFilterPreferenceProvider provider = componentManager.getInstance(NotificationFilterPreferenceProvider.class, providerHint);
provider.saveFilterPreferences(preferencesMapping.get(providerHint));
} catch (ComponentLookupException e) {
logger.error("Unable to retrieve the notification filter preference provider for hint [{}]: [{}]", providerHint, e);
} catch (NotificationException e) {
logger.warn("Unable save the filter preferences [{}] against the provider [{}]: [{}]", preferencesMapping.get(providerHint), providerHint, ExceptionUtils.getRootCauseMessage(e));
}
}
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultNotificationFilterManager method fetchAllFilters.
/**
* Fetches every filter available to the user, without taking care of whether the filter is disabled by the user
* or not.
*
* @param user the user to use
* @return a set of filters
* @throws NotificationException if an error occurs
*/
private Set<NotificationFilter> fetchAllFilters(DocumentReference user) throws NotificationException {
// If the user is from the main wiki, get filters from all wikis
if (user.getWikiReference().getName().equals(wikiDescriptorManager.getMainWikiId())) {
String currentWikiId = wikiDescriptorManager.getCurrentWikiId();
Map<String, NotificationFilter> filters = new HashMap<>();
try {
for (String wikiId : wikiDescriptorManager.getAllIds()) {
modelContext.setCurrentEntityReference(new WikiReference(wikiId));
filters.putAll(componentManager.getInstanceMap(NotificationFilter.class));
}
} catch (Exception e) {
throw new NotificationException(ERROR_MESSAGE, e);
} finally {
modelContext.setCurrentEntityReference(new WikiReference(currentWikiId));
}
return new HashSet<>(filters.values());
} else {
// If the user is local, get filters from the current wiki only (we assume it's the wiki of the user).
try {
return new HashSet<>(componentManager.getInstanceList(NotificationFilter.class));
} catch (Exception e) {
throw new NotificationException(ERROR_MESSAGE, e);
}
}
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultModelBridge method getFilterPreferences.
@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException {
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
Set<NotificationFilterPreference> preferences = new HashSet<>();
try {
XWikiDocument doc = xwiki.getDocument(user, context);
List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
if (preferencesObj != null) {
for (BaseObject obj : preferencesObj) {
if (obj != null) {
Map<NotificationFilterProperty, List<String>> filterPreferenceProperties = createNotificationFilterPropertiesMap(obj);
NotificationFilterType filterType = NotificationFilterType.valueOf(obj.getStringValue(FIELD_FILTER_TYPE).toUpperCase());
Set<NotificationFormat> filterFormats = new HashSet<>();
for (String format : (List<String>) obj.getListValue(FIELD_FILTER_FORMATS)) {
filterFormats.add(NotificationFormat.valueOf(format.toUpperCase()));
}
// Create the new filter preference and add it to the list of preferences
DefaultNotificationFilterPreference notificationFilterPreference = new DefaultNotificationFilterPreference(obj.getStringValue(FILTER_PREFERENCE_NAME));
notificationFilterPreference.setProviderHint("userProfile");
notificationFilterPreference.setFilterName(obj.getStringValue(FIELD_FILTER_NAME));
notificationFilterPreference.setEnabled(obj.getIntValue(FIELD_IS_ENABLED, 1) == 1);
notificationFilterPreference.setActive(obj.getIntValue(FIELD_IS_ACTIVE, 1) == 1);
notificationFilterPreference.setFilterType(filterType);
notificationFilterPreference.setNotificationFormats(filterFormats);
notificationFilterPreference.setPreferenceProperties(filterPreferenceProperties);
preferences.add(notificationFilterPreference);
}
}
}
} catch (Exception e) {
throw new NotificationException(String.format("Failed to get the notification preferences scope for the user [%s].", user), e);
}
return preferences;
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultModelBridge method saveFilterPreferences.
@Override
public void saveFilterPreferences(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences) throws NotificationException {
if (user == null) {
return;
}
// Convert the collection of preferences to save to a Map sorted by filter names
Map<String, NotificationFilterPreference> toSave = filterPreferences.stream().collect(Collectors.toMap(NotificationFilterPreference::getFilterPreferenceName, Function.identity()));
// Usual XWiki objects
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
try {
XWikiDocument doc = xwiki.getDocument(user, context);
// Update existing objects if they match the filter preferences to save
updateExistingObjects(toSave, notificationFilterPreferenceClass, doc);
// Create objects from the remaining filter preferences to save
createNewObjects(toSave, notificationFilterPreferenceClass, doc, context);
// Make this change a minor edit so it's not displayed, by default, in notifications
xwiki.saveDocument(doc, "Save notification filter preferences.", true, context);
} catch (Exception e) {
throw new NotificationException(String.format("Failed to save the notification preferences scope for the user [%s].", user), e);
}
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultModelBridge method deleteFilterPreference.
@Override
public void deleteFilterPreference(DocumentReference user, String filterPreferenceName) 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))) {
doc.removeXObject(obj);
shouldSave = true;
}
}
}
if (shouldSave) {
xwiki.saveDocument(doc, String.format("Remove filter preference [%s].", filterPreferenceName), context);
}
} catch (Exception e) {
throw new NotificationException(String.format("Failed to delete filters [%s] for user [%s].", filterPreferenceName, user), e);
}
}
Aggregations