Search in sources :

Example 1 with NotificationFormat

use of org.xwiki.notifications.NotificationFormat 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;
}
Also used : NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) 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) NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFormat(org.xwiki.notifications.NotificationFormat) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) ArrayList(java.util.ArrayList) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 2 with NotificationFormat

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

the class XWikiEventTypesEnabler method ensureXWikiNotificationsAreEnabled.

/**
 * Make sure that notification are enabled for at least one XWiki Event Types (create, update, delete,
 * addComment) for the current user.
 * @throws NotificationException if an error occurs
 */
public void ensureXWikiNotificationsAreEnabled() throws NotificationException {
    if (isNotificationDisabled()) {
        TargetableNotificationPreferenceBuilder builder = targetableNotificationPreferenceBuilderProvider.get();
        List<NotificationPreference> preferencesToCreate = new ArrayList<>();
        Date now = new Date();
        for (String eventType : XWIKI_EVENT_TYPES) {
            for (NotificationFormat format : NotificationFormat.values()) {
                preferencesToCreate.add(createNotificationPreference(builder, eventType, format, now));
            }
        }
        notificationPreferenceManager.savePreferences(preferencesToCreate);
    }
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) TargetableNotificationPreferenceBuilder(org.xwiki.notifications.preferences.TargetableNotificationPreferenceBuilder) NotificationFormat(org.xwiki.notifications.NotificationFormat) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 3 with NotificationFormat

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

the class NotificationPreferenceScriptService method saveNotificationPreferences.

private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException {
    /*
            The JSON we get is a "snapshot" of the states of the buttons the user has in front of her eyes when she is
            managing her preferences.

            We did that so we can save several preferences in the same time without making too much requests (for
            example when the user enable an application, it actually enable all the app's event types).

            However, this snapshot also "freeze" the default preferences, ie the preferences set at the wiki level and
            that the user has not changed.

            Example:
            1. Wiki Administrator enables the application A by default, and doesn't touch the settings for the
               application B.
            2. John Doe disables the application A on HIS profile. He doesn't touch the setting for the application B.
               A "snapshot" of his preferences is saved.
            3. Wiki Administrator enables the application B by default.
            4. On the John Doe's preferences, application B is still disabled, because of the snapshot done on step 2.

            I don't think this situation is good. If a user did not EXPLICITLY change a setting, the default settings
            should be applied.

            For this reason, this code will only save the settings THAT ARE DIFFERENT FROM THE DEFAULT (INHERITED).

            Since this logic is totally related to the way the UI is built, I think it does not deserve its own
            component or API. In a way, it should even be internal.
        */
    List<NotificationPreference> existingPreferences = Collections.emptyList();
    if (target instanceof DocumentReference) {
        existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target);
    } else if (target instanceof WikiReference) {
        existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target);
    }
    // Instantiate a new copy of TargetableNotificationPreferenceBuilder because this component is not thread-safe.
    TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get();
    List<NotificationPreference> toSave = new ArrayList<>();
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json);
        for (Map<String, Object> item : preferences) {
            String eventType = (String) item.get("eventType");
            NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase());
            boolean enabled = (Boolean) item.get("enabled");
            targetableNotificationPreferenceBuilder.prepare();
            targetableNotificationPreferenceBuilder.setEnabled(enabled);
            targetableNotificationPreferenceBuilder.setFormat(format);
            targetableNotificationPreferenceBuilder.setProviderHint(providerHint);
            targetableNotificationPreferenceBuilder.setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType));
            targetableNotificationPreferenceBuilder.setTarget(target);
            targetableNotificationPreferenceBuilder.setCategory(category);
            TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.build();
            // This part is explained by the long comment below
            NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference);
            if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) {
                toSave.add(newPreference);
            }
        }
        notificationPreferenceManager.savePreferences(toSave);
    } catch (Exception e) {
        throw new NotificationException("Failed to save preferences for notifications given as JSON.", e);
    }
}
Also used : TargetableNotificationPreferenceBuilder(org.xwiki.notifications.preferences.TargetableNotificationPreferenceBuilder) ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) NotificationException(org.xwiki.notifications.NotificationException) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) NotificationFormat(org.xwiki.notifications.NotificationFormat) ArrayList(java.util.ArrayList) List(java.util.List) WikiReference(org.xwiki.model.reference.WikiReference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with NotificationFormat

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

the class DefaultNotificationFilterPreferenceTest method defaultNotificationFilterPreference.

@Test
public void defaultNotificationFilterPreference() throws Exception {
    String filterPreferenceName = "fp1";
    String filterName = "f1";
    boolean isEnabled = true;
    boolean isActive = true;
    NotificationFilterType filterType = NotificationFilterType.INCLUSIVE;
    Set<NotificationFormat> notificationFormats = Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL);
    Map<NotificationFilterProperty, List<String>> propertiesMap = new HashMap<>();
    propertiesMap.put(NotificationFilterProperty.APPLICATION, Arrays.asList("a1", "a2"));
    DefaultNotificationFilterPreference preference = new DefaultNotificationFilterPreference(filterPreferenceName);
    preference.setFilterName(filterName);
    preference.setEnabled(isEnabled);
    preference.setActive(isActive);
    preference.setFilterType(filterType);
    preference.setNotificationFormats(notificationFormats);
    preference.setPreferenceProperties(propertiesMap);
    assertEquals(filterPreferenceName, preference.getFilterPreferenceName());
    assertEquals(filterName, preference.getFilterName());
    assertEquals(isEnabled, preference.isEnabled());
    assertEquals(isActive, preference.isActive());
    assertEquals(filterType, preference.getFilterType());
    assertEquals(notificationFormats, preference.getFilterFormats());
    assertEquals(propertiesMap.get(NotificationFilterProperty.APPLICATION), preference.getProperties(NotificationFilterProperty.APPLICATION));
}
Also used : NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) HashMap(java.util.HashMap) NotificationFormat(org.xwiki.notifications.NotificationFormat) List(java.util.List) Test(org.junit.Test)

Aggregations

NotificationFormat (org.xwiki.notifications.NotificationFormat)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DocumentReference (org.xwiki.model.reference.DocumentReference)2 NotificationException (org.xwiki.notifications.NotificationException)2 NotificationFilterProperty (org.xwiki.notifications.filters.NotificationFilterProperty)2 NotificationFilterType (org.xwiki.notifications.filters.NotificationFilterType)2 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)2 TargetableNotificationPreference (org.xwiki.notifications.preferences.TargetableNotificationPreference)2 TargetableNotificationPreferenceBuilder (org.xwiki.notifications.preferences.TargetableNotificationPreferenceBuilder)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1