Added support for fine grained notifications

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@76700 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2013-06-03 16:12:13 +00:00
parent f197269497
commit ec4dc5f623
8 changed files with 34609 additions and 66 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -6,4 +6,7 @@
<Change>Added support for people taggings (mentions)</Change>
<Change>Added fix for method getAllPortalPrivacyLevelFeeds() not recongnizing application feeds</Change>
</Changeset>
<Changeset component="org.gcube.portal.social-networking-library.1-2-0" date="2013-05-29">
<Change>Added support for fine grained notifications</Change>
</Changeset>
</ReleaseNotes>

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-library</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<name>gCube Social Networking Library</name>
<description>
The gCube Social Networking Library is the 'bridge' between your gCube Applications and the social networking facilities.

View File

@ -1,9 +1,7 @@
package org.gcube.portal.databook.server;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
@ -187,7 +185,7 @@ public class CassandraClusterConnection {
ColumnFamilyDefinition cfDefUserLikedFeeds = getDynamicCFDef(DBCassandraAstyanaxImpl.USER_LIKED_FEEDS);
ColumnFamilyDefinition cfDefUserNotifications = getDynamicCFDef(DBCassandraAstyanaxImpl.USER_NOTIFICATIONS);
ColumnFamilyDefinition cfDefUserMessagesNotifications = getDynamicCFDef(DBCassandraAstyanaxImpl.USER_MESSAGES_NOTIFICATIONS);
ColumnFamilyDefinition cfDefUserNotificationsSettings = getDynamicCFDef(DBCassandraAstyanaxImpl.USER_NOTIFICATIONS_SETTINGS);
ColumnFamilyDefinition cfDefUserNotificationsPreferences = getDynamicCFDef(DBCassandraAstyanaxImpl.USER_NOTIFICATIONS_PREFERENCES);
ksDef.setName(keyspaceName)
@ -204,7 +202,7 @@ public class CassandraClusterConnection {
.addColumnFamily(cfDefUserTimeline)
.addColumnFamily(cfDefUserNotifications)
.addColumnFamily(cfDefUserMessagesNotifications)
.addColumnFamily(cfDefUserNotificationsSettings)
.addColumnFamily(cfDefUserNotificationsPreferences)
.addColumnFamily(cfDefUserLikedFeeds);
cluster.addKeyspace(ksDef);

View File

@ -4,7 +4,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.NullArgumentException;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
@ -65,7 +67,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public static final String USER_LIKED_FEEDS = "USERLikes";
public static final String USER_NOTIFICATIONS = "USERNotifications"; // regular user notifications timeline
public static final String USER_MESSAGES_NOTIFICATIONS = "USERMessagesNotifications"; // user messages notifications timeline
public static final String USER_NOTIFICATIONS_SETTINGS = "USERNotificationsSettings"; // settings for notifications
public static final String USER_NOTIFICATIONS_PREFERENCES = "USERNotificationsPreferences"; // preferences for notifications
private static ColumnFamily<String, String> cf_Connections = new ColumnFamily<String, String>(
CONNECTIONS, // Column Family Name
@ -118,11 +120,13 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
USER_MESSAGES_NOTIFICATIONS, // Column Family Name
StringSerializer.get(), // Key Serializer
StringSerializer.get()); // Column Serializer
private static ColumnFamily<String, String> cf_UserNotificationsSettings = new ColumnFamily<String, String>(
USER_NOTIFICATIONS_SETTINGS, // Column Family Name
private static ColumnFamily<String, String> cf_UserNotificationsPreferences = new ColumnFamily<String, String>(
USER_NOTIFICATIONS_PREFERENCES, // Column Family Name
StringSerializer.get(), // Key Serializer
StringSerializer.get()); // Column Serializer
/**
* connection instance
*/
@ -907,29 +911,45 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
* @throws NotificationChannelTypeNotFoundException
*/
@Override
public List<NotificationChannelType> getUserNotificationChannels(String userid) throws NotificationChannelTypeNotFoundException {
public List<NotificationChannelType> getUserNotificationChannels(String userid, NotificationType notificationType) throws NotificationChannelTypeNotFoundException {
_log.trace("Asking for Notification preference of " + userid);
List<NotificationChannelType> toReturn = new ArrayList<NotificationChannelType>();
OperationResult<Rows<String, String>> result = null;
try {
result = conn.getKeyspace().prepareQuery(cf_UserNotificationsSettings)
result = conn.getKeyspace().prepareQuery(cf_UserNotificationsPreferences)
.getKeySlice(userid)
.execute();
} catch (ConnectionException e) {
e.printStackTrace();
}
//if there are no settings create them and put all of them at true
//if there are no settings for this user create an entry and put all of them at true
if (result.getResult().getRowByIndex(0).getColumns().size() == 0) {
_log.info("Userid " + userid + " settings not found, initiating its settings...");
for (int i = 0; i < NotificationChannelType.values().length; i++) {
setUserNotificationChannel(userid, NotificationChannelType.values()[i], true);
toReturn.add(NotificationChannelType.values()[i]);
_log.info("Userid " + userid + " settings not found, initiating its preferences...");
HashMap<NotificationType, NotificationChannelType[]> toCreate = new HashMap<NotificationType, NotificationChannelType[]>();
for (int i = 0; i < NotificationType.values().length; i++) {
toCreate.put(NotificationType.values()[i], NotificationChannelType.values()); //create a map with all notification enabled
}
setUserNotificationChannels(userid, toCreate); //commit the map
for (int i = 0; i < NotificationChannelType.values().length; i++)
toReturn.add(NotificationChannelType.values()[i]); //return all the channels ON
return toReturn;
}
else {
_log.trace("Notification preferences EXIST for " + userid + " - " + result.getResult().getRowByIndex(0).getColumns().size());
for (Row<String, String> row : result.getResult())
for (Column<String> column : row.getColumns())
if (column.getStringValue().compareTo("true") == 0) toReturn.add(getChannelType(column.getName()));
for (Column<String> column : row.getColumns()) {
if (column.getName().compareTo(notificationType.toString()) == 0) {
if (column.getStringValue() != null && column.getStringValue().compareTo("") != 0) {
String[] channels = column.getStringValue().split(",");
for (int i = 0; i < channels.length; i++) {
toReturn.add(getChannelType(channels[i]));
}
}
else return toReturn; //is empty here
}
}
}
return toReturn;
}
@ -937,38 +957,25 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
* {@inheritDoc}
*/
@Override
public boolean setUserNotificationChannel(String userid, NotificationChannelType channel, boolean enabled) {
public boolean setUserNotificationChannels(String userid, Map<NotificationType, NotificationChannelType[]> enabledChannels) {
MutationBatch m = conn.getKeyspace().prepareMutationBatch();
//check if the value exists and if so update it
OperationResult<Rows<String, String>> result = null;
try {
result = conn.getKeyspace().prepareQuery(cf_UserNotificationsSettings)
.getKeySlice(userid)
.execute();
} catch (ConnectionException e) {
e.printStackTrace();
for (NotificationType nType : enabledChannels.keySet()) {
String valueToInsert = "";
int channelsNo = (enabledChannels.get(nType) != null) ? enabledChannels.get(nType).length : 0;
for (int i = 0; i < channelsNo; i++) {
valueToInsert += enabledChannels.get(nType)[i];
if (i < channelsNo-1)
valueToInsert += ",";
}
m.withRow(cf_UserNotificationsPreferences, userid).putColumn(nType.toString(), valueToInsert, null);
}
boolean settingExists = false;
//if the setting exists and is different from enabled value update it
for (Row<String, String> row : result.getResult()) {
for (Column<String> column : row.getColumns())
if (column.getName().compareTo(channel.toString()) == 0 && (column.getBooleanValue() != enabled) ) {
m.withRow(cf_UserNotificationsSettings, userid).putColumn(channel.toString(), ""+enabled, null);
settingExists = true;
break;
}
}
if (! settingExists)
m.withRow(cf_UserNotificationsSettings, userid).putColumn(channel.toString(), ""+enabled, null);
boolean overAllresult = execute(m);
if (overAllresult)
_log.trace("Set Notification Channel for " + userid + " to " + enabled + " OK");
_log.trace("Set Notification Map for " + userid + " OK");
else
_log.error("Set Notification Channel for " + userid + " to " + enabled + " KO");
_log.trace("Set Notification Map for " + userid + " FAILED");
return overAllresult;
}
/*

View File

@ -4,7 +4,9 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
@ -25,6 +27,12 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.common.collect.ImmutableMap;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.ddl.ColumnFamilyDefinition;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.serializers.StringSerializer;
public class DatabookCassandraTest {
private static DatabookStore store;
@ -39,6 +47,28 @@ public class DatabookCassandraTest {
System.out.println("End");
}
/**
* use exclusively to add a new CF to a keyspace
*/
@Test
public void addColumnFamily() {
// ColumnFamily<String, String> cf_UserNotificationsPreferences = new ColumnFamily<String, String>(
// DBCassandraAstyanaxImpl.USER_NOTIFICATIONS_PREFERENCES, // Column Family Name
// StringSerializer.get(), // Key Serializer
// StringSerializer.get()); // Column Serializer
//
// try {
// new CassandraClusterConnection(false).getKeyspace().createColumnFamily(cf_UserNotificationsPreferences, ImmutableMap.<String, Object>builder()
// .put("default_validation_class", "UTF8Type")
// .put("key_validation_class", "UTF8Type")
// .put("comparator_type", "UTF8Type")
// .build());
// } catch (ConnectionException e) {
// e.printStackTrace();
// }
}
@Test
public void testFriendships() {
assertTrue(store.requestFriendship("massimiliano.assante", "leonardo.candela"));
@ -71,17 +101,38 @@ public class DatabookCassandraTest {
e.printStackTrace();
}
}
@Test
public void testNotificationSettings() {
public void testNotificationPreferences() {
try {
for (NotificationChannelType channel : store.getUserNotificationChannels("luca.frosini")) {
for (NotificationChannelType channel : store.getUserNotificationChannels("luca.frosini", NotificationType.COMMENT)) {
System.out.println(channel);
}
} catch (NotificationChannelTypeNotFoundException e) {
e.printStackTrace();
};
// store.setUserNotificationChannel("luca.frosini", NotificationChannelType.EMAIL, true);
HashMap<NotificationType, NotificationChannelType[]> toCreate = new HashMap<NotificationType, NotificationChannelType[]>();
for (int i = 0; i < NotificationType.values().length; i++) {
if (i == 0)
toCreate.put(NotificationType.values()[i], null);
else if (i ==3) {
NotificationChannelType[] nots = {NotificationChannelType.EMAIL};
toCreate.put(NotificationType.values()[i], nots);
}
else
toCreate.put(NotificationType.values()[i], NotificationChannelType.values());
}
store.setUserNotificationChannels("luca.frosini", toCreate);
try {
for (NotificationChannelType channel : store.getUserNotificationChannels("luca.frosini", NotificationType.WP_FOLDER_RENAMED)) { //i == 3
System.out.println(channel);
}
} catch (NotificationChannelTypeNotFoundException e) {
e.printStackTrace();
};
}
@Test
@ -98,7 +149,7 @@ public class DatabookCassandraTest {
"leonardo.candela", "Leonardo Candela",
"thumburl");
assertTrue(store.saveNotification(not));
not = new Notification(
UUID.randomUUID().toString(),
NotificationType.MESSAGE,
@ -113,7 +164,7 @@ public class DatabookCassandraTest {
assertTrue(store.saveNotification(not));
System.out.println("Writing one Notification " + not);
}
@Test
public void testNotifications() {
Notification not = null;
@ -138,17 +189,17 @@ public class DatabookCassandraTest {
Notification rNot= null;
try {
//read
rNot = store.readNotification(not.getKey().toString());
assertNotNull(rNot);
System.out.println("Reading one Notification " + rNot.getKey() + ": " + rNot.getDescription() + " Type: " + rNot.getType());
//set Read
assertTrue(store.setNotificationRead(rNot.getKey().toString()));
System.out.println("Notification " + rNot.getKey() + " of Type: " + rNot.getType() + " was set to READ");
not = new Notification(UUID.randomUUID().toString(), NotificationType.LIKE,
"leonardo.candela", "FEEDID", new Date(), "uri", "This is notification of a Liked Leo feed by Massi", false, "massimiliano.assante", "Massimiliano Assante", "thumburl");
assertTrue(store.saveNotification(not));
@ -160,19 +211,19 @@ public class DatabookCassandraTest {
}
rNot = store.readNotification(not.getKey().toString());
System.out.println("Reading another Notification " + rNot.getKey() + " of Type: " + rNot.getType() + " Read:? " + rNot.isRead());
// //set Read
// assertTrue(store.setNotificationRead(rNot.getKey().toString()));
// System.out.println("Notification " + rNot.getKey() + " of Type: " + rNot.getType() + " was set to READ subject was this: " + rNot.getSubjectid());
//
// //set Read
// assertTrue(store.setNotificationRead(rNot.getKey().toString()));
// System.out.println("Notification " + rNot.getKey() + " of Type: " + rNot.getType() + " was set to READ subject was this: " + rNot.getSubjectid());
//
Random randomGenerator = new Random();
System.out.println("leonardo.candela Notifications: ");
List<Notification> recentNots = store.getAllNotificationByUser("leonardo.candela", randomGenerator.nextInt(50));
assertNotNull(recentNots);
for (Notification notif :recentNots)
System.out.println(notif);
System.out.println("massimiliano.assante Notifications: ");
recentNots = store.getUnreadNotificationsByUser("massimiliano.assante");
assertNotNull(recentNots);

View File

@ -1,12 +1,14 @@
package org.gcube.portal.databook.server;
import java.util.List;
import java.util.Map;
import org.gcube.portal.databook.shared.Comment;
import org.gcube.portal.databook.shared.Feed;
import org.gcube.portal.databook.shared.Like;
import org.gcube.portal.databook.shared.Notification;
import org.gcube.portal.databook.shared.NotificationChannelType;
import org.gcube.portal.databook.shared.NotificationType;
import org.gcube.portal.databook.shared.ex.ColumnNameNotFoundException;
import org.gcube.portal.databook.shared.ex.CommentIDNotFoundException;
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException;
@ -207,19 +209,20 @@ public interface DatabookStore {
boolean checkUnreadMessagesNotifications(String userid) throws NotificationIDNotFoundException, NotificationTypeNotFoundException, ColumnNameNotFoundException;
/**
* return the channels a user chose for being notified
* return the channels a user chose for being notified for a given notification type
* @param userid user identifier
* @param notificationType the type of the notification
* @return a list of <class>NotificationChannelType</class> that represents the channels this user wants to be notified
*/
List<NotificationChannelType> getUserNotificationChannels(String userid) throws NotificationChannelTypeNotFoundException;
List<NotificationChannelType> getUserNotificationChannels(String userid, NotificationType notificationType) throws NotificationChannelTypeNotFoundException;
/**
* enable or disable the channels to be used for notifying the user
* @param userid user identifier
* @param channel the type of the channel
* @param enabled is you want it to be enabled or not
* @param notificationType the type of the notification
* @param enabledChannels a map of the channels to which reach the user per notification, empty array or null values to set the key notification type off
* @return true if everything was fine
*/
boolean setUserNotificationChannel(String userid, NotificationChannelType channel, boolean enabled);
boolean setUserNotificationChannels(String userid, Map<NotificationType, NotificationChannelType[]> enabledChannels);
/**