Skip to content
Snippets Groups Projects
Commit 24d72c31 authored by Clément Aubin's avatar Clément Aubin
Browse files

XWIKI-14575: Split the notifications module in multiple submodules

* Create module xwiki-platform-notifications-preferences
parent 756e3503
No related branches found
No related tags found
No related merge requests found
Showing
with 296 additions and 353 deletions
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications.internal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.inject.Provider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.NotificationPreference;
import org.xwiki.test.mockito.MockitoComponentMockingRule;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link DefaultModelBridge}.
*
* @since 9.7RC1
* @version $Id$
*/
public class DefaultModelBridgeTest
{
@Rule
public final MockitoComponentMockingRule<ModelBridge> mocker =
new MockitoComponentMockingRule<>(DefaultModelBridge.class);
private Provider<XWikiContext> provider;
private BaseObject fakeNotificationPreference;
private XWikiContext fakeContext;
private XWiki fakeWiki;
@Before
public void setUp() throws Exception
{
this.fakeNotificationPreference = mock(BaseObject.class);
when(fakeNotificationPreference.getStringValue("eventType")).thenReturn("fakeEventType");
when(fakeNotificationPreference.getDateValue("startDate")).thenReturn(new Date(10));
when(fakeNotificationPreference.getStringValue("format")).thenReturn("email");
when(fakeNotificationPreference.getIntValue("notificationEnabled", 0)).thenReturn(1);
this.provider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
this.fakeContext = new XWikiContext();
this.fakeWiki = mock(XWiki.class);
this.fakeContext.setWiki(this.fakeWiki);
when(this.provider.get()).thenReturn(fakeContext);
}
@Test
public void testGetNotificationsPreferences() throws Exception
{
DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "Admin");
XWikiDocument fakeDocument = mock(XWikiDocument.class);
when(this.fakeWiki.getDocument(userReference, this.fakeContext)).thenReturn(fakeDocument);
when(fakeDocument.getXObjects(any(DocumentReference.class))).thenReturn(
Collections.singletonList(this.fakeNotificationPreference));
List<NotificationPreference> preferences =
this.mocker.getComponentUnderTest().getNotificationsPreferences(userReference);
assertEquals(1, preferences.size());
assertEquals("fakeEventType", preferences.get(0).getEventType());
assertEquals(new Date(10), preferences.get(0).getStartDate());
assertEquals(NotificationFormat.EMAIL, preferences.get(0).getFormat());
assertTrue(preferences.get(0).isNotificationEnabled());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-notifications</artifactId>
<version>9.7-SNAPSHOT</version>
</parent>
<artifactId>xwiki-platform-notifications-preferences</artifactId>
<name>XWiki Platform - Notifications - Preferences - Parent POM</name>
<description>Handle notifications preferences</description>
<packaging>pom</packaging>
<modules>
<module>xwiki-platform-notifications-preferences-api</module>
<module>xwiki-platform-notifications-preferences-default</module>
</modules>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-notifications-preferences</artifactId>
<version>9.7-SNAPSHOT</version>
</parent>
<artifactId>xwiki-platform-notifications-preferences-api</artifactId>
<name>XWiki Platform - Notifications - Preferences - API</name>
<description>Handle notifications user preferences</description>
<properties>
<xwiki.jacoco.instructionRatio>0.26</xwiki.jacoco.instructionRatio>
</properties>
<dependencies>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-notifications-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-tool-test-component</artifactId>
<version>${commons.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
......@@ -17,10 +17,11 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications;
package org.xwiki.notifications.preferences;
import java.util.Date;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.stability.Unstable;
/**
......
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications.preferences;
import java.util.Date;
import java.util.List;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationException;
/**
* Provide an interface for interacting with user notification preferences.
*
* @version $Id$
* @since 9.7RC1
*/
public interface NotificationPreferenceManager
{
/**
* Get a list of registered {@link NotificationPreference} for the given user.
*
* @param user the user to use
* @return every {@link NotificationPreference} linked to this user
* @throws NotificationException if an error occurs
*/
List<NotificationPreference> getNotificationsPreferences(DocumentReference user) throws NotificationException;
/**
* Update the start date for every notification preference that the user has.
*
* @param user the user to use
* @param startDate the new start date
* @throws NotificationException if an error occurs
*/
void setStartDateForUser(DocumentReference user, Date startDate) throws NotificationException;
/**
* Save the given {@link NotificationPreference} for the given user. If such notification already exists, it will
* be updated.
*
* @param userReference the user we want to work on
* @param notificationPreferences the list of notification preference to save
* @throws NotificationException if error happens
*
* @since 9.7RC1
*/
void saveNotificationsPreferences(DocumentReference userReference,
List<NotificationPreference> notificationPreferences) throws NotificationException;
}
......@@ -17,7 +17,7 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications.internal;
package org.xwiki.notifications.preferences.internal;
import java.util.Date;
import java.util.List;
......@@ -31,10 +31,7 @@
import org.xwiki.context.ExecutionContext;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationException;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.NotificationPreference;
import com.xpn.xwiki.objects.BaseObjectReference;
import org.xwiki.notifications.preferences.NotificationPreference;
/**
* Wrap the default {@link ModelBridge} to store in the execution context the notification preferences to avoid
......@@ -50,10 +47,6 @@ public class CachedModelBridge implements ModelBridge
{
private static final String USER_NOTIFICATIONS_PREFERENCES = "userNotificationsPreferences";
private static final String USER_NOTIFICATIONS_PREFERENCES_SCOPE = "userNotificationsPreferencesScope";
private static final String UNDERSCORE = "_";
@Inject
private ModelBridge modelBridge;
......@@ -83,56 +76,6 @@ public void setStartDateForUser(DocumentReference userReference, Date startDate)
modelBridge.setStartDateForUser(userReference, startDate);
}
@Override
public List<NotificationPreferenceScope> getNotificationPreferenceScopes(DocumentReference user,
NotificationFormat format) throws NotificationException
{
final String contextEntry = USER_NOTIFICATIONS_PREFERENCES_SCOPE + UNDERSCORE + format;
ExecutionContext context = execution.getContext();
if (context.hasProperty(contextEntry)) {
return (List<NotificationPreferenceScope>) context.getProperty(contextEntry);
}
List<NotificationPreferenceScope> preferences = modelBridge.getNotificationPreferenceScopes(user, format);
context.setProperty(contextEntry, preferences);
return preferences;
}
@Override
public List<NotificationPreferenceScope> getNotificationPreferenceScopes(DocumentReference user,
NotificationFormat format, NotificationPreferenceScopeFilterType type) throws NotificationException
{
final String contextEntry = USER_NOTIFICATIONS_PREFERENCES_SCOPE + UNDERSCORE + format + UNDERSCORE
+ type.name();
ExecutionContext context = execution.getContext();
if (context.hasProperty(contextEntry)) {
return (List<NotificationPreferenceScope>) context.getProperty(contextEntry);
}
List<NotificationPreferenceScope> preferences = modelBridge.getNotificationPreferenceScopes(user, format, type);
context.setProperty(contextEntry, preferences);
return preferences;
}
@Override
public void savePropertyInHiddenDocument(BaseObjectReference objectReference, String property, Object value)
throws NotificationException
{
// Obviously there is nothing to cache
modelBridge.savePropertyInHiddenDocument(objectReference, property, value);
}
@Override
public String getDocumentURL(DocumentReference documentReference, String action, String parameters)
{
// We don’t need any cache on that
return modelBridge.getDocumentURL(documentReference, action, parameters);
}
@Override
public void saveNotificationsPreferences(DocumentReference userReference,
List<NotificationPreference> notificationPreferences) throws NotificationException
......
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications.preferences.internal;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationException;
import org.xwiki.notifications.preferences.NotificationPreference;
import org.xwiki.notifications.preferences.NotificationPreferenceManager;
/**
* This is the default implementation of {@link NotificationPreferenceManager}.
*
* @version $Id$
* @since 9.7RC1
*/
@Component
@Singleton
public class DefaultNotificationPreferenceManager implements NotificationPreferenceManager
{
@Inject
@Named("cached")
private ModelBridge cachedModelBridge;
@Override
public List<NotificationPreference> getNotificationsPreferences(DocumentReference user)
throws NotificationException
{
return cachedModelBridge.getNotificationsPreferences(user);
}
@Override
public void setStartDateForUser(DocumentReference user, Date startDate) throws NotificationException
{
cachedModelBridge.setStartDateForUser(user, startDate);
}
@Override
public void saveNotificationsPreferences(DocumentReference userReference,
List<NotificationPreference> notificationPreferences) throws NotificationException
{
cachedModelBridge.saveNotificationsPreferences(userReference, notificationPreferences);
}
}
......@@ -17,7 +17,7 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications.internal;
package org.xwiki.notifications.preferences.internal;
import java.util.Date;
import java.util.List;
......@@ -25,10 +25,7 @@
import org.xwiki.component.annotation.Role;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationException;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.NotificationPreference;
import com.xpn.xwiki.objects.BaseObjectReference;
import org.xwiki.notifications.preferences.NotificationPreference;
/**
* Internal role that make requests to the model and avoid a direct dependency to oldcore.
......@@ -59,58 +56,6 @@ List<NotificationPreference> getNotificationsPreferences(DocumentReference userR
*/
void setStartDateForUser(DocumentReference userReference, Date startDate) throws NotificationException;
/**
* Get all notification preference scope of the given user.
*
* @param user user interested in the notifications
* @param format format on which the preferences apply
* @return the list of notification preference scopes.
* @throws NotificationException if error happens
*
* @since 9.5RC1
*/
List<NotificationPreferenceScope> getNotificationPreferenceScopes(DocumentReference user, NotificationFormat format)
throws NotificationException;
/**
* Get all notification preference scope of the given user.
*
* @param user user interested in the notifications
* @param format format on which the preferences apply
* @param type the filter type of the scope we want to retrieve, see {@link NotificationPreferenceScopeFilterType}
* @return the list of notification preference scopes.
* @throws NotificationException if error happens
*
* @since 9.7RC1
*/
List<NotificationPreferenceScope> getNotificationPreferenceScopes(DocumentReference user, NotificationFormat format,
NotificationPreferenceScopeFilterType type) throws NotificationException;
/**
* Save an object's property in an hidden document.
*
* @param objectReference reference of the object to save
* @param property the name of the property to set
* @param value the value of the property to set
* @throws NotificationException if error happens
*
* @since 9.5RC1
*/
void savePropertyInHiddenDocument(BaseObjectReference objectReference, String property, Object value)
throws NotificationException;
/**
* Return the URL of the given {@link DocumentReference} for the given action.
*
* @param documentReference the reference
* @param action the request action
* @param parameters the request parameters
* @return the URL of the given reference
*
* @since 9.6RC1
*/
String getDocumentURL(DocumentReference documentReference, String action, String parameters);
/**
* Save the given {@link NotificationPreference} for the given user. If such notification already exists, it will
* be updated.
......
org.xwiki.notifications.preferences.internal.CachedModelBridge
org.xwiki.notifications.preferences.internal.DefaultNotificationPreferenceManager
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-notifications-preferences</artifactId>
<version>9.7-SNAPSHOT</version>
</parent>
<artifactId>xwiki-platform-notifications-preferences-default</artifactId>
<name>XWiki Platform - Notifications - Preferences - Default bridge</name>
<description>Default bridge to oldcore for the notifications preferences API module.</description>
<properties>
<xwiki.jacoco.instructionRatio>0.19</xwiki.jacoco.instructionRatio>
</properties>
<dependencies>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-notifications-preferences-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-oldcore</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-tool-test-component</artifactId>
<version>${commons.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -17,7 +17,7 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.notifications.internal;
package org.xwiki.notifications.preferences.internal;
import java.util.ArrayList;
import java.util.Date;
......@@ -27,16 +27,13 @@
import javax.inject.Provider;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.notifications.NotificationException;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.NotificationPreference;
import org.xwiki.notifications.preferences.NotificationPreference;
import org.xwiki.text.StringUtils;
import com.xpn.xwiki.XWiki;
......@@ -44,7 +41,6 @@
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.BaseObjectReference;
/**
* Default implementation for {@link ModelBridge}.
......@@ -73,10 +69,6 @@ public class DefaultModelBridge implements ModelBridge
"NotificationPreferenceClass", NOTIFICATION_CODE_SPACE
);
private static final DocumentReference NOTIFICATION_PREFERENCE_SCOPE_CLASS = new DocumentReference(
"NotificationPreferenceScopeClass", NOTIFICATION_CODE_SPACE
);
private static final String NOTIFICATION_START_DATE_UPDATE_COMMENT = "Update start date for the notifications.";
private static final String SET_USER_START_DATE_ERROR_MESSAGE = "Failed to set the user start date for [%s].";
......@@ -84,12 +76,6 @@ public class DefaultModelBridge implements ModelBridge
@Inject
private Provider<XWikiContext> contextProvider;
@Inject
private EntityReferenceResolver<String> entityReferenceResolver;
@Inject
private Logger logger;
@Override
public List<NotificationPreference> getNotificationsPreferences(DocumentReference userReference)
throws NotificationException
......@@ -123,7 +109,8 @@ public List<NotificationPreference> getNotificationsPreferences(DocumentReferenc
}
} catch (Exception e) {
throw new NotificationException(
String.format("Failed to get the notification preferences for the user [%s].", userReference), e);
String.format("Failed to get the notification preferences for the user [%s].",
userReference), e);
}
return preferences;
......@@ -155,91 +142,6 @@ public void setStartDateForUser(DocumentReference userReference, Date startDate)
}
}
@Override
public List<NotificationPreferenceScope> getNotificationPreferenceScopes(DocumentReference userReference,
NotificationFormat format) throws NotificationException
{
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
final DocumentReference notificationPreferencesScopeClass
= NOTIFICATION_PREFERENCE_SCOPE_CLASS.setWikiReference(userReference.getWikiReference());
List<NotificationPreferenceScope> preferences = new ArrayList<>();
try {
XWikiDocument doc = xwiki.getDocument(userReference, context);
List<BaseObject> preferencesObj = doc.getXObjects(notificationPreferencesScopeClass);
if (preferencesObj != null) {
for (BaseObject obj : preferencesObj) {
if (obj != null && isCompatibleFormat(obj.getStringValue(FORMAT_FIELD), format)) {
String scopeType = obj.getStringValue("scope");
NotificationPreferenceScopeFilterType scopeFilterType = this.extractScopeFilterType(obj);
EntityType type;
if (scopeType.equals("pageOnly")) {
type = EntityType.DOCUMENT;
} else if (scopeType.equals("pageAndChildren")) {
type = EntityType.SPACE;
} else if (scopeType.equals("wiki")) {
type = EntityType.WIKI;
} else {
logger.warn("Scope [{}] is not supported as a NotificationPreferenceScope (user [{}]).",
scopeType, userReference);
continue;
}
preferences.add(new NotificationPreferenceScope(
obj.getStringValue(EVENT_TYPE_FIELD),
entityReferenceResolver.resolve(obj.getStringValue("scopeReference"), type),
scopeFilterType
));
}
}
}
} catch (Exception e) {
throw new NotificationException(
String.format("Failed to get the notification preferences scope for the user [%s].", userReference),
e);
}
return preferences;
}
/**
* Extract the scopeFilterType parameter in the given {@link BaseObject}.
* This is done in order to eliminate too much cyclomatic complexity in
* {@link #getNotificationPreferenceScopes(DocumentReference, NotificationFormat)}.
* If no scopeFilterType is defined, the default is {@link NotificationPreferenceScopeFilterType#INCLUSIVE}.
*
* @param object the related base object
* @return the corresponding {@link NotificationPreferenceScopeFilterType}
* @since 9.7RC1
*/
private NotificationPreferenceScopeFilterType extractScopeFilterType(BaseObject object)
{
String rawScopeFilterType = object.getStringValue("scopeFilterType");
return (rawScopeFilterType != null && StringUtils.isNotBlank(rawScopeFilterType))
? NotificationPreferenceScopeFilterType.valueOf(rawScopeFilterType.toUpperCase())
: NotificationPreferenceScopeFilterType.INCLUSIVE;
}
@Override
public List<NotificationPreferenceScope> getNotificationPreferenceScopes(DocumentReference userReference,
NotificationFormat format, NotificationPreferenceScopeFilterType scopeFilterType)
throws NotificationException
{
List<NotificationPreferenceScope> preferences = new ArrayList<>();
for (NotificationPreferenceScope preference
: this.getNotificationPreferenceScopes(userReference, format)) {
if (preference.getScopeFilterType().equals(scopeFilterType)) {
preferences.add(preference);
}
}
return preferences;
}
/**
* Search for the XObject corresponding to the given notification preference in the given document. If no object
* is found, returns null.
......@@ -275,36 +177,6 @@ private BaseObject findNotificationPreference(XWikiDocument xWikiDocument,
return null;
}
private boolean isCompatibleFormat(String format, NotificationFormat expectedFormat)
{
return format != null && NotificationFormat.valueOf(format.toUpperCase()) == expectedFormat;
}
@Override
public void savePropertyInHiddenDocument(BaseObjectReference objectReference, String property, Object value)
throws NotificationException
{
try {
XWikiContext xcontext = contextProvider.get();
DocumentReference documentReference = (DocumentReference) objectReference.getParent();
XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
doc.setHidden(true);
BaseObject obj = doc.getXObject(objectReference.getXClassReference(), true, xcontext);
if (obj != null) {
obj.set(property, value, xcontext);
xcontext.getWiki().saveDocument(doc, String.format("Property [%s] set.", property), xcontext);
}
} catch (XWikiException e) {
throw new NotificationException(String.format("Failed to update the object [%s].", objectReference), e);
}
}
@Override
public String getDocumentURL(DocumentReference documentReference, String action, String parameters) {
XWikiContext context = contextProvider.get();
return context.getWiki().getExternalURL(documentReference, action, parameters, null, context);
}
@Override
public void saveNotificationsPreferences(DocumentReference userReference,
List<NotificationPreference> notificationPreferences) throws NotificationException
......
org.xwiki.notifications.preferences.internal.DefaultModelBridge
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment