Likes and Favorites
Valid from Datafari X.X
Problematic :
Users have the possibility to store pointers to documents shown in results list.
In the following document, “favorites” corresponds to the functionality where a connected user can save a pointer to a document he found in the results list.
Technically wise, we use Apache Cassandra for saving all the favorites.
Using Cassandra, we cannot use standard relational db schemas, like the following:
Modeling with Nosql in mind:
For our nosql modeling, we took our inspiration from this article http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/ .
Applied to our problematic, we get the following :
That means we have dedicated to favorites (which is distinct from the collection dedicated to user authentication and authorization).
The request :
The aim is to get for the connected user the list of favorite documents out of the ones which are currently displayed. After the standard search query, we send only one http request which contains only the username . In return, we get all the documents in the index that are favorites.
On the browser side, we go through this documents list and pick the ones that match the currently displayed documents to identify the favorites.
The Implementation :
There are two parts: Frontend and backend. For the frontend, we added a widget that builds the inteface and makes all the Ajax Requests. For the backend, we created two classes: Like and Favorite. We created the corresponding servlets that use these two classes and send back the result.
Frontend :
The widget is called
LikesAndFavoritesWidget
. This widget inherits fromResultWidget
and is only included inSearchView.jsp
if the functionality of likes was activated by the admin.This inheritance is important because we need to execute first the code of the
ResultWidget
and then theLikesAndFavoriteWidget
.Once the
buildWidget
is executed, we execute thebeforeRequest
which will add the flnbLikes:field(nbLikes)
in the Solr query so we can retrieve as part of the results the number of likes per document.Then the
afterRequest
will fire. TheafterRequest
retrieves the documents liked and saved by the user from the server (ajax request) and stores it inwindow.globalVariableLikes
andwindow.globalVariableFavorites
. Then we parse the results retrieved from Solr and put the correct values in html by checking if the doc belong in thewindow.globalVariableLikes
andwindow.globalVariableFavorites
.Next, we put a listener to the “Like” button and a listener for saving a favorite. Depending on which button is clicked, we send a request to add/delete the favorite or the like from the server and from the lists saved in the global variable window of javascript.
Illustration 1: State Diagram of AfterRequest of LikesAndFavoritesWidget
Backend :
We use two major classes which are Like
And Favorite
. They provide the method that will get, delete and add a Favorite or a Like by querying the Cassandra database. Whenever a change is needed to likes and favorites, we use directly these classes.
Like.class
and Favorite.class
belong to the package com.francelabs.datafari.user
which also contains UserConstants.class
where we put all the constant Strings used in this package.
We also use the following servlets:
AddFavorite
: add a document to favorites,DeleteFavorite
: delete a document from favorites,GetLikesAndFavorites
: gets all the likes and the favorites of a user,,, .
AddLike
, AddFavorite
, DeleteFavorite
are very similar. Therefore we will detail only one of them, the others can be derived from it. This is the sequence diagram involving the AddLike
servlet:
Illustration 2: Sequence Diagram of AddLike for a normal Behaviour
Here is the sequence diagram that involves the LikesAndFavoritesWidget
to retrieve the likes and favorites of a user :
Illustration 3: Sequence Diagram of getting the likes and favorites from server