use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class QueryGenerator method handleEventPreferences.
/**
* For each notification preference of the given user, add a constraint on the events to
* - have one of the notification types that have been subscribed by the user;
* - have a date superior to the start date corresponding to this type;
* - match the custom defined user filters.
*
* @param user the current user
* @param preferences a list of the user preferences
* @return a list of maps that contains query parameters
* @throws NotificationException if an error occurred
*/
private AbstractOperatorNode handleEventPreferences(DocumentReference user, List<NotificationPreference> preferences) throws NotificationException {
AbstractOperatorNode preferencesNode = null;
// Filter the notification preferences that are not bound to a specific EVENT_TYPE
Iterator<NotificationPreference> it = preferences.stream().filter(pref -> pref.getProperties().containsKey(NotificationPreferenceProperty.EVENT_TYPE)).iterator();
while (it.hasNext()) {
NotificationPreference preference = it.next();
AbstractOperatorNode preferenceTypeNode = new AndNode(new EqualsNode(value(EventProperty.TYPE), value((String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE))), new GreaterThanNode(value(EventProperty.DATE), value(preference.getStartDate())));
// Get the notification filters that can be applied to the current preference
Collection<NotificationFilter> filters = notificationFilterManager.getFilters(user, preference);
for (NotificationFilter filter : filters) {
ExpressionNode node = filter.filterExpression(user, preference);
if (node != null && node instanceof AbstractOperatorNode) {
preferenceTypeNode = preferenceTypeNode.and((AbstractOperatorNode) node);
}
}
if (preferencesNode == null) {
preferencesNode = preferenceTypeNode;
} else {
preferencesNode = preferencesNode.or(preferenceTypeNode);
}
}
return preferencesNode;
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultNotificationManagerTest method getEventsWhenException.
@Test
public void getEventsWhenException() throws Exception {
// Mocks
NotificationException exception = new NotificationException("Error");
when(queryGenerator.generateQuery(eq(userReference), any(NotificationFormat.class), isNull(), isNull(), any(List.class))).thenThrow(exception);
// Test
NotificationException caughtException = null;
try {
mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 2);
} catch (NotificationException e) {
caughtException = e;
}
// Verify
assertNotNull(caughtException);
assertEquals("Fail to get the list of notifications.", caughtException.getMessage());
assertEquals(exception, caughtException.getCause());
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultModelBridge method setStartDateForUser.
@Override
public void setStartDateForUser(DocumentReference userReference, Date startDate) throws NotificationException {
try {
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
XWikiDocument document = xwiki.getDocument(userReference, context);
List<BaseObject> objects = document.getXObjects(NOTIFICATION_PREFERENCE_CLASS);
if (objects != null) {
for (BaseObject object : objects) {
if (object != null) {
object.setDateValue(START_DATE_FIELD, startDate);
}
}
}
// Make this change a minor edit so it's not displayed, by default, in notifications
xwiki.saveDocument(document, NOTIFICATION_START_DATE_UPDATE_COMMENT, true, context);
} catch (Exception e) {
throw new NotificationException(String.format(SET_USER_START_DATE_ERROR_MESSAGE, userReference), e);
}
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultModelBridge method getNotificationPreferences.
private List<NotificationPreference> getNotificationPreferences(DocumentReference document, String providerHint) throws NotificationException {
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
final DocumentReference notificationPreferencesClass = NOTIFICATION_PREFERENCE_CLASS.setWikiReference(document.getWikiReference());
List<NotificationPreference> preferences = new ArrayList<>();
try {
XWikiDocument doc = xwiki.getDocument(document, context);
List<BaseObject> preferencesObj = doc.getXObjects(notificationPreferencesClass);
if (preferencesObj != null) {
for (BaseObject obj : preferencesObj) {
if (obj != null) {
String objFormat = obj.getStringValue(FORMAT_FIELD);
Date objStartDate = obj.getDateValue(START_DATE_FIELD);
Map<NotificationPreferenceProperty, Object> properties = extractNotificationPreferenceProperties(obj);
notificationPreferenceBuilder.prepare();
notificationPreferenceBuilder.setProperties(properties);
notificationPreferenceBuilder.setStartDate((objStartDate != null) ? objStartDate : doc.getCreationDate());
notificationPreferenceBuilder.setFormat(StringUtils.isNotBlank(objFormat) ? NotificationFormat.valueOf(objFormat.toUpperCase()) : NotificationFormat.ALERT);
notificationPreferenceBuilder.setTarget(document);
notificationPreferenceBuilder.setProviderHint(providerHint);
notificationPreferenceBuilder.setEnabled(obj.getIntValue(NOTIFICATION_ENABLED_FIELD, 0) != 0);
notificationPreferenceBuilder.setCategory(NotificationPreferenceCategory.DEFAULT);
preferences.add(notificationPreferenceBuilder.build());
}
}
}
} catch (Exception e) {
throw new NotificationException(String.format("Failed to get the notification preferences from the document [%s].", document), e);
}
return preferences;
}
use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.
the class DefaultNotificationManager method getEvents.
private List<CompositeEvent> getEvents(List<CompositeEvent> results, Parameters parameters) throws NotificationException {
// Because the user might not be able to see all notifications because of the rights, we take from the database
// more events than expected and we will filter afterwards.
final int batchSize = parameters.expectedCount * 2;
try {
// Create the query
Query query = queryGenerator.generateQuery(parameters.userReference, parameters.format, parameters.endDate, parameters.fromDate, parameters.blackList);
if (query == null) {
return Collections.emptyList();
}
query.setLimit(batchSize);
// Get a batch of events
List<Event> batch = eventStream.searchEvents(query);
// Add to the results the events the user has the right to see
for (Event event : batch) {
DocumentReference document = event.getDocument();
// Don't record events concerning a doc the user cannot see
if (document != null && !authorizationManager.hasAccess(Right.VIEW, parameters.userReference, document)) {
continue;
}
if (filterEvent(event, parameters)) {
continue;
}
// Record this event
recordEvent(results, event);
// If the expected count is reached, stop now
if (results.size() >= parameters.expectedCount) {
return results;
}
}
// If we haven't get the expected number of events, perform a new batch
if (results.size() < parameters.expectedCount && batch.size() == batchSize) {
parameters.blackList.addAll(getEventsIds(batch));
getEvents(results, parameters);
}
return results;
} catch (Exception e) {
throw new NotificationException("Fail to get the list of notifications.", e);
}
}
Aggregations