The portlet can now be deployed in a profile page

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/user-statistics@128552 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-05-10 16:40:55 +00:00
parent 10feabeefb
commit e589b6079f
9 changed files with 394 additions and 91 deletions

View File

@ -4,6 +4,9 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/> <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="callout-tour-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/callout-tour/callout-tour">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/user-statistics/target/user-statistics-portlet-0.0.1-SNAPSHOT/WEB-INF/classes"/> <property name="java-output-path" value="/user-statistics/target/user-statistics-portlet-0.0.1-SNAPSHOT/WEB-INF/classes"/>
<property name="context-root" value="user-statistics"/> <property name="context-root" value="user-statistics"/>
</wb-module> </wb-module>

View File

@ -72,6 +72,12 @@
<scope>provided</scope> <scope>provided</scope>
<version>2.7.0</version> <version>2.7.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<scope>provided</scope>
<version>2.7.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.gcube.portlets.user</groupId> <groupId>org.gcube.portlets.user</groupId>
<artifactId>gcube-widgets</artifactId> <artifactId>gcube-widgets</artifactId>

View File

@ -5,6 +5,8 @@ import net.eliasbalasis.tibcopagebus4gwt.client.PageBusAdapterException;
import net.eliasbalasis.tibcopagebus4gwt.client.PageBusEvent; import net.eliasbalasis.tibcopagebus4gwt.client.PageBusEvent;
import net.eliasbalasis.tibcopagebus4gwt.client.PageBusListener; import net.eliasbalasis.tibcopagebus4gwt.client.PageBusListener;
import org.gcube.portal.databook.client.GCubeSocialNetworking;
import org.gcube.portal.databook.client.util.Encoder;
import org.gcube.portlet.user.userstatisticsportlet.client.resources.Images; import org.gcube.portlet.user.userstatisticsportlet.client.resources.Images;
import org.gcube.portlet.user.userstatisticsportlet.client.ui.CommentsAndLikesWidget; import org.gcube.portlet.user.userstatisticsportlet.client.ui.CommentsAndLikesWidget;
import org.gcube.portlet.user.userstatisticsportlet.client.ui.StatisticWidget; import org.gcube.portlet.user.userstatisticsportlet.client.ui.StatisticWidget;
@ -15,10 +17,13 @@ import org.gcube.portlets.widgets.widgettour.client.extendedclasses.GCubeTour;
import com.ait.toolkit.hopscotch.client.Placement; import com.ait.toolkit.hopscotch.client.Placement;
import com.ait.toolkit.hopscotch.client.TourStep; import com.ait.toolkit.hopscotch.client.TourStep;
import com.github.gwtbootstrap.client.ui.Button; import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.CheckBox;
import com.github.gwtbootstrap.client.ui.constants.ButtonType; import com.github.gwtbootstrap.client.ui.constants.ButtonType;
import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window; import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.AsyncCallback;
@ -64,7 +69,7 @@ public class StatisticsPanel extends Composite {
*/ */
private final static String POSTS_LABEL = "Posts"; private final static String POSTS_LABEL = "Posts";
private final static String STORAGE_LABEL = "Total Space Used"; private final static String STORAGE_LABEL = "Total Space Used";
private final static String LIKES_COMMENTS_LABEL = "You got"; private final static String LIKES_COMMENTS_LABEL = "Got";
private final static String PROFILE_STRENGTH_LABEL = "Profile Strength"; private final static String PROFILE_STRENGTH_LABEL = "Profile Strength";
/** /**
@ -146,8 +151,12 @@ public class StatisticsPanel extends Composite {
//show loader, waiting for the answer coming by the server //show loader, waiting for the answer coming by the server
showLoader(); showLoader();
// does the profile belong to someone? (when we are on a profile page of someone other, we need
// to retrieve the statistics of that person)
final String userid = getUserToShowId();
// request user's information // request user's information
statisticsService.getUserSettings(new AsyncCallback<UserInformation>() { statisticsService.getUserSettings(userid, new AsyncCallback<UserInformation>() {
@Override @Override
public void onFailure(Throwable arg0) { public void onFailure(Throwable arg0) {
@ -157,11 +166,24 @@ public class StatisticsPanel extends Composite {
} }
@Override @Override
public void onSuccess(UserInformation information) { public void onSuccess(final UserInformation information) {
// remove loading image // remove loading image
mainPanel.remove(loadingImage); mainPanel.remove(loadingImage);
// first of all check if the statistics can be shown to other people
if(userid != null && !userid.equals(information.getAslSessionUsername()) && !information.isProfileShowable()){
mainPanel.add(new HTML("Sorry but the user set his Statistics to private."));
return;
}
// is a user profile page?
final boolean isProfilePage =
Window.Location.getHref().endsWith("profile")
|| Window.Location.getHref().contains("profile?");
// check which kind of information we have to show // check which kind of information we have to show
isRoot = information.isRoot(); isRoot = information.isRoot();
@ -211,7 +233,7 @@ public class StatisticsPanel extends Composite {
tour.startTour(); tour.startTour();
} }
if(!isRoot){ if(!isRoot && !isProfilePage){
// add the border to the panel and the VRE name (check for VRE name lenght) // add the border to the panel and the VRE name (check for VRE name lenght)
mainPanel.addStyleName("user-stats-frame-border"); mainPanel.addStyleName("user-stats-frame-border");
@ -234,8 +256,14 @@ public class StatisticsPanel extends Composite {
// check if the user has an avatar // check if the user has an avatar
if(information.getUrlAvatar() == null) if(information.getUrlAvatar() == null)
userImage.setResource(image.avatarDefaultImage()); userImage.setResource(image.avatarDefaultImage());
else else{
userImage.setUrl(information.getUrlAvatar()); userImage.setUrl(information.getUrlAvatar());
if(userid != null)
userImage.setTitle("User's avatar");
else
userImage.setTitle("Your current avatar");
}
// set the style for the user image // set the style for the user image
userImage.setStyleName("user-image"); userImage.setStyleName("user-image");
@ -247,10 +275,10 @@ public class StatisticsPanel extends Composite {
final StatisticWidget feeds = new StatisticWidget(isRoot); final StatisticWidget feeds = new StatisticWidget(isRoot);
feeds.setHeader(POSTS_LABEL); feeds.setHeader(POSTS_LABEL);
if(isRoot) if(isRoot || isProfilePage)
feeds.setToolTip("Your posts during the last year."); feeds.setToolTip("Posts during the last year.");
else else
feeds.setToolTip("Your posts during the last year in this VRE."); feeds.setToolTip("Posts during the last year in this VRE.");
// add loading image that will be replaced by the incoming values // add loading image that will be replaced by the incoming values
@ -265,10 +293,10 @@ public class StatisticsPanel extends Composite {
final StatisticWidget likesAndComments = new StatisticWidget(isRoot); final StatisticWidget likesAndComments = new StatisticWidget(isRoot);
likesAndComments.setHeader(LIKES_COMMENTS_LABEL); likesAndComments.setHeader(LIKES_COMMENTS_LABEL);
if(isRoot) if(isRoot || isProfilePage)
likesAndComments.setToolTip("Likes and post replies you got during the last year."); likesAndComments.setToolTip("Likes and post replies got during the last year.");
else else
likesAndComments.setToolTip("Likes and post replies you got during the last year in this VRE."); likesAndComments.setToolTip("Likes and post replies got during the last year in this VRE.");
// add loading image that will be replaced by the incoming values // add loading image that will be replaced by the incoming values
Image commentsLikesLoader = new Image(imagePath); Image commentsLikesLoader = new Image(imagePath);
@ -282,7 +310,7 @@ public class StatisticsPanel extends Composite {
final StatisticWidget storage = new StatisticWidget(isRoot); final StatisticWidget storage = new StatisticWidget(isRoot);
final StatisticWidget profileStrength = new StatisticWidget(isRoot); final StatisticWidget profileStrength = new StatisticWidget(isRoot);
if(isRoot){ if(isRoot || isProfilePage){
storage.setHeader(STORAGE_LABEL); storage.setHeader(STORAGE_LABEL);
storage.setToolTip("Total amount of space used in the infrastructure."); storage.setToolTip("Total amount of space used in the infrastructure.");
@ -295,7 +323,7 @@ public class StatisticsPanel extends Composite {
mainPanel.add(storage); mainPanel.add(storage);
profileStrength.setHeader(PROFILE_STRENGTH_LABEL); profileStrength.setHeader(PROFILE_STRENGTH_LABEL);
profileStrength.setToolTip("Your profile strength."); profileStrength.setToolTip("Profile strength.");
// add loading image that will be replaced by the incoming values // add loading image that will be replaced by the incoming values
Image profileStrengthLoader = new Image(imagePath); Image profileStrengthLoader = new Image(imagePath);
@ -306,7 +334,7 @@ public class StatisticsPanel extends Composite {
mainPanel.add(profileStrength); mainPanel.add(profileStrength);
// async requests that must be performed in root context // async requests that must be performed in root context
statisticsService.getTotalSpaceInUse(new AsyncCallback<String>() { statisticsService.getTotalSpaceInUse(userid, new AsyncCallback<String>() {
@Override @Override
public void onFailure(Throwable arg0) { public void onFailure(Throwable arg0) {
@ -331,7 +359,7 @@ public class StatisticsPanel extends Composite {
} }
}); });
statisticsService.getProfileStrength(new AsyncCallback<Integer>() { statisticsService.getProfileStrength(userid, new AsyncCallback<Integer>() {
@Override @Override
public void onFailure(Throwable arg0) { public void onFailure(Throwable arg0) {
@ -358,7 +386,7 @@ public class StatisticsPanel extends Composite {
profileStrength.appendToPanel(profileStrengthLabel); profileStrength.appendToPanel(profileStrengthLabel);
// in case too low information within the user profile // in case too low information within the user profile
if(profileStrengthInt < profileImproveThreshold){ if(profileStrengthInt < profileImproveThreshold && information.isOwner()){
final Button improveProfileButton = new Button(IMPROVE_BUTTON_LABEL); final Button improveProfileButton = new Button(IMPROVE_BUTTON_LABEL);
improveProfileButton.setType(ButtonType.WARNING); improveProfileButton.setType(ButtonType.WARNING);
@ -387,7 +415,7 @@ public class StatisticsPanel extends Composite {
} }
// retrieve othe information about number of feeds and post replies/likes // retrieve othe information about number of feeds and post replies/likes
statisticsService.getPostsStats(new AsyncCallback<PostsStatsBean>(){ statisticsService.getPostsStats(userid, new AsyncCallback<PostsStatsBean>(){
public void onFailure(Throwable arg0) { public void onFailure(Throwable arg0) {
@ -410,10 +438,10 @@ public class StatisticsPanel extends Composite {
// update feeds number // update feeds number
feeds.clearPanelValues(); feeds.clearPanelValues();
numberOfFeedsLabel = new Label(formattedNumbers(postsBean.getFeedsNumber())); numberOfFeedsLabel = new Label(formattedNumbers(postsBean.getFeedsNumber()));
if(isRoot) if(isRoot || isProfilePage)
numberOfFeedsLabel.setTitle("Your posts during the last year (" + postsBean.getFeedsNumber() + ")."); numberOfFeedsLabel.setTitle("Posts during the last year (" + postsBean.getFeedsNumber() + ").");
else else
numberOfFeedsLabel.setTitle("Your posts during the last year in this VRE (" + postsBean.getFeedsNumber() + ")."); numberOfFeedsLabel.setTitle("Posts during the last year in this VRE (" + postsBean.getFeedsNumber() + ").");
numberOfWrittenFeeds = postsBean.getFeedsNumber(); numberOfWrittenFeeds = postsBean.getFeedsNumber();
numberOfFeedsLabel.setStyleName("statistic-value"); numberOfFeedsLabel.setStyleName("statistic-value");
@ -424,34 +452,34 @@ public class StatisticsPanel extends Composite {
content = new CommentsAndLikesWidget(); content = new CommentsAndLikesWidget();
String urlLikesIcon = GWT.getModuleBaseURL() + "../images/star_blue.png"; String urlLikesIcon = GWT.getModuleBaseURL() + "../images/star_blue.png";
if(isRoot) if(isRoot || isProfilePage)
content.setLikes( content.setLikes(
urlLikesIcon, urlLikesIcon,
formattedNumbers(postsBean.getLikesReceived()), formattedNumbers(postsBean.getLikesReceived()),
"Likes you got during the last year.", "Likes got during the last year.",
"Likes you got during the last year (" + postsBean.getLikesReceived() + ")."); "Likes got during the last year (" + postsBean.getLikesReceived() + ").");
else else
content.setLikes( content.setLikes(
urlLikesIcon, urlLikesIcon,
formattedNumbers(postsBean.getLikesReceived()), formattedNumbers(postsBean.getLikesReceived()),
"Likes you got during the last year in this VRE", "Likes got during the last year in this VRE",
"Likes you got during the last year in this VRE (" + postsBean.getLikesReceived() + ")."); "Likes got during the last year in this VRE (" + postsBean.getLikesReceived() + ").");
numberOfLikesGot = postsBean.getLikesReceived(); numberOfLikesGot = postsBean.getLikesReceived();
String urlCommentsIcon = GWT.getModuleBaseURL() + "../images/comment_edit.png"; String urlCommentsIcon = GWT.getModuleBaseURL() + "../images/comment_edit.png";
if(isRoot) if(isRoot || isProfilePage)
content.setComments( content.setComments(
urlCommentsIcon, urlCommentsIcon,
formattedNumbers(postsBean.getCommentsReceived()), formattedNumbers(postsBean.getCommentsReceived()),
"Post replies you got during the last year.", "Post replies got during the last year.",
"Post replies you got during the last year (" + postsBean.getCommentsReceived() + ")."); "Post replies got during the last year (" + postsBean.getCommentsReceived() + ").");
else else
content.setComments( content.setComments(
urlCommentsIcon, urlCommentsIcon,
formattedNumbers(postsBean.getCommentsReceived()), formattedNumbers(postsBean.getCommentsReceived()),
"Post replies you got during the last year in this VRE.", "Post replies got during the last year in this VRE.",
"Post replies you got during the last year in this VRE (" + postsBean.getCommentsReceived() + ")."); "Post replies got during the last year in this VRE (" + postsBean.getCommentsReceived() + ").");
numberOfCommentsGot = postsBean.getCommentsReceived(); numberOfCommentsGot = postsBean.getCommentsReceived();
likesAndComments.appendToPanel(content); likesAndComments.appendToPanel(content);
@ -459,9 +487,41 @@ public class StatisticsPanel extends Composite {
}); });
// check if we need to show the checkbox to allow the user's profile owner to edit privacy options
if(information.isOwner() && (isProfilePage)){
// add a checkbox with the settable privacy option
CheckBox privacyOption = new CheckBox("Show my statistics to VRE Members");
privacyOption.setTitle("Show Statistics to members viewing your profile");
privacyOption.setValue(information.isProfileShowable());
privacyOption.addStyleName("privacy-checkbox-statistics-style");
privacyOption.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
// set this privacy option
statisticsService.setShowMyOwnStatisticsToOtherPeople(event.getValue(), new AsyncCallback<Void>(){
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(Void result) {
}
});
} }
}); });
mainPanel.add(privacyOption);
}
}
});
} }
/** /**
@ -754,4 +814,16 @@ public class StatisticsPanel extends Composite {
return formattedString; return formattedString;
} }
/**
* decode the userid from the location param
* @return the decoded (base64) userid
*/
public static String getUserToShowId() {
String encodedOid = Encoder.encode(GCubeSocialNetworking.USER_PROFILE_OID);
if (Window.Location.getParameter(encodedOid) == null)
return null;
String encodedUserId = Window.Location.getParameter(encodedOid);
return Encoder.decode(encodedUserId);
}
} }

View File

@ -17,21 +17,27 @@ public interface UserStatisticsService extends RemoteService {
* get other user's information * get other user's information
* @return an object with user's information (actual vre, his name, url of his image) * @return an object with user's information (actual vre, his name, url of his image)
*/ */
UserInformation getUserSettings(); UserInformation getUserSettings(String userid);
/** /**
* get information relatives to feeds(posts), comment replies and likes received * get information relatives to feeds(posts), comment replies and likes received
* @return an object with user's statistics (number of feeds(posts), comment replies, likes received) * @return an object with user's statistics (number of feeds(posts), comment replies, likes received)
*/ */
PostsStatsBean getPostsStats(); PostsStatsBean getPostsStats(String userid);
/** /**
* get the total space in use on the workspace * get the total space in use on the workspace
*/ */
String getTotalSpaceInUse(); String getTotalSpaceInUse(String userid);
/** /**
* get profile strenght * get profile strenght
*/ */
int getProfileStrength(); int getProfileStrength(String userid);
/**
* when the portlet is deployed on the user profile page, allows him to decide to show his statistics
* to other members or not
*/
void setShowMyOwnStatisticsToOtherPeople(boolean show);
} }

View File

@ -12,12 +12,14 @@ import com.google.gwt.user.client.rpc.AsyncCallback;
*/ */
public interface UserStatisticsServiceAsync { public interface UserStatisticsServiceAsync {
void getUserSettings(AsyncCallback<UserInformation> callback); void getUserSettings(String userid, AsyncCallback<UserInformation> callback);
void getPostsStats(AsyncCallback<PostsStatsBean> callback); void getPostsStats(String userid, AsyncCallback<PostsStatsBean> callback);
void getTotalSpaceInUse(AsyncCallback<String> callback); void getTotalSpaceInUse(String userid, AsyncCallback<String> callback);
void getProfileStrength(AsyncCallback<Integer> callback); void getProfileStrength(String userid, AsyncCallback<Integer> callback);
void setShowMyOwnStatisticsToOtherPeople(boolean show,
AsyncCallback<Void> callback);
} }

View File

@ -1,7 +1,8 @@
package org.gcube.portlet.user.userstatisticsportlet.server; package org.gcube.portlet.user.userstatisticsportlet.server;
import java.io.Serializable;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.Date; import java.util.Calendar;
import java.util.List; import java.util.List;
import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.ASLSession;
@ -17,15 +18,25 @@ import org.gcube.portlet.user.userstatisticsportlet.client.UserStatisticsService
import org.gcube.portlet.user.userstatisticsportlet.shared.PostsStatsBean; import org.gcube.portlet.user.userstatisticsportlet.shared.PostsStatsBean;
import org.gcube.portlet.user.userstatisticsportlet.shared.UserInformation; import org.gcube.portlet.user.userstatisticsportlet.shared.UserInformation;
import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.util.ManagementUtils;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.liferay.portal.model.Contact; import com.liferay.portal.model.Contact;
import com.liferay.portal.model.User; import com.liferay.portal.model.User;
import com.liferay.portal.model.Website; import com.liferay.portal.model.Website;
import com.liferay.portal.security.auth.PrincipalThreadLocal;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
import com.liferay.portal.security.permission.PermissionThreadLocal;
import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.WebsiteLocalServiceUtil; import com.liferay.portal.service.WebsiteLocalServiceUtil;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;
/** /**
* The server side implementation of the RPC service. * The server side implementation of the RPC service.
@ -37,7 +48,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
private static final org.slf4j.Logger _log = LoggerFactory.getLogger(UserStatisticsServiceImpl.class); private static final org.slf4j.Logger _log = LoggerFactory.getLogger(UserStatisticsServiceImpl.class);
//dev user //dev user
public static final String userid = "test.user"; public static final String defaultUserId = "test.user";
//dev vre //dev vre
private static final String vreID = "/gcube/devsec/devVRE"; private static final String vreID = "/gcube/devsec/devVRE";
@ -45,12 +56,18 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
// Cassandra connection // Cassandra connection
private DatabookStore store; private DatabookStore store;
// custom field' name to remember action to take for the portlet deployed in the user profile
private static final String CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY = "show_user_statistics_other_people";
@Override @Override
public void init() { public void init() {
// get connection to Cassandra // get connection to Cassandra
_log.debug("Getting connection to Cassandra.."); _log.debug("Getting connection to Cassandra..");
store = new DBCassandraAstyanaxImpl(); store = new DBCassandraAstyanaxImpl();
// add statistics option for profile pages and set to true
createUserCustomField(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY, true);
} }
@Override @Override
@ -71,7 +88,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
if (user == null) { if (user == null) {
_log.warn("USER IS NULL setting " + userid + " and Running OUTSIDE PORTAL"); _log.warn("USER IS NULL setting " + defaultUserId + " and Running OUTSIDE PORTAL");
user = getDevelopmentUser(); user = getDevelopmentUser();
SessionManager.getInstance().getASLSession(sessionID, user).setScope(vreID); SessionManager.getInstance().getASLSession(sessionID, user).setScope(vreID);
@ -118,39 +135,45 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
* @return . * @return .
*/ */
public String getDevelopmentUser() { public String getDevelopmentUser() {
String user = userid; String user = defaultUserId;
// user = "costantino.perciante"; // user = "costantino.perciante";
return user; return user;
} }
@Override @Override
public String getTotalSpaceInUse() { public String getTotalSpaceInUse(String userid) {
String storageInUse = null; String storageInUse = null;
// get the session // get the session
ASLSession session = getASLSession(); ASLSession session = getASLSession();
//username // username in the session
String userName = session.getUsername(); String userName = session.getUsername();
// retrieve statistics of...
String statisticsOfUsername = userName;
if(userid != null && !userid.equals(userName))
statisticsOfUsername = userid;
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens //this check just return nothing if that happens
if (userName.compareTo(userid) == 0) { if (userName.compareTo(defaultUserId) == 0) {
_log.debug("Found " + userName + " returning nothing"); _log.debug("Found " + userName + " returning nothing");
return null; return null;
}else{ }else{
_log.debug("Getting " + userName + " amount of workspace in use."); _log.debug("Getting " + statisticsOfUsername + " amount of workspace in use.");
try{ try{
long init = System.currentTimeMillis(); long init = System.currentTimeMillis();
Workspace workspace = HomeLibrary.getUserWorkspace(userName); Workspace workspace = HomeLibrary.getUserWorkspace(statisticsOfUsername);
long storage = workspace.getDiskUsage(); long storage = workspace.getDiskUsage();
storageInUse = formatFileSize(storage); storageInUse = formatFileSize(storage);
@ -168,7 +191,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
} }
@Override @Override
public int getProfileStrength() { public int getProfileStrength(String userid) {
int profileStrenght = -1; int profileStrenght = -1;
@ -178,9 +201,15 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
// username // username
String userName = session.getUsername(); String userName = session.getUsername();
// retrieve statistics of...
String statisticsOfUsername = userName;
if(userid != null && !userid.equals(userName))
statisticsOfUsername = userid;
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens //this check just return nothing if that happens
if (userName.compareTo(userid) == 0) { if (userName.compareTo(defaultUserId) == 0) {
_log.debug("Found " + userName + " returning nothing"); _log.debug("Found " + userName + " returning nothing");
return profileStrenght; return profileStrenght;
}else{ }else{
@ -191,11 +220,11 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
try{ try{
// check if the avatar is present // check if the avatar is present
boolean avatarPresent = session.getUserAvatarId() != null; boolean avatarPresent = (new LiferayUserManager().getUserAvatarBytes(statisticsOfUsername) != null);
long init = System.currentTimeMillis(); long init = System.currentTimeMillis();
User user = UserLocalServiceUtil.getUserByScreenName(SiteManagerUtil.getCompany().getCompanyId(), session.getUsername()); User user = UserLocalServiceUtil.getUserByScreenName(SiteManagerUtil.getCompany().getCompanyId(), statisticsOfUsername);
profileStrenght = evaluateProfileStrenght(user, avatarPresent); profileStrenght = evaluateProfileStrenght(user, avatarPresent);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
@ -213,17 +242,37 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
} }
@Override @Override
public UserInformation getUserSettings() { public UserInformation getUserSettings(String userid) {
// get the session // get the session
ASLSession session = getASLSession(); ASLSession session = getASLSession();
//username //username of the asl session
String userName = session.getUsername(); String userName = session.getUsername();
// retrieve statistics of...
String statisticsOfUsername = userName;
// is he the owner of the profile?
boolean isOwner = false;
// can we show this profile to other people?
boolean isProfileShowable = true;
if(userid == null || (userid !=null && userid.equals(userName))){
isOwner = true;
isProfileShowable = checkUserPrivacyOption(userName);
}
if(userid != null && !userid.equals(userName)){
// the stastics to show will be of the userid
statisticsOfUsername = userid;
isProfileShowable = checkUserPrivacyOption(statisticsOfUsername);
}
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens //this check just return nothing if that happens
if (userName.compareTo(userid) == 0) { if (userName.compareTo(defaultUserId) == 0) {
_log.debug("Found " + userName + " returning nothing"); _log.debug("Found " + userName + " returning nothing");
return null; return null;
} }
@ -235,8 +284,18 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
_log.debug("User scope is " + (isInfrastructure ? " the whole infrastucture " : " a VRE")); _log.debug("User scope is " + (isInfrastructure ? " the whole infrastucture " : " a VRE"));
// get path of the avatar // get path of the avatar
String thumbnailURL = session.getUserAvatarId(); UserManager um = new LiferayUserManager();
_log.debug(userName + " avatar has url " + thumbnailURL);
String thumbnailURL = null;
try {
thumbnailURL = um.getUserByUsername(statisticsOfUsername).getUserAvatarURL();
} catch (UserManagementSystemException e) {
_log.error("Unable to retrieve avatar url for user " + statisticsOfUsername, e);
} catch (UserRetrievalFault e) {
_log.error("Unable to retrieve avatar url for user " + statisticsOfUsername, e);
}
_log.debug(statisticsOfUsername + " avatar has url " + thumbnailURL);
// get the vre (if not in the infrastructure) // get the vre (if not in the infrastructure)
String actualVre = null; String actualVre = null;
@ -248,14 +307,39 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
} }
return new UserInformation(isInfrastructure, thumbnailURL, userName, actualVre); return new UserInformation(isInfrastructure, thumbnailURL, userName, actualVre, isOwner, isProfileShowable);
}
else return new UserInformation(true, null, userName, vreID, true, true);
}
/**
* Check privacy option for user's own statistics
* @param username
* @return
*/
private boolean checkUserPrivacyOption(String username) {
try{
// set permission checker
setPermissionChecker();
User user = UserLocalServiceUtil.getUserByScreenName(ManagementUtils.getCompany().getCompanyId(), username);
// the user has not decided yet
if(!user.getExpandoBridge().hasAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY))
return true;
return (boolean)user.getExpandoBridge().getAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY);
}catch(Exception e){
_log.error("Unable to retrieve user's privacy option for his statistics");
return true;
} }
else return new UserInformation(true, null, userName, vreID);
} }
@SuppressWarnings("deprecation")
@Override @Override
public PostsStatsBean getPostsStats(){ public PostsStatsBean getPostsStats(String userid){
// get the session // get the session
ASLSession session = getASLSession(); ASLSession session = getASLSession();
@ -263,9 +347,15 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
//username //username
String userName = session.getUsername(); String userName = session.getUsername();
// retrieve statistics of...
String statisticsOfUsername = userName;
if(userid != null && !userid.equals(userName))
statisticsOfUsername = userid;
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens //this check just return nothing if that happens
if (userName.compareTo(userid) == 0) { if (userName.compareTo(defaultUserId) == 0) {
_log.debug("Found " + userName + " returning nothing"); _log.debug("Found " + userName + " returning nothing");
return null; return null;
} }
@ -276,19 +366,19 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
boolean isInfrastructure = isInfrastructureScope(); boolean isInfrastructure = isInfrastructureScope();
// date corresponding to one year ago // date corresponding to one year ago
Date oneYearAgo = new Date(); Calendar oneYearAgo = Calendar.getInstance();
oneYearAgo.setYear(oneYearAgo.getYear() - 1); oneYearAgo.set(Calendar.YEAR, oneYearAgo.get(Calendar.YEAR) - 1);
_log.debug("Reference time is " + oneYearAgo.toString()); _log.debug("Reference time is " + oneYearAgo.getTime());
try { try {
long init = System.currentTimeMillis(); long init = System.currentTimeMillis();
_log.debug("Getting " + userName + " feeds in the last year."); _log.debug("Getting " + statisticsOfUsername + " feeds in the last year.");
List<Feed> userFeeds = store.getRecentFeedsByUserAndDate(userName, oneYearAgo.getTime()); List<Feed> userFeeds = store.getRecentFeedsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime());
_log.debug("Evaluating number of comments and likes of " + userName + "'s feeds."); _log.debug("Evaluating number of comments and likes of " + statisticsOfUsername + "'s feeds.");
for (Feed feed : userFeeds) { for (Feed feed : userFeeds) {
@ -313,9 +403,9 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
_log.debug("[USER-STATISTICS] time taken to retrieve and filter user feeds, get likes and replies got is " + (end - init) + "ms"); _log.debug("[USER-STATISTICS] time taken to retrieve and filter user feeds, get likes and replies got is " + (end - init) + "ms");
_log.debug("Total number of feeds (after time filtering) of " + userName + " is " + totalFeeds); _log.debug("Total number of feeds (after time filtering) of " + statisticsOfUsername + " is " + totalFeeds);
_log.debug("Total number of likes (after time filtering) for " + userName + " is " + totalLikes); _log.debug("Total number of likes (after time filtering) for " + statisticsOfUsername + " is " + totalLikes);
_log.debug("Total number of comments (after time filtering) for " + userName + " is " + totalComments); _log.debug("Total number of comments (after time filtering) for " + statisticsOfUsername + " is " + totalComments);
}catch(Exception e){ }catch(Exception e){
_log.error(e.toString()); _log.error(e.toString());
@ -326,6 +416,86 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
return new PostsStatsBean(totalFeeds, totalLikes, totalComments); return new PostsStatsBean(totalFeeds, totalLikes, totalComments);
} }
@Override
public void setShowMyOwnStatisticsToOtherPeople(boolean show) {
if(isWithinPortal()){
ASLSession session = getASLSession();
String username = session.getUsername();
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens
if (username.compareTo(defaultUserId) == 0) {
_log.debug("Found " + username + " returning nothing");
return;
}
try{
// set permission checker
setPermissionChecker();
_log.debug("User " + username + (show ? " want to show " : " doesn't want to show ") + " his statistics");
User user = UserLocalServiceUtil.getUserByScreenName(ManagementUtils.getCompany().getCompanyId(), username);
boolean hasAttribute = user.getExpandoBridge().hasAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY);
if(hasAttribute){
// set the new value
_log.debug("Setting custom field value to " + show + " for user " + username);
// set the current value
user.getExpandoBridge().setAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY, show);
}
}catch(Exception e){
_log.error("Unable to check user's privacy for his statistics", e);
}
}
}
/**
* On servlet instanciation, create the custom field and set it to startingValue
* @param customFieldNameUserStatisticsVisibility
* @param b
*/
private void createUserCustomField(
String customFieldNameUserStatisticsVisibility, boolean startingValue) {
// set permission checker
setPermissionChecker();
try{
User defaultUser = UserLocalServiceUtil.getDefaultUser(ManagementUtils.getCompany().getCompanyId());
// check if it exists
boolean exists = defaultUser.getExpandoBridge().hasAttribute(customFieldNameUserStatisticsVisibility);
if(exists){
_log.debug("Custom field already exists... There is no need to create it");
}else{
_log.debug("Creating custom field " + customFieldNameUserStatisticsVisibility +
" with starting value " + startingValue);
// create
defaultUser.getExpandoBridge().addAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY, ExpandoColumnConstants.BOOLEAN, (Serializable)(true));
}
}catch(Exception e){
_log.error("Unable to create custom field " + customFieldNameUserStatisticsVisibility);
}
}
/** /**
* returns dynamically the formated size. * returns dynamically the formated size.
* *
@ -450,4 +620,21 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U
return score; return score;
} }
/**
* Set the permission checker to set/get custom fields into liferay
*/
private void setPermissionChecker(){
// set permission checker
try{
long adminId = LiferayUserManager.getAdmin().getUserId();
PrincipalThreadLocal.setName(adminId);
PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(UserLocalServiceUtil.getUser(adminId));
PermissionThreadLocal.setPermissionChecker(permissionChecker);
}catch(Exception e){
_log.error("Unable to set permission checker. Custom fields set/get operations are likely to fail...");
}
}
} }

View File

@ -9,27 +9,37 @@ import java.io.Serializable;
*/ */
public class UserInformation implements Serializable { public class UserInformation implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6360825683813448487L; private static final long serialVersionUID = 6360825683813448487L;
private boolean isRoot; private boolean isRoot;
private String urlAvatar; private String urlAvatar;
private String displayName; private String aslSessionUsername;
private String actualVre; private String actualVre;
private boolean isOwner;
// this value will be set to true when the portlet is deployed in the user profile page
// and the user that visits the page is the owner of the profile
// if it is deployed in a vre or in the root infrastructure, it will be set to false
// of course, the same is true if the user that visits the profile is not the owner
private boolean isProfileShowable = true;
// this field is used when a user visit a user profile page.
// If the visiting user is the owner of the page, there is no proble
// if the visiting user is not the owner and the real owner doesn't want
// to show the portlet to the other users, we need to hide the statistics.
public UserInformation(){ public UserInformation(){
super(); super();
} }
public UserInformation(boolean isRoot, String urlAvatar, public UserInformation(boolean isRoot, String urlAvatar,
String displayName, String actualVre) { String aslSessionUsername, String actualVre, boolean isOwner, boolean isProfileShowable) {
super(); super();
this.isRoot = isRoot; this.isRoot = isRoot;
this.urlAvatar = urlAvatar; this.urlAvatar = urlAvatar;
this.displayName = displayName; this.aslSessionUsername = aslSessionUsername;
this.actualVre = actualVre; this.actualVre = actualVre;
this.isOwner = isOwner;
this.isProfileShowable = isProfileShowable;
} }
@ -52,17 +62,14 @@ public class UserInformation implements Serializable {
this.urlAvatar = urlAvatar; this.urlAvatar = urlAvatar;
} }
public String getAslSessionUsername() {
public String getDisplayName() { return aslSessionUsername;
return displayName;
} }
public void setAslSessionUsername(String aslSessionUsername) {
public void setDisplayName(String displayName) { this.aslSessionUsername = aslSessionUsername;
this.displayName = displayName;
} }
public String getActualVre() { public String getActualVre() {
return actualVre; return actualVre;
} }
@ -72,11 +79,27 @@ public class UserInformation implements Serializable {
this.actualVre = actualVre; this.actualVre = actualVre;
} }
public boolean isOwner() {
return isOwner;
}
public void setOwner(boolean isOwner) {
this.isOwner = isOwner;
}
public boolean isProfileShowable() {
return isProfileShowable;
}
public void setProfileShowable(boolean isProfileShowable) {
this.isProfileShowable = isProfileShowable;
}
@Override @Override
public String toString() { public String toString() {
return "UserInformation [isRoot=" + isRoot + ", urlAvatar=" + urlAvatar return "UserInformation [isRoot=" + isRoot + ", urlAvatar=" + urlAvatar
+ ", displayName=" + displayName + ", actualVre=" + actualVre + ", aslSessionUsername=" + aslSessionUsername + ", actualVre="
+ "]"; + actualVre + ", isOwner=" + isOwner + ", isProfileShowable="
+ isProfileShowable + "]";
} }
} }

View File

@ -61,3 +61,7 @@
font-weight: normal !important; font-weight: normal !important;
display: inline !important; display: inline !important;
} }
.privacy-checkbox-statistics-style {
margin-top: 10px;
}

View File

@ -12,7 +12,7 @@ public class TestForDeploy {
UserStatisticsServiceImpl serviceImpl = new UserStatisticsServiceImpl(); UserStatisticsServiceImpl serviceImpl = new UserStatisticsServiceImpl();
String username = serviceImpl.getDevelopmentUser(); String username = serviceImpl.getDevelopmentUser();
System.out.println("username for deploy is: " + username); System.out.println("username for deploy is: " + username);
assertTrue(username.compareTo(UserStatisticsServiceImpl.userid) == 0); assertTrue(username.compareTo(UserStatisticsServiceImpl.defaultUserId) == 0);
System.out.println("Test OK!"); System.out.println("Test OK!");
} }